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