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