Xiaozhe Shi | 72a72f2 | 2013-12-26 13:54:29 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014, 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 | 2737582 | 2013-08-22 11:40:15 -0700 | [diff] [blame] | 26 | #include <linux/sched.h> |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 27 | #include <linux/qpnp/qpnp-adc.h> |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 28 | #include <linux/qpnp/power-on.h> |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 29 | #include <linux/of_batterydata.h> |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 30 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 31 | /* BMS Register Offsets */ |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 32 | #define REVISION1 0x0 |
| 33 | #define REVISION2 0x1 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 34 | #define BMS1_STATUS1 0x8 |
| 35 | #define BMS1_MODE_CTL 0X40 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 36 | /* Coulomb counter clear registers */ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 37 | #define BMS1_CC_DATA_CTL 0x42 |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 38 | #define BMS1_CC_CLEAR_CTL 0x43 |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 39 | /* BMS Tolerances */ |
| 40 | #define BMS1_TOL_CTL 0X44 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 41 | /* OCV limit registers */ |
| 42 | #define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48 |
| 43 | #define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49 |
| 44 | #define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A |
| 45 | #define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B |
| 46 | #define BMS1_OCV_USE_LIMIT_CTL 0x4C |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 47 | /* Delay control */ |
| 48 | #define BMS1_S1_DELAY_CTL 0x5A |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 49 | /* OCV interrupt threshold */ |
| 50 | #define BMS1_OCV_THR0 0x50 |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 51 | #define BMS1_S2_SAMP_AVG_CTL 0x61 |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 52 | /* SW CC interrupt threshold */ |
| 53 | #define BMS1_SW_CC_THR0 0xA0 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 54 | /* OCV for r registers */ |
| 55 | #define BMS1_OCV_FOR_R_DATA0 0x80 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 56 | #define BMS1_VSENSE_FOR_R_DATA0 0x82 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 57 | /* Coulomb counter data */ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 58 | #define BMS1_CC_DATA0 0x8A |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 59 | /* Shadow Coulomb counter data */ |
| 60 | #define BMS1_SW_CC_DATA0 0xA8 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 61 | /* OCV for soc data */ |
| 62 | #define BMS1_OCV_FOR_SOC_DATA0 0x90 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 63 | #define BMS1_VSENSE_PON_DATA0 0x94 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 64 | #define BMS1_VSENSE_AVG_DATA0 0x98 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 65 | #define BMS1_VBAT_AVG_DATA0 0x9E |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 66 | /* Extra bms registers */ |
Xiaozhe Shi | 5705894 | 2013-03-27 16:54:54 -0700 | [diff] [blame] | 67 | #define SOC_STORAGE_REG 0xB0 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 68 | #define IAVG_STORAGE_REG 0xB1 |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 69 | #define BMS_FCC_COUNT 0xB2 |
| 70 | #define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */ |
| 71 | #define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */ |
| 72 | #define CHARGE_INCREASE_STORAGE 0xBD |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 73 | #define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */ |
| 74 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 75 | /* IADC Channel Select */ |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 76 | #define IADC1_BMS_REVISION2 0x01 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 77 | #define IADC1_BMS_ADC_CH_SEL_CTL 0x48 |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 78 | #define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49 |
| 79 | #define IADC1_BMS_FAST_AVG_EN 0x5B |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 80 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 81 | /* Configuration for saving of shutdown soc/iavg */ |
| 82 | #define IGNORE_SOC_TEMP_DECIDEG 50 |
Xiaozhe Shi | 68a1bb2 | 2013-09-23 14:52:00 -0700 | [diff] [blame] | 83 | #define IAVG_STEP_SIZE_MA 10 |
Xiaozhe Shi | f5f966d | 2013-02-19 14:23:11 -0800 | [diff] [blame] | 84 | #define IAVG_INVALID 0xFF |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 85 | #define SOC_INVALID 0x7E |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 86 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 87 | #define IAVG_SAMPLES 16 |
| 88 | |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 89 | /* FCC learning constants */ |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 90 | #define MAX_FCC_CYCLES 5 |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 91 | #define DELTA_FCC_PERCENT 5 |
| 92 | #define VALID_FCC_CHGCYL_RANGE 50 |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 93 | #define CHGCYL_RESOLUTION 20 |
| 94 | #define FCC_DEFAULT_TEMP 250 |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 95 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 96 | #define QPNP_BMS_DEV_NAME "qcom,qpnp-bms" |
| 97 | |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 98 | enum { |
| 99 | SHDW_CC, |
| 100 | CC |
| 101 | }; |
| 102 | |
| 103 | enum { |
| 104 | NORESET, |
| 105 | RESET |
| 106 | }; |
| 107 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 108 | struct soc_params { |
| 109 | int fcc_uah; |
| 110 | int cc_uah; |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 111 | int rbatt_mohm; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 112 | int iavg_ua; |
| 113 | int uuc_uah; |
| 114 | int ocv_charge_uah; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 115 | int delta_time_s; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct raw_soc_params { |
| 119 | uint16_t last_good_ocv_raw; |
| 120 | int64_t cc; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 121 | int64_t shdw_cc; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 122 | int last_good_ocv_uv; |
| 123 | }; |
| 124 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 125 | struct fcc_sample { |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 126 | int fcc_new; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 127 | int chargecycles; |
| 128 | }; |
| 129 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 130 | struct bms_irq { |
| 131 | unsigned int irq; |
| 132 | unsigned long disabled; |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 133 | unsigned long wake_enabled; |
Xiaozhe Shi | f511a6e | 2014-02-20 14:37:18 -0800 | [diff] [blame] | 134 | bool ready; |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 135 | bool is_wake; |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | struct bms_wakeup_source { |
| 139 | struct wakeup_source source; |
| 140 | unsigned long disabled; |
| 141 | }; |
| 142 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 143 | struct qpnp_bms_chip { |
| 144 | struct device *dev; |
| 145 | struct power_supply bms_psy; |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 146 | bool bms_psy_registered; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 147 | struct power_supply *batt_psy; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 148 | struct spmi_device *spmi; |
Xiaozhe Shi | 2737582 | 2013-08-22 11:40:15 -0700 | [diff] [blame] | 149 | wait_queue_head_t bms_wait_queue; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 150 | u16 base; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 151 | u16 iadc_base; |
Xiaozhe Shi | 7c41a29 | 2013-08-16 16:50:17 -0700 | [diff] [blame] | 152 | u16 batt_pres_addr; |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 153 | u16 soc_storage_addr; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 154 | |
| 155 | u8 revision1; |
| 156 | u8 revision2; |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 157 | |
| 158 | u8 iadc_bms_revision1; |
| 159 | u8 iadc_bms_revision2; |
| 160 | |
Xiaozhe Shi | d5d2141 | 2013-02-06 17:14:41 -0800 | [diff] [blame] | 161 | int battery_present; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 162 | int battery_status; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 163 | bool batfet_closed; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 164 | bool new_battery; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 165 | bool done_charging; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 166 | bool last_soc_invalid; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 167 | /* platform data */ |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 168 | int r_sense_uohm; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 169 | unsigned int v_cutoff_uv; |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 170 | int max_voltage_uv; |
| 171 | int r_conn_mohm; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 172 | int shutdown_soc_valid_limit; |
| 173 | int adjust_soc_low_threshold; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 174 | int chg_term_ua; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 175 | enum battery_type batt_type; |
Xiaozhe Shi | 976618f | 2013-04-30 10:49:30 -0700 | [diff] [blame] | 176 | unsigned int fcc_mah; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 177 | struct single_row_lut *fcc_temp_lut; |
| 178 | struct single_row_lut *fcc_sf_lut; |
| 179 | struct pc_temp_ocv_lut *pc_temp_ocv_lut; |
| 180 | struct sf_lut *pc_sf_lut; |
| 181 | struct sf_lut *rbatt_sf_lut; |
| 182 | int default_rbatt_mohm; |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 183 | int rbatt_capacitive_mohm; |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 184 | int rbatt_mohm; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 185 | |
| 186 | struct delayed_work calculate_soc_delayed_work; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 187 | struct work_struct recalc_work; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 188 | struct work_struct batfet_open_work; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 189 | |
| 190 | struct mutex bms_output_lock; |
| 191 | struct mutex last_ocv_uv_mutex; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 192 | struct mutex vbat_monitor_mutex; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 193 | struct mutex soc_invalidation_mutex; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 194 | struct mutex last_soc_mutex; |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 195 | struct mutex status_lock; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 196 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 197 | bool use_external_rsense; |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 198 | bool use_ocv_thresholds; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 199 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 200 | bool ignore_shutdown_soc; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 201 | bool shutdown_soc_invalid; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 202 | int shutdown_soc; |
| 203 | int shutdown_iavg_ma; |
| 204 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 205 | struct wake_lock low_voltage_wake_lock; |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 206 | int low_voltage_threshold; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 207 | int low_soc_calc_threshold; |
| 208 | int low_soc_calculate_soc_ms; |
Xiaozhe Shi | cb487b1 | 2013-10-14 17:42:07 -0700 | [diff] [blame] | 209 | int low_voltage_calculate_soc_ms; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 210 | int calculate_soc_ms; |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 211 | struct bms_wakeup_source soc_wake_source; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 212 | struct wake_lock cv_wake_lock; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 213 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 214 | uint16_t ocv_reading_at_100; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 215 | uint16_t prev_last_good_ocv_raw; |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 216 | int insertion_ocv_uv; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 217 | int last_ocv_uv; |
Xiaozhe Shi | cc48e99 | 2013-05-28 16:42:24 -0700 | [diff] [blame] | 218 | int charging_adjusted_ocv; |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 219 | int last_ocv_temp; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 220 | int last_cc_uah; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 221 | unsigned long last_soc_change_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 222 | unsigned long tm_sec; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 223 | unsigned long report_tm_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 224 | bool first_time_calc_soc; |
| 225 | bool first_time_calc_uuc; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 226 | int64_t software_cc_uah; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 227 | int64_t software_shdw_cc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 228 | |
| 229 | int iavg_samples_ma[IAVG_SAMPLES]; |
| 230 | int iavg_index; |
| 231 | int iavg_num_samples; |
| 232 | struct timespec t_soc_queried; |
| 233 | int last_soc; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 234 | int last_soc_est; |
Xiaozhe Shi | cc13726 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 235 | int last_soc_unbound; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 236 | bool was_charging_at_sleep; |
| 237 | int charge_start_tm_sec; |
| 238 | int catch_up_time_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 239 | struct single_row_lut *adjusted_fcc_temp_lut; |
| 240 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 241 | struct qpnp_adc_tm_btm_param vbat_monitor_params; |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 242 | struct qpnp_adc_tm_btm_param die_temp_monitor_params; |
| 243 | int temperature_margin; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 244 | unsigned int vadc_v0625; |
| 245 | unsigned int vadc_v1250; |
| 246 | |
Xiaozhe Shi | ee39e5d | 2013-10-07 13:56:04 -0700 | [diff] [blame] | 247 | int system_load_count; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 248 | int prev_uuc_iavg_ma; |
| 249 | int prev_pc_unusable; |
| 250 | int ibat_at_cv_ua; |
| 251 | int soc_at_cv; |
| 252 | int prev_chg_soc; |
| 253 | int calculated_soc; |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 254 | int prev_voltage_based_soc; |
| 255 | bool use_voltage_soc; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 256 | bool in_cv_range; |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 257 | |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 258 | int prev_batt_terminal_uv; |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 259 | int high_ocv_correction_limit_uv; |
| 260 | int low_ocv_correction_limit_uv; |
| 261 | int flat_ocv_threshold_uv; |
| 262 | int hold_soc_est; |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 263 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 264 | int ocv_high_threshold_uv; |
| 265 | int ocv_low_threshold_uv; |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 266 | unsigned long last_recalc_time; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 267 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 268 | struct fcc_sample *fcc_learning_samples; |
| 269 | u8 fcc_sample_count; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 270 | int enable_fcc_learning; |
| 271 | int min_fcc_learning_soc; |
| 272 | int min_fcc_ocv_pc; |
| 273 | int min_fcc_learning_samples; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 274 | int start_soc; |
| 275 | int end_soc; |
| 276 | int start_pc; |
| 277 | int start_cc_uah; |
| 278 | int start_real_soc; |
| 279 | int end_cc_uah; |
| 280 | uint16_t fcc_new_mah; |
| 281 | int fcc_new_batt_temp; |
| 282 | uint16_t charge_cycles; |
| 283 | u8 charge_increase; |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 284 | int fcc_resolution; |
| 285 | bool battery_removed; |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 286 | bool in_taper_charge; |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 287 | struct bms_irq sw_cc_thr_irq; |
| 288 | struct bms_irq ocv_thr_irq; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 289 | struct qpnp_vadc_chip *vadc_dev; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 290 | struct qpnp_iadc_chip *iadc_dev; |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 291 | struct qpnp_adc_tm_chip *adc_tm_dev; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | static struct of_device_id qpnp_bms_match_table[] = { |
| 295 | { .compatible = QPNP_BMS_DEV_NAME }, |
| 296 | {} |
| 297 | }; |
| 298 | |
| 299 | static char *qpnp_bms_supplicants[] = { |
| 300 | "battery" |
| 301 | }; |
| 302 | |
| 303 | static enum power_supply_property msm_bms_power_props[] = { |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 304 | POWER_SUPPLY_PROP_CAPACITY, |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 305 | POWER_SUPPLY_PROP_STATUS, |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 306 | POWER_SUPPLY_PROP_CURRENT_NOW, |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 307 | POWER_SUPPLY_PROP_RESISTANCE, |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 308 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 309 | POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW, |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 310 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 311 | POWER_SUPPLY_PROP_CHARGE_FULL, |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 312 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 313 | }; |
wangxl | ee3614a | 2015-02-03 13:13:11 +0800 | [diff] [blame] | 314 | #ifdef CONFIG_TCMD |
| 315 | static struct qpnp_bms_chip *bms_chip; |
| 316 | #endif |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 317 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 318 | static int discard_backup_fcc_data(struct qpnp_bms_chip *chip); |
| 319 | static void backup_charge_cycle(struct qpnp_bms_chip *chip); |
| 320 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 321 | static bool bms_reset; |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 322 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 323 | static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val, |
| 324 | u16 base, int count) |
| 325 | { |
| 326 | int rc; |
| 327 | struct spmi_device *spmi = chip->spmi; |
| 328 | |
| 329 | rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 330 | if (rc) { |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 331 | pr_err("SPMI read failed rc=%d\n", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 332 | return rc; |
| 333 | } |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 337 | static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val, |
| 338 | u16 base, int count) |
| 339 | { |
| 340 | int rc; |
| 341 | struct spmi_device *spmi = chip->spmi; |
| 342 | |
| 343 | rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count); |
| 344 | if (rc) { |
| 345 | pr_err("SPMI write failed rc=%d\n", rc); |
| 346 | return rc; |
| 347 | } |
| 348 | return 0; |
| 349 | } |
| 350 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 351 | 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] | 352 | u8 mask, u8 val) |
| 353 | { |
| 354 | int rc; |
| 355 | u8 reg; |
| 356 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 357 | rc = qpnp_read_wrapper(chip, ®, addr, 1); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 358 | if (rc) { |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 359 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 360 | return rc; |
| 361 | } |
| 362 | reg &= ~mask; |
| 363 | reg |= val & mask; |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 364 | rc = qpnp_write_wrapper(chip, ®, addr, 1); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 365 | if (rc) { |
| 366 | 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] | 367 | addr, val, mask, reg, rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 368 | return rc; |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 373 | static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr, |
| 374 | u8 mask, u8 val) |
| 375 | { |
| 376 | return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val); |
| 377 | } |
| 378 | |
| 379 | static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr, |
| 380 | u8 mask, u8 val) |
| 381 | { |
| 382 | return qpnp_masked_write_base(chip, chip->base + addr, mask, val); |
| 383 | } |
| 384 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 385 | static void bms_stay_awake(struct bms_wakeup_source *source) |
| 386 | { |
| 387 | if (__test_and_clear_bit(0, &source->disabled)) { |
| 388 | __pm_stay_awake(&source->source); |
| 389 | pr_debug("enabled source %s\n", source->source.name); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | static void bms_relax(struct bms_wakeup_source *source) |
| 394 | { |
| 395 | if (!__test_and_set_bit(0, &source->disabled)) { |
| 396 | __pm_relax(&source->source); |
| 397 | pr_debug("disabled source %s\n", source->source.name); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void enable_bms_irq(struct bms_irq *irq) |
| 402 | { |
Xiaozhe Shi | f511a6e | 2014-02-20 14:37:18 -0800 | [diff] [blame] | 403 | if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) { |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 404 | enable_irq(irq->irq); |
| 405 | pr_debug("enabled irq %d\n", irq->irq); |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 406 | if ((irq->is_wake) && |
| 407 | !__test_and_set_bit(0, &irq->wake_enabled)) |
| 408 | enable_irq_wake(irq->irq); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | static void disable_bms_irq(struct bms_irq *irq) |
| 413 | { |
Xiaozhe Shi | f511a6e | 2014-02-20 14:37:18 -0800 | [diff] [blame] | 414 | if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) { |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 415 | disable_irq(irq->irq); |
| 416 | pr_debug("disabled irq %d\n", irq->irq); |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 417 | if ((irq->is_wake) && |
| 418 | __test_and_clear_bit(0, &irq->wake_enabled)) |
| 419 | disable_irq_wake(irq->irq); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Anirudh Ghayal | 1166eef | 2013-12-23 19:05:33 +0530 | [diff] [blame] | 423 | static void disable_bms_irq_nosync(struct bms_irq *irq) |
| 424 | { |
Xiaozhe Shi | f511a6e | 2014-02-20 14:37:18 -0800 | [diff] [blame] | 425 | if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) { |
Anirudh Ghayal | 1166eef | 2013-12-23 19:05:33 +0530 | [diff] [blame] | 426 | disable_irq_nosync(irq->irq); |
| 427 | pr_debug("disabled irq %d\n", irq->irq); |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 428 | if ((irq->is_wake) && |
| 429 | __test_and_clear_bit(0, &irq->wake_enabled)) |
| 430 | disable_irq_wake(irq->irq); |
Anirudh Ghayal | 1166eef | 2013-12-23 19:05:33 +0530 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 434 | #define HOLD_OREG_DATA BIT(0) |
| 435 | static int lock_output_data(struct qpnp_bms_chip *chip) |
| 436 | { |
| 437 | int rc; |
| 438 | |
| 439 | rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, |
| 440 | HOLD_OREG_DATA, HOLD_OREG_DATA); |
| 441 | if (rc) { |
| 442 | pr_err("couldnt lock bms output rc = %d\n", rc); |
| 443 | return rc; |
| 444 | } |
Xiaozhe Shi | ed4a552 | 2014-09-05 14:56:13 -0700 | [diff] [blame] | 445 | /* |
| 446 | * Sleep for at least 60 microseconds here to make sure there has |
| 447 | * been at least two cycles of the sleep clock so that the registers |
| 448 | * are correctly locked. |
| 449 | */ |
| 450 | usleep_range(60, 2000); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static int unlock_output_data(struct qpnp_bms_chip *chip) |
| 455 | { |
| 456 | int rc; |
| 457 | |
| 458 | rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0); |
| 459 | if (rc) { |
| 460 | pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc); |
| 461 | return rc; |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | #define V_PER_BIT_MUL_FACTOR 97656 |
| 467 | #define V_PER_BIT_DIV_FACTOR 1000 |
| 468 | #define VADC_INTRINSIC_OFFSET 0x6000 |
| 469 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 470 | static int vadc_reading_to_uv(int reading) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 471 | { |
| 472 | if (reading <= VADC_INTRINSIC_OFFSET) |
| 473 | return 0; |
| 474 | |
| 475 | return (reading - VADC_INTRINSIC_OFFSET) |
| 476 | * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR; |
| 477 | } |
| 478 | |
| 479 | #define VADC_CALIB_UV 625000 |
| 480 | #define VBATT_MUL_FACTOR 3 |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 481 | 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] | 482 | { |
| 483 | s64 numerator, denominator; |
| 484 | |
| 485 | if (reading_uv == 0) |
| 486 | return 0; |
| 487 | |
| 488 | /* don't adjust if not calibrated */ |
| 489 | if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) { |
| 490 | pr_debug("No cal yet return %d\n", |
| 491 | VBATT_MUL_FACTOR * reading_uv); |
| 492 | return VBATT_MUL_FACTOR * reading_uv; |
| 493 | } |
| 494 | |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 495 | numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 496 | denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625; |
| 497 | if (denominator == 0) |
| 498 | return reading_uv * VBATT_MUL_FACTOR; |
| 499 | return (VADC_CALIB_UV + div_s64(numerator, denominator)) |
| 500 | * VBATT_MUL_FACTOR; |
| 501 | } |
| 502 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 503 | static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip, |
| 504 | int unadjusted_vbatt) |
| 505 | { |
| 506 | int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR; |
| 507 | |
| 508 | if (scaled_vbatt <= 0) |
| 509 | return VADC_INTRINSIC_OFFSET; |
| 510 | return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR) |
| 511 | + VADC_INTRINSIC_OFFSET; |
| 512 | } |
| 513 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 514 | static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip, |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 515 | uint16_t reading, bool is_pon_ocv) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 516 | { |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 517 | int64_t uv; |
| 518 | int rc; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 519 | |
| 520 | uv = vadc_reading_to_uv(reading); |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 521 | pr_debug("%u raw converted into %lld uv\n", reading, uv); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 522 | uv = adjust_vbatt_reading(chip, uv); |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 523 | pr_debug("adjusted into %lld uv\n", uv); |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 524 | rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv); |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 525 | if (rc) |
| 526 | pr_debug("could not compensate vbatt\n"); |
| 527 | pr_debug("compensated into %lld uv\n", uv); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 528 | return uv; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | #define CC_READING_RESOLUTION_N 542535 |
| 532 | #define CC_READING_RESOLUTION_D 100000 |
Xiaozhe Shi | 8f5dd1b | 2013-04-30 10:27:58 -0700 | [diff] [blame] | 533 | static s64 cc_reading_to_uv(s64 reading) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 534 | { |
| 535 | return div_s64(reading * CC_READING_RESOLUTION_N, |
| 536 | CC_READING_RESOLUTION_D); |
| 537 | } |
| 538 | |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 539 | #define QPNP_ADC_GAIN_IDEAL 3291LL |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 540 | static s64 cc_adjust_for_gain(s64 uv, uint16_t gain) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 541 | { |
| 542 | s64 result_uv; |
| 543 | |
| 544 | pr_debug("adjusting_uv = %lld\n", uv); |
Xiaozhe Shi | 820a47a | 2012-11-27 13:23:27 -0800 | [diff] [blame] | 545 | if (gain == 0) { |
| 546 | pr_debug("gain is %d, not adjusting\n", gain); |
| 547 | return uv; |
| 548 | } |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 549 | pr_debug("adjusting by factor: %lld/%hu = %lld%%\n", |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 550 | QPNP_ADC_GAIN_IDEAL, gain, |
| 551 | div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain)); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 552 | |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 553 | result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 554 | pr_debug("result_uv = %lld\n", result_uv); |
| 555 | return result_uv; |
| 556 | } |
| 557 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 558 | static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv) |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 559 | { |
| 560 | struct qpnp_iadc_calib calibration; |
| 561 | int gain; |
| 562 | s64 result_uv; |
| 563 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 564 | qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 565 | gain = (int)calibration.gain_raw - (int)calibration.offset_raw; |
| 566 | |
| 567 | pr_debug("reverse adjusting_uv = %lld\n", uv); |
| 568 | if (gain == 0) { |
| 569 | pr_debug("gain is %d, not adjusting\n", gain); |
| 570 | return uv; |
| 571 | } |
| 572 | pr_debug("adjusting by factor: %hu/%lld = %lld%%\n", |
| 573 | gain, QPNP_ADC_GAIN_IDEAL, |
| 574 | div64_s64((s64)gain * 100LL, |
| 575 | (s64)QPNP_ADC_GAIN_IDEAL)); |
| 576 | |
| 577 | result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL); |
| 578 | pr_debug("result_uv = %lld\n", result_uv); |
| 579 | return result_uv; |
| 580 | } |
| 581 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 582 | static int convert_vsense_to_uv(struct qpnp_bms_chip *chip, |
| 583 | int16_t reading) |
| 584 | { |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 585 | struct qpnp_iadc_calib calibration; |
| 586 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 587 | qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 588 | return cc_adjust_for_gain(cc_reading_to_uv(reading), |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 589 | calibration.gain_raw - calibration.offset_raw); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv) |
| 593 | { |
| 594 | int rc; |
| 595 | int16_t reading; |
| 596 | |
| 597 | rc = qpnp_read_wrapper(chip, (u8 *)&reading, |
| 598 | chip->base + BMS1_VSENSE_AVG_DATA0, 2); |
| 599 | |
| 600 | if (rc) { |
| 601 | pr_err("fail to read VSENSE_AVG rc = %d\n", rc); |
| 602 | return rc; |
| 603 | } |
| 604 | |
| 605 | *result_uv = convert_vsense_to_uv(chip, reading); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua) |
| 610 | { |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 611 | int rc, vsense_uv = 0; |
| 612 | int64_t temp_current; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 613 | |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 614 | if (chip->r_sense_uohm == 0) { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 615 | pr_err("r_sense is zero\n"); |
| 616 | return -EINVAL; |
| 617 | } |
| 618 | |
| 619 | mutex_lock(&chip->bms_output_lock); |
| 620 | lock_output_data(chip); |
| 621 | read_vsense_avg(chip, &vsense_uv); |
| 622 | unlock_output_data(chip); |
| 623 | mutex_unlock(&chip->bms_output_lock); |
| 624 | |
| 625 | pr_debug("vsense_uv=%duV\n", vsense_uv); |
| 626 | /* cast for signed division */ |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 627 | temp_current = div_s64((vsense_uv * 1000000LL), |
| 628 | (int)chip->r_sense_uohm); |
| 629 | |
Xiaozhe Shi | 4c8458a | 2013-11-26 13:00:56 -0800 | [diff] [blame] | 630 | *result_ua = temp_current; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 631 | rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 632 | if (rc) |
| 633 | pr_debug("error compensation failed: %d\n", rc); |
| 634 | |
Xiaozhe Shi | 4c8458a | 2013-11-26 13:00:56 -0800 | [diff] [blame] | 635 | pr_debug("%d uA err compensated ibat=%llduA\n", |
| 636 | *result_ua, temp_current); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 637 | *result_ua = temp_current; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 638 | return 0; |
| 639 | } |
wangxl | ee3614a | 2015-02-03 13:13:11 +0800 | [diff] [blame] | 640 | #ifdef CONFIG_TCMD |
| 641 | int tcmd_get_battery_current(int *result_ua) |
| 642 | { |
| 643 | int result_ibat_ua; |
| 644 | get_battery_current(bms_chip,&result_ibat_ua); |
| 645 | *result_ua = -1 * result_ibat_ua; |
| 646 | pr_debug("ibat=%duA\n", *result_ua); |
| 647 | return 0; |
| 648 | } |
| 649 | #endif |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 650 | static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 651 | { |
| 652 | int rc; |
| 653 | struct qpnp_vadc_result adc_result; |
| 654 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 655 | rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 656 | if (rc) { |
| 657 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 658 | VBAT_SNS, rc); |
| 659 | return rc; |
| 660 | } |
| 661 | pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical, |
| 662 | adc_result.measurement); |
| 663 | *result_uv = (int)adc_result.physical; |
| 664 | return 0; |
| 665 | } |
| 666 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 667 | #define CC_36_BIT_MASK 0xFFFFFFFFFLL |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 668 | static uint64_t convert_s64_to_s36(int64_t raw64) |
| 669 | { |
| 670 | return (uint64_t) raw64 & CC_36_BIT_MASK; |
| 671 | } |
| 672 | |
| 673 | #define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK) |
| 674 | static int64_t convert_s36_to_s64(uint64_t raw36) |
| 675 | { |
| 676 | raw36 = raw36 & CC_36_BIT_MASK; |
| 677 | /* convert 36 bit signed value into 64 signed value */ |
| 678 | return (raw36 >> 35) == 0LL ? |
| 679 | raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36); |
| 680 | } |
| 681 | |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 682 | static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading, |
| 683 | int cc_type) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 684 | { |
| 685 | int64_t raw_reading; |
| 686 | int rc; |
| 687 | |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 688 | if (cc_type == SHDW_CC) |
| 689 | rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading, |
| 690 | chip->base + BMS1_SW_CC_DATA0, 5); |
| 691 | else |
| 692 | rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading, |
| 693 | chip->base + BMS1_CC_DATA0, 5); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 694 | if (rc) { |
| 695 | pr_err("Error reading cc: rc = %d\n", rc); |
| 696 | return -ENXIO; |
| 697 | } |
| 698 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 699 | *reading = convert_s36_to_s64(raw_reading); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 704 | static int calib_vadc(struct qpnp_bms_chip *chip) |
| 705 | { |
Xiaozhe Shi | f62c015 | 2013-03-28 17:57:19 -0700 | [diff] [blame] | 706 | int rc, raw_0625, raw_1250; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 707 | struct qpnp_vadc_result result; |
| 708 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 709 | rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 710 | if (rc) { |
| 711 | pr_debug("vadc read failed with rc = %d\n", rc); |
| 712 | return rc; |
| 713 | } |
Xiaozhe Shi | f62c015 | 2013-03-28 17:57:19 -0700 | [diff] [blame] | 714 | raw_0625 = result.adc_code; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 715 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 716 | rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 717 | if (rc) { |
| 718 | pr_debug("vadc read failed with rc = %d\n", rc); |
| 719 | return rc; |
| 720 | } |
Xiaozhe Shi | f62c015 | 2013-03-28 17:57:19 -0700 | [diff] [blame] | 721 | raw_1250 = result.adc_code; |
| 722 | chip->vadc_v0625 = vadc_reading_to_uv(raw_0625); |
| 723 | chip->vadc_v1250 = vadc_reading_to_uv(raw_1250); |
| 724 | pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n", |
| 725 | raw_0625, chip->vadc_v0625, |
| 726 | raw_1250, chip->vadc_v1250); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 730 | static void convert_and_store_ocv(struct qpnp_bms_chip *chip, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 731 | struct raw_soc_params *raw, |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 732 | int batt_temp, bool is_pon_ocv) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 733 | { |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 734 | int rc; |
| 735 | |
| 736 | pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n", |
| 737 | chip->prev_last_good_ocv_raw, |
| 738 | raw->last_good_ocv_raw); |
| 739 | rc = calib_vadc(chip); |
| 740 | if (rc) |
| 741 | pr_err("Vadc reference voltage read failed, rc = %d\n", rc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 742 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 743 | raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip, |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 744 | raw->last_good_ocv_raw, is_pon_ocv); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 745 | chip->last_ocv_uv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 746 | chip->last_ocv_temp = batt_temp; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 747 | chip->software_cc_uah = 0; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 748 | 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] | 749 | } |
| 750 | |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 751 | #define CLEAR_CC BIT(7) |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 752 | #define CLEAR_SHDW_CC BIT(6) |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 753 | /** |
| 754 | * reset both cc and sw-cc. |
| 755 | * note: this should only be ever called from one thread |
| 756 | * or there may be a race condition where CC is never enabled |
| 757 | * again |
| 758 | */ |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 759 | static void reset_cc(struct qpnp_bms_chip *chip, u8 flags) |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 760 | { |
| 761 | int rc; |
| 762 | |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 763 | pr_debug("resetting cc manually with flags %hhu\n", flags); |
| 764 | mutex_lock(&chip->bms_output_lock); |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 765 | rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL, |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 766 | flags, |
| 767 | flags); |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 768 | if (rc) |
| 769 | pr_err("cc reset failed: %d\n", rc); |
| 770 | |
| 771 | /* wait for 100us for cc to reset */ |
| 772 | udelay(100); |
| 773 | |
| 774 | rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL, |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 775 | flags, 0); |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 776 | if (rc) |
| 777 | pr_err("cc reenable failed: %d\n", rc); |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 778 | mutex_unlock(&chip->bms_output_lock); |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 781 | static int get_battery_status(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 782 | { |
| 783 | union power_supply_propval ret = {0,}; |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 784 | int rc; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 785 | |
| 786 | if (chip->batt_psy == NULL) |
| 787 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 788 | if (chip->batt_psy) { |
| 789 | /* if battery has been registered, use the status property */ |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 790 | rc = chip->batt_psy->get_property(chip->batt_psy, |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 791 | POWER_SUPPLY_PROP_STATUS, &ret); |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 792 | if (rc) { |
| 793 | pr_debug("Battery does not export status: %d\n", rc); |
| 794 | return POWER_SUPPLY_STATUS_UNKNOWN; |
| 795 | } |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 796 | return ret.intval; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* Default to false if the battery power supply is not registered. */ |
| 800 | pr_debug("battery power supply is not registered\n"); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 801 | return POWER_SUPPLY_STATUS_UNKNOWN; |
| 802 | } |
| 803 | |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 804 | static int get_battery_charge_type(struct qpnp_bms_chip *chip) |
| 805 | { |
| 806 | union power_supply_propval ret = {0,}; |
| 807 | int rc; |
| 808 | |
| 809 | if (chip->batt_psy == NULL) |
| 810 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 811 | if (chip->batt_psy) { |
| 812 | /* if battery has been registered, use the type property */ |
| 813 | rc = chip->batt_psy->get_property(chip->batt_psy, |
| 814 | POWER_SUPPLY_PROP_CHARGE_TYPE, &ret); |
| 815 | if (rc) { |
| 816 | pr_debug("Battery does not export charge type: %d\n" |
| 817 | , rc); |
| 818 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 819 | } |
| 820 | return ret.intval; |
| 821 | } |
| 822 | |
| 823 | /* Default to false if the battery power supply is not registered. */ |
| 824 | pr_debug("battery power supply is not registered\n"); |
| 825 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 826 | } |
| 827 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 828 | static bool is_battery_charging(struct qpnp_bms_chip *chip) |
| 829 | { |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 830 | return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 833 | static bool is_battery_full(struct qpnp_bms_chip *chip) |
| 834 | { |
| 835 | return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL; |
| 836 | } |
| 837 | |
Xiaozhe Shi | 4515c76 | 2013-11-13 16:36:54 -0800 | [diff] [blame] | 838 | #define BAT_PRES_BIT BIT(7) |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 839 | static bool is_battery_present(struct qpnp_bms_chip *chip) |
| 840 | { |
| 841 | union power_supply_propval ret = {0,}; |
Xiaozhe Shi | 4515c76 | 2013-11-13 16:36:54 -0800 | [diff] [blame] | 842 | int rc; |
| 843 | u8 batt_pres; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 844 | |
Xiaozhe Shi | 4515c76 | 2013-11-13 16:36:54 -0800 | [diff] [blame] | 845 | /* first try to use the batt_pres register if given */ |
| 846 | if (chip->batt_pres_addr) { |
| 847 | rc = qpnp_read_wrapper(chip, &batt_pres, |
| 848 | chip->batt_pres_addr, 1); |
| 849 | if (!rc && (batt_pres & BAT_PRES_BIT)) |
| 850 | return true; |
| 851 | else |
| 852 | return false; |
| 853 | } |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 854 | if (chip->batt_psy == NULL) |
| 855 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 856 | if (chip->batt_psy) { |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 857 | /* if battery has been registered, use the present property */ |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 858 | rc = chip->batt_psy->get_property(chip->batt_psy, |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 859 | POWER_SUPPLY_PROP_PRESENT, &ret); |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 860 | if (rc) { |
| 861 | pr_debug("battery does not export present: %d\n", rc); |
| 862 | return true; |
| 863 | } |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 864 | return ret.intval; |
| 865 | } |
| 866 | |
| 867 | /* Default to false if the battery power supply is not registered. */ |
| 868 | pr_debug("battery power supply is not registered\n"); |
| 869 | return false; |
| 870 | } |
| 871 | |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 872 | static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip) |
| 873 | { |
| 874 | union power_supply_propval ret = {0,}; |
| 875 | int rc, vbat; |
| 876 | |
| 877 | if (chip->batt_psy == NULL) |
| 878 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 879 | if (chip->batt_psy) { |
| 880 | /* if battery has been registered, use the ocv property */ |
| 881 | rc = chip->batt_psy->get_property(chip->batt_psy, |
| 882 | POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret); |
| 883 | if (rc) { |
| 884 | /* |
| 885 | * Default to vbatt if the battery OCV is not |
| 886 | * registered. |
| 887 | */ |
| 888 | pr_debug("Battery psy does not have voltage ocv\n"); |
| 889 | rc = get_battery_voltage(chip, &vbat); |
| 890 | if (rc) |
| 891 | return -EINVAL; |
| 892 | return vbat; |
| 893 | } |
| 894 | return ret.intval; |
| 895 | } |
| 896 | |
| 897 | pr_debug("battery power supply is not registered\n"); |
| 898 | return -EINVAL; |
| 899 | } |
| 900 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 901 | static bool is_batfet_closed(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 902 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 903 | union power_supply_propval ret = {0,}; |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 904 | int rc; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 905 | |
| 906 | if (chip->batt_psy == NULL) |
| 907 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 908 | if (chip->batt_psy) { |
| 909 | /* if battery has been registered, use the online property */ |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 910 | rc = chip->batt_psy->get_property(chip->batt_psy, |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 911 | POWER_SUPPLY_PROP_ONLINE, &ret); |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 912 | if (rc) { |
| 913 | pr_debug("Battery does not export online: %d\n", rc); |
| 914 | return true; |
| 915 | } |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 916 | return !!ret.intval; |
| 917 | } |
| 918 | |
| 919 | /* Default to true if the battery power supply is not registered. */ |
| 920 | pr_debug("battery power supply is not registered\n"); |
| 921 | return true; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip, |
| 925 | int *ibat_ua, int *vbat_uv) |
| 926 | { |
| 927 | struct qpnp_iadc_result i_result; |
| 928 | struct qpnp_vadc_result v_result; |
| 929 | enum qpnp_iadc_channels iadc_channel; |
| 930 | int rc; |
| 931 | |
| 932 | iadc_channel = chip->use_external_rsense ? |
| 933 | EXTERNAL_RSENSE : INTERNAL_RSENSE; |
Xiaozhe Shi | 8658c98 | 2013-04-30 11:33:07 -0700 | [diff] [blame] | 934 | if (is_battery_full(chip)) { |
| 935 | rc = get_battery_current(chip, ibat_ua); |
| 936 | if (rc) { |
| 937 | pr_err("bms current read failed with rc: %d\n", rc); |
| 938 | return rc; |
| 939 | } |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 940 | rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result); |
Xiaozhe Shi | 8658c98 | 2013-04-30 11:33:07 -0700 | [diff] [blame] | 941 | if (rc) { |
| 942 | pr_err("vadc read failed with rc: %d\n", rc); |
| 943 | return rc; |
| 944 | } |
| 945 | *vbat_uv = (int)v_result.physical; |
| 946 | } else { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 947 | rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev, |
| 948 | iadc_channel, &i_result, |
Xiaozhe Shi | 8658c98 | 2013-04-30 11:33:07 -0700 | [diff] [blame] | 949 | VBAT_SNS, &v_result); |
| 950 | if (rc) { |
| 951 | pr_err("adc sync read failed with rc: %d\n", rc); |
| 952 | return rc; |
| 953 | } |
| 954 | /* |
| 955 | * reverse the current read by the iadc, since the bms uses |
| 956 | * flipped battery current polarity. |
| 957 | */ |
| 958 | *ibat_ua = -1 * (int)i_result.result_ua; |
| 959 | *vbat_uv = (int)v_result.physical; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 960 | } |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Xiaozhe Shi | 3ede2ad | 2014-02-04 13:25:38 -0800 | [diff] [blame] | 965 | static int get_rbatt(struct qpnp_bms_chip *chip, |
| 966 | int soc_rbatt_mohm, int batt_temp) |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 967 | { |
Xiaozhe Shi | 3ede2ad | 2014-02-04 13:25:38 -0800 | [diff] [blame] | 968 | int rbatt_mohm, scalefactor; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 969 | |
Xiaozhe Shi | 3ede2ad | 2014-02-04 13:25:38 -0800 | [diff] [blame] | 970 | rbatt_mohm = chip->default_rbatt_mohm; |
| 971 | if (chip->rbatt_sf_lut == NULL) { |
| 972 | pr_debug("RBATT = %d\n", rbatt_mohm); |
| 973 | return rbatt_mohm; |
| 974 | } |
| 975 | /* Convert the batt_temp to DegC from deciDegC */ |
| 976 | scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut, |
| 977 | batt_temp, soc_rbatt_mohm); |
| 978 | rbatt_mohm = (rbatt_mohm * scalefactor) / 100; |
| 979 | |
| 980 | rbatt_mohm += chip->r_conn_mohm; |
| 981 | rbatt_mohm += chip->rbatt_capacitive_mohm; |
| 982 | return rbatt_mohm; |
| 983 | } |
| 984 | |
| 985 | #define DEFAULT_RBATT_SOC 50 |
| 986 | static int estimate_ocv(struct qpnp_bms_chip *chip, int batt_temp) |
| 987 | { |
| 988 | int ibat_ua, vbat_uv, ocv_est_uv, rbatt_mohm, rc; |
| 989 | |
| 990 | rbatt_mohm = get_rbatt(chip, DEFAULT_RBATT_SOC, batt_temp); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 991 | rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv); |
| 992 | if (rc) { |
| 993 | pr_err("simultaneous failed rc = %d\n", rc); |
| 994 | return rc; |
| 995 | } |
| 996 | |
| 997 | ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000; |
Xiaozhe Shi | 3ede2ad | 2014-02-04 13:25:38 -0800 | [diff] [blame] | 998 | pr_debug("estimated pon ocv = %d, vbat_uv = %d ibat_ua = %d rbatt_mohm = %d\n", |
| 999 | ocv_est_uv, vbat_uv, ibat_ua, rbatt_mohm); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1000 | return ocv_est_uv; |
| 1001 | } |
| 1002 | |
Xiaozhe Shi | 2c17117 | 2013-12-03 13:27:37 -0800 | [diff] [blame] | 1003 | #define MIN_IAVG_MA 250 |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1004 | static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp) |
| 1005 | { |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 1006 | chip->last_ocv_uv = chip->insertion_ocv_uv; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1007 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1008 | chip->last_soc = -EINVAL; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1009 | chip->last_soc_invalid = true; |
| 1010 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1011 | chip->soc_at_cv = -EINVAL; |
| 1012 | chip->shutdown_soc_invalid = true; |
| 1013 | chip->shutdown_soc = 0; |
Xiaozhe Shi | 2c17117 | 2013-12-03 13:27:37 -0800 | [diff] [blame] | 1014 | chip->shutdown_iavg_ma = MIN_IAVG_MA; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1015 | chip->prev_pc_unusable = -EINVAL; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1016 | reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1017 | chip->software_cc_uah = 0; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1018 | chip->software_shdw_cc_uah = 0; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1019 | chip->last_cc_uah = INT_MIN; |
| 1020 | chip->last_ocv_temp = batt_temp; |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1021 | chip->prev_batt_terminal_uv = 0; |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 1022 | if (chip->enable_fcc_learning) { |
| 1023 | chip->adjusted_fcc_temp_lut = NULL; |
| 1024 | chip->fcc_new_mah = -EINVAL; |
| 1025 | /* reset the charge-cycle and charge-increase registers */ |
| 1026 | chip->charge_increase = 0; |
| 1027 | chip->charge_cycles = 0; |
| 1028 | backup_charge_cycle(chip); |
| 1029 | /* discard all the FCC learnt data and reset the local table */ |
| 1030 | discard_backup_fcc_data(chip); |
| 1031 | memset(chip->fcc_learning_samples, 0, |
| 1032 | chip->min_fcc_learning_samples * |
| 1033 | sizeof(struct fcc_sample)); |
| 1034 | } |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1037 | #define SIGN(x) ((x) < 0 ? -1 : 1) |
| 1038 | #define UV_PER_SPIN 50000 |
| 1039 | static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc) |
| 1040 | { |
| 1041 | int new_pc; |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1042 | int ocv_mv; |
| 1043 | int delta_mv = 5; |
| 1044 | int max_spin_count; |
| 1045 | int count = 0; |
| 1046 | int sign, new_sign; |
| 1047 | |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 1048 | ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp, pc); |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1049 | |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 1050 | new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp, ocv_mv); |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1051 | pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv); |
| 1052 | max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv) |
| 1053 | / UV_PER_SPIN; |
| 1054 | sign = SIGN(pc - new_pc); |
| 1055 | |
| 1056 | while (abs(new_pc - pc) != 0 && count < max_spin_count) { |
| 1057 | /* |
| 1058 | * If the newly interpolated pc is larger than the lookup pc, |
| 1059 | * the ocv should be reduced and vice versa |
| 1060 | */ |
| 1061 | new_sign = SIGN(pc - new_pc); |
| 1062 | /* |
| 1063 | * If the sign has changed, then we have passed the lookup pc. |
| 1064 | * reduce the ocv step size to get finer results. |
| 1065 | * |
| 1066 | * If we have already reduced the ocv step size and still |
| 1067 | * passed the lookup pc, just stop and use the current ocv. |
| 1068 | * This can only happen if the batterydata profile is |
| 1069 | * non-monotonic anyways. |
| 1070 | */ |
| 1071 | if (new_sign != sign) { |
| 1072 | if (delta_mv > 1) |
| 1073 | delta_mv = 1; |
| 1074 | else |
| 1075 | break; |
| 1076 | } |
| 1077 | sign = new_sign; |
| 1078 | |
| 1079 | ocv_mv = ocv_mv + delta_mv * sign; |
| 1080 | new_pc = interpolate_pc(chip->pc_temp_ocv_lut, |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 1081 | batt_temp, ocv_mv); |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1082 | pr_debug("test revlookup pc = %d for ocv = %d\n", |
| 1083 | new_pc, ocv_mv); |
| 1084 | count++; |
| 1085 | } |
| 1086 | |
| 1087 | return ocv_mv * 1000; |
| 1088 | } |
| 1089 | |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 1090 | #define OCV_RAW_UNINITIALIZED 0xFFFF |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 1091 | #define MIN_OCV_UV 2000000 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1092 | static int read_soc_params_raw(struct qpnp_bms_chip *chip, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1093 | struct raw_soc_params *raw, |
| 1094 | int batt_temp) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1095 | { |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1096 | int warm_reset, rc; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1097 | |
| 1098 | mutex_lock(&chip->bms_output_lock); |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 1099 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1100 | lock_output_data(chip); |
| 1101 | |
| 1102 | rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw, |
| 1103 | chip->base + BMS1_OCV_FOR_SOC_DATA0, 2); |
| 1104 | if (rc) { |
| 1105 | pr_err("Error reading ocv: rc = %d\n", rc); |
Xiaozhe Shi | abbd607 | 2013-12-11 14:24:02 -0800 | [diff] [blame] | 1106 | goto param_err; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1109 | rc = read_cc_raw(chip, &raw->cc, CC); |
Xiaozhe Shi | ed4a552 | 2014-09-05 14:56:13 -0700 | [diff] [blame] | 1110 | rc |= read_cc_raw(chip, &raw->shdw_cc, SHDW_CC); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1111 | if (rc) { |
| 1112 | pr_err("Failed to read raw cc data, rc = %d\n", rc); |
Xiaozhe Shi | abbd607 | 2013-12-11 14:24:02 -0800 | [diff] [blame] | 1113 | goto param_err; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | unlock_output_data(chip); |
| 1117 | mutex_unlock(&chip->bms_output_lock); |
| 1118 | |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 1119 | if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) { |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 1120 | convert_and_store_ocv(chip, raw, batt_temp, true); |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 1121 | pr_debug("PON_OCV_UV = %d, cc = %llx\n", |
| 1122 | chip->last_ocv_uv, raw->cc); |
| 1123 | warm_reset = qpnp_pon_is_warm_reset(); |
Xiaozhe Shi | 3ede2ad | 2014-02-04 13:25:38 -0800 | [diff] [blame] | 1124 | if (raw->last_good_ocv_uv < MIN_OCV_UV || warm_reset > 0) { |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 1125 | pr_debug("OCV is stale or bad, estimating new OCV.\n"); |
Xiaozhe Shi | 3ede2ad | 2014-02-04 13:25:38 -0800 | [diff] [blame] | 1126 | chip->last_ocv_uv = estimate_ocv(chip, batt_temp); |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 1127 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1128 | reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC); |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 1129 | pr_debug("New PON_OCV_UV = %d, cc = %llx\n", |
| 1130 | chip->last_ocv_uv, raw->cc); |
| 1131 | } |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1132 | } else if (chip->new_battery) { |
| 1133 | /* if a new battery was inserted, estimate the ocv */ |
| 1134 | reset_for_new_battery(chip, batt_temp); |
| 1135 | raw->cc = 0; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1136 | raw->shdw_cc = 0; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1137 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
| 1138 | chip->new_battery = false; |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 1139 | } else if (chip->done_charging) { |
| 1140 | chip->done_charging = false; |
| 1141 | /* if we just finished charging, reset CC and fake 100% */ |
| 1142 | chip->ocv_reading_at_100 = raw->last_good_ocv_raw; |
Xiaozhe Shi | 39a5dc6 | 2013-10-30 11:33:27 -0700 | [diff] [blame] | 1143 | chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100); |
| 1144 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 1145 | raw->cc = 0; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1146 | raw->shdw_cc = 0; |
| 1147 | reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC); |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 1148 | chip->last_ocv_temp = batt_temp; |
| 1149 | chip->software_cc_uah = 0; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1150 | chip->software_shdw_cc_uah = 0; |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 1151 | chip->last_cc_uah = INT_MIN; |
| 1152 | pr_debug("EOC Battery full ocv_reading = 0x%x\n", |
| 1153 | chip->ocv_reading_at_100); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1154 | } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) { |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 1155 | convert_and_store_ocv(chip, raw, batt_temp, false); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1156 | /* forget the old cc value upon ocv */ |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1157 | chip->last_cc_uah = INT_MIN; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1158 | } else { |
| 1159 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
| 1160 | } |
| 1161 | |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 1162 | /* stop faking a high OCV if we get a new OCV */ |
| 1163 | if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 1164 | chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 1165 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1166 | pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n", |
| 1167 | raw->last_good_ocv_raw, raw->last_good_ocv_uv); |
| 1168 | pr_debug("cc_raw= 0x%llx\n", raw->cc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1169 | return 0; |
Xiaozhe Shi | abbd607 | 2013-12-11 14:24:02 -0800 | [diff] [blame] | 1170 | |
| 1171 | param_err: |
| 1172 | unlock_output_data(chip); |
| 1173 | mutex_unlock(&chip->bms_output_lock); |
| 1174 | return rc; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1177 | static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv, |
| 1178 | int batt_temp) |
| 1179 | { |
| 1180 | int pc; |
| 1181 | |
| 1182 | pc = interpolate_pc(chip->pc_temp_ocv_lut, |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 1183 | batt_temp, ocv_uv / 1000); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1184 | pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n", |
| 1185 | pc, ocv_uv, batt_temp); |
| 1186 | /* Multiply the initial FCC value by the scale factor. */ |
| 1187 | return pc; |
| 1188 | } |
| 1189 | |
| 1190 | static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp) |
| 1191 | { |
| 1192 | int fcc_uah; |
| 1193 | |
| 1194 | if (chip->adjusted_fcc_temp_lut == NULL) { |
| 1195 | /* interpolate_fcc returns a mv value. */ |
| 1196 | fcc_uah = interpolate_fcc(chip->fcc_temp_lut, |
| 1197 | batt_temp) * 1000; |
| 1198 | pr_debug("fcc = %d uAh\n", fcc_uah); |
| 1199 | return fcc_uah; |
| 1200 | } else { |
| 1201 | return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut, |
| 1202 | batt_temp); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* calculate remaining charge at the time of ocv */ |
| 1207 | static int calculate_ocv_charge(struct qpnp_bms_chip *chip, |
| 1208 | struct raw_soc_params *raw, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1209 | int fcc_uah) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1210 | { |
| 1211 | int ocv_uv, pc; |
| 1212 | |
| 1213 | ocv_uv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1214 | pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1215 | pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc); |
| 1216 | return (fcc_uah * pc) / 100; |
| 1217 | } |
| 1218 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1219 | #define CC_READING_TICKS 56 |
| 1220 | #define SLEEP_CLK_HZ 32764 |
| 1221 | #define SECONDS_PER_HOUR 3600 |
| 1222 | |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 1223 | static s64 cc_uv_to_pvh(s64 cc_uv) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1224 | { |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 1225 | /* Note that it is necessary need to multiply by 1000000 to convert |
| 1226 | * from uvh to pvh here. |
| 1227 | * However, the maximum Coulomb Counter value is 2^35, which can cause |
| 1228 | * an over flow. |
| 1229 | * Multiply by 100000 first to perserve as much precision as possible |
| 1230 | * then multiply by 10 after doing the division in order to avoid |
| 1231 | * overflow on the maximum Coulomb Counter value. |
| 1232 | */ |
| 1233 | return div_s64(cc_uv * CC_READING_TICKS * 100000, |
| 1234 | SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 1238 | * calculate_cc() - converts a hardware coulomb counter reading into uah |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1239 | * @chip: the bms chip pointer |
| 1240 | * @cc: the cc reading from bms h/w |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1241 | * @cc_type: calcualte cc from regular or shadow coulomb counter |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 1242 | * @clear_cc: whether this function should clear the hardware counter |
| 1243 | * after reading |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1244 | * |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 1245 | * Converts the 64 bit hardware coulomb counter into microamp-hour by taking |
| 1246 | * into account hardware resolution and adc errors. |
| 1247 | * |
| 1248 | * Return: the coulomb counter based charge in uAh (micro-amp hour) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1249 | */ |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1250 | static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc, |
| 1251 | int cc_type, int clear_cc) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1252 | { |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 1253 | struct qpnp_iadc_calib calibration; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1254 | struct qpnp_vadc_result result; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1255 | int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter; |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 1256 | int rc; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1257 | |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1258 | software_counter = cc_type == SHDW_CC ? |
| 1259 | &chip->software_shdw_cc_uah : &chip->software_cc_uah; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1260 | rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1261 | if (rc) { |
| 1262 | pr_err("could not read pmic die temperature: %d\n", rc); |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1263 | return *software_counter; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1266 | qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration); |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1267 | pr_debug("%scc = %lld, die_temp = %lld\n", |
| 1268 | cc_type == SHDW_CC ? "shdw_" : "", |
| 1269 | cc, result.physical); |
Xiaozhe Shi | 8f5dd1b | 2013-04-30 10:27:58 -0700 | [diff] [blame] | 1270 | cc_voltage_uv = cc_reading_to_uv(cc); |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 1271 | cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv, |
| 1272 | calibration.gain_raw |
| 1273 | - calibration.offset_raw); |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 1274 | cc_pvh = cc_uv_to_pvh(cc_voltage_uv); |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 1275 | cc_uah = div_s64(cc_pvh, chip->r_sense_uohm); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1276 | rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1277 | if (rc) |
| 1278 | pr_debug("error compensation failed: %d\n", rc); |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1279 | if (clear_cc == RESET) { |
| 1280 | pr_debug("software_%scc = %lld, added cc_uah = %lld\n", |
| 1281 | cc_type == SHDW_CC ? "sw_" : "", |
| 1282 | *software_counter, cc_uah); |
| 1283 | *software_counter += cc_uah; |
| 1284 | reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC); |
| 1285 | return (int)*software_counter; |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 1286 | } else { |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1287 | pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n", |
| 1288 | cc_type == SHDW_CC ? "shdw_" : "", |
| 1289 | *software_counter, cc_uah, |
| 1290 | *software_counter + cc_uah); |
| 1291 | return *software_counter + cc_uah; |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 1292 | } |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Xiaozhe Shi | 06b67cc | 2013-03-29 12:07:40 -0700 | [diff] [blame] | 1295 | #define IAVG_MINIMAL_TIME 2 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1296 | static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah, |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1297 | int *iavg_ua, int delta_time_s) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1298 | { |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1299 | int delta_cc_uah = 0; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1300 | |
Xiaozhe Shi | 06b67cc | 2013-03-29 12:07:40 -0700 | [diff] [blame] | 1301 | /* |
| 1302 | * use the battery current if called too quickly |
| 1303 | */ |
| 1304 | if (delta_time_s < IAVG_MINIMAL_TIME |
| 1305 | || chip->last_cc_uah == INT_MIN) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1306 | get_battery_current(chip, iavg_ua); |
| 1307 | goto out; |
| 1308 | } |
| 1309 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1310 | delta_cc_uah = cc_uah - chip->last_cc_uah; |
| 1311 | |
| 1312 | *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s); |
| 1313 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1314 | out: |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1315 | 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] | 1316 | |
| 1317 | /* remember cc_uah */ |
| 1318 | chip->last_cc_uah = cc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | static int calculate_termination_uuc(struct qpnp_bms_chip *chip, |
| 1322 | struct soc_params *params, |
| 1323 | int batt_temp, int uuc_iavg_ma, |
| 1324 | int *ret_pc_unusable) |
| 1325 | { |
| 1326 | int unusable_uv, pc_unusable, uuc_uah; |
| 1327 | int i = 0; |
| 1328 | int ocv_mv; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1329 | int rbatt_mohm; |
| 1330 | int delta_uv; |
| 1331 | int prev_delta_uv = 0; |
| 1332 | int prev_rbatt_mohm = 0; |
| 1333 | int uuc_rbatt_mohm; |
| 1334 | |
| 1335 | for (i = 0; i <= 100; i++) { |
| 1336 | ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 1337 | batt_temp, i); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1338 | rbatt_mohm = get_rbatt(chip, i, batt_temp); |
| 1339 | unusable_uv = (rbatt_mohm * uuc_iavg_ma) |
| 1340 | + (chip->v_cutoff_uv); |
| 1341 | delta_uv = ocv_mv * 1000 - unusable_uv; |
| 1342 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1343 | if (delta_uv > 0) |
| 1344 | break; |
| 1345 | |
| 1346 | prev_delta_uv = delta_uv; |
| 1347 | prev_rbatt_mohm = rbatt_mohm; |
| 1348 | } |
| 1349 | |
| 1350 | uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv, |
| 1351 | prev_rbatt_mohm, prev_delta_uv, |
| 1352 | 0); |
| 1353 | |
| 1354 | unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv); |
| 1355 | |
| 1356 | pc_unusable = calculate_pc(chip, unusable_uv, batt_temp); |
| 1357 | uuc_uah = (params->fcc_uah * pc_unusable) / 100; |
Xiaozhe Shi | 794607c | 2013-02-19 10:25:59 -0800 | [diff] [blame] | 1358 | 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] | 1359 | uuc_iavg_ma, |
| 1360 | uuc_rbatt_mohm, unusable_uv, |
Xiaozhe Shi | 794607c | 2013-02-19 10:25:59 -0800 | [diff] [blame] | 1361 | pc_unusable, i, uuc_uah); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1362 | *ret_pc_unusable = pc_unusable; |
| 1363 | return uuc_uah; |
| 1364 | } |
| 1365 | |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1366 | #define TIME_PER_PERCENT_UUC 60 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1367 | static int adjust_uuc(struct qpnp_bms_chip *chip, |
| 1368 | struct soc_params *params, |
| 1369 | int new_pc_unusable, |
| 1370 | int new_uuc_uah, |
| 1371 | int batt_temp) |
| 1372 | { |
| 1373 | int new_unusable_mv, new_iavg_ma; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1374 | int max_percent_change; |
| 1375 | |
| 1376 | max_percent_change = max(params->delta_time_s |
| 1377 | / TIME_PER_PERCENT_UUC, 1); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1378 | |
Xiaozhe Shi | 2c17117 | 2013-12-03 13:27:37 -0800 | [diff] [blame] | 1379 | if (chip->first_time_calc_uuc || chip->prev_pc_unusable == -EINVAL |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1380 | || abs(chip->prev_pc_unusable - new_pc_unusable) |
| 1381 | <= max_percent_change) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1382 | chip->prev_pc_unusable = new_pc_unusable; |
| 1383 | return new_uuc_uah; |
| 1384 | } |
| 1385 | |
| 1386 | /* the uuc is trying to change more than 1% restrict it */ |
| 1387 | if (new_pc_unusable > chip->prev_pc_unusable) |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1388 | chip->prev_pc_unusable += max_percent_change; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1389 | else |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1390 | chip->prev_pc_unusable -= max_percent_change; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1391 | |
| 1392 | new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100; |
| 1393 | |
| 1394 | /* also find update the iavg_ma accordingly */ |
| 1395 | new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut, |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 1396 | batt_temp, chip->prev_pc_unusable); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1397 | if (new_unusable_mv < chip->v_cutoff_uv/1000) |
| 1398 | new_unusable_mv = chip->v_cutoff_uv/1000; |
| 1399 | |
| 1400 | new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv) |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 1401 | / params->rbatt_mohm; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1402 | if (new_iavg_ma == 0) |
| 1403 | new_iavg_ma = 1; |
| 1404 | chip->prev_uuc_iavg_ma = new_iavg_ma; |
| 1405 | pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n", |
| 1406 | new_uuc_uah, chip->prev_pc_unusable, |
| 1407 | new_unusable_mv, new_iavg_ma); |
| 1408 | |
| 1409 | return new_uuc_uah; |
| 1410 | } |
| 1411 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1412 | static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip, |
| 1413 | struct soc_params *params, |
| 1414 | int batt_temp) |
| 1415 | { |
| 1416 | int uuc_uah_iavg; |
| 1417 | int i; |
| 1418 | int uuc_iavg_ma = params->iavg_ua / 1000; |
| 1419 | int pc_unusable; |
| 1420 | |
| 1421 | /* |
| 1422 | * if called first time, fill all the samples with |
| 1423 | * the shutdown_iavg_ma |
| 1424 | */ |
| 1425 | if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) { |
| 1426 | pr_debug("Using shutdown_iavg_ma = %d in all samples\n", |
| 1427 | chip->shutdown_iavg_ma); |
| 1428 | for (i = 0; i < IAVG_SAMPLES; i++) |
| 1429 | chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma; |
| 1430 | |
| 1431 | chip->iavg_index = 0; |
| 1432 | chip->iavg_num_samples = IAVG_SAMPLES; |
| 1433 | } |
| 1434 | |
Xiaozhe Shi | 7063392 | 2013-09-23 15:50:53 -0700 | [diff] [blame] | 1435 | if (params->delta_time_s >= IAVG_MINIMAL_TIME) { |
| 1436 | /* |
| 1437 | * if charging use a nominal avg current to keep |
| 1438 | * a reasonable UUC while charging |
| 1439 | */ |
| 1440 | if (uuc_iavg_ma < MIN_IAVG_MA) |
| 1441 | uuc_iavg_ma = MIN_IAVG_MA; |
| 1442 | chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma; |
| 1443 | chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES; |
| 1444 | chip->iavg_num_samples++; |
| 1445 | if (chip->iavg_num_samples >= IAVG_SAMPLES) |
| 1446 | chip->iavg_num_samples = IAVG_SAMPLES; |
| 1447 | } |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1448 | |
| 1449 | /* now that this sample is added calcualte the average */ |
| 1450 | uuc_iavg_ma = 0; |
| 1451 | if (chip->iavg_num_samples != 0) { |
| 1452 | for (i = 0; i < chip->iavg_num_samples; i++) { |
| 1453 | pr_debug("iavg_samples_ma[%d] = %d\n", i, |
| 1454 | chip->iavg_samples_ma[i]); |
| 1455 | uuc_iavg_ma += chip->iavg_samples_ma[i]; |
| 1456 | } |
| 1457 | |
| 1458 | uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma, |
| 1459 | chip->iavg_num_samples); |
| 1460 | } |
| 1461 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1462 | /* |
| 1463 | * if we're in bms reset mode, force uuc to be 3% of fcc |
| 1464 | */ |
| 1465 | if (bms_reset) |
| 1466 | return (params->fcc_uah * 3) / 100; |
| 1467 | |
Xiaozhe Shi | 75e5efe | 2013-02-07 09:51:43 -0800 | [diff] [blame] | 1468 | uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp, |
| 1469 | uuc_iavg_ma, &pc_unusable); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1470 | pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n", |
| 1471 | uuc_iavg_ma, uuc_uah_iavg); |
| 1472 | |
| 1473 | chip->prev_uuc_iavg_ma = uuc_iavg_ma; |
| 1474 | /* restrict the uuc such that it can increase only by one percent */ |
| 1475 | uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable, |
| 1476 | uuc_uah_iavg, batt_temp); |
| 1477 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1478 | return uuc_uah_iavg; |
| 1479 | } |
| 1480 | |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 1481 | static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip, |
| 1482 | struct soc_params *params, int soc) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1483 | { |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 1484 | return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah), |
| 1485 | 100) + params->cc_uah + params->uuc_uah; |
| 1486 | } |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1487 | |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 1488 | static int find_pc_for_soc(struct qpnp_bms_chip *chip, |
| 1489 | struct soc_params *params, int soc) |
| 1490 | { |
| 1491 | int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc); |
| 1492 | int pc; |
| 1493 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1494 | pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah); |
| 1495 | pc = clamp(pc, 0, 100); |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 1496 | pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n", |
| 1497 | soc, params->fcc_uah, params->uuc_uah, |
| 1498 | ocv_charge_uah, pc); |
| 1499 | return pc; |
| 1500 | } |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1501 | |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1502 | static int get_current_time(unsigned long *now_tm_sec) |
| 1503 | { |
| 1504 | struct rtc_time tm; |
| 1505 | struct rtc_device *rtc; |
| 1506 | int rc; |
| 1507 | |
| 1508 | rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
| 1509 | if (rtc == NULL) { |
| 1510 | pr_err("%s: unable to open rtc device (%s)\n", |
| 1511 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); |
Xiaozhe Shi | 0e01af6 | 2013-05-06 12:56:08 -0700 | [diff] [blame] | 1512 | return -EINVAL; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | rc = rtc_read_time(rtc, &tm); |
| 1516 | if (rc) { |
| 1517 | pr_err("Error reading rtc device (%s) : %d\n", |
| 1518 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
| 1519 | goto close_time; |
| 1520 | } |
| 1521 | |
| 1522 | rc = rtc_valid_tm(&tm); |
| 1523 | if (rc) { |
| 1524 | pr_err("Invalid RTC time (%s): %d\n", |
| 1525 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
| 1526 | goto close_time; |
| 1527 | } |
| 1528 | rtc_tm_to_time(&tm, now_tm_sec); |
| 1529 | |
| 1530 | close_time: |
| 1531 | rtc_class_close(rtc); |
| 1532 | return rc; |
| 1533 | } |
| 1534 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 1535 | /* Returns estimated battery resistance */ |
| 1536 | static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip) |
| 1537 | { |
| 1538 | return chip->rbatt_mohm * 1000; |
| 1539 | } |
| 1540 | |
| 1541 | /* Returns instantaneous current in uA */ |
| 1542 | static int get_prop_bms_current_now(struct qpnp_bms_chip *chip) |
| 1543 | { |
| 1544 | int rc, result_ua; |
| 1545 | |
| 1546 | rc = get_battery_current(chip, &result_ua); |
| 1547 | if (rc) { |
| 1548 | pr_err("failed to get current: %d\n", rc); |
| 1549 | return rc; |
| 1550 | } |
| 1551 | return result_ua; |
| 1552 | } |
| 1553 | |
| 1554 | /* Returns coulomb counter in uAh */ |
| 1555 | static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip) |
| 1556 | { |
| 1557 | int64_t cc_raw; |
| 1558 | |
| 1559 | mutex_lock(&chip->bms_output_lock); |
| 1560 | lock_output_data(chip); |
Xiaozhe Shi | e11c949 | 2013-12-11 14:37:36 -0800 | [diff] [blame] | 1561 | read_cc_raw(chip, &cc_raw, CC); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 1562 | unlock_output_data(chip); |
| 1563 | mutex_unlock(&chip->bms_output_lock); |
| 1564 | |
| 1565 | return calculate_cc(chip, cc_raw, CC, NORESET); |
| 1566 | } |
| 1567 | |
| 1568 | /* Returns shadow coulomb counter in uAh */ |
| 1569 | static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip) |
| 1570 | { |
| 1571 | int64_t cc_raw; |
| 1572 | |
| 1573 | mutex_lock(&chip->bms_output_lock); |
| 1574 | lock_output_data(chip); |
Xiaozhe Shi | e11c949 | 2013-12-11 14:37:36 -0800 | [diff] [blame] | 1575 | read_cc_raw(chip, &cc_raw, SHDW_CC); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 1576 | unlock_output_data(chip); |
| 1577 | mutex_unlock(&chip->bms_output_lock); |
| 1578 | |
| 1579 | return calculate_cc(chip, cc_raw, SHDW_CC, NORESET); |
| 1580 | } |
| 1581 | |
| 1582 | /* Returns full charge design in uAh */ |
| 1583 | static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip) |
| 1584 | { |
| 1585 | return chip->fcc_mah * 1000; |
| 1586 | } |
| 1587 | |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 1588 | /* Returns the current full charge in uAh */ |
| 1589 | static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip) |
| 1590 | { |
| 1591 | int rc; |
| 1592 | struct qpnp_vadc_result result; |
| 1593 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1594 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result); |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 1595 | if (rc) { |
| 1596 | pr_err("Unable to read battery temperature\n"); |
| 1597 | return rc; |
| 1598 | } |
| 1599 | |
| 1600 | return calculate_fcc(chip, (int)result.physical); |
| 1601 | } |
| 1602 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1603 | 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] | 1604 | { |
| 1605 | unsigned long now_tm_sec = 0; |
| 1606 | |
| 1607 | /* default to delta time = 0 if anything fails */ |
| 1608 | *delta_time_s = 0; |
| 1609 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1610 | if (get_current_time(&now_tm_sec)) { |
| 1611 | pr_err("RTC read failed\n"); |
| 1612 | return 0; |
| 1613 | } |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1614 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1615 | *delta_time_s = (now_tm_sec - *time_stamp); |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1616 | |
| 1617 | /* remember this time */ |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1618 | *time_stamp = now_tm_sec; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1622 | static void calculate_soc_params(struct qpnp_bms_chip *chip, |
| 1623 | struct raw_soc_params *raw, |
| 1624 | struct soc_params *params, |
| 1625 | int batt_temp) |
| 1626 | { |
Xiaozhe Shi | 219cb22 | 2013-06-10 15:49:59 -0700 | [diff] [blame] | 1627 | int soc_rbatt, shdw_cc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1628 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1629 | calculate_delta_time(&chip->tm_sec, ¶ms->delta_time_s); |
| 1630 | pr_debug("tm_sec = %ld, delta_s = %d\n", |
| 1631 | chip->tm_sec, params->delta_time_s); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1632 | params->fcc_uah = calculate_fcc(chip, batt_temp); |
| 1633 | pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp); |
| 1634 | |
| 1635 | /* calculate remainging charge */ |
| 1636 | params->ocv_charge_uah = calculate_ocv_charge( |
| 1637 | chip, raw, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1638 | params->fcc_uah); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1639 | pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah); |
| 1640 | |
| 1641 | /* calculate cc micro_volt_hour */ |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1642 | params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET); |
Xiaozhe Shi | 219cb22 | 2013-06-10 15:49:59 -0700 | [diff] [blame] | 1643 | shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET); |
| 1644 | pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n", |
| 1645 | params->cc_uah, raw->cc, |
| 1646 | shdw_cc_uah, raw->shdw_cc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1647 | |
| 1648 | soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100) |
| 1649 | / params->fcc_uah; |
| 1650 | if (soc_rbatt < 0) |
| 1651 | soc_rbatt = 0; |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 1652 | params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp); |
Xiaozhe Shi | 794607c | 2013-02-19 10:25:59 -0800 | [diff] [blame] | 1653 | pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1654 | |
Xiaozhe Shi | 1e87cda | 2013-05-17 10:18:56 -0700 | [diff] [blame] | 1655 | if (params->rbatt_mohm != chip->rbatt_mohm) { |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 1656 | chip->rbatt_mohm = params->rbatt_mohm; |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 1657 | if (chip->bms_psy_registered) |
Xiaozhe Shi | 1e87cda | 2013-05-17 10:18:56 -0700 | [diff] [blame] | 1658 | power_supply_changed(&chip->bms_psy); |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1661 | calculate_iavg(chip, params->cc_uah, ¶ms->iavg_ua, |
| 1662 | params->delta_time_s); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1663 | |
| 1664 | params->uuc_uah = calculate_unusable_charge_uah(chip, params, |
| 1665 | batt_temp); |
| 1666 | pr_debug("UUC = %uuAh\n", params->uuc_uah); |
| 1667 | } |
| 1668 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1669 | static int bound_soc(int soc) |
| 1670 | { |
| 1671 | soc = max(0, soc); |
| 1672 | soc = min(100, soc); |
| 1673 | return soc; |
| 1674 | } |
| 1675 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1676 | #define IBAT_TOL_MASK 0x0F |
| 1677 | #define OCV_TOL_MASK 0xF0 |
| 1678 | #define IBAT_TOL_DEFAULT 0x03 |
| 1679 | #define IBAT_TOL_NOCHG 0x0F |
| 1680 | #define OCV_TOL_DEFAULT 0x20 |
| 1681 | #define OCV_TOL_NO_OCV 0x00 |
| 1682 | static int stop_ocv_updates(struct qpnp_bms_chip *chip) |
| 1683 | { |
| 1684 | pr_debug("stopping ocv updates\n"); |
| 1685 | return qpnp_masked_write(chip, BMS1_TOL_CTL, |
| 1686 | OCV_TOL_MASK, OCV_TOL_NO_OCV); |
| 1687 | } |
| 1688 | |
| 1689 | static int reset_bms_for_test(struct qpnp_bms_chip *chip) |
| 1690 | { |
Xiaozhe Shi | 95da77f | 2013-02-20 13:40:06 -0800 | [diff] [blame] | 1691 | int ibat_ua = 0, vbat_uv = 0, rc; |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1692 | int ocv_est_uv; |
| 1693 | |
| 1694 | if (!chip) { |
| 1695 | pr_err("BMS driver has not been initialized yet!\n"); |
| 1696 | return -EINVAL; |
| 1697 | } |
| 1698 | |
| 1699 | rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv); |
| 1700 | |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 1701 | /* |
| 1702 | * Don't include rbatt and rbatt_capacitative since we expect this to |
| 1703 | * be used with a fake battery which does not have internal resistances |
| 1704 | */ |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1705 | ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000; |
| 1706 | pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv); |
| 1707 | chip->last_ocv_uv = ocv_est_uv; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1708 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1709 | chip->last_soc = -EINVAL; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1710 | chip->last_soc_invalid = true; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1711 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1712 | reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1713 | chip->software_cc_uah = 0; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 1714 | chip->software_shdw_cc_uah = 0; |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1715 | chip->last_cc_uah = INT_MIN; |
| 1716 | stop_ocv_updates(chip); |
| 1717 | |
| 1718 | pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n", |
| 1719 | chip->last_ocv_uv, vbat_uv, ibat_ua); |
| 1720 | |
| 1721 | return rc; |
| 1722 | } |
| 1723 | |
| 1724 | static int bms_reset_set(const char *val, const struct kernel_param *kp) |
| 1725 | { |
| 1726 | int rc; |
| 1727 | |
| 1728 | rc = param_set_bool(val, kp); |
| 1729 | if (rc) { |
| 1730 | pr_err("Unable to set bms_reset: %d\n", rc); |
| 1731 | return rc; |
| 1732 | } |
| 1733 | |
| 1734 | if (*(bool *)kp->arg) { |
| 1735 | struct power_supply *bms_psy = power_supply_get_by_name("bms"); |
| 1736 | struct qpnp_bms_chip *chip = container_of(bms_psy, |
| 1737 | struct qpnp_bms_chip, bms_psy); |
| 1738 | |
| 1739 | rc = reset_bms_for_test(chip); |
| 1740 | if (rc) { |
| 1741 | pr_err("Unable to modify bms_reset: %d\n", rc); |
| 1742 | return rc; |
| 1743 | } |
| 1744 | } |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | static struct kernel_param_ops bms_reset_ops = { |
| 1749 | .set = bms_reset_set, |
| 1750 | .get = param_get_bool, |
| 1751 | }; |
| 1752 | |
| 1753 | module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644); |
| 1754 | |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 1755 | #define SOC_STORAGE_MASK 0xFE |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1756 | static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp, |
| 1757 | int soc) |
| 1758 | { |
| 1759 | u8 temp; |
| 1760 | int rc; |
| 1761 | int iavg_ma = chip->prev_uuc_iavg_ma; |
| 1762 | |
Xiaozhe Shi | 68a1bb2 | 2013-09-23 14:52:00 -0700 | [diff] [blame] | 1763 | if (iavg_ma > MIN_IAVG_MA) |
| 1764 | temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA; |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1765 | else |
| 1766 | temp = 0; |
| 1767 | |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 1768 | rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1); |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1769 | |
Xiaozhe Shi | e945a8a | 2013-11-11 10:20:14 -0800 | [diff] [blame] | 1770 | /* store an invalid soc if temperature is below 5degC */ |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1771 | if (batt_temp > IGNORE_SOC_TEMP_DECIDEG) |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 1772 | qpnp_masked_write_base(chip, chip->soc_storage_addr, |
| 1773 | SOC_STORAGE_MASK, (soc + 1) << 1); |
Xiaozhe Shi | e945a8a | 2013-11-11 10:20:14 -0800 | [diff] [blame] | 1774 | else |
| 1775 | qpnp_masked_write_base(chip, chip->soc_storage_addr, |
| 1776 | SOC_STORAGE_MASK, SOC_STORAGE_MASK); |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec, |
| 1780 | int catch_up_sec, int new_soc, int prev_soc) |
| 1781 | { |
| 1782 | int scaled_soc; |
| 1783 | int numerator; |
| 1784 | |
| 1785 | /* |
| 1786 | * Don't report a high value immediately slowly scale the |
| 1787 | * value from prev_soc to the new soc based on a charge time |
| 1788 | * weighted average |
| 1789 | */ |
| 1790 | pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec); |
| 1791 | if (catch_up_sec == 0) |
| 1792 | return new_soc; |
| 1793 | |
| 1794 | if (chg_time_sec > catch_up_sec) |
| 1795 | return new_soc; |
| 1796 | |
| 1797 | numerator = (catch_up_sec - chg_time_sec) * prev_soc |
| 1798 | + chg_time_sec * new_soc; |
| 1799 | scaled_soc = numerator / catch_up_sec; |
| 1800 | |
| 1801 | pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n", |
| 1802 | chg_time_sec, new_soc, prev_soc, scaled_soc); |
| 1803 | |
| 1804 | return scaled_soc; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * bms_fake_battery is set in setups where a battery emulator is used instead |
| 1809 | * of a real battery. This makes the bms driver report a different/fake value |
| 1810 | * regardless of the calculated state of charge. |
| 1811 | */ |
| 1812 | static int bms_fake_battery = -EINVAL; |
| 1813 | module_param(bms_fake_battery, int, 0644); |
| 1814 | |
| 1815 | static int report_voltage_based_soc(struct qpnp_bms_chip *chip) |
| 1816 | { |
| 1817 | pr_debug("Reported voltage based soc = %d\n", |
| 1818 | chip->prev_voltage_based_soc); |
| 1819 | return chip->prev_voltage_based_soc; |
| 1820 | } |
| 1821 | |
| 1822 | #define SOC_CATCHUP_SEC_MAX 600 |
| 1823 | #define SOC_CATCHUP_SEC_PER_PERCENT 60 |
| 1824 | #define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT) |
Xiaozhe Shi | e7fafe6 | 2013-06-05 15:25:16 -0700 | [diff] [blame] | 1825 | #define SOC_CHANGE_PER_SEC 5 |
Xiaozhe Shi | 2737582 | 2013-08-22 11:40:15 -0700 | [diff] [blame] | 1826 | #define REPORT_SOC_WAIT_MS 10000 |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1827 | static int report_cc_based_soc(struct qpnp_bms_chip *chip) |
| 1828 | { |
| 1829 | int soc, soc_change; |
| 1830 | int time_since_last_change_sec, charge_time_sec = 0; |
| 1831 | unsigned long last_change_sec; |
| 1832 | struct timespec now; |
| 1833 | struct qpnp_vadc_result result; |
| 1834 | int batt_temp; |
| 1835 | int rc; |
| 1836 | bool charging, charging_since_last_report; |
| 1837 | |
Xiaozhe Shi | 2737582 | 2013-08-22 11:40:15 -0700 | [diff] [blame] | 1838 | rc = wait_event_interruptible_timeout(chip->bms_wait_queue, |
| 1839 | chip->calculated_soc != -EINVAL, |
| 1840 | round_jiffies_relative(msecs_to_jiffies |
| 1841 | (REPORT_SOC_WAIT_MS))); |
| 1842 | |
| 1843 | if (rc == 0 && chip->calculated_soc == -EINVAL) { |
| 1844 | pr_debug("calculate soc timed out\n"); |
| 1845 | } else if (rc == -ERESTARTSYS) { |
| 1846 | pr_err("Wait for SoC interrupted.\n"); |
| 1847 | return rc; |
| 1848 | } |
| 1849 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1850 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result); |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1851 | |
| 1852 | if (rc) { |
| 1853 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1854 | LR_MUX1_BATT_THERM, rc); |
| 1855 | return rc; |
| 1856 | } |
| 1857 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1858 | result.measurement); |
| 1859 | batt_temp = (int)result.physical; |
| 1860 | |
| 1861 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | fa6ea69 | 2013-05-31 11:15:13 -0700 | [diff] [blame] | 1862 | soc = chip->calculated_soc; |
| 1863 | |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1864 | last_change_sec = chip->last_soc_change_sec; |
| 1865 | calculate_delta_time(&last_change_sec, &time_since_last_change_sec); |
| 1866 | |
Xiaozhe Shi | 9e2422b | 2014-01-27 15:47:18 -0800 | [diff] [blame] | 1867 | charging = chip->battery_status == POWER_SUPPLY_STATUS_CHARGING; |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1868 | charging_since_last_report = charging || (chip->last_soc_unbound |
| 1869 | && chip->was_charging_at_sleep); |
| 1870 | /* |
| 1871 | * account for charge time - limit it to SOC_CATCHUP_SEC to |
| 1872 | * avoid overflows when charging continues for extended periods |
| 1873 | */ |
| 1874 | if (charging) { |
| 1875 | if (chip->charge_start_tm_sec == 0) { |
| 1876 | /* |
| 1877 | * calculating soc for the first time |
| 1878 | * after start of chg. Initialize catchup time |
| 1879 | */ |
| 1880 | if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC) |
| 1881 | chip->catch_up_time_sec = |
| 1882 | (soc - chip->last_soc) |
| 1883 | * SOC_CATCHUP_SEC_PER_PERCENT; |
| 1884 | else |
| 1885 | chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX; |
| 1886 | |
| 1887 | if (chip->catch_up_time_sec < 0) |
| 1888 | chip->catch_up_time_sec = 0; |
| 1889 | chip->charge_start_tm_sec = last_change_sec; |
| 1890 | } |
| 1891 | |
| 1892 | charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec |
| 1893 | - chip->charge_start_tm_sec); |
| 1894 | |
| 1895 | /* end catchup if calculated soc and last soc are same */ |
| 1896 | if (chip->last_soc == soc) |
| 1897 | chip->catch_up_time_sec = 0; |
| 1898 | } |
| 1899 | |
| 1900 | if (chip->last_soc != -EINVAL) { |
| 1901 | /* |
| 1902 | * last_soc < soc ... if we have not been charging at all |
| 1903 | * since the last time this was called, report previous SoC. |
| 1904 | * Otherwise, scale and catch up. |
| 1905 | */ |
| 1906 | if (chip->last_soc < soc && !charging_since_last_report) |
| 1907 | soc = chip->last_soc; |
| 1908 | else if (chip->last_soc < soc && soc != 100) |
| 1909 | soc = scale_soc_while_chg(chip, charge_time_sec, |
| 1910 | chip->catch_up_time_sec, |
| 1911 | soc, chip->last_soc); |
| 1912 | |
Xiaozhe Shi | bd56b05 | 2013-10-21 11:51:30 -0700 | [diff] [blame] | 1913 | /* if the battery is close to cutoff allow more change */ |
| 1914 | if (wake_lock_active(&chip->low_voltage_wake_lock)) |
| 1915 | soc_change = min((int)abs(chip->last_soc - soc), |
| 1916 | time_since_last_change_sec); |
| 1917 | else |
| 1918 | soc_change = min((int)abs(chip->last_soc - soc), |
| 1919 | time_since_last_change_sec |
| 1920 | / SOC_CHANGE_PER_SEC); |
| 1921 | |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1922 | if (chip->last_soc_unbound) { |
| 1923 | chip->last_soc_unbound = false; |
| 1924 | } else { |
| 1925 | /* |
| 1926 | * if soc have not been unbound by resume, |
| 1927 | * only change reported SoC by 1. |
| 1928 | */ |
| 1929 | soc_change = min(1, soc_change); |
| 1930 | } |
| 1931 | |
| 1932 | if (soc < chip->last_soc && soc != 0) |
| 1933 | soc = chip->last_soc - soc_change; |
| 1934 | if (soc > chip->last_soc && soc != 100) |
| 1935 | soc = chip->last_soc + soc_change; |
| 1936 | } |
| 1937 | |
Xiaozhe Shi | 208b8e5 | 2013-05-28 10:16:32 -0700 | [diff] [blame] | 1938 | if (chip->last_soc != soc && !chip->last_soc_unbound) |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 1939 | chip->last_soc_change_sec = last_change_sec; |
| 1940 | |
| 1941 | pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n", |
| 1942 | chip->last_soc, chip->calculated_soc, |
| 1943 | soc, time_since_last_change_sec); |
| 1944 | chip->last_soc = bound_soc(soc); |
| 1945 | backup_soc_and_iavg(chip, batt_temp, chip->last_soc); |
| 1946 | pr_debug("Reported SOC = %d\n", chip->last_soc); |
| 1947 | chip->t_soc_queried = now; |
| 1948 | mutex_unlock(&chip->last_soc_mutex); |
| 1949 | |
| 1950 | return soc; |
| 1951 | } |
| 1952 | |
| 1953 | static int report_state_of_charge(struct qpnp_bms_chip *chip) |
| 1954 | { |
| 1955 | if (bms_fake_battery != -EINVAL) { |
| 1956 | pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery); |
| 1957 | return bms_fake_battery; |
| 1958 | } else if (chip->use_voltage_soc) |
| 1959 | return report_voltage_based_soc(chip); |
| 1960 | else |
| 1961 | return report_cc_based_soc(chip); |
| 1962 | } |
| 1963 | |
Xiaozhe Shi | ee39e5d | 2013-10-07 13:56:04 -0700 | [diff] [blame] | 1964 | #define VDD_MAX_ERR 5000 |
| 1965 | #define VDD_STEP_SIZE 10000 |
| 1966 | #define MAX_COUNT_BEFORE_RESET_TO_CC 3 |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1967 | static int charging_adjustments(struct qpnp_bms_chip *chip, |
| 1968 | struct soc_params *params, int soc, |
| 1969 | int vbat_uv, int ibat_ua, int batt_temp) |
| 1970 | { |
Xiaozhe Shi | fd98ddf | 2013-05-20 15:20:19 -0700 | [diff] [blame] | 1971 | int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1972 | |
Xiaozhe Shi | eabcebf | 2013-05-28 10:41:09 -0700 | [diff] [blame] | 1973 | batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1974 | |
| 1975 | if (chip->soc_at_cv == -EINVAL) { |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 1976 | if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR || |
| 1977 | chip->in_taper_charge) { |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1978 | chip->soc_at_cv = soc; |
| 1979 | chip->prev_chg_soc = soc; |
Xiaozhe Shi | fc7af17 | 2013-11-04 14:15:44 -0800 | [diff] [blame] | 1980 | chip->ibat_at_cv_ua = params->iavg_ua; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1981 | pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n", |
| 1982 | ibat_ua, soc); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1983 | } else { |
| 1984 | /* In constant current charging return the calc soc */ |
| 1985 | pr_debug("CC CHG SOC %d\n", soc); |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1986 | } |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1987 | |
| 1988 | chip->prev_batt_terminal_uv = batt_terminal_uv; |
Xiaozhe Shi | ee39e5d | 2013-10-07 13:56:04 -0700 | [diff] [blame] | 1989 | chip->system_load_count = 0; |
| 1990 | return soc; |
| 1991 | } else if (ibat_ua > 0 && batt_terminal_uv |
| 1992 | < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) { |
| 1993 | if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) { |
| 1994 | chip->soc_at_cv = -EINVAL; |
| 1995 | pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n"); |
| 1996 | chip->system_load_count = 0; |
| 1997 | } else { |
| 1998 | chip->system_load_count += 1; |
| 1999 | pr_debug("Vbat below CV threshold, count: %d\n", |
| 2000 | chip->system_load_count); |
| 2001 | } |
| 2002 | return soc; |
| 2003 | } else if (ibat_ua > 0) { |
| 2004 | pr_debug("NOT CHARGING SOC %d\n", soc); |
| 2005 | chip->system_load_count = 0; |
| 2006 | chip->prev_chg_soc = soc; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2007 | return soc; |
| 2008 | } |
| 2009 | |
Xiaozhe Shi | ee39e5d | 2013-10-07 13:56:04 -0700 | [diff] [blame] | 2010 | chip->system_load_count = 0; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2011 | /* |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 2012 | * battery is in CV phase - begin linear interpolation of soc based on |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2013 | * battery charge current |
| 2014 | */ |
| 2015 | |
| 2016 | /* |
| 2017 | * if voltage lessened (possibly because of a system load) |
| 2018 | * keep reporting the prev chg soc |
| 2019 | */ |
Xiaozhe Shi | eabcebf | 2013-05-28 10:41:09 -0700 | [diff] [blame] | 2020 | if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) { |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 2021 | 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] | 2022 | batt_terminal_uv, chip->prev_batt_terminal_uv, |
| 2023 | chip->prev_chg_soc); |
| 2024 | chip->prev_batt_terminal_uv = batt_terminal_uv; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2025 | return chip->prev_chg_soc; |
| 2026 | } |
| 2027 | |
Xiaozhe Shi | fd98ddf | 2013-05-20 15:20:19 -0700 | [diff] [blame] | 2028 | soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv, |
| 2029 | chip->ibat_at_cv_ua, |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 2030 | 100, -1 * chip->chg_term_ua, |
Xiaozhe Shi | fc7af17 | 2013-11-04 14:15:44 -0800 | [diff] [blame] | 2031 | params->iavg_ua)); |
Xiaozhe Shi | fd98ddf | 2013-05-20 15:20:19 -0700 | [diff] [blame] | 2032 | weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv, |
| 2033 | 100, 100, chip->prev_chg_soc)); |
| 2034 | weight_cc = 100 - weight_ibat; |
Xiaozhe Shi | eabcebf | 2013-05-28 10:41:09 -0700 | [diff] [blame] | 2035 | chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat |
| 2036 | + weight_cc * soc, 100)); |
| 2037 | |
Xiaozhe Shi | fd98ddf | 2013-05-20 15:20:19 -0700 | [diff] [blame] | 2038 | pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n", |
| 2039 | weight_ibat, weight_cc, soc_ibat, soc); |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2040 | |
| 2041 | /* always report a higher soc */ |
| 2042 | if (chg_soc > chip->prev_chg_soc) { |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2043 | chip->prev_chg_soc = chg_soc; |
| 2044 | |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 2045 | chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp, |
| 2046 | find_pc_for_soc(chip, params, chg_soc)); |
| 2047 | pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n", |
| 2048 | chip->charging_adjusted_ocv, |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2049 | chip->prev_chg_soc); |
| 2050 | } |
| 2051 | |
| 2052 | pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc); |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 2053 | chip->prev_batt_terminal_uv = batt_terminal_uv; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 2054 | return chip->prev_chg_soc; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2057 | static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv) |
| 2058 | { |
| 2059 | /* |
| 2060 | * if battery is very low (v_cutoff voltage + 20mv) hold |
| 2061 | * a wakelock untill soc = 0% |
| 2062 | */ |
| 2063 | if (vbat_uv <= chip->low_voltage_threshold |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2064 | && !wake_lock_active(&chip->low_voltage_wake_lock)) { |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2065 | pr_debug("voltage = %d low holding wakelock\n", vbat_uv); |
| 2066 | wake_lock(&chip->low_voltage_wake_lock); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2067 | } else if (vbat_uv > chip->low_voltage_threshold |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2068 | && wake_lock_active(&chip->low_voltage_wake_lock)) { |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2069 | pr_debug("voltage = %d releasing wakelock\n", vbat_uv); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2070 | wake_unlock(&chip->low_voltage_wake_lock); |
| 2071 | } |
| 2072 | } |
| 2073 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2074 | #define VBATT_ERROR_MARGIN 20000 |
| 2075 | static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv) |
| 2076 | { |
| 2077 | /* |
| 2078 | * if battery is very low (v_cutoff voltage + 20mv) hold |
| 2079 | * a wakelock untill soc = 0% |
| 2080 | */ |
| 2081 | if (wake_lock_active(&chip->cv_wake_lock)) { |
| 2082 | if (chip->soc_at_cv != -EINVAL) { |
| 2083 | pr_debug("hit CV, releasing cv wakelock\n"); |
| 2084 | wake_unlock(&chip->cv_wake_lock); |
| 2085 | } else if (!is_battery_charging(chip)) { |
| 2086 | pr_debug("charging stopped, releasing cv wakelock\n"); |
| 2087 | wake_unlock(&chip->cv_wake_lock); |
| 2088 | } |
| 2089 | } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN |
| 2090 | && chip->soc_at_cv == -EINVAL |
| 2091 | && is_battery_charging(chip) |
| 2092 | && !wake_lock_active(&chip->cv_wake_lock)) { |
| 2093 | pr_debug("voltage = %d holding cv wakelock\n", vbat_uv); |
| 2094 | wake_lock(&chip->cv_wake_lock); |
| 2095 | } |
| 2096 | } |
| 2097 | |
Xiaozhe Shi | 2b64787 | 2013-10-31 14:30:27 -0700 | [diff] [blame] | 2098 | #define NO_ADJUST_HIGH_SOC_THRESHOLD 98 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2099 | static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params, |
| 2100 | int soc, int batt_temp) |
| 2101 | { |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2102 | int ibat_ua = 0, vbat_uv = 0; |
| 2103 | int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0; |
| 2104 | int delta_ocv_uv = 0; |
| 2105 | int n = 0; |
| 2106 | int rc_new_uah = 0; |
| 2107 | int pc_new = 0; |
| 2108 | int soc_new = 0; |
| 2109 | int slope = 0; |
| 2110 | int rc = 0; |
| 2111 | int delta_ocv_uv_limit = 0; |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 2112 | int correction_limit_uv = 0; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2113 | |
| 2114 | rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv); |
| 2115 | if (rc < 0) { |
| 2116 | pr_err("simultaneous vbat ibat failed err = %d\n", rc); |
| 2117 | goto out; |
| 2118 | } |
| 2119 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2120 | very_low_voltage_check(chip, vbat_uv); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2121 | cv_voltage_check(chip, vbat_uv); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2122 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2123 | delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000); |
| 2124 | |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 2125 | ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000; |
| 2126 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2127 | pc_est = calculate_pc(chip, ocv_est_uv, batt_temp); |
| 2128 | soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100, |
| 2129 | (s64)params->fcc_uah - params->uuc_uah); |
| 2130 | soc_est = bound_soc(soc_est); |
| 2131 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 2132 | /* never adjust during bms reset mode */ |
| 2133 | if (bms_reset) { |
| 2134 | pr_debug("bms reset mode, SOC adjustment skipped\n"); |
| 2135 | goto out; |
| 2136 | } |
| 2137 | |
Xiaozhe Shi | ee39e5d | 2013-10-07 13:56:04 -0700 | [diff] [blame] | 2138 | if (is_battery_charging(chip)) { |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2139 | soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua, |
| 2140 | batt_temp); |
Xiaozhe Shi | ee39e5d | 2013-10-07 13:56:04 -0700 | [diff] [blame] | 2141 | /* Skip adjustments if we are in CV or ibat is negative */ |
| 2142 | if (chip->soc_at_cv != -EINVAL || ibat_ua < 0) |
| 2143 | goto out; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * do not adjust |
Xiaozhe Shi | 561ebf7 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 2148 | * if soc_est is same as what bms calculated |
| 2149 | * OR if soc_est > adjust_soc_low_threshold |
| 2150 | * OR if soc is above 90 |
| 2151 | * because we might pull it low |
| 2152 | * and cause a bad user experience |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2153 | */ |
Xiaozhe Shi | bd56b05 | 2013-10-21 11:51:30 -0700 | [diff] [blame] | 2154 | if (!wake_lock_active(&chip->low_voltage_wake_lock) && |
| 2155 | (soc_est == soc |
| 2156 | || soc_est > chip->adjust_soc_low_threshold |
| 2157 | || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD)) |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2158 | goto out; |
| 2159 | |
| 2160 | if (chip->last_soc_est == -EINVAL) |
| 2161 | chip->last_soc_est = soc; |
| 2162 | |
| 2163 | n = min(200, max(1 , soc + soc_est + chip->last_soc_est)); |
| 2164 | chip->last_soc_est = soc_est; |
| 2165 | |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2166 | pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2167 | if (pc > 0) { |
| 2168 | pc_new = calculate_pc(chip, |
| 2169 | chip->last_ocv_uv - (++slope * 1000), |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2170 | chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2171 | while (pc_new == pc) { |
| 2172 | /* start taking 10mV steps */ |
| 2173 | slope = slope + 10; |
| 2174 | pc_new = calculate_pc(chip, |
| 2175 | chip->last_ocv_uv - (slope * 1000), |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2176 | chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2177 | } |
| 2178 | } else { |
| 2179 | /* |
| 2180 | * pc is already at the lowest point, |
| 2181 | * assume 1 millivolt translates to 1% pc |
| 2182 | */ |
| 2183 | pc = 1; |
| 2184 | pc_new = 0; |
| 2185 | slope = 1; |
| 2186 | } |
| 2187 | |
| 2188 | delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000, |
| 2189 | n * (pc - pc_new)); |
| 2190 | |
| 2191 | if (abs(delta_ocv_uv) > delta_ocv_uv_limit) { |
| 2192 | pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv, |
| 2193 | delta_ocv_uv_limit); |
| 2194 | |
| 2195 | if (delta_ocv_uv > 0) |
| 2196 | delta_ocv_uv = delta_ocv_uv_limit; |
| 2197 | else |
| 2198 | delta_ocv_uv = -1 * delta_ocv_uv_limit; |
| 2199 | pr_debug("new delta ocv = %d\n", delta_ocv_uv); |
| 2200 | } |
| 2201 | |
Xiaozhe Shi | bd56b05 | 2013-10-21 11:51:30 -0700 | [diff] [blame] | 2202 | if (wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2203 | /* when in the cutoff region, do not correct upwards */ |
| 2204 | delta_ocv_uv = max(0, delta_ocv_uv); |
Xiaozhe Shi | 08d9aa7 | 2013-07-31 17:15:12 -0700 | [diff] [blame] | 2205 | goto skip_limits; |
Xiaozhe Shi | bd56b05 | 2013-10-21 11:51:30 -0700 | [diff] [blame] | 2206 | } |
Xiaozhe Shi | 08d9aa7 | 2013-07-31 17:15:12 -0700 | [diff] [blame] | 2207 | |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 2208 | if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv) |
| 2209 | correction_limit_uv = chip->high_ocv_correction_limit_uv; |
| 2210 | else |
| 2211 | correction_limit_uv = chip->low_ocv_correction_limit_uv; |
| 2212 | |
| 2213 | if (abs(delta_ocv_uv) > correction_limit_uv) { |
| 2214 | pr_debug("limiting delta ocv %d limit = %d\n", |
| 2215 | delta_ocv_uv, correction_limit_uv); |
| 2216 | if (delta_ocv_uv > 0) |
| 2217 | delta_ocv_uv = correction_limit_uv; |
| 2218 | else |
| 2219 | delta_ocv_uv = -correction_limit_uv; |
| 2220 | pr_debug("new delta ocv = %d\n", delta_ocv_uv); |
| 2221 | } |
| 2222 | |
Xiaozhe Shi | 08d9aa7 | 2013-07-31 17:15:12 -0700 | [diff] [blame] | 2223 | skip_limits: |
| 2224 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2225 | chip->last_ocv_uv -= delta_ocv_uv; |
| 2226 | |
| 2227 | if (chip->last_ocv_uv >= chip->max_voltage_uv) |
| 2228 | chip->last_ocv_uv = chip->max_voltage_uv; |
| 2229 | |
| 2230 | /* calculate the soc based on this new ocv */ |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2231 | 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] | 2232 | rc_new_uah = (params->fcc_uah * pc_new) / 100; |
| 2233 | soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100 |
| 2234 | / (params->fcc_uah - params->uuc_uah); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2235 | |
| 2236 | /* |
| 2237 | * 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] | 2238 | * 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] | 2239 | */ |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 2240 | if (soc_new == 0 && soc_est >= chip->hold_soc_est) |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2241 | soc_new = 1; |
| 2242 | |
| 2243 | soc = soc_new; |
| 2244 | |
| 2245 | out: |
| 2246 | 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", |
| 2247 | ibat_ua, vbat_uv, ocv_est_uv, pc_est, |
| 2248 | soc_est, n, delta_ocv_uv, chip->last_ocv_uv, |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 2249 | pc_new, soc_new, params->rbatt_mohm, slope); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2250 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2251 | return soc; |
| 2252 | } |
| 2253 | |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 2254 | static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc) |
| 2255 | { |
| 2256 | int rc, vbat_uv; |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 2257 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2258 | rc = get_battery_voltage(chip, &vbat_uv); |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 2259 | if (rc < 0) { |
| 2260 | pr_err("adc vbat failed err = %d\n", rc); |
| 2261 | return soc; |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 2262 | } |
Xiaozhe Shi | 9c14f3d | 2014-03-18 14:27:43 -0700 | [diff] [blame] | 2263 | |
| 2264 | /* only clamp when discharging */ |
| 2265 | if (is_battery_charging(chip)) |
| 2266 | return soc; |
| 2267 | |
David Keitel | e50091e | 2014-03-05 09:55:57 -0800 | [diff] [blame] | 2268 | if (soc <= 0 && vbat_uv > chip->v_cutoff_uv) { |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 2269 | pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n", |
| 2270 | vbat_uv, chip->v_cutoff_uv); |
| 2271 | return 1; |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 2272 | } else { |
| 2273 | pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n", |
| 2274 | soc, vbat_uv, chip->v_cutoff_uv); |
| 2275 | return soc; |
| 2276 | } |
| 2277 | } |
| 2278 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2279 | static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah) |
| 2280 | { |
| 2281 | int64_t cc_uv, cc_pvh, cc_raw; |
| 2282 | |
| 2283 | cc_pvh = cc_uah * chip->r_sense_uohm; |
| 2284 | cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR, |
| 2285 | CC_READING_TICKS * 1000000LL); |
| 2286 | cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D, |
| 2287 | CC_READING_RESOLUTION_N); |
| 2288 | return cc_raw; |
| 2289 | } |
| 2290 | |
| 2291 | #define CC_STEP_INCREMENT_UAH 1500 |
| 2292 | #define OCV_STEP_INCREMENT 0x10 |
| 2293 | static void configure_soc_wakeup(struct qpnp_bms_chip *chip, |
| 2294 | struct soc_params *params, |
| 2295 | int batt_temp, int target_soc) |
| 2296 | { |
| 2297 | int target_ocv_uv; |
| 2298 | int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64; |
| 2299 | int64_t current_shdw_cc_uah, iadc_comp_factor; |
| 2300 | uint64_t cc_raw, current_shdw_cc_raw; |
| 2301 | int16_t ocv_raw, current_ocv_raw; |
| 2302 | |
| 2303 | current_shdw_cc_raw = 0; |
| 2304 | mutex_lock(&chip->bms_output_lock); |
| 2305 | lock_output_data(chip); |
| 2306 | qpnp_read_wrapper(chip, (u8 *)¤t_ocv_raw, |
| 2307 | chip->base + BMS1_OCV_FOR_SOC_DATA0, 2); |
| 2308 | unlock_output_data(chip); |
| 2309 | mutex_unlock(&chip->bms_output_lock); |
| 2310 | current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip); |
| 2311 | current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip, |
| 2312 | current_shdw_cc_uah); |
| 2313 | |
| 2314 | /* |
| 2315 | * Calculate the target shadow coulomb counter threshold for when |
| 2316 | * the SoC changes. |
| 2317 | * |
| 2318 | * Since the BMS driver resets the shadow coulomb counter every |
| 2319 | * 20 seconds when the device is awake, calculate the threshold as |
| 2320 | * a delta from the current shadow coulomb count. |
| 2321 | */ |
| 2322 | target_cc_uah = (100 - target_soc) |
| 2323 | * (params->fcc_uah - params->uuc_uah) |
| 2324 | / 100 - current_shdw_cc_uah; |
| 2325 | if (target_cc_uah < 0) { |
| 2326 | /* |
| 2327 | * If the target cc is below 0, that means we have already |
| 2328 | * passed the point where SoC should have fallen. |
| 2329 | * Set a wakeup in a few more mAh and check back again |
| 2330 | */ |
| 2331 | target_cc_uah = CC_STEP_INCREMENT_UAH; |
| 2332 | } |
| 2333 | iadc_comp_factor = 100000; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 2334 | qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2335 | target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 2336 | target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2337 | cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah); |
| 2338 | cc_raw = convert_s64_to_s36(cc_raw_64); |
| 2339 | |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 2340 | target_ocv_uv = find_ocv_for_pc(chip, batt_temp, |
| 2341 | find_pc_for_soc(chip, params, target_soc)); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2342 | ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv); |
| 2343 | |
| 2344 | /* |
| 2345 | * If the current_ocv_raw was updated since reaching 100% and is lower |
| 2346 | * than the calculated target ocv threshold, set the new target |
| 2347 | * threshold 1.5mAh lower in order to check if the SoC changed yet. |
| 2348 | */ |
| 2349 | if (current_ocv_raw != chip->ocv_reading_at_100 |
| 2350 | && current_ocv_raw < ocv_raw) |
| 2351 | ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT; |
| 2352 | |
| 2353 | qpnp_write_wrapper(chip, (u8 *)&cc_raw, |
| 2354 | chip->base + BMS1_SW_CC_THR0, 5); |
| 2355 | qpnp_write_wrapper(chip, (u8 *)&ocv_raw, |
| 2356 | chip->base + BMS1_OCV_THR0, 2); |
| 2357 | |
Xiaozhe Shi | f5d8783 | 2013-11-21 17:05:39 -0800 | [diff] [blame] | 2358 | enable_bms_irq(&chip->ocv_thr_irq); |
| 2359 | enable_bms_irq(&chip->sw_cc_thr_irq); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2360 | pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n", |
| 2361 | current_shdw_cc_raw, (uint16_t)current_ocv_raw); |
| 2362 | pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n", |
| 2363 | target_cc_uah, |
| 2364 | (uint64_t)cc_raw_64, cc_raw, |
| 2365 | (uint16_t)ocv_raw); |
| 2366 | } |
| 2367 | |
Xiaozhe Shi | 5e79103 | 2013-10-25 11:30:42 -0700 | [diff] [blame] | 2368 | #define BAD_SOC_THRESH -10 |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2369 | static int calculate_raw_soc(struct qpnp_bms_chip *chip, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2370 | struct raw_soc_params *raw, |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2371 | struct soc_params *params, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2372 | int batt_temp) |
| 2373 | { |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 2374 | int soc, remaining_usable_charge_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2375 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2376 | /* calculate remaining usable charge */ |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2377 | remaining_usable_charge_uah = params->ocv_charge_uah |
| 2378 | - params->cc_uah |
| 2379 | - params->uuc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2380 | pr_debug("RUC = %duAh\n", remaining_usable_charge_uah); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2381 | |
Xiaozhe Shi | fd8cd48 | 2013-02-12 10:00:38 -0800 | [diff] [blame] | 2382 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2383 | (params->fcc_uah - params->uuc_uah)); |
Xiaozhe Shi | fd8cd48 | 2013-02-12 10:00:38 -0800 | [diff] [blame] | 2384 | |
Xiaozhe Shi | 5e79103 | 2013-10-25 11:30:42 -0700 | [diff] [blame] | 2385 | if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2386 | /* |
| 2387 | * first time calcualtion and the pon ocv is too low resulting |
| 2388 | * in a bad soc. Adjust ocv to get 0 soc |
| 2389 | */ |
| 2390 | pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc); |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 2391 | chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, |
| 2392 | find_pc_for_soc(chip, params, 0)); |
| 2393 | params->ocv_charge_uah = find_ocv_charge_for_soc(chip, |
| 2394 | params, 0); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2395 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2396 | remaining_usable_charge_uah = params->ocv_charge_uah |
| 2397 | - params->cc_uah |
| 2398 | - params->uuc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2399 | |
| 2400 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2401 | (params->fcc_uah |
| 2402 | - params->uuc_uah)); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2403 | pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n", |
| 2404 | soc, chip->last_ocv_uv); |
| 2405 | } |
| 2406 | |
| 2407 | if (soc > 100) |
| 2408 | soc = 100; |
| 2409 | |
Xiaozhe Shi | 5e79103 | 2013-10-25 11:30:42 -0700 | [diff] [blame] | 2410 | if (soc > BAD_SOC_THRESH && soc < 0) { |
Xiaozhe Shi | cb386a2 | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 2411 | 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] | 2412 | remaining_usable_charge_uah, |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2413 | params->ocv_charge_uah, |
| 2414 | params->cc_uah, params->uuc_uah); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2415 | |
Xiaozhe Shi | cb386a2 | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 2416 | 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] | 2417 | chip->last_ocv_uv, batt_temp, |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2418 | params->fcc_uah, soc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2419 | soc = 0; |
| 2420 | } |
| 2421 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2422 | return soc; |
| 2423 | } |
| 2424 | |
| 2425 | #define SLEEP_RECALC_INTERVAL 3 |
| 2426 | static int calculate_state_of_charge(struct qpnp_bms_chip *chip, |
| 2427 | struct raw_soc_params *raw, |
| 2428 | int batt_temp) |
| 2429 | { |
| 2430 | struct soc_params params; |
| 2431 | int soc, previous_soc, shutdown_soc, new_calculated_soc; |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 2432 | int remaining_usable_charge_uah; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2433 | |
| 2434 | calculate_soc_params(chip, raw, ¶ms, batt_temp); |
| 2435 | if (!is_battery_present(chip)) { |
| 2436 | pr_debug("battery gone, reporting 100\n"); |
| 2437 | new_calculated_soc = 100; |
| 2438 | goto done_calculating; |
| 2439 | } |
| 2440 | |
| 2441 | if (params.fcc_uah - params.uuc_uah <= 0) { |
| 2442 | pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n", |
| 2443 | params.fcc_uah, |
| 2444 | params.uuc_uah); |
| 2445 | new_calculated_soc = 0; |
| 2446 | goto done_calculating; |
| 2447 | } |
| 2448 | |
| 2449 | soc = calculate_raw_soc(chip, raw, ¶ms, batt_temp); |
| 2450 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2451 | mutex_lock(&chip->soc_invalidation_mutex); |
| 2452 | shutdown_soc = chip->shutdown_soc; |
| 2453 | |
| 2454 | if (chip->first_time_calc_soc && soc != shutdown_soc |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2455 | && !chip->shutdown_soc_invalid) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2456 | /* |
| 2457 | * soc for the first time - use shutdown soc |
| 2458 | * to adjust pon ocv since it is a small percent away from |
| 2459 | * the real soc |
| 2460 | */ |
| 2461 | pr_debug("soc = %d before forcing shutdown_soc = %d\n", |
| 2462 | soc, shutdown_soc); |
Xiaozhe Shi | d9ffdaf | 2013-09-19 16:23:21 -0700 | [diff] [blame] | 2463 | chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, |
| 2464 | find_pc_for_soc(chip, ¶ms, shutdown_soc)); |
| 2465 | params.ocv_charge_uah = find_ocv_charge_for_soc(chip, |
| 2466 | ¶ms, shutdown_soc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2467 | |
| 2468 | remaining_usable_charge_uah = params.ocv_charge_uah |
| 2469 | - params.cc_uah |
| 2470 | - params.uuc_uah; |
| 2471 | |
| 2472 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 2473 | (params.fcc_uah |
| 2474 | - params.uuc_uah)); |
| 2475 | |
| 2476 | pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n", |
| 2477 | shutdown_soc, soc, chip->last_ocv_uv); |
| 2478 | } |
| 2479 | mutex_unlock(&chip->soc_invalidation_mutex); |
| 2480 | |
Xiaozhe Shi | cbce804 | 2014-02-13 14:08:47 -0800 | [diff] [blame] | 2481 | if (chip->first_time_calc_soc && !chip->shutdown_soc_invalid) { |
| 2482 | pr_debug("Skip adjustment when shutdown SOC has been forced\n"); |
| 2483 | new_calculated_soc = soc; |
| 2484 | } else { |
| 2485 | pr_debug("SOC before adjustment = %d\n", soc); |
| 2486 | new_calculated_soc = adjust_soc(chip, ¶ms, soc, batt_temp); |
| 2487 | } |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2488 | |
Xiaozhe Shi | 445d249 | 2013-03-27 18:10:18 -0700 | [diff] [blame] | 2489 | /* always clamp soc due to BMS hw/sw immaturities */ |
| 2490 | new_calculated_soc = clamp_soc_based_on_voltage(chip, |
| 2491 | new_calculated_soc); |
Xiaozhe Shi | 422c27d | 2014-08-06 11:22:30 -0700 | [diff] [blame] | 2492 | |
| 2493 | new_calculated_soc = bound_soc(new_calculated_soc); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2494 | /* |
| 2495 | * If the battery is full, configure the cc threshold so the system |
| 2496 | * wakes up after SoC changes |
| 2497 | */ |
Xiaozhe Shi | f5d8783 | 2013-11-21 17:05:39 -0800 | [diff] [blame] | 2498 | if (is_battery_full(chip)) { |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2499 | configure_soc_wakeup(chip, ¶ms, |
| 2500 | batt_temp, bound_soc(new_calculated_soc - 1)); |
Xiaozhe Shi | f5d8783 | 2013-11-21 17:05:39 -0800 | [diff] [blame] | 2501 | } else { |
| 2502 | disable_bms_irq(&chip->ocv_thr_irq); |
| 2503 | disable_bms_irq(&chip->sw_cc_thr_irq); |
| 2504 | } |
Xiaozhe Shi | fd8cd48 | 2013-02-12 10:00:38 -0800 | [diff] [blame] | 2505 | done_calculating: |
Xiaozhe Shi | fa6ea69 | 2013-05-31 11:15:13 -0700 | [diff] [blame] | 2506 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | e7fafe6 | 2013-06-05 15:25:16 -0700 | [diff] [blame] | 2507 | previous_soc = chip->calculated_soc; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2508 | chip->calculated_soc = new_calculated_soc; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2509 | pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 2510 | if (chip->last_soc_invalid) { |
| 2511 | chip->last_soc_invalid = false; |
| 2512 | chip->last_soc = -EINVAL; |
| 2513 | } |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2514 | /* |
| 2515 | * Check if more than a long time has passed since the last |
| 2516 | * calculation (more than n times compared to the soc recalculation |
| 2517 | * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true, |
| 2518 | * then the system must have gone through a long sleep, and SoC can be |
| 2519 | * allowed to become unbounded by the last reported SoC |
| 2520 | */ |
| 2521 | if (params.delta_time_s * 1000 > |
| 2522 | chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL |
| 2523 | && !chip->first_time_calc_soc) { |
| 2524 | chip->last_soc_unbound = true; |
| 2525 | chip->last_soc_change_sec = chip->last_recalc_time; |
| 2526 | pr_debug("last_soc unbound because elapsed time = %d\n", |
| 2527 | params.delta_time_s); |
| 2528 | } |
| 2529 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | 2737582 | 2013-08-22 11:40:15 -0700 | [diff] [blame] | 2530 | wake_up_interruptible(&chip->bms_wait_queue); |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 2531 | |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 2532 | if (new_calculated_soc != previous_soc && chip->bms_psy_registered) { |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 2533 | power_supply_changed(&chip->bms_psy); |
| 2534 | pr_debug("power supply changed\n"); |
| 2535 | } else { |
| 2536 | /* |
| 2537 | * Call report state of charge anyways to periodically update |
| 2538 | * reported SoC. This prevents reported SoC from being stuck |
| 2539 | * when calculated soc doesn't change. |
| 2540 | */ |
| 2541 | report_state_of_charge(chip); |
| 2542 | } |
| 2543 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2544 | get_current_time(&chip->last_recalc_time); |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2545 | chip->first_time_calc_soc = 0; |
Xiaozhe Shi | 7063392 | 2013-09-23 15:50:53 -0700 | [diff] [blame] | 2546 | chip->first_time_calc_uuc = 0; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2547 | return chip->calculated_soc; |
| 2548 | } |
| 2549 | |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2550 | static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip) |
| 2551 | { |
| 2552 | int voltage_range_uv, voltage_remaining_uv, voltage_based_soc; |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 2553 | int rc, vbat_uv; |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2554 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2555 | rc = get_battery_voltage(chip, &vbat_uv); |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 2556 | if (rc < 0) { |
| 2557 | pr_err("adc vbat failed err = %d\n", rc); |
| 2558 | return rc; |
| 2559 | } |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2560 | voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv; |
| 2561 | voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv; |
| 2562 | voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv; |
| 2563 | |
| 2564 | voltage_based_soc = clamp(voltage_based_soc, 0, 100); |
| 2565 | |
| 2566 | if (chip->prev_voltage_based_soc != voltage_based_soc |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 2567 | && chip->bms_psy_registered) { |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2568 | power_supply_changed(&chip->bms_psy); |
| 2569 | pr_debug("power supply changed\n"); |
| 2570 | } |
| 2571 | chip->prev_voltage_based_soc = voltage_based_soc; |
| 2572 | |
| 2573 | pr_debug("vbat used = %duv\n", vbat_uv); |
| 2574 | pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc); |
| 2575 | return voltage_based_soc; |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2578 | static int recalculate_raw_soc(struct qpnp_bms_chip *chip) |
| 2579 | { |
| 2580 | int batt_temp, rc, soc; |
| 2581 | struct qpnp_vadc_result result; |
| 2582 | struct raw_soc_params raw; |
| 2583 | struct soc_params params; |
| 2584 | |
| 2585 | bms_stay_awake(&chip->soc_wake_source); |
| 2586 | if (chip->use_voltage_soc) { |
| 2587 | soc = calculate_soc_from_voltage(chip); |
| 2588 | } else { |
| 2589 | if (!chip->batfet_closed) |
Xiaozhe Shi | ffb7cfc | 2014-01-03 10:47:36 -0800 | [diff] [blame] | 2590 | qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false); |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2591 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, |
| 2592 | &result); |
| 2593 | if (rc) { |
| 2594 | pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n", |
| 2595 | LR_MUX1_BATT_THERM, rc); |
| 2596 | soc = chip->calculated_soc; |
| 2597 | } else { |
| 2598 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", |
| 2599 | result.physical, |
| 2600 | result.measurement); |
| 2601 | batt_temp = (int)result.physical; |
| 2602 | |
| 2603 | mutex_lock(&chip->last_ocv_uv_mutex); |
Xiaozhe Shi | ed4a552 | 2014-09-05 14:56:13 -0700 | [diff] [blame] | 2604 | rc = read_soc_params_raw(chip, &raw, batt_temp); |
| 2605 | if (rc) { |
| 2606 | pr_err("Unable to read params, rc: %d\n", rc); |
| 2607 | soc = 0; |
| 2608 | goto done; |
| 2609 | } |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2610 | calculate_soc_params(chip, &raw, ¶ms, batt_temp); |
| 2611 | if (!is_battery_present(chip)) { |
| 2612 | pr_debug("battery gone\n"); |
| 2613 | soc = 0; |
| 2614 | } else if (params.fcc_uah - params.uuc_uah <= 0) { |
| 2615 | pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n", |
| 2616 | params.fcc_uah, |
| 2617 | params.uuc_uah); |
| 2618 | soc = 0; |
| 2619 | } else { |
| 2620 | soc = calculate_raw_soc(chip, &raw, |
| 2621 | ¶ms, batt_temp); |
| 2622 | } |
Xiaozhe Shi | ed4a552 | 2014-09-05 14:56:13 -0700 | [diff] [blame] | 2623 | done: |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 2624 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 2625 | } |
| 2626 | } |
| 2627 | bms_relax(&chip->soc_wake_source); |
| 2628 | return soc; |
| 2629 | } |
| 2630 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2631 | static int recalculate_soc(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2632 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2633 | int batt_temp, rc, soc; |
| 2634 | struct qpnp_vadc_result result; |
| 2635 | struct raw_soc_params raw; |
| 2636 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2637 | bms_stay_awake(&chip->soc_wake_source); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2638 | mutex_lock(&chip->vbat_monitor_mutex); |
Xiaozhe Shi | b5689fb | 2013-07-15 17:20:49 -0700 | [diff] [blame] | 2639 | if (chip->vbat_monitor_params.state_request != |
| 2640 | ADC_TM_HIGH_LOW_THR_DISABLE) |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2641 | qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 2642 | &chip->vbat_monitor_params); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2643 | mutex_unlock(&chip->vbat_monitor_mutex); |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2644 | if (chip->use_voltage_soc) { |
| 2645 | soc = calculate_soc_from_voltage(chip); |
| 2646 | } else { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 2647 | if (!chip->batfet_closed) |
Xiaozhe Shi | ffb7cfc | 2014-01-03 10:47:36 -0800 | [diff] [blame] | 2648 | qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false); |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2649 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, |
| 2650 | &result); |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2651 | if (rc) { |
| 2652 | pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n", |
| 2653 | LR_MUX1_BATT_THERM, rc); |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2654 | soc = chip->calculated_soc; |
| 2655 | } else { |
| 2656 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", |
| 2657 | result.physical, |
| 2658 | result.measurement); |
| 2659 | batt_temp = (int)result.physical; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2660 | |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2661 | mutex_lock(&chip->last_ocv_uv_mutex); |
Xiaozhe Shi | ed4a552 | 2014-09-05 14:56:13 -0700 | [diff] [blame] | 2662 | rc = read_soc_params_raw(chip, &raw, batt_temp); |
| 2663 | if (rc) { |
| 2664 | pr_err("Unable to read params, rc: %d\n", rc); |
| 2665 | soc = chip->calculated_soc; |
| 2666 | } else { |
| 2667 | soc = calculate_state_of_charge(chip, |
| 2668 | &raw, batt_temp); |
| 2669 | } |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2670 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 2671 | } |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2672 | } |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 2673 | bms_relax(&chip->soc_wake_source); |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2674 | return soc; |
| 2675 | } |
| 2676 | |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 2677 | static void recalculate_work(struct work_struct *work) |
| 2678 | { |
| 2679 | struct qpnp_bms_chip *chip = container_of(work, |
| 2680 | struct qpnp_bms_chip, |
| 2681 | recalc_work); |
| 2682 | |
| 2683 | recalculate_soc(chip); |
| 2684 | } |
| 2685 | |
Xiaozhe Shi | cb487b1 | 2013-10-14 17:42:07 -0700 | [diff] [blame] | 2686 | static int get_calculation_delay_ms(struct qpnp_bms_chip *chip) |
| 2687 | { |
| 2688 | if (wake_lock_active(&chip->low_voltage_wake_lock)) |
| 2689 | return chip->low_voltage_calculate_soc_ms; |
| 2690 | else if (chip->calculated_soc < chip->low_soc_calc_threshold) |
| 2691 | return chip->low_soc_calculate_soc_ms; |
| 2692 | else |
| 2693 | return chip->calculate_soc_ms; |
| 2694 | } |
| 2695 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2696 | static void calculate_soc_work(struct work_struct *work) |
| 2697 | { |
| 2698 | struct qpnp_bms_chip *chip = container_of(work, |
| 2699 | struct qpnp_bms_chip, |
| 2700 | calculate_soc_delayed_work.work); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2701 | |
Xiaozhe Shi | cb487b1 | 2013-10-14 17:42:07 -0700 | [diff] [blame] | 2702 | recalculate_soc(chip); |
| 2703 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 2704 | round_jiffies_relative(msecs_to_jiffies |
| 2705 | (get_calculation_delay_ms(chip)))); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2708 | static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip) |
| 2709 | { |
| 2710 | mutex_lock(&chip->vbat_monitor_mutex); |
| 2711 | if (chip->vbat_monitor_params.state_request |
| 2712 | == ADC_TM_HIGH_LOW_THR_ENABLE) { |
| 2713 | /* |
| 2714 | * Battery is now around or below v_cutoff |
| 2715 | */ |
| 2716 | pr_debug("battery entered cutoff range\n"); |
| 2717 | if (!wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2718 | pr_debug("voltage low, holding wakelock\n"); |
| 2719 | wake_lock(&chip->low_voltage_wake_lock); |
| 2720 | cancel_delayed_work_sync( |
| 2721 | &chip->calculate_soc_delayed_work); |
| 2722 | schedule_delayed_work( |
| 2723 | &chip->calculate_soc_delayed_work, 0); |
| 2724 | } |
| 2725 | chip->vbat_monitor_params.state_request = |
| 2726 | ADC_TM_HIGH_THR_ENABLE; |
| 2727 | chip->vbat_monitor_params.high_thr = |
| 2728 | (chip->low_voltage_threshold + VBATT_ERROR_MARGIN); |
| 2729 | pr_debug("set low thr to %d and high to %d\n", |
| 2730 | chip->vbat_monitor_params.low_thr, |
| 2731 | chip->vbat_monitor_params.high_thr); |
| 2732 | chip->vbat_monitor_params.low_thr = 0; |
| 2733 | } else if (chip->vbat_monitor_params.state_request |
| 2734 | == ADC_TM_LOW_THR_ENABLE) { |
| 2735 | /* |
| 2736 | * Battery is in normal operation range. |
| 2737 | */ |
| 2738 | pr_debug("battery entered normal range\n"); |
| 2739 | if (wake_lock_active(&chip->cv_wake_lock)) { |
| 2740 | wake_unlock(&chip->cv_wake_lock); |
| 2741 | pr_debug("releasing cv wake lock\n"); |
| 2742 | } |
| 2743 | chip->in_cv_range = false; |
| 2744 | chip->vbat_monitor_params.state_request = |
| 2745 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2746 | chip->vbat_monitor_params.high_thr = chip->max_voltage_uv |
| 2747 | - VBATT_ERROR_MARGIN; |
| 2748 | chip->vbat_monitor_params.low_thr = |
| 2749 | chip->low_voltage_threshold; |
| 2750 | pr_debug("set low thr to %d and high to %d\n", |
| 2751 | chip->vbat_monitor_params.low_thr, |
| 2752 | chip->vbat_monitor_params.high_thr); |
| 2753 | } |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2754 | qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 2755 | &chip->vbat_monitor_params); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2756 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 2757 | } |
| 2758 | |
| 2759 | #define CV_LOW_THRESHOLD_HYST_UV 100000 |
| 2760 | static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip) |
| 2761 | { |
| 2762 | mutex_lock(&chip->vbat_monitor_mutex); |
| 2763 | if (chip->vbat_monitor_params.state_request |
| 2764 | == ADC_TM_HIGH_LOW_THR_ENABLE) { |
| 2765 | /* |
| 2766 | * Battery is around vddmax |
| 2767 | */ |
| 2768 | pr_debug("battery entered vddmax range\n"); |
| 2769 | chip->in_cv_range = true; |
| 2770 | if (!wake_lock_active(&chip->cv_wake_lock)) { |
| 2771 | wake_lock(&chip->cv_wake_lock); |
| 2772 | pr_debug("holding cv wake lock\n"); |
| 2773 | } |
| 2774 | schedule_work(&chip->recalc_work); |
| 2775 | chip->vbat_monitor_params.state_request = |
| 2776 | ADC_TM_LOW_THR_ENABLE; |
| 2777 | chip->vbat_monitor_params.low_thr = |
| 2778 | (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV); |
| 2779 | chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2; |
| 2780 | pr_debug("set low thr to %d and high to %d\n", |
| 2781 | chip->vbat_monitor_params.low_thr, |
| 2782 | chip->vbat_monitor_params.high_thr); |
| 2783 | } else if (chip->vbat_monitor_params.state_request |
| 2784 | == ADC_TM_HIGH_THR_ENABLE) { |
| 2785 | /* |
| 2786 | * Battery is in normal operation range. |
| 2787 | */ |
| 2788 | pr_debug("battery entered normal range\n"); |
| 2789 | if (wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2790 | pr_debug("voltage high, releasing wakelock\n"); |
| 2791 | wake_unlock(&chip->low_voltage_wake_lock); |
| 2792 | } |
| 2793 | chip->vbat_monitor_params.state_request = |
| 2794 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2795 | chip->vbat_monitor_params.high_thr = |
| 2796 | chip->max_voltage_uv - VBATT_ERROR_MARGIN; |
| 2797 | chip->vbat_monitor_params.low_thr = |
| 2798 | chip->low_voltage_threshold; |
| 2799 | pr_debug("set low thr to %d and high to %d\n", |
| 2800 | chip->vbat_monitor_params.low_thr, |
| 2801 | chip->vbat_monitor_params.high_thr); |
| 2802 | } |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2803 | qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 2804 | &chip->vbat_monitor_params); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2805 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 2806 | } |
| 2807 | |
| 2808 | static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx) |
| 2809 | { |
| 2810 | struct qpnp_bms_chip *chip = ctx; |
| 2811 | int vbat_uv; |
| 2812 | struct qpnp_vadc_result result; |
| 2813 | int rc; |
| 2814 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2815 | rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2816 | pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code); |
| 2817 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2818 | get_battery_voltage(chip, &vbat_uv); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2819 | pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state); |
| 2820 | |
| 2821 | if (state == ADC_TM_LOW_STATE) { |
| 2822 | pr_debug("low voltage btm notification triggered\n"); |
| 2823 | if (vbat_uv - VBATT_ERROR_MARGIN |
| 2824 | < chip->vbat_monitor_params.low_thr) { |
| 2825 | configure_vbat_monitor_low(chip); |
| 2826 | } else { |
| 2827 | pr_debug("faulty btm trigger, discarding\n"); |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2828 | qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2829 | &chip->vbat_monitor_params); |
| 2830 | } |
| 2831 | } else if (state == ADC_TM_HIGH_STATE) { |
| 2832 | pr_debug("high voltage btm notification triggered\n"); |
| 2833 | if (vbat_uv + VBATT_ERROR_MARGIN |
| 2834 | > chip->vbat_monitor_params.high_thr) { |
| 2835 | configure_vbat_monitor_high(chip); |
| 2836 | } else { |
| 2837 | pr_debug("faulty btm trigger, discarding\n"); |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2838 | qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2839 | &chip->vbat_monitor_params); |
| 2840 | } |
| 2841 | } else { |
| 2842 | pr_debug("unknown voltage notification state: %d\n", state); |
| 2843 | } |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 2844 | if (chip->bms_psy_registered) |
Xiaozhe Shi | fa120db | 2013-06-06 15:57:19 -0700 | [diff] [blame] | 2845 | power_supply_changed(&chip->bms_psy); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | static int reset_vbat_monitoring(struct qpnp_bms_chip *chip) |
| 2849 | { |
| 2850 | int rc; |
| 2851 | |
| 2852 | chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE; |
Xiaozhe Shi | b5689fb | 2013-07-15 17:20:49 -0700 | [diff] [blame] | 2853 | |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2854 | rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 2855 | &chip->vbat_monitor_params); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2856 | if (rc) { |
Xiaozhe Shi | b5689fb | 2013-07-15 17:20:49 -0700 | [diff] [blame] | 2857 | pr_err("tm disable failed: %d\n", rc); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2858 | return rc; |
| 2859 | } |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2860 | if (wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2861 | pr_debug("battery removed, releasing wakelock\n"); |
| 2862 | wake_unlock(&chip->low_voltage_wake_lock); |
| 2863 | } |
| 2864 | if (chip->in_cv_range) { |
| 2865 | pr_debug("battery removed, removing in_cv_range state\n"); |
| 2866 | chip->in_cv_range = false; |
| 2867 | } |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2868 | return 0; |
| 2869 | } |
| 2870 | |
| 2871 | static int setup_vbat_monitoring(struct qpnp_bms_chip *chip) |
| 2872 | { |
| 2873 | int rc; |
| 2874 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2875 | chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold; |
| 2876 | chip->vbat_monitor_params.high_thr = chip->max_voltage_uv |
| 2877 | - VBATT_ERROR_MARGIN; |
| 2878 | chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2879 | chip->vbat_monitor_params.channel = VBAT_SNS; |
| 2880 | chip->vbat_monitor_params.btm_ctx = (void *)chip; |
| 2881 | chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S; |
| 2882 | chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat; |
| 2883 | pr_debug("set low thr to %d and high to %d\n", |
| 2884 | chip->vbat_monitor_params.low_thr, |
| 2885 | chip->vbat_monitor_params.high_thr); |
Xiaozhe Shi | b5689fb | 2013-07-15 17:20:49 -0700 | [diff] [blame] | 2886 | |
| 2887 | if (!is_battery_present(chip)) { |
| 2888 | pr_debug("no battery inserted, do not enable vbat monitoring\n"); |
| 2889 | chip->vbat_monitor_params.state_request = |
| 2890 | ADC_TM_HIGH_LOW_THR_DISABLE; |
| 2891 | } else { |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2892 | rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 2893 | &chip->vbat_monitor_params); |
Xiaozhe Shi | b5689fb | 2013-07-15 17:20:49 -0700 | [diff] [blame] | 2894 | if (rc) { |
| 2895 | pr_err("tm setup failed: %d\n", rc); |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2896 | return rc; |
Xiaozhe Shi | b5689fb | 2013-07-15 17:20:49 -0700 | [diff] [blame] | 2897 | } |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2898 | } |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 2899 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2900 | pr_debug("setup complete\n"); |
| 2901 | return 0; |
| 2902 | } |
| 2903 | |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2904 | static void readjust_fcc_table(struct qpnp_bms_chip *chip) |
| 2905 | { |
| 2906 | struct single_row_lut *temp, *old; |
| 2907 | int i, fcc, ratio; |
| 2908 | |
| 2909 | if (!chip->enable_fcc_learning) |
| 2910 | return; |
| 2911 | |
| 2912 | if (!chip->fcc_temp_lut) { |
| 2913 | pr_err("The static fcc lut table is NULL\n"); |
| 2914 | return; |
| 2915 | } |
| 2916 | |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 2917 | temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut), |
| 2918 | GFP_KERNEL); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2919 | if (!temp) { |
| 2920 | pr_err("Cannot allocate memory for adjusted fcc table\n"); |
| 2921 | return; |
| 2922 | } |
| 2923 | |
| 2924 | fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp); |
| 2925 | |
| 2926 | temp->cols = chip->fcc_temp_lut->cols; |
| 2927 | for (i = 0; i < chip->fcc_temp_lut->cols; i++) { |
| 2928 | temp->x[i] = chip->fcc_temp_lut->x[i]; |
| 2929 | ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc); |
| 2930 | temp->y[i] = (ratio * chip->fcc_new_mah); |
| 2931 | temp->y[i] /= 1000; |
| 2932 | } |
| 2933 | |
| 2934 | old = chip->adjusted_fcc_temp_lut; |
| 2935 | chip->adjusted_fcc_temp_lut = temp; |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 2936 | devm_kfree(chip->dev, old); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2937 | } |
| 2938 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 2939 | static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip) |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2940 | { |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 2941 | int rc, i; |
| 2942 | u8 fcc = 0, chgcyl = 0; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2943 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 2944 | for (i = 0; i < chip->min_fcc_learning_samples; i++) { |
| 2945 | rc = qpnp_read_wrapper(chip, &fcc, |
| 2946 | chip->base + BMS_FCC_BASE_REG + i, 1); |
| 2947 | rc |= qpnp_read_wrapper(chip, &chgcyl, |
| 2948 | chip->base + BMS_CHGCYL_BASE_REG + i, 1); |
| 2949 | if (rc) { |
| 2950 | pr_err("Unable to read FCC data\n"); |
| 2951 | return rc; |
| 2952 | } |
| 2953 | if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) { |
| 2954 | /* FCC invalid/not present */ |
| 2955 | chip->fcc_learning_samples[i].fcc_new = 0; |
| 2956 | chip->fcc_learning_samples[i].chargecycles = 0; |
| 2957 | } else { |
| 2958 | /* valid FCC data */ |
| 2959 | chip->fcc_sample_count++; |
| 2960 | chip->fcc_learning_samples[i].fcc_new = |
| 2961 | fcc * chip->fcc_resolution; |
| 2962 | chip->fcc_learning_samples[i].chargecycles = |
| 2963 | chgcyl * CHGCYL_RESOLUTION; |
| 2964 | } |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2965 | } |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 2966 | |
| 2967 | return 0; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 2968 | } |
| 2969 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 2970 | static int discard_backup_fcc_data(struct qpnp_bms_chip *chip) |
| 2971 | { |
| 2972 | int rc = 0, i; |
| 2973 | u8 temp_u8 = 0; |
| 2974 | |
| 2975 | chip->fcc_sample_count = 0; |
| 2976 | for (i = 0; i < chip->min_fcc_learning_samples; i++) { |
| 2977 | rc = qpnp_write_wrapper(chip, &temp_u8, |
| 2978 | chip->base + BMS_FCC_BASE_REG + i, 1); |
| 2979 | rc |= qpnp_write_wrapper(chip, &temp_u8, |
| 2980 | chip->base + BMS_CHGCYL_BASE_REG + i, 1); |
| 2981 | if (rc) { |
| 2982 | pr_err("Unable to clear FCC data\n"); |
| 2983 | return rc; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | static void |
| 2991 | average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip) |
| 2992 | { |
| 2993 | int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0; |
| 2994 | struct fcc_sample *ft; |
| 2995 | |
| 2996 | for (i = 0; i < chip->min_fcc_learning_samples; i++) |
| 2997 | temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new; |
| 2998 | |
| 2999 | temp_fcc_avg /= chip->min_fcc_learning_samples; |
| 3000 | temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100); |
| 3001 | |
| 3002 | /* fix the fcc if its an outlier i.e. > 5% of the average */ |
| 3003 | for (i = 0; i < chip->min_fcc_learning_samples; i++) { |
| 3004 | ft = &chip->fcc_learning_samples[i]; |
| 3005 | if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta) |
| 3006 | new_fcc_avg += temp_fcc_avg; |
| 3007 | else |
| 3008 | new_fcc_avg += ft->fcc_new; |
| 3009 | } |
| 3010 | new_fcc_avg /= chip->min_fcc_learning_samples; |
| 3011 | |
| 3012 | chip->fcc_new_mah = new_fcc_avg; |
| 3013 | chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP; |
| 3014 | pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n", |
| 3015 | new_fcc_avg, FCC_DEFAULT_TEMP); |
| 3016 | readjust_fcc_table(chip); |
| 3017 | } |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3018 | |
| 3019 | static void backup_charge_cycle(struct qpnp_bms_chip *chip) |
| 3020 | { |
| 3021 | int rc = 0; |
| 3022 | |
| 3023 | if (chip->charge_increase >= 0) { |
| 3024 | rc = qpnp_write_wrapper(chip, &chip->charge_increase, |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3025 | chip->base + CHARGE_INCREASE_STORAGE, 1); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3026 | if (rc) |
| 3027 | pr_err("Unable to backup charge_increase\n"); |
| 3028 | } |
| 3029 | |
| 3030 | if (chip->charge_cycles >= 0) { |
| 3031 | rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles, |
| 3032 | chip->base + CHARGE_CYCLE_STORAGE_LSB, 2); |
| 3033 | if (rc) |
| 3034 | pr_err("Unable to backup charge_cycles\n"); |
| 3035 | } |
| 3036 | } |
| 3037 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3038 | static bool chargecycles_in_range(struct qpnp_bms_chip *chip) |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3039 | { |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3040 | int i, min_cycle, max_cycle, valid_range; |
| 3041 | |
| 3042 | /* find the smallest and largest charge cycle */ |
| 3043 | max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles; |
| 3044 | for (i = 1; i < chip->min_fcc_learning_samples; i++) { |
| 3045 | if (min_cycle > chip->fcc_learning_samples[i].chargecycles) |
| 3046 | min_cycle = chip->fcc_learning_samples[i].chargecycles; |
| 3047 | if (max_cycle < chip->fcc_learning_samples[i].chargecycles) |
| 3048 | max_cycle = chip->fcc_learning_samples[i].chargecycles; |
| 3049 | } |
| 3050 | |
| 3051 | /* check if chargecyles are in range to continue with FCC update */ |
| 3052 | valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE, |
| 3053 | CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION; |
| 3054 | if (abs(max_cycle - min_cycle) > valid_range) |
| 3055 | return false; |
| 3056 | |
| 3057 | return true; |
| 3058 | } |
| 3059 | |
| 3060 | static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip) |
| 3061 | { |
| 3062 | int rc; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3063 | uint16_t temp_u16 = 0; |
| 3064 | u8 temp_u8 = 0; |
| 3065 | |
| 3066 | rc = qpnp_read_wrapper(chip, &temp_u8, |
| 3067 | chip->base + CHARGE_INCREASE_STORAGE, 1); |
| 3068 | if (!rc && temp_u8 != 0xFF) |
| 3069 | chip->charge_increase = temp_u8; |
| 3070 | |
| 3071 | rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16, |
| 3072 | chip->base + CHARGE_CYCLE_STORAGE_LSB, 2); |
| 3073 | if (!rc && temp_u16 != 0xFFFF) |
| 3074 | chip->charge_cycles = temp_u16; |
| 3075 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3076 | return rc; |
| 3077 | } |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3078 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3079 | static void |
| 3080 | attempt_learning_new_fcc(struct qpnp_bms_chip *chip) |
| 3081 | { |
| 3082 | pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count); |
| 3083 | |
| 3084 | /* update FCC if we have the required samples */ |
| 3085 | if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) && |
| 3086 | chargecycles_in_range(chip)) |
| 3087 | average_fcc_samples_and_readjust_fcc_table(chip); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | static int calculate_real_soc(struct qpnp_bms_chip *chip, |
| 3091 | int batt_temp, struct raw_soc_params *raw, int cc_uah) |
| 3092 | { |
| 3093 | int fcc_uah, rc_uah; |
| 3094 | |
| 3095 | fcc_uah = calculate_fcc(chip, batt_temp); |
| 3096 | rc_uah = calculate_ocv_charge(chip, raw, fcc_uah); |
| 3097 | |
| 3098 | return ((rc_uah - cc_uah) * 100) / fcc_uah; |
| 3099 | } |
| 3100 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3101 | #define MAX_U8_VALUE ((u8)(~0U)) |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3102 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3103 | static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah, |
| 3104 | int chargecycles) |
| 3105 | { |
| 3106 | int rc, min_cycle, i; |
| 3107 | u8 fcc_new, chgcyl, pos = 0; |
| 3108 | struct fcc_sample *ft; |
| 3109 | |
| 3110 | if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) || |
| 3111 | (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) { |
| 3112 | pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n", |
| 3113 | fcc_mah, chargecycles); |
| 3114 | return -EINVAL; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3115 | } |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3116 | |
| 3117 | if (chip->fcc_sample_count == chip->min_fcc_learning_samples) { |
| 3118 | /* search best location - oldest entry */ |
| 3119 | min_cycle = chip->fcc_learning_samples[0].chargecycles; |
| 3120 | for (i = 1; i < chip->min_fcc_learning_samples; i++) { |
| 3121 | if (min_cycle > |
Zhenhua Huang | 82663ea | 2014-09-26 10:42:31 +0800 | [diff] [blame] | 3122 | chip->fcc_learning_samples[i].chargecycles) { |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3123 | pos = i; |
Zhenhua Huang | 82663ea | 2014-09-26 10:42:31 +0800 | [diff] [blame] | 3124 | break; |
| 3125 | } |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3126 | } |
| 3127 | } else { |
| 3128 | /* find an empty location */ |
| 3129 | for (i = 0; i < chip->min_fcc_learning_samples; i++) { |
| 3130 | ft = &chip->fcc_learning_samples[i]; |
| 3131 | if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF && |
| 3132 | ft->chargecycles == 0xFF)) { |
| 3133 | pos = i; |
| 3134 | break; |
| 3135 | } |
| 3136 | } |
| 3137 | chip->fcc_sample_count++; |
| 3138 | } |
| 3139 | chip->fcc_learning_samples[pos].fcc_new = fcc_mah; |
| 3140 | chip->fcc_learning_samples[pos].chargecycles = chargecycles; |
| 3141 | |
| 3142 | fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution); |
| 3143 | rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new, |
| 3144 | chip->base + BMS_FCC_BASE_REG + pos, 1); |
| 3145 | if (rc) |
| 3146 | return rc; |
| 3147 | |
| 3148 | chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION); |
| 3149 | rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl, |
| 3150 | chip->base + BMS_CHGCYL_BASE_REG + pos, 1); |
| 3151 | if (rc) |
| 3152 | return rc; |
| 3153 | |
| 3154 | pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n", |
| 3155 | fcc_new, chgcyl, pos); |
| 3156 | |
| 3157 | return rc; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | static void update_fcc_learning_table(struct qpnp_bms_chip *chip, |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3161 | int new_fcc_uah, int chargecycles, int batt_temp) |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3162 | { |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3163 | int rc, fcc_default, fcc_temp; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3164 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3165 | /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */ |
| 3166 | fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000; |
| 3167 | fcc_temp = calculate_fcc(chip, batt_temp) / 1000; |
| 3168 | new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3169 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3170 | rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles); |
| 3171 | if (rc) { |
| 3172 | pr_err("Unable to backup new FCC\n"); |
| 3173 | return; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3174 | } |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3175 | /* check if FCC can be updated */ |
| 3176 | attempt_learning_new_fcc(chip); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah) |
| 3180 | { |
| 3181 | if ((new_fcc_uah >= (fcc_uah / 2)) && |
| 3182 | ((new_fcc_uah * 100) <= (fcc_uah * 105))) |
| 3183 | return true; |
| 3184 | |
| 3185 | pr_debug("FCC rejected - not within valid limit\n"); |
| 3186 | return false; |
| 3187 | } |
| 3188 | |
| 3189 | static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start) |
| 3190 | { |
| 3191 | int rc, batt_temp; |
| 3192 | struct raw_soc_params raw; |
| 3193 | struct qpnp_vadc_result result; |
| 3194 | int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc; |
| 3195 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 3196 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3197 | if (rc) { |
| 3198 | pr_err("Unable to read batt_temp\n"); |
| 3199 | return; |
| 3200 | } else { |
| 3201 | batt_temp = (int)result.physical; |
| 3202 | } |
| 3203 | |
| 3204 | rc = read_soc_params_raw(chip, &raw, batt_temp); |
| 3205 | if (rc) { |
| 3206 | pr_err("Unable to read CC, cannot update FCC\n"); |
| 3207 | return; |
| 3208 | } |
| 3209 | |
| 3210 | if (start) { |
| 3211 | chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut, |
Xiaozhe Shi | ae4375f | 2013-11-11 10:55:04 -0800 | [diff] [blame] | 3212 | batt_temp, raw.last_good_ocv_uv / 1000); |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 3213 | chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3214 | chip->start_real_soc = calculate_real_soc(chip, |
| 3215 | batt_temp, &raw, chip->start_cc_uah); |
| 3216 | pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n", |
| 3217 | chip->start_pc, chip->start_cc_uah, |
| 3218 | chip->start_soc, chip->start_real_soc); |
| 3219 | } else { |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 3220 | chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3221 | delta_soc = 100 - chip->start_real_soc; |
| 3222 | delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah); |
| 3223 | new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc); |
| 3224 | fcc_uah = calculate_fcc(chip, batt_temp); |
| 3225 | pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n", |
| 3226 | chip->start_soc, chip->start_pc, chip->start_real_soc, |
| 3227 | chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah); |
| 3228 | |
| 3229 | if (is_new_fcc_valid(new_fcc_uah, fcc_uah)) |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3230 | update_fcc_learning_table(chip, new_fcc_uah, |
| 3231 | chip->charge_cycles, batt_temp); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3235 | #define MAX_CAL_TRIES 200 |
| 3236 | #define MIN_CAL_UA 3000 |
| 3237 | static void batfet_open_work(struct work_struct *work) |
| 3238 | { |
| 3239 | int i; |
| 3240 | int rc; |
| 3241 | int result_ua; |
| 3242 | u8 orig_delay, sample_delay; |
| 3243 | struct qpnp_bms_chip *chip = container_of(work, |
| 3244 | struct qpnp_bms_chip, |
| 3245 | batfet_open_work); |
| 3246 | |
| 3247 | rc = qpnp_read_wrapper(chip, &orig_delay, |
| 3248 | chip->base + BMS1_S1_DELAY_CTL, 1); |
| 3249 | |
| 3250 | sample_delay = 0x0; |
| 3251 | rc = qpnp_write_wrapper(chip, &sample_delay, |
| 3252 | chip->base + BMS1_S1_DELAY_CTL, 1); |
| 3253 | |
| 3254 | /* |
| 3255 | * In certain PMICs there is a coupling issue which causes |
| 3256 | * bad calibration value that result in a huge battery current |
| 3257 | * even when the BATFET is open. Do continious calibrations until |
| 3258 | * we hit reasonable cal values which result in low battery current |
| 3259 | */ |
| 3260 | |
| 3261 | for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 3262 | rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3263 | /* |
| 3264 | * Wait 20mS after calibration and before reading battery |
| 3265 | * current. The BMS h/w uses calibration values in the |
| 3266 | * next sampling of vsense. |
| 3267 | */ |
| 3268 | msleep(20); |
| 3269 | rc |= get_battery_current(chip, &result_ua); |
| 3270 | if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) { |
| 3271 | pr_debug("good cal at %d attempt\n", i); |
| 3272 | break; |
| 3273 | } |
| 3274 | } |
| 3275 | pr_debug("batfet_closed = %d i = %d result_ua = %d\n", |
| 3276 | chip->batfet_closed, i, result_ua); |
| 3277 | |
| 3278 | rc = qpnp_write_wrapper(chip, &orig_delay, |
| 3279 | chip->base + BMS1_S1_DELAY_CTL, 1); |
| 3280 | } |
| 3281 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3282 | static void charging_began(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 3283 | { |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3284 | mutex_lock(&chip->last_soc_mutex); |
| 3285 | chip->charge_start_tm_sec = 0; |
| 3286 | chip->catch_up_time_sec = 0; |
| 3287 | mutex_unlock(&chip->last_soc_mutex); |
| 3288 | |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3289 | chip->start_soc = report_state_of_charge(chip); |
| 3290 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3291 | mutex_lock(&chip->last_ocv_uv_mutex); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3292 | if (chip->enable_fcc_learning) |
| 3293 | fcc_learning_config(chip, true); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3294 | chip->soc_at_cv = -EINVAL; |
| 3295 | chip->prev_chg_soc = -EINVAL; |
| 3296 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 3297 | } |
| 3298 | |
| 3299 | static void charging_ended(struct qpnp_bms_chip *chip) |
| 3300 | { |
| 3301 | mutex_lock(&chip->last_soc_mutex); |
| 3302 | chip->charge_start_tm_sec = 0; |
| 3303 | chip->catch_up_time_sec = 0; |
| 3304 | mutex_unlock(&chip->last_soc_mutex); |
| 3305 | |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3306 | chip->end_soc = report_state_of_charge(chip); |
| 3307 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3308 | mutex_lock(&chip->last_ocv_uv_mutex); |
| 3309 | chip->soc_at_cv = -EINVAL; |
| 3310 | chip->prev_chg_soc = -EINVAL; |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 3311 | chip->in_taper_charge = false; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3312 | |
| 3313 | /* update the chargecycles */ |
| 3314 | if (chip->end_soc > chip->start_soc) { |
| 3315 | chip->charge_increase += (chip->end_soc - chip->start_soc); |
| 3316 | if (chip->charge_increase > 100) { |
| 3317 | chip->charge_cycles++; |
| 3318 | chip->charge_increase = chip->charge_increase % 100; |
| 3319 | } |
| 3320 | if (chip->enable_fcc_learning) |
| 3321 | backup_charge_cycle(chip); |
| 3322 | } |
| 3323 | |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 3324 | if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) { |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3325 | if (chip->enable_fcc_learning && |
| 3326 | (chip->start_soc <= chip->min_fcc_learning_soc) && |
| 3327 | (chip->start_pc <= chip->min_fcc_ocv_pc)) |
| 3328 | fcc_learning_config(chip, false); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3329 | chip->done_charging = true; |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 3330 | chip->last_soc_invalid = true; |
Xiaozhe Shi | cc48e99 | 2013-05-28 16:42:24 -0700 | [diff] [blame] | 3331 | } else if (chip->charging_adjusted_ocv > 0) { |
| 3332 | pr_debug("Charging stopped before full, adjusted OCV = %d\n", |
| 3333 | chip->charging_adjusted_ocv); |
| 3334 | chip->last_ocv_uv = chip->charging_adjusted_ocv; |
Xiaozhe Shi | 83484e3 | 2013-05-16 10:59:59 -0700 | [diff] [blame] | 3335 | } |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3336 | |
Xiaozhe Shi | cc48e99 | 2013-05-28 16:42:24 -0700 | [diff] [blame] | 3337 | chip->charging_adjusted_ocv = -EINVAL; |
| 3338 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3339 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 3340 | } |
| 3341 | |
| 3342 | static void battery_status_check(struct qpnp_bms_chip *chip) |
| 3343 | { |
| 3344 | int status = get_battery_status(chip); |
| 3345 | |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 3346 | mutex_lock(&chip->status_lock); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3347 | if (chip->battery_status != status) { |
Xiaozhe Shi | 30e9480 | 2013-08-19 16:40:53 -0700 | [diff] [blame] | 3348 | pr_debug("status = %d, shadow status = %d\n", |
| 3349 | status, chip->battery_status); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3350 | if (status == POWER_SUPPLY_STATUS_CHARGING) { |
| 3351 | pr_debug("charging started\n"); |
| 3352 | charging_began(chip); |
| 3353 | } else if (chip->battery_status |
| 3354 | == POWER_SUPPLY_STATUS_CHARGING) { |
| 3355 | pr_debug("charging ended\n"); |
| 3356 | charging_ended(chip); |
| 3357 | } |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3358 | |
| 3359 | if (status == POWER_SUPPLY_STATUS_FULL) { |
| 3360 | pr_debug("battery full\n"); |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 3361 | recalculate_soc(chip); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3362 | } else if (chip->battery_status |
| 3363 | == POWER_SUPPLY_STATUS_FULL) { |
| 3364 | pr_debug("battery not full any more\n"); |
| 3365 | disable_bms_irq(&chip->ocv_thr_irq); |
| 3366 | disable_bms_irq(&chip->sw_cc_thr_irq); |
| 3367 | } |
| 3368 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3369 | chip->battery_status = status; |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3370 | /* battery charge status has changed, so force a soc |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3371 | * recalculation to update the SoC */ |
| 3372 | schedule_work(&chip->recalc_work); |
| 3373 | } |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 3374 | mutex_unlock(&chip->status_lock); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 3377 | #define CALIB_WRKARND_DIG_MAJOR_MAX 0x03 |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3378 | static void batfet_status_check(struct qpnp_bms_chip *chip) |
| 3379 | { |
| 3380 | bool batfet_closed; |
| 3381 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3382 | batfet_closed = is_batfet_closed(chip); |
| 3383 | if (chip->batfet_closed != batfet_closed) { |
| 3384 | chip->batfet_closed = batfet_closed; |
Xiaozhe Shi | ffb7cfc | 2014-01-03 10:47:36 -0800 | [diff] [blame] | 3385 | if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) |
| 3386 | return; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3387 | if (batfet_closed == false) { |
| 3388 | /* batfet opened */ |
| 3389 | schedule_work(&chip->batfet_open_work); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 3390 | qpnp_iadc_skip_calibration(chip->iadc_dev); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3391 | } else { |
| 3392 | /* batfet closed */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 3393 | qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true); |
| 3394 | qpnp_iadc_resume_calibration(chip->iadc_dev); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | } |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 3398 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3399 | static void battery_insertion_check(struct qpnp_bms_chip *chip) |
| 3400 | { |
Xiaozhe Shi | 90f3a41 | 2013-08-21 10:31:35 -0700 | [diff] [blame] | 3401 | int present = (int)is_battery_present(chip); |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 3402 | int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip); |
| 3403 | int insertion_ocv_taken = (insertion_ocv_uv > 0); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 3404 | |
| 3405 | mutex_lock(&chip->vbat_monitor_mutex); |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 3406 | if (chip->battery_present != present |
| 3407 | && (present == insertion_ocv_taken |
| 3408 | || chip->battery_present == -EINVAL)) { |
| 3409 | pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n", |
| 3410 | present, chip->battery_present, |
| 3411 | insertion_ocv_uv); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 3412 | if (chip->battery_present != -EINVAL) { |
| 3413 | if (present) { |
Xiaozhe Shi | 24f91a0 | 2013-08-29 17:15:05 -0700 | [diff] [blame] | 3414 | chip->insertion_ocv_uv = insertion_ocv_uv; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 3415 | setup_vbat_monitoring(chip); |
| 3416 | chip->new_battery = true; |
| 3417 | } else { |
| 3418 | reset_vbat_monitoring(chip); |
| 3419 | } |
| 3420 | } |
| 3421 | chip->battery_present = present; |
| 3422 | /* a new battery was inserted or removed, so force a soc |
| 3423 | * recalculation to update the SoC */ |
| 3424 | schedule_work(&chip->recalc_work); |
| 3425 | } |
| 3426 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 3427 | } |
| 3428 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3429 | /* Returns capacity as a SoC percentage between 0 and 100 */ |
| 3430 | static int get_prop_bms_capacity(struct qpnp_bms_chip *chip) |
| 3431 | { |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 3432 | return report_state_of_charge(chip); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3433 | } |
| 3434 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3435 | static void qpnp_bms_external_power_changed(struct power_supply *psy) |
| 3436 | { |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3437 | struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip, |
| 3438 | bms_psy); |
| 3439 | |
| 3440 | battery_insertion_check(chip); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3441 | batfet_status_check(chip); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3442 | battery_status_check(chip); |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 3443 | |
| 3444 | if (POWER_SUPPLY_CHARGE_TYPE_TAPER == get_battery_charge_type(chip)) |
| 3445 | chip->in_taper_charge = true; |
| 3446 | else |
| 3447 | chip->in_taper_charge = false; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | static int qpnp_bms_power_get_property(struct power_supply *psy, |
| 3451 | enum power_supply_property psp, |
| 3452 | union power_supply_propval *val) |
| 3453 | { |
| 3454 | struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip, |
| 3455 | bms_psy); |
| 3456 | |
| 3457 | switch (psp) { |
| 3458 | case POWER_SUPPLY_PROP_CAPACITY: |
| 3459 | val->intval = get_prop_bms_capacity(chip); |
| 3460 | break; |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 3461 | case POWER_SUPPLY_PROP_STATUS: |
| 3462 | val->intval = chip->battery_status; |
| 3463 | break; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3464 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 3465 | val->intval = get_prop_bms_current_now(chip); |
| 3466 | break; |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 3467 | case POWER_SUPPLY_PROP_RESISTANCE: |
| 3468 | val->intval = get_prop_bms_batt_resistance(chip); |
| 3469 | break; |
Xiaozhe Shi | fb37f3b | 2013-05-20 16:56:19 -0700 | [diff] [blame] | 3470 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
| 3471 | val->intval = get_prop_bms_charge_counter(chip); |
| 3472 | break; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 3473 | case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW: |
| 3474 | val->intval = get_prop_bms_charge_counter_shadow(chip); |
| 3475 | break; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3476 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 3477 | val->intval = get_prop_bms_charge_full_design(chip); |
| 3478 | break; |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 3479 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 3480 | val->intval = get_prop_bms_charge_full(chip); |
| 3481 | break; |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 3482 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 3483 | val->intval = chip->charge_cycles; |
| 3484 | break; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3485 | default: |
| 3486 | return -EINVAL; |
| 3487 | } |
| 3488 | return 0; |
| 3489 | } |
| 3490 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 3491 | #define OCV_USE_LIMIT_EN BIT(7) |
| 3492 | static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip, |
| 3493 | int low_voltage_threshold, |
| 3494 | int high_voltage_threshold) |
| 3495 | { |
| 3496 | uint16_t low_voltage_raw, high_voltage_raw; |
| 3497 | int rc; |
| 3498 | |
| 3499 | low_voltage_raw = convert_vbatt_uv_to_raw(chip, |
| 3500 | low_voltage_threshold); |
| 3501 | high_voltage_raw = convert_vbatt_uv_to_raw(chip, |
| 3502 | high_voltage_threshold); |
| 3503 | rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw, |
| 3504 | chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2); |
| 3505 | if (rc) { |
| 3506 | pr_err("Failed to set ocv low voltage threshold: %d\n", rc); |
| 3507 | return rc; |
| 3508 | } |
| 3509 | rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw, |
| 3510 | chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2); |
| 3511 | if (rc) { |
| 3512 | pr_err("Failed to set ocv high voltage threshold: %d\n", rc); |
| 3513 | return rc; |
| 3514 | } |
| 3515 | rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL, |
| 3516 | OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN); |
| 3517 | if (rc) { |
| 3518 | pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc); |
| 3519 | return rc; |
| 3520 | } |
| 3521 | pr_debug("ocv low threshold set to %d uv or 0x%x raw\n", |
| 3522 | low_voltage_threshold, low_voltage_raw); |
| 3523 | pr_debug("ocv high threshold set to %d uv or 0x%x raw\n", |
| 3524 | high_voltage_threshold, high_voltage_raw); |
| 3525 | return 0; |
| 3526 | } |
| 3527 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3528 | static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3529 | { |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3530 | u8 iavg; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3531 | int rc; |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 3532 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3533 | rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1); |
| 3534 | if (rc) { |
| 3535 | pr_err("failed to read addr = %d %d assuming %d\n", |
| 3536 | chip->base + IAVG_STORAGE_REG, rc, |
Xiaozhe Shi | 68a1bb2 | 2013-09-23 14:52:00 -0700 | [diff] [blame] | 3537 | MIN_IAVG_MA); |
| 3538 | return MIN_IAVG_MA; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3539 | } else if (iavg == IAVG_INVALID) { |
| 3540 | pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n", |
Xiaozhe Shi | 68a1bb2 | 2013-09-23 14:52:00 -0700 | [diff] [blame] | 3541 | MIN_IAVG_MA); |
| 3542 | return MIN_IAVG_MA; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3543 | } else { |
| 3544 | if (iavg == 0) |
Xiaozhe Shi | 68a1bb2 | 2013-09-23 14:52:00 -0700 | [diff] [blame] | 3545 | return MIN_IAVG_MA; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3546 | else |
Xiaozhe Shi | 68a1bb2 | 2013-09-23 14:52:00 -0700 | [diff] [blame] | 3547 | return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | static int read_shutdown_soc(struct qpnp_bms_chip *chip) |
| 3552 | { |
| 3553 | u8 stored_soc; |
| 3554 | int rc, shutdown_soc; |
| 3555 | |
| 3556 | /* |
| 3557 | * The previous SOC is stored in the first 7 bits of the register as |
| 3558 | * (Shutdown SOC + 1). This allows for register reset values of both |
| 3559 | * 0x00 and 0x7F. |
| 3560 | */ |
| 3561 | rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1); |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 3562 | if (rc) { |
| 3563 | pr_err("failed to read addr = %d %d\n", |
| 3564 | chip->soc_storage_addr, rc); |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3565 | return SOC_INVALID; |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 3566 | } |
| 3567 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3568 | if ((stored_soc >> 1) > 0) |
| 3569 | shutdown_soc = (stored_soc >> 1) - 1; |
| 3570 | else |
| 3571 | shutdown_soc = SOC_INVALID; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3572 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3573 | pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n", |
| 3574 | stored_soc, shutdown_soc); |
| 3575 | return shutdown_soc; |
| 3576 | } |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3577 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3578 | #define BAT_REMOVED_OFFMODE_BIT BIT(6) |
| 3579 | static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip) |
| 3580 | { |
| 3581 | u8 batt_pres; |
| 3582 | int rc; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3583 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3584 | if (chip->batt_pres_addr) { |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 3585 | rc = qpnp_read_wrapper(chip, &batt_pres, |
| 3586 | chip->batt_pres_addr, 1); |
| 3587 | pr_debug("offmode removed: %02x\n", batt_pres); |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3588 | if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT)) |
| 3589 | return true; |
| 3590 | } |
| 3591 | return false; |
| 3592 | } |
| 3593 | |
| 3594 | static void load_shutdown_data(struct qpnp_bms_chip *chip) |
| 3595 | { |
| 3596 | int calculated_soc, shutdown_soc; |
| 3597 | bool invalid_stored_soc; |
| 3598 | bool offmode_battery_replaced; |
| 3599 | bool shutdown_soc_out_of_limit; |
| 3600 | |
| 3601 | /* |
| 3602 | * Read the saved shutdown SoC from the configured register and |
| 3603 | * check if the value has been reset |
| 3604 | */ |
| 3605 | shutdown_soc = read_shutdown_soc(chip); |
| 3606 | invalid_stored_soc = (shutdown_soc == SOC_INVALID); |
| 3607 | |
| 3608 | /* |
| 3609 | * Do a quick run of SoC calculation to find whether the shutdown soc |
| 3610 | * is close enough. |
| 3611 | */ |
Xiaozhe Shi | 2c17117 | 2013-12-03 13:27:37 -0800 | [diff] [blame] | 3612 | chip->shutdown_iavg_ma = MIN_IAVG_MA; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3613 | calculated_soc = recalculate_raw_soc(chip); |
| 3614 | shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc) |
| 3615 | > chip->shutdown_soc_valid_limit); |
| 3616 | pr_debug("calculated_soc = %d, valid_limit = %d\n", |
| 3617 | calculated_soc, chip->shutdown_soc_valid_limit); |
| 3618 | |
| 3619 | /* |
| 3620 | * Check if the battery has been replaced while the system was powered |
| 3621 | * down. |
| 3622 | */ |
| 3623 | offmode_battery_replaced = is_battery_replaced_in_offmode(chip); |
| 3624 | |
| 3625 | /* Invalidate the shutdown SoC if any of these conditions hold true */ |
| 3626 | if (chip->ignore_shutdown_soc |
| 3627 | || invalid_stored_soc |
| 3628 | || offmode_battery_replaced |
| 3629 | || shutdown_soc_out_of_limit) { |
| 3630 | chip->battery_removed = true; |
| 3631 | chip->shutdown_soc_invalid = true; |
Xiaozhe Shi | c92cfd9 | 2013-10-25 11:36:42 -0700 | [diff] [blame] | 3632 | chip->shutdown_iavg_ma = MIN_IAVG_MA; |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3633 | pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n", |
| 3634 | invalid_stored_soc, offmode_battery_replaced, |
| 3635 | shutdown_soc_out_of_limit); |
| 3636 | } else { |
| 3637 | chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip); |
| 3638 | chip->shutdown_soc = shutdown_soc; |
Xiaozhe Shi | 7c41a29 | 2013-08-16 16:50:17 -0700 | [diff] [blame] | 3639 | } |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3640 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 3641 | pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n", |
| 3642 | calculated_soc, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3643 | chip->shutdown_soc, |
| 3644 | chip->shutdown_iavg_ma, |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3645 | chip->shutdown_soc_invalid, |
| 3646 | chip->battery_removed); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3647 | } |
| 3648 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3649 | static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip) |
| 3650 | { |
| 3651 | struct qpnp_bms_chip *chip = _chip; |
| 3652 | |
| 3653 | pr_debug("ocv_thr irq triggered\n"); |
| 3654 | bms_stay_awake(&chip->soc_wake_source); |
| 3655 | schedule_work(&chip->recalc_work); |
| 3656 | return IRQ_HANDLED; |
| 3657 | } |
| 3658 | |
| 3659 | static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip) |
| 3660 | { |
| 3661 | struct qpnp_bms_chip *chip = _chip; |
| 3662 | |
| 3663 | pr_debug("sw_cc_thr irq triggered\n"); |
Anirudh Ghayal | 1166eef | 2013-12-23 19:05:33 +0530 | [diff] [blame] | 3664 | disable_bms_irq_nosync(&chip->sw_cc_thr_irq); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3665 | bms_stay_awake(&chip->soc_wake_source); |
| 3666 | schedule_work(&chip->recalc_work); |
| 3667 | return IRQ_HANDLED; |
| 3668 | } |
| 3669 | |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3670 | static int64_t read_battery_id(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 3671 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3672 | int rc; |
| 3673 | struct qpnp_vadc_result result; |
| 3674 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 3675 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3676 | if (rc) { |
| 3677 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 3678 | LR_MUX2_BAT_ID, rc); |
| 3679 | return rc; |
| 3680 | } |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3681 | |
| 3682 | return result.physical; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | static int set_battery_data(struct qpnp_bms_chip *chip) |
| 3686 | { |
| 3687 | int64_t battery_id; |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3688 | int rc = 0, dt_data = false; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3689 | struct bms_battery_data *batt_data; |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3690 | struct device_node *node; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 3691 | |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3692 | if (chip->batt_type == BATT_DESAY) { |
| 3693 | batt_data = &desay_5200_data; |
| 3694 | } else if (chip->batt_type == BATT_PALLADIUM) { |
| 3695 | batt_data = &palladium_1500_data; |
| 3696 | } else if (chip->batt_type == BATT_OEM) { |
| 3697 | batt_data = &oem_batt_data; |
Wu Fenglin | 2ac88aa | 2013-04-25 12:43:40 +0800 | [diff] [blame] | 3698 | } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) { |
| 3699 | batt_data = &QRD_4v35_2000mAh_data; |
tingting | f50326f | 2013-06-05 15:07:24 +0800 | [diff] [blame] | 3700 | } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) { |
| 3701 | batt_data = &qrd_4v2_1300mah_data; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 3702 | } else { |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3703 | battery_id = read_battery_id(chip); |
| 3704 | if (battery_id < 0) { |
| 3705 | pr_err("cannot read battery id err = %lld\n", |
| 3706 | battery_id); |
| 3707 | return battery_id; |
| 3708 | } |
| 3709 | |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3710 | node = of_find_node_by_name(chip->spmi->dev.of_node, |
| 3711 | "qcom,battery-data"); |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3712 | if (!node) { |
| 3713 | pr_warn("No available batterydata, using palladium 1500\n"); |
| 3714 | batt_data = &palladium_1500_data; |
| 3715 | goto assign_data; |
| 3716 | } |
| 3717 | batt_data = devm_kzalloc(chip->dev, |
| 3718 | sizeof(struct bms_battery_data), GFP_KERNEL); |
| 3719 | if (!batt_data) { |
| 3720 | pr_err("Could not alloc battery data\n"); |
| 3721 | batt_data = &palladium_1500_data; |
| 3722 | goto assign_data; |
| 3723 | } |
| 3724 | batt_data->fcc_temp_lut = devm_kzalloc(chip->dev, |
| 3725 | sizeof(struct single_row_lut), |
| 3726 | GFP_KERNEL); |
| 3727 | batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev, |
| 3728 | sizeof(struct pc_temp_ocv_lut), |
| 3729 | GFP_KERNEL); |
| 3730 | batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev, |
| 3731 | sizeof(struct sf_lut), |
| 3732 | GFP_KERNEL); |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3733 | |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3734 | batt_data->max_voltage_uv = -1; |
| 3735 | batt_data->cutoff_uv = -1; |
| 3736 | batt_data->iterm_ua = -1; |
Xiaozhe Shi | 23174ea | 2013-07-30 17:51:09 -0700 | [diff] [blame] | 3737 | |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3738 | /* |
| 3739 | * if the alloced luts are 0s, of_batterydata_read_data ignores |
| 3740 | * them. |
| 3741 | */ |
| 3742 | rc = of_batterydata_read_data(node, batt_data, battery_id); |
| 3743 | if (rc == 0 && batt_data->fcc_temp_lut |
| 3744 | && batt_data->pc_temp_ocv_lut |
| 3745 | && batt_data->rbatt_sf_lut) { |
| 3746 | dt_data = true; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3747 | } else { |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3748 | pr_err("battery data load failed, using palladium 1500\n"); |
| 3749 | devm_kfree(chip->dev, batt_data->fcc_temp_lut); |
| 3750 | devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut); |
| 3751 | devm_kfree(chip->dev, batt_data->rbatt_sf_lut); |
| 3752 | devm_kfree(chip->dev, batt_data); |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3753 | batt_data = &palladium_1500_data; |
| 3754 | } |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3757 | assign_data: |
Xiaozhe Shi | 976618f | 2013-04-30 10:49:30 -0700 | [diff] [blame] | 3758 | chip->fcc_mah = batt_data->fcc; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3759 | chip->fcc_temp_lut = batt_data->fcc_temp_lut; |
| 3760 | chip->fcc_sf_lut = batt_data->fcc_sf_lut; |
| 3761 | chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut; |
| 3762 | chip->pc_sf_lut = batt_data->pc_sf_lut; |
| 3763 | chip->rbatt_sf_lut = batt_data->rbatt_sf_lut; |
| 3764 | chip->default_rbatt_mohm = batt_data->default_rbatt_mohm; |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 3765 | chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm; |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 3766 | chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3767 | |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3768 | /* Override battery properties if specified in the battery profile */ |
Xiaozhe Shi | 23174ea | 2013-07-30 17:51:09 -0700 | [diff] [blame] | 3769 | if (batt_data->max_voltage_uv >= 0 && dt_data) |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3770 | chip->max_voltage_uv = batt_data->max_voltage_uv; |
Xiaozhe Shi | 23174ea | 2013-07-30 17:51:09 -0700 | [diff] [blame] | 3771 | if (batt_data->cutoff_uv >= 0 && dt_data) |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3772 | chip->v_cutoff_uv = batt_data->cutoff_uv; |
Xiaozhe Shi | 23174ea | 2013-07-30 17:51:09 -0700 | [diff] [blame] | 3773 | if (batt_data->iterm_ua >= 0 && dt_data) |
Xiaozhe Shi | af203c2 | 2013-06-19 12:01:38 -0700 | [diff] [blame] | 3774 | chip->chg_term_ua = batt_data->iterm_ua; |
| 3775 | |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3776 | if (chip->pc_temp_ocv_lut == NULL) { |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3777 | pr_err("temp ocv lut table has not been loaded\n"); |
| 3778 | if (dt_data) { |
| 3779 | devm_kfree(chip->dev, batt_data->fcc_temp_lut); |
| 3780 | devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut); |
| 3781 | devm_kfree(chip->dev, batt_data->rbatt_sf_lut); |
| 3782 | devm_kfree(chip->dev, batt_data); |
| 3783 | } |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3784 | return -EINVAL; |
| 3785 | } |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3786 | |
| 3787 | if (dt_data) |
| 3788 | devm_kfree(chip->dev, batt_data); |
| 3789 | |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 3790 | return 0; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 3793 | static int bms_get_adc(struct qpnp_bms_chip *chip, |
| 3794 | struct spmi_device *spmi) |
| 3795 | { |
| 3796 | int rc = 0; |
| 3797 | |
| 3798 | chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms"); |
| 3799 | if (IS_ERR(chip->vadc_dev)) { |
| 3800 | rc = PTR_ERR(chip->vadc_dev); |
| 3801 | if (rc != -EPROBE_DEFER) |
| 3802 | pr_err("vadc property missing, rc=%d\n", rc); |
| 3803 | return rc; |
| 3804 | } |
| 3805 | |
| 3806 | chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms"); |
| 3807 | if (IS_ERR(chip->iadc_dev)) { |
| 3808 | rc = PTR_ERR(chip->iadc_dev); |
| 3809 | if (rc != -EPROBE_DEFER) |
| 3810 | pr_err("iadc property missing, rc=%d\n", rc); |
| 3811 | return rc; |
| 3812 | } |
| 3813 | |
| 3814 | chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms"); |
| 3815 | if (IS_ERR(chip->adc_tm_dev)) { |
| 3816 | rc = PTR_ERR(chip->adc_tm_dev); |
| 3817 | if (rc != -EPROBE_DEFER) |
| 3818 | pr_err("adc-tm not ready, defer probe\n"); |
| 3819 | return rc; |
| 3820 | } |
| 3821 | |
| 3822 | return 0; |
| 3823 | } |
| 3824 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3825 | #define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3826 | do { \ |
Xiaozhe Shi | 2e47668 | 2013-07-22 14:57:22 -0700 | [diff] [blame] | 3827 | if (retval) \ |
| 3828 | break; \ |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3829 | retval = of_property_read_u32(chip->spmi->dev.of_node, \ |
Xiaozhe Shi | 9bd2462 | 2013-01-23 15:54:54 -0800 | [diff] [blame] | 3830 | "qcom," qpnp_spmi_property, \ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3831 | &chip->chip_prop); \ |
| 3832 | if (retval) { \ |
| 3833 | pr_err("Error reading " #qpnp_spmi_property \ |
| 3834 | " property %d\n", rc); \ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3835 | } \ |
| 3836 | } while (0) |
| 3837 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3838 | #define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \ |
| 3839 | do { \ |
| 3840 | chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\ |
| 3841 | "qcom," qpnp_spmi_property); \ |
| 3842 | } while (0) |
| 3843 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3844 | static inline int bms_read_properties(struct qpnp_bms_chip *chip) |
| 3845 | { |
Xiaozhe Shi | 2e47668 | 2013-07-22 14:57:22 -0700 | [diff] [blame] | 3846 | int rc = 0; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3847 | |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 3848 | SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3849 | SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc); |
| 3850 | SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc); |
| 3851 | SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc); |
| 3852 | SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc); |
| 3853 | SPMI_PROP_READ(shutdown_soc_valid_limit, |
| 3854 | "shutdown-soc-valid-limit", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3855 | SPMI_PROP_READ(adjust_soc_low_threshold, |
| 3856 | "adjust-soc-low-threshold", rc); |
| 3857 | SPMI_PROP_READ(batt_type, "batt-type", rc); |
| 3858 | SPMI_PROP_READ(low_soc_calc_threshold, |
| 3859 | "low-soc-calculate-soc-threshold", rc); |
| 3860 | SPMI_PROP_READ(low_soc_calculate_soc_ms, |
| 3861 | "low-soc-calculate-soc-ms", rc); |
Xiaozhe Shi | cb487b1 | 2013-10-14 17:42:07 -0700 | [diff] [blame] | 3862 | SPMI_PROP_READ(low_voltage_calculate_soc_ms, |
| 3863 | "low-voltage-calculate-soc-ms", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3864 | SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc); |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 3865 | SPMI_PROP_READ(high_ocv_correction_limit_uv, |
| 3866 | "high-ocv-correction-limit-uv", rc); |
| 3867 | SPMI_PROP_READ(low_ocv_correction_limit_uv, |
| 3868 | "low-ocv-correction-limit-uv", rc); |
| 3869 | SPMI_PROP_READ(hold_soc_est, |
| 3870 | "hold-soc-est", rc); |
| 3871 | SPMI_PROP_READ(ocv_high_threshold_uv, |
| 3872 | "ocv-voltage-high-threshold-uv", rc); |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 3873 | SPMI_PROP_READ(ocv_low_threshold_uv, |
| 3874 | "ocv-voltage-low-threshold-uv", rc); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 3875 | SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc); |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 3876 | SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3877 | |
Xiaozhe Shi | 2e47668 | 2013-07-22 14:57:22 -0700 | [diff] [blame] | 3878 | chip->use_external_rsense = of_property_read_bool( |
| 3879 | chip->spmi->dev.of_node, |
| 3880 | "qcom,use-external-rsense"); |
| 3881 | chip->ignore_shutdown_soc = of_property_read_bool( |
| 3882 | chip->spmi->dev.of_node, |
| 3883 | "qcom,ignore-shutdown-soc"); |
| 3884 | chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node, |
| 3885 | "qcom,use-voltage-soc"); |
| 3886 | chip->use_ocv_thresholds = of_property_read_bool( |
| 3887 | chip->spmi->dev.of_node, |
| 3888 | "qcom,use-ocv-thresholds"); |
| 3889 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3890 | if (chip->adjust_soc_low_threshold >= 45) |
| 3891 | chip->adjust_soc_low_threshold = 45; |
| 3892 | |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3893 | SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning"); |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3894 | if (chip->enable_fcc_learning) { |
| 3895 | SPMI_PROP_READ(min_fcc_learning_soc, |
| 3896 | "min-fcc-learning-soc", rc); |
| 3897 | SPMI_PROP_READ(min_fcc_ocv_pc, |
| 3898 | "min-fcc-ocv-pc", rc); |
| 3899 | SPMI_PROP_READ(min_fcc_learning_samples, |
| 3900 | "min-fcc-learning-samples", rc); |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 3901 | SPMI_PROP_READ(fcc_resolution, |
| 3902 | "fcc-resolution", rc); |
| 3903 | if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES) |
| 3904 | chip->min_fcc_learning_samples = MAX_FCC_CYCLES; |
| 3905 | chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev, |
| 3906 | (sizeof(struct fcc_sample) * |
| 3907 | chip->min_fcc_learning_samples), GFP_KERNEL); |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 3908 | if (chip->fcc_learning_samples == NULL) |
| 3909 | return -ENOMEM; |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 3910 | pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n", |
| 3911 | chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc, |
| 3912 | chip->min_fcc_learning_samples); |
| 3913 | } |
| 3914 | |
Xiaozhe Shi | 2e47668 | 2013-07-22 14:57:22 -0700 | [diff] [blame] | 3915 | if (rc) { |
| 3916 | pr_err("Missing required properties.\n"); |
| 3917 | return rc; |
| 3918 | } |
| 3919 | |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 3920 | pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n", |
| 3921 | chip->r_sense_uohm, chip->v_cutoff_uv, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3922 | chip->max_voltage_uv); |
| 3923 | pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n", |
| 3924 | chip->r_conn_mohm, chip->shutdown_soc_valid_limit, |
| 3925 | chip->adjust_soc_low_threshold); |
Xiaozhe Shi | 561ebf7 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 3926 | pr_debug("chg_term_ua:%d, batt_type:%d\n", |
| 3927 | chip->chg_term_ua, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3928 | chip->batt_type); |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 3929 | pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n", |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 3930 | chip->ignore_shutdown_soc, chip->use_voltage_soc); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 3931 | pr_debug("use external rsense: %d\n", chip->use_external_rsense); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3932 | return 0; |
| 3933 | } |
| 3934 | |
| 3935 | static inline void bms_initialize_constants(struct qpnp_bms_chip *chip) |
| 3936 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3937 | chip->prev_pc_unusable = -EINVAL; |
| 3938 | chip->soc_at_cv = -EINVAL; |
| 3939 | chip->calculated_soc = -EINVAL; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 3940 | chip->last_soc = -EINVAL; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 3941 | chip->last_soc_est = -EINVAL; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 3942 | chip->battery_present = -EINVAL; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 3943 | chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 3944 | chip->last_cc_uah = INT_MIN; |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 3945 | chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
| 3946 | chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 3947 | chip->first_time_calc_soc = 1; |
| 3948 | chip->first_time_calc_uuc = 1; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 3951 | #define SPMI_FIND_IRQ(chip, irq_name) \ |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3952 | do { \ |
| 3953 | chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \ |
| 3954 | resource, #irq_name); \ |
| 3955 | if (chip->irq_name##_irq.irq < 0) { \ |
| 3956 | pr_err("Unable to get " #irq_name " irq\n"); \ |
| 3957 | return -ENXIO; \ |
| 3958 | } \ |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 3959 | } while (0) |
| 3960 | |
| 3961 | static int bms_find_irqs(struct qpnp_bms_chip *chip, |
| 3962 | struct spmi_resource *resource) |
| 3963 | { |
| 3964 | SPMI_FIND_IRQ(chip, sw_cc_thr); |
| 3965 | SPMI_FIND_IRQ(chip, ocv_thr); |
| 3966 | return 0; |
| 3967 | } |
| 3968 | |
| 3969 | #define SPMI_REQUEST_IRQ(chip, rc, irq_name) \ |
| 3970 | do { \ |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3971 | rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \ |
| 3972 | bms_##irq_name##_irq_handler, \ |
| 3973 | IRQF_TRIGGER_RISING, #irq_name, chip); \ |
| 3974 | if (rc < 0) { \ |
| 3975 | pr_err("Unable to request " #irq_name " irq: %d\n", rc);\ |
| 3976 | return -ENXIO; \ |
| 3977 | } \ |
Xiaozhe Shi | f511a6e | 2014-02-20 14:37:18 -0800 | [diff] [blame] | 3978 | chip->irq_name##_irq.ready = true; \ |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3979 | } while (0) |
| 3980 | |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 3981 | static int bms_request_irqs(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3982 | { |
| 3983 | int rc; |
| 3984 | |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 3985 | SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr); |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 3986 | chip->sw_cc_thr_irq.is_wake = true; |
Abhijeet Dharmapurikar | dce21e6 | 2013-08-07 15:42:32 -0700 | [diff] [blame] | 3987 | disable_bms_irq(&chip->sw_cc_thr_irq); |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 3988 | SPMI_REQUEST_IRQ(chip, rc, ocv_thr); |
Fenglin Wu | c17e97b | 2015-01-20 16:49:58 +0800 | [diff] [blame] | 3989 | chip->ocv_thr_irq.is_wake = true; |
Abhijeet Dharmapurikar | dce21e6 | 2013-08-07 15:42:32 -0700 | [diff] [blame] | 3990 | disable_bms_irq(&chip->ocv_thr_irq); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 3991 | return 0; |
| 3992 | } |
| 3993 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 3994 | #define REG_OFFSET_PERP_TYPE 0x04 |
| 3995 | #define REG_OFFSET_PERP_SUBTYPE 0x05 |
| 3996 | #define BMS_BMS_TYPE 0xD |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 3997 | #define BMS_BMS1_SUBTYPE 0x1 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 3998 | #define BMS_IADC_TYPE 0x8 |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 3999 | #define BMS_IADC1_SUBTYPE 0x3 |
| 4000 | #define BMS_IADC2_SUBTYPE 0x5 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4001 | |
| 4002 | static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi) |
| 4003 | { |
| 4004 | struct spmi_resource *spmi_resource; |
| 4005 | struct resource *resource; |
| 4006 | int rc; |
| 4007 | u8 type, subtype; |
| 4008 | |
| 4009 | chip->dev = &(spmi->dev); |
| 4010 | chip->spmi = spmi; |
| 4011 | |
| 4012 | spmi_for_each_container_dev(spmi_resource, spmi) { |
| 4013 | if (!spmi_resource) { |
| 4014 | pr_err("qpnp_bms: spmi resource absent\n"); |
| 4015 | return -ENXIO; |
| 4016 | } |
| 4017 | |
| 4018 | resource = spmi_get_resource(spmi, spmi_resource, |
| 4019 | IORESOURCE_MEM, 0); |
| 4020 | if (!(resource && resource->start)) { |
| 4021 | pr_err("node %s IO resource absent!\n", |
| 4022 | spmi->dev.of_node->full_name); |
| 4023 | return -ENXIO; |
| 4024 | } |
| 4025 | |
Xiaozhe Shi | 7c41a29 | 2013-08-16 16:50:17 -0700 | [diff] [blame] | 4026 | pr_debug("Node name = %s\n", spmi_resource->of_node->name); |
| 4027 | |
| 4028 | if (strcmp("qcom,batt-pres-status", |
| 4029 | spmi_resource->of_node->name) == 0) { |
| 4030 | chip->batt_pres_addr = resource->start; |
| 4031 | continue; |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 4032 | } else if (strcmp("qcom,soc-storage-reg", |
| 4033 | spmi_resource->of_node->name) == 0) { |
| 4034 | chip->soc_storage_addr = resource->start; |
| 4035 | continue; |
Xiaozhe Shi | 7c41a29 | 2013-08-16 16:50:17 -0700 | [diff] [blame] | 4036 | } |
| 4037 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4038 | rc = qpnp_read_wrapper(chip, &type, |
| 4039 | resource->start + REG_OFFSET_PERP_TYPE, 1); |
| 4040 | if (rc) { |
| 4041 | pr_err("Peripheral type read failed rc=%d\n", rc); |
| 4042 | return rc; |
| 4043 | } |
| 4044 | rc = qpnp_read_wrapper(chip, &subtype, |
| 4045 | resource->start + REG_OFFSET_PERP_SUBTYPE, 1); |
| 4046 | if (rc) { |
| 4047 | pr_err("Peripheral subtype read failed rc=%d\n", rc); |
| 4048 | return rc; |
| 4049 | } |
| 4050 | |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 4051 | if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) { |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4052 | chip->base = resource->start; |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 4053 | rc = bms_find_irqs(chip, spmi_resource); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 4054 | if (rc) { |
Abhijeet Dharmapurikar | 3bd4bbf | 2013-07-01 12:06:37 -0700 | [diff] [blame] | 4055 | pr_err("Could not find irqs\n"); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 4056 | return rc; |
| 4057 | } |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4058 | } else if (type == BMS_IADC_TYPE |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 4059 | && (subtype == BMS_IADC1_SUBTYPE |
| 4060 | || subtype == BMS_IADC2_SUBTYPE)) { |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4061 | chip->iadc_base = resource->start; |
| 4062 | } else { |
| 4063 | pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n", |
| 4064 | resource->start, type, subtype); |
| 4065 | } |
| 4066 | } |
| 4067 | |
| 4068 | if (chip->base == 0) { |
| 4069 | dev_err(&spmi->dev, "BMS peripheral was not registered\n"); |
| 4070 | return -EINVAL; |
| 4071 | } |
| 4072 | if (chip->iadc_base == 0) { |
| 4073 | dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n"); |
| 4074 | return -EINVAL; |
| 4075 | } |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 4076 | if (chip->soc_storage_addr == 0) { |
| 4077 | /* default to dvdd backed BMS data reg0 */ |
| 4078 | chip->soc_storage_addr = chip->base + SOC_STORAGE_REG; |
| 4079 | } |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4080 | |
Xiaozhe Shi | 12b25be | 2013-08-29 11:51:59 -0700 | [diff] [blame] | 4081 | pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n", |
| 4082 | chip->base, chip->iadc_base, |
| 4083 | chip->batt_pres_addr, chip->soc_storage_addr); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4084 | return 0; |
| 4085 | } |
| 4086 | |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 4087 | #define ADC_CH_SEL_MASK 0x7 |
| 4088 | #define ADC_INT_RSNSN_CTL_MASK 0x3 |
| 4089 | #define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2 |
| 4090 | #define FAST_AVG_EN_MASK 0x80 |
| 4091 | #define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4092 | static int read_iadc_channel_select(struct qpnp_bms_chip *chip) |
| 4093 | { |
| 4094 | u8 iadc_channel_select; |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 4095 | int32_t rds_rsense_nohm; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4096 | int rc; |
| 4097 | |
| 4098 | rc = qpnp_read_wrapper(chip, &iadc_channel_select, |
| 4099 | chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1); |
| 4100 | if (rc) { |
| 4101 | pr_err("Error reading bms_iadc channel register %d\n", rc); |
| 4102 | return rc; |
| 4103 | } |
| 4104 | |
| 4105 | iadc_channel_select &= ADC_CH_SEL_MASK; |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 4106 | if (iadc_channel_select != EXTERNAL_RSENSE |
| 4107 | && iadc_channel_select != INTERNAL_RSENSE) { |
| 4108 | pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n", |
| 4109 | iadc_channel_select); |
| 4110 | return -EINVAL; |
| 4111 | } |
| 4112 | |
| 4113 | if (chip->use_external_rsense) { |
| 4114 | pr_debug("External rsense selected\n"); |
| 4115 | if (iadc_channel_select == INTERNAL_RSENSE) { |
| 4116 | pr_debug("Internal rsense detected; Changing rsense to external\n"); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 4117 | rc = qpnp_masked_write_iadc(chip, |
| 4118 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 4119 | ADC_CH_SEL_MASK, |
| 4120 | EXTERNAL_RSENSE); |
| 4121 | if (rc) { |
| 4122 | pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n", |
| 4123 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 4124 | EXTERNAL_RSENSE, rc); |
| 4125 | return rc; |
| 4126 | } |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 4127 | reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 4128 | chip->software_cc_uah = 0; |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 4129 | chip->software_shdw_cc_uah = 0; |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 4130 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 4131 | } else { |
| 4132 | pr_debug("Internal rsense selected\n"); |
| 4133 | if (iadc_channel_select == EXTERNAL_RSENSE) { |
| 4134 | pr_debug("External rsense detected; Changing rsense to internal\n"); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 4135 | rc = qpnp_masked_write_iadc(chip, |
| 4136 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 4137 | ADC_CH_SEL_MASK, |
| 4138 | INTERNAL_RSENSE); |
| 4139 | if (rc) { |
| 4140 | pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n", |
| 4141 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 4142 | INTERNAL_RSENSE, rc); |
| 4143 | return rc; |
| 4144 | } |
Xiaozhe Shi | f3da862 | 2013-06-10 14:50:56 -0700 | [diff] [blame] | 4145 | reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC); |
| 4146 | chip->software_shdw_cc_uah = 0; |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 4147 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 4148 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 4149 | rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm); |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 4150 | if (rc) { |
| 4151 | pr_err("Unable to read RDS resistance value from IADC; rc = %d\n", |
| 4152 | rc); |
| 4153 | return rc; |
| 4154 | } |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 4155 | chip->r_sense_uohm = rds_rsense_nohm/1000; |
| 4156 | pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n", |
| 4157 | rds_rsense_nohm, chip->r_sense_uohm); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4158 | } |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 4159 | /* prevent shorting of leads by IADC_BMS when external Rsense is used */ |
| 4160 | if (chip->use_external_rsense) { |
| 4161 | if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) { |
| 4162 | rc = qpnp_masked_write_iadc(chip, |
| 4163 | IADC1_BMS_ADC_INT_RSNSN_CTL, |
| 4164 | ADC_INT_RSNSN_CTL_MASK, |
| 4165 | ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE); |
| 4166 | if (rc) { |
| 4167 | pr_err("Unable to set batfet config %x to %x: %d\n", |
| 4168 | IADC1_BMS_ADC_INT_RSNSN_CTL, |
| 4169 | ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc); |
| 4170 | return rc; |
| 4171 | } |
| 4172 | } else { |
| 4173 | /* In older PMICS use FAST_AVG_EN register bit 7 */ |
| 4174 | rc = qpnp_masked_write_iadc(chip, |
| 4175 | IADC1_BMS_FAST_AVG_EN, |
| 4176 | FAST_AVG_EN_MASK, |
| 4177 | FAST_AVG_EN_VALUE_EXT_RSENSE); |
| 4178 | if (rc) { |
| 4179 | pr_err("Unable to set batfet config %x to %x: %d\n", |
| 4180 | IADC1_BMS_FAST_AVG_EN, |
| 4181 | FAST_AVG_EN_VALUE_EXT_RSENSE, rc); |
| 4182 | return rc; |
| 4183 | } |
| 4184 | } |
| 4185 | } |
| 4186 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
| 4189 | |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4190 | static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip) |
| 4191 | { |
| 4192 | struct qpnp_vadc_result result; |
| 4193 | int rc; |
| 4194 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 4195 | rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result); |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4196 | |
| 4197 | pr_debug("low = %lld, high = %lld\n", |
| 4198 | result.physical - chip->temperature_margin, |
| 4199 | result.physical + chip->temperature_margin); |
| 4200 | chip->die_temp_monitor_params.high_temp = result.physical |
| 4201 | + chip->temperature_margin; |
| 4202 | chip->die_temp_monitor_params.low_temp = result.physical |
| 4203 | - chip->temperature_margin; |
| 4204 | chip->die_temp_monitor_params.state_request = |
| 4205 | ADC_TM_HIGH_LOW_THR_ENABLE; |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 4206 | return qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 4207 | &chip->die_temp_monitor_params); |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx) |
| 4211 | { |
| 4212 | struct qpnp_bms_chip *chip = ctx; |
| 4213 | struct qpnp_vadc_result result; |
| 4214 | int rc; |
| 4215 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 4216 | rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result); |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4217 | |
| 4218 | if (state == ADC_TM_LOW_STATE) |
| 4219 | pr_debug("low state triggered\n"); |
| 4220 | else if (state == ADC_TM_HIGH_STATE) |
| 4221 | pr_debug("high state triggered\n"); |
| 4222 | pr_debug("die temp = %lld, raw = 0x%x\n", |
| 4223 | result.physical, result.adc_code); |
| 4224 | schedule_work(&chip->recalc_work); |
| 4225 | refresh_die_temp_monitor(chip); |
| 4226 | } |
| 4227 | |
| 4228 | static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip) |
| 4229 | { |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 4230 | int rc; |
| 4231 | |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4232 | chip->die_temp_monitor_params.channel = DIE_TEMP; |
| 4233 | chip->die_temp_monitor_params.btm_ctx = (void *)chip; |
| 4234 | chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S; |
| 4235 | chip->die_temp_monitor_params.threshold_notification = |
| 4236 | &btm_notify_die_temp; |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 4237 | rc = refresh_die_temp_monitor(chip); |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4238 | if (rc) { |
| 4239 | pr_err("tm setup failed: %d\n", rc); |
| 4240 | return rc; |
| 4241 | } |
| 4242 | pr_debug("setup complete\n"); |
| 4243 | return 0; |
| 4244 | } |
| 4245 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4246 | static int __devinit qpnp_bms_probe(struct spmi_device *spmi) |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4247 | { |
| 4248 | struct qpnp_bms_chip *chip; |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 4249 | bool warm_reset; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4250 | int rc, vbatt; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4251 | |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 4252 | chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip), |
| 4253 | GFP_KERNEL); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4254 | |
| 4255 | if (chip == NULL) { |
| 4256 | pr_err("kzalloc() failed.\n"); |
| 4257 | return -ENOMEM; |
| 4258 | } |
| 4259 | |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 4260 | rc = bms_get_adc(chip, spmi); |
| 4261 | if (rc < 0) |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4262 | goto error_read; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4263 | |
Xiaozhe Shi | 3eaf0b6 | 2013-07-11 09:48:08 -0700 | [diff] [blame] | 4264 | mutex_init(&chip->bms_output_lock); |
| 4265 | mutex_init(&chip->last_ocv_uv_mutex); |
| 4266 | mutex_init(&chip->vbat_monitor_mutex); |
| 4267 | mutex_init(&chip->soc_invalidation_mutex); |
| 4268 | mutex_init(&chip->last_soc_mutex); |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 4269 | mutex_init(&chip->status_lock); |
Xiaozhe Shi | 2737582 | 2013-08-22 11:40:15 -0700 | [diff] [blame] | 4270 | init_waitqueue_head(&chip->bms_wait_queue); |
Xiaozhe Shi | 3eaf0b6 | 2013-07-11 09:48:08 -0700 | [diff] [blame] | 4271 | |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 4272 | warm_reset = qpnp_pon_is_warm_reset(); |
| 4273 | rc = warm_reset; |
| 4274 | if (rc < 0) |
| 4275 | goto error_read; |
| 4276 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4277 | rc = register_spmi(chip, spmi); |
| 4278 | if (rc) { |
| 4279 | pr_err("error registering spmi resource %d\n", rc); |
| 4280 | goto error_resource; |
| 4281 | } |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4282 | |
| 4283 | rc = qpnp_read_wrapper(chip, &chip->revision1, |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 4284 | chip->base + REVISION1, 1); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4285 | if (rc) { |
| 4286 | pr_err("error reading version register %d\n", rc); |
| 4287 | goto error_read; |
| 4288 | } |
| 4289 | |
| 4290 | rc = qpnp_read_wrapper(chip, &chip->revision2, |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 4291 | chip->base + REVISION2, 1); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4292 | if (rc) { |
| 4293 | pr_err("Error reading version register %d\n", rc); |
| 4294 | goto error_read; |
| 4295 | } |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 4296 | pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4297 | |
Abhijeet Dharmapurikar | 604461d | 2013-07-09 13:34:33 -0700 | [diff] [blame] | 4298 | rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2, |
| 4299 | chip->iadc_base + REVISION2, 1); |
| 4300 | if (rc) { |
| 4301 | pr_err("Error reading version register %d\n", rc); |
| 4302 | goto error_read; |
| 4303 | } |
| 4304 | |
| 4305 | rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1, |
| 4306 | chip->iadc_base + REVISION1, 1); |
| 4307 | if (rc) { |
| 4308 | pr_err("Error reading version register %d\n", rc); |
| 4309 | goto error_read; |
| 4310 | } |
| 4311 | pr_debug("IADC_BMS version: %hhu.%hhu\n", |
| 4312 | chip->iadc_bms_revision2, chip->iadc_bms_revision1); |
| 4313 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4314 | rc = bms_read_properties(chip); |
| 4315 | if (rc) { |
| 4316 | pr_err("Unable to read all bms properties, rc = %d\n", rc); |
| 4317 | goto error_read; |
| 4318 | } |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4319 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 4320 | rc = read_iadc_channel_select(chip); |
| 4321 | if (rc) { |
| 4322 | pr_err("Unable to get iadc selected channel = %d\n", rc); |
| 4323 | goto error_read; |
| 4324 | } |
| 4325 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 4326 | if (chip->use_ocv_thresholds) { |
| 4327 | rc = set_ocv_voltage_thresholds(chip, |
| 4328 | chip->ocv_low_threshold_uv, |
| 4329 | chip->ocv_high_threshold_uv); |
| 4330 | if (rc) { |
| 4331 | pr_err("Could not set ocv voltage thresholds: %d\n", |
| 4332 | rc); |
| 4333 | goto error_read; |
| 4334 | } |
| 4335 | } |
| 4336 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4337 | rc = set_battery_data(chip); |
| 4338 | if (rc) { |
| 4339 | pr_err("Bad battery data %d\n", rc); |
| 4340 | goto error_read; |
| 4341 | } |
| 4342 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4343 | bms_initialize_constants(chip); |
| 4344 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 4345 | wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake"); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 4346 | wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND, |
| 4347 | "qpnp_low_voltage_lock"); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 4348 | wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND, |
| 4349 | "qpnp_cv_lock"); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4350 | INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work, |
| 4351 | calculate_soc_work); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 4352 | INIT_WORK(&chip->recalc_work, recalculate_work); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 4353 | INIT_WORK(&chip->batfet_open_work, batfet_open_work); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4354 | |
Xiaozhe Shi | f9f9924 | 2013-08-29 12:27:50 -0700 | [diff] [blame] | 4355 | dev_set_drvdata(&spmi->dev, chip); |
| 4356 | device_init_wakeup(&spmi->dev, 1); |
| 4357 | |
| 4358 | load_shutdown_data(chip); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4359 | |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 4360 | if (chip->enable_fcc_learning) { |
Anirudh Ghayal | e0c0293 | 2013-07-08 16:26:35 +0530 | [diff] [blame] | 4361 | if (chip->battery_removed) { |
| 4362 | rc = discard_backup_fcc_data(chip); |
| 4363 | if (rc) |
| 4364 | pr_err("Could not discard backed-up FCC data\n"); |
| 4365 | } else { |
| 4366 | rc = read_chgcycle_data_from_backup(chip); |
| 4367 | if (rc) |
| 4368 | pr_err("Unable to restore charge-cycle data\n"); |
| 4369 | |
| 4370 | rc = read_fcc_data_from_backup(chip); |
| 4371 | if (rc) |
| 4372 | pr_err("Unable to restore FCC-learning data\n"); |
| 4373 | else |
| 4374 | attempt_learning_new_fcc(chip); |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 4375 | } |
| 4376 | } |
Anirudh Ghayal | d71d9f8 | 2013-06-05 11:11:46 +0530 | [diff] [blame] | 4377 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 4378 | rc = setup_vbat_monitoring(chip); |
| 4379 | if (rc < 0) { |
| 4380 | pr_err("failed to set up voltage notifications: %d\n", rc); |
| 4381 | goto error_setup; |
Xiaozhe Shi | d5d2141 | 2013-02-06 17:14:41 -0800 | [diff] [blame] | 4382 | } |
| 4383 | |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 4384 | rc = setup_die_temp_monitoring(chip); |
| 4385 | if (rc < 0) { |
| 4386 | pr_err("failed to set up die temp notifications: %d\n", rc); |
| 4387 | goto error_setup; |
| 4388 | } |
| 4389 | |
Xu Kai | 870f8e8 | 2014-01-16 19:21:01 +0800 | [diff] [blame] | 4390 | rc = bms_request_irqs(chip); |
| 4391 | if (rc) { |
| 4392 | pr_err("error requesting bms irqs, rc = %d\n", rc); |
| 4393 | goto error_setup; |
| 4394 | } |
| 4395 | |
Xiaozhe Shi | e7fafe6 | 2013-06-05 15:25:16 -0700 | [diff] [blame] | 4396 | battery_insertion_check(chip); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 4397 | batfet_status_check(chip); |
Xiaozhe Shi | e7fafe6 | 2013-06-05 15:25:16 -0700 | [diff] [blame] | 4398 | battery_status_check(chip); |
| 4399 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4400 | calculate_soc_work(&(chip->calculate_soc_delayed_work.work)); |
| 4401 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4402 | /* setup & register the battery power supply */ |
| 4403 | chip->bms_psy.name = "bms"; |
| 4404 | chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS; |
| 4405 | chip->bms_psy.properties = msm_bms_power_props; |
| 4406 | chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props); |
| 4407 | chip->bms_psy.get_property = qpnp_bms_power_get_property; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4408 | chip->bms_psy.external_power_changed = |
| 4409 | qpnp_bms_external_power_changed; |
| 4410 | chip->bms_psy.supplied_to = qpnp_bms_supplicants; |
| 4411 | chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants); |
| 4412 | |
| 4413 | rc = power_supply_register(chip->dev, &chip->bms_psy); |
| 4414 | |
| 4415 | if (rc < 0) { |
| 4416 | pr_err("power_supply_register bms failed rc = %d\n", rc); |
| 4417 | goto unregister_dc; |
| 4418 | } |
| 4419 | |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 4420 | chip->bms_psy_registered = true; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4421 | vbatt = 0; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 4422 | rc = get_battery_voltage(chip, &vbatt); |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 4423 | if (rc) { |
| 4424 | pr_err("error reading vbat_sns adc channel = %d, rc = %d\n", |
| 4425 | VBAT_SNS, rc); |
| 4426 | goto unregister_dc; |
| 4427 | } |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 4428 | |
wangxl | f0e6b1f | 2014-12-31 09:51:40 +0800 | [diff] [blame] | 4429 | #ifdef CONFIG_TCMD |
| 4430 | bms_chip = chip; |
| 4431 | #endif |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 4432 | pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n", |
| 4433 | get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv, |
| 4434 | chip->r_sense_uohm, warm_reset); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4435 | return 0; |
| 4436 | |
| 4437 | unregister_dc: |
Abhijeet Dharmapurikar | 8e32249 | 2013-06-25 19:48:18 -0700 | [diff] [blame] | 4438 | chip->bms_psy_registered = false; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 4439 | power_supply_unregister(&chip->bms_psy); |
| 4440 | error_setup: |
| 4441 | dev_set_drvdata(&spmi->dev, NULL); |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 4442 | wakeup_source_trash(&chip->soc_wake_source.source); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 4443 | wake_lock_destroy(&chip->low_voltage_wake_lock); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 4444 | wake_lock_destroy(&chip->cv_wake_lock); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 4445 | error_resource: |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4446 | error_read: |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4447 | return rc; |
| 4448 | } |
| 4449 | |
Xiaozhe Shi | 81f66b5 | 2013-09-20 11:43:52 -0700 | [diff] [blame] | 4450 | static int qpnp_bms_remove(struct spmi_device *spmi) |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4451 | { |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4452 | dev_set_drvdata(&spmi->dev, NULL); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4453 | return 0; |
| 4454 | } |
| 4455 | |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 4456 | static int bms_suspend(struct device *dev) |
| 4457 | { |
| 4458 | struct qpnp_bms_chip *chip = dev_get_drvdata(dev); |
| 4459 | |
| 4460 | cancel_delayed_work_sync(&chip->calculate_soc_delayed_work); |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 4461 | chip->was_charging_at_sleep = is_battery_charging(chip); |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 4462 | return 0; |
| 4463 | } |
| 4464 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 4465 | static int bms_resume(struct device *dev) |
| 4466 | { |
| 4467 | int rc; |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 4468 | int soc_calc_period; |
Xiaozhe Shi | d921f9f7 | 2013-05-23 18:55:15 -0700 | [diff] [blame] | 4469 | int time_until_next_recalc = 0; |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 4470 | unsigned long time_since_last_recalc; |
| 4471 | unsigned long tm_now_sec; |
| 4472 | struct qpnp_bms_chip *chip = dev_get_drvdata(dev); |
| 4473 | |
| 4474 | rc = get_current_time(&tm_now_sec); |
| 4475 | if (rc) { |
| 4476 | pr_err("Could not read current time: %d\n", rc); |
Xiaozhe Shi | d921f9f7 | 2013-05-23 18:55:15 -0700 | [diff] [blame] | 4477 | } else { |
Xiaozhe Shi | cb487b1 | 2013-10-14 17:42:07 -0700 | [diff] [blame] | 4478 | soc_calc_period = get_calculation_delay_ms(chip); |
Xiaozhe Shi | d921f9f7 | 2013-05-23 18:55:15 -0700 | [diff] [blame] | 4479 | time_since_last_recalc = tm_now_sec - chip->last_recalc_time; |
| 4480 | pr_debug("Time since last recalc: %lu\n", |
| 4481 | time_since_last_recalc); |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 4482 | time_until_next_recalc = max(0, soc_calc_period |
| 4483 | - (int)(time_since_last_recalc * 1000)); |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 4484 | } |
Xiaozhe Shi | d921f9f7 | 2013-05-23 18:55:15 -0700 | [diff] [blame] | 4485 | |
Xiaozhe Shi | 1ae8a95 | 2013-02-14 12:23:31 -0800 | [diff] [blame] | 4486 | if (time_until_next_recalc == 0) |
| 4487 | bms_stay_awake(&chip->soc_wake_source); |
Xiaozhe Shi | d921f9f7 | 2013-05-23 18:55:15 -0700 | [diff] [blame] | 4488 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 4489 | round_jiffies_relative(msecs_to_jiffies |
| 4490 | (time_until_next_recalc))); |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 4491 | return 0; |
| 4492 | } |
| 4493 | |
| 4494 | static const struct dev_pm_ops qpnp_bms_pm_ops = { |
| 4495 | .resume = bms_resume, |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 4496 | .suspend = bms_suspend, |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 4497 | }; |
| 4498 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4499 | static struct spmi_driver qpnp_bms_driver = { |
| 4500 | .probe = qpnp_bms_probe, |
| 4501 | .remove = __devexit_p(qpnp_bms_remove), |
| 4502 | .driver = { |
| 4503 | .name = QPNP_BMS_DEV_NAME, |
| 4504 | .owner = THIS_MODULE, |
| 4505 | .of_match_table = qpnp_bms_match_table, |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 4506 | .pm = &qpnp_bms_pm_ops, |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 4507 | }, |
| 4508 | }; |
| 4509 | |
| 4510 | static int __init qpnp_bms_init(void) |
| 4511 | { |
| 4512 | pr_info("QPNP BMS INIT\n"); |
| 4513 | return spmi_driver_register(&qpnp_bms_driver); |
| 4514 | } |
| 4515 | |
| 4516 | static void __exit qpnp_bms_exit(void) |
| 4517 | { |
| 4518 | pr_info("QPNP BMS EXIT\n"); |
| 4519 | return spmi_driver_unregister(&qpnp_bms_driver); |
| 4520 | } |
| 4521 | |
| 4522 | module_init(qpnp_bms_init); |
| 4523 | module_exit(qpnp_bms_exit); |
| 4524 | |
| 4525 | MODULE_DESCRIPTION("QPNP BMS Driver"); |
| 4526 | MODULE_LICENSE("GPL v2"); |
| 4527 | MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME); |