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