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