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