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