Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -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 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 13 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/errno.h> |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 19 | #include <linux/power_supply.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | #include <linux/mfd/pm8xxx/pm8921-bms.h> |
| 21 | #include <linux/mfd/pm8xxx/core.h> |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 22 | #include <linux/mfd/pm8xxx/pm8xxx-adc.h> |
Abhijeet Dharmapurikar | b268013 | 2012-05-15 16:32:51 -0700 | [diff] [blame] | 23 | #include <linux/mfd/pm8xxx/pm8921-charger.h> |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 24 | #include <linux/mfd/pm8xxx/ccadc.h> |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 25 | #include <linux/mfd/pm8xxx/batterydata-lib.h> |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 26 | #include <linux/mfd/pm8xxx/batt-alarm.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/bitops.h> |
| 29 | #include <linux/debugfs.h> |
| 30 | #include <linux/slab.h> |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 33 | #include <linux/rtc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | |
| 35 | #define BMS_CONTROL 0x224 |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 36 | #define BMS_S1_DELAY 0x225 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | #define BMS_OUTPUT0 0x230 |
| 38 | #define BMS_OUTPUT1 0x231 |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 39 | #define BMS_TOLERANCES 0x232 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | #define BMS_TEST1 0x237 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 41 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 42 | #define ADC_ARB_SECP_CNTRL 0x190 |
| 43 | #define ADC_ARB_SECP_AMUX_CNTRL 0x191 |
| 44 | #define ADC_ARB_SECP_ANA_PARAM 0x192 |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 45 | #define ADC_ARB_SECP_DIG_PARAM 0x193 |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 46 | #define ADC_ARB_SECP_RSV 0x194 |
| 47 | #define ADC_ARB_SECP_DATA1 0x195 |
| 48 | #define ADC_ARB_SECP_DATA0 0x196 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 49 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 50 | #define ADC_ARB_BMS_CNTRL 0x18D |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 51 | #define AMUX_TRIM_2 0x322 |
| 52 | #define TEST_PROGRAM_REV 0x339 |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 53 | |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 54 | #define TEMP_SOC_STORAGE 0x107 |
| 55 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 56 | #define TEMP_IAVG_STORAGE 0x105 |
| 57 | #define TEMP_IAVG_STORAGE_USE_MASK 0x0F |
| 58 | |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 59 | #define PON_CNTRL_6 0x018 |
| 60 | #define WD_BIT BIT(7) |
| 61 | |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 62 | #define BATT_ALARM_ACCURACY 50 /* 50mV */ |
| 63 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 64 | enum pmic_bms_interrupts { |
| 65 | PM8921_BMS_SBI_WRITE_OK, |
| 66 | PM8921_BMS_CC_THR, |
| 67 | PM8921_BMS_VSENSE_THR, |
| 68 | PM8921_BMS_VSENSE_FOR_R, |
| 69 | PM8921_BMS_OCV_FOR_R, |
| 70 | PM8921_BMS_GOOD_OCV, |
| 71 | PM8921_BMS_VSENSE_AVG, |
| 72 | PM_BMS_MAX_INTS, |
| 73 | }; |
| 74 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 75 | struct pm8921_soc_params { |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 76 | uint16_t last_good_ocv_raw; |
| 77 | int cc; |
| 78 | |
| 79 | int last_good_ocv_uv; |
| 80 | }; |
| 81 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 82 | /** |
| 83 | * struct pm8921_bms_chip - |
| 84 | * @bms_output_lock: lock to prevent concurrent bms reads |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 85 | * |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 86 | * @last_ocv_uv_mutex: mutex to protect simultaneous invocations of calculate |
| 87 | * state of charge, note that last_ocv_uv could be |
| 88 | * changed as soc is adjusted. This mutex protects |
| 89 | * simultaneous updates of last_ocv_uv as well. This mutex |
| 90 | * also protects changes to *_at_100 variables used in |
| 91 | * faking 100% SOC. |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 92 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 93 | struct pm8921_bms_chip { |
| 94 | struct device *dev; |
| 95 | struct dentry *dent; |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 96 | int r_sense_uohm; |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 97 | unsigned int v_cutoff; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | unsigned int fcc; |
| 99 | struct single_row_lut *fcc_temp_lut; |
| 100 | struct single_row_lut *fcc_sf_lut; |
| 101 | struct pc_temp_ocv_lut *pc_temp_ocv_lut; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 102 | struct sf_lut *pc_sf_lut; |
| 103 | struct sf_lut *rbatt_sf_lut; |
Abhijeet Dharmapurikar | ded189b | 2012-03-30 10:12:28 -0700 | [diff] [blame] | 104 | int delta_rbatt_mohm; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 105 | struct work_struct calib_hkadc_work; |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 106 | unsigned long last_calib_time; |
| 107 | int last_calib_temp; |
Abhijeet Dharmapurikar | 9c0e604 | 2012-07-12 14:55:13 -0700 | [diff] [blame] | 108 | struct mutex calib_mutex; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 109 | unsigned int revision; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 110 | unsigned int xoadc_v0625_usb_present; |
| 111 | unsigned int xoadc_v0625_usb_absent; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 112 | unsigned int xoadc_v0625; |
| 113 | unsigned int xoadc_v125; |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 114 | unsigned int batt_temp_channel; |
| 115 | unsigned int vbat_channel; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 116 | unsigned int ref625mv_channel; |
| 117 | unsigned int ref1p25v_channel; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 118 | unsigned int batt_id_channel; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 119 | unsigned int pmic_bms_irq[PM_BMS_MAX_INTS]; |
| 120 | DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 121 | struct mutex bms_output_lock; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 122 | struct single_row_lut *adjusted_fcc_temp_lut; |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 123 | unsigned int charging_began; |
| 124 | unsigned int start_percent; |
| 125 | unsigned int end_percent; |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 126 | unsigned int alarm_low_mv; |
| 127 | unsigned int alarm_high_mv; |
| 128 | |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 129 | int charge_time_us; |
| 130 | int catch_up_time_us; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 131 | enum battery_type batt_type; |
Abhijeet Dharmapurikar | 57b06b5 | 2011-11-07 19:14:08 -0800 | [diff] [blame] | 132 | uint16_t ocv_reading_at_100; |
Abhijeet Dharmapurikar | a93ede8 | 2011-11-17 12:20:03 -0800 | [diff] [blame] | 133 | int max_voltage_uv; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 134 | |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 135 | int chg_term_ua; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 136 | int default_rbatt_mohm; |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 137 | int amux_2_trim_delta; |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 138 | uint16_t prev_last_good_ocv_raw; |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 139 | int rconn_mohm; |
Abhijeet Dharmapurikar | 03c6c23 | 2013-02-13 17:57:40 -0800 | [diff] [blame] | 140 | int rbatt_capacitive_mohm; |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 141 | struct mutex last_ocv_uv_mutex; |
| 142 | int last_ocv_uv; |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 143 | int last_ocv_temp_decidegc; |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 144 | int pon_ocv_uv; |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 145 | int last_cc_uah; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 146 | unsigned long tm_sec; |
Abhijeet Dharmapurikar | e88f246 | 2012-04-25 18:22:38 -0700 | [diff] [blame] | 147 | int enable_fcc_learning; |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 148 | int shutdown_soc; |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 149 | int shutdown_iavg_ua; |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 150 | struct delayed_work calculate_soc_delayed_work; |
| 151 | struct timespec t_soc_queried; |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 152 | unsigned long last_recalc_time; |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 153 | int shutdown_soc_valid_limit; |
Abhijeet Dharmapurikar | 1f4cc51 | 2012-07-21 00:10:39 -0700 | [diff] [blame] | 154 | int ignore_shutdown_soc; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 155 | int prev_iavg_ua; |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 156 | int prev_uuc_iavg_ma; |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 157 | int prev_pc_unusable; |
Abhijeet Dharmapurikar | 720b00e | 2012-08-01 21:33:09 -0700 | [diff] [blame] | 158 | int adjust_soc_low_threshold; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 159 | |
| 160 | int ibat_at_cv_ua; |
| 161 | int soc_at_cv; |
| 162 | int prev_chg_soc; |
Abhijeet Dharmapurikar | 0622a43 | 2012-08-28 16:34:18 -0700 | [diff] [blame] | 163 | struct power_supply *batt_psy; |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 164 | struct wake_lock low_voltage_wake_lock; |
Abhijeet Dharmapurikar | 683472d | 2012-10-30 16:21:58 -0700 | [diff] [blame] | 165 | int soc_calc_period; |
| 166 | int normal_voltage_calc_ms; |
| 167 | int low_voltage_calc_ms; |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 168 | int imax_ua; |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 169 | struct wake_lock soc_wake_lock; |
Abhijeet Dharmapurikar | 1939ede | 2013-01-10 15:59:33 -0800 | [diff] [blame] | 170 | int disable_flat_portion_ocv; |
| 171 | int ocv_dis_high_soc; |
| 172 | int ocv_dis_low_soc; |
Abhijeet Dharmapurikar | 4870761 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 173 | int high_ocv_correction_limit_uv; |
| 174 | int low_ocv_correction_limit_uv; |
| 175 | int hold_soc_est; |
Abhijeet Dharmapurikar | cbde3db | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 176 | int prev_vbat_batt_terminal_uv; |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 177 | int vbatt_cutoff_count; |
| 178 | int low_voltage_detect; |
| 179 | int vbatt_cutoff_retries; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 182 | /* |
| 183 | * protects against simultaneous adjustment of ocv based on shutdown soc and |
| 184 | * invalidating the shutdown soc |
| 185 | */ |
| 186 | static DEFINE_MUTEX(soc_invalidation_mutex); |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 187 | static int shutdown_soc_invalid; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | static struct pm8921_bms_chip *the_chip; |
| 189 | |
| 190 | #define DEFAULT_RBATT_MOHMS 128 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | #define DEFAULT_OCV_MICROVOLTS 3900000 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | #define DEFAULT_CHARGE_CYCLES 0 |
| 193 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 194 | static int last_usb_cal_delta_uv = 1800; |
| 195 | module_param(last_usb_cal_delta_uv, int, 0644); |
| 196 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 197 | static int last_chargecycles = DEFAULT_CHARGE_CYCLES; |
| 198 | static int last_charge_increase; |
| 199 | module_param(last_chargecycles, int, 0644); |
| 200 | module_param(last_charge_increase, int, 0644); |
| 201 | |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 202 | static int calculated_soc = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | static int last_soc = -EINVAL; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 204 | static int last_real_fcc_mah = -EINVAL; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 205 | static int last_real_fcc_batt_temp = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 207 | static int pm8921_battery_gauge_alarm_notify(struct notifier_block *nb, |
| 208 | unsigned long status, void *unused); |
| 209 | |
| 210 | static struct notifier_block alarm_notifier = { |
| 211 | .notifier_call = pm8921_battery_gauge_alarm_notify, |
| 212 | }; |
| 213 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 214 | static int bms_ops_set(const char *val, const struct kernel_param *kp) |
| 215 | { |
| 216 | if (*(int *)kp->arg == -EINVAL) |
| 217 | return param_set_int(val, kp); |
| 218 | else |
| 219 | return 0; |
| 220 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 222 | static struct kernel_param_ops bms_param_ops = { |
| 223 | .set = bms_ops_set, |
| 224 | .get = param_get_int, |
| 225 | }; |
| 226 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 227 | module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644); |
| 228 | |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 229 | /* |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 230 | * bms_fake_battery is set in setups where a battery emulator is used instead |
| 231 | * of a real battery. This makes the bms driver report a different/fake value |
| 232 | * regardless of the calculated state of charge. |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 233 | */ |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 234 | static int bms_fake_battery = -EINVAL; |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 235 | module_param(bms_fake_battery, int, 0644); |
| 236 | |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 237 | /* bms_start_XXX and bms_end_XXX are read only */ |
| 238 | static int bms_start_percent; |
| 239 | static int bms_start_ocv_uv; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 240 | static int bms_start_cc_uah; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 241 | static int bms_end_percent; |
| 242 | static int bms_end_ocv_uv; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 243 | static int bms_end_cc_uah; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 244 | |
| 245 | static int bms_ro_ops_set(const char *val, const struct kernel_param *kp) |
| 246 | { |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | static struct kernel_param_ops bms_ro_param_ops = { |
| 251 | .set = bms_ro_ops_set, |
| 252 | .get = param_get_int, |
| 253 | }; |
| 254 | module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644); |
| 255 | module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 256 | module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644); |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 257 | |
| 258 | module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644); |
| 259 | module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 260 | module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644); |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 261 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 262 | static void readjust_fcc_table(void) |
| 263 | { |
| 264 | struct single_row_lut *temp, *old; |
| 265 | int i, fcc, ratio; |
| 266 | |
| 267 | if (!the_chip->fcc_temp_lut) { |
| 268 | pr_err("The static fcc lut table is NULL\n"); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL); |
| 273 | if (!temp) { |
| 274 | pr_err("Cannot allocate memory for adjusted fcc table\n"); |
| 275 | return; |
| 276 | } |
| 277 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 278 | fcc = interpolate_fcc(the_chip->fcc_temp_lut, last_real_fcc_batt_temp); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 279 | |
| 280 | temp->cols = the_chip->fcc_temp_lut->cols; |
| 281 | for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) { |
| 282 | temp->x[i] = the_chip->fcc_temp_lut->x[i]; |
| 283 | ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 284 | temp->y[i] = (ratio * last_real_fcc_mah); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 285 | temp->y[i] /= 1000; |
| 286 | pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n", |
| 287 | temp->x[i], the_chip->fcc_temp_lut->y[i], |
| 288 | temp->y[i], ratio); |
| 289 | } |
| 290 | |
| 291 | old = the_chip->adjusted_fcc_temp_lut; |
| 292 | the_chip->adjusted_fcc_temp_lut = temp; |
| 293 | kfree(old); |
| 294 | } |
| 295 | |
| 296 | static int bms_last_real_fcc_set(const char *val, |
| 297 | const struct kernel_param *kp) |
| 298 | { |
| 299 | int rc = 0; |
| 300 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 301 | if (last_real_fcc_mah == -EINVAL) |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 302 | rc = param_set_int(val, kp); |
| 303 | if (rc) { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 304 | pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 305 | return rc; |
| 306 | } |
| 307 | if (last_real_fcc_batt_temp != -EINVAL) |
| 308 | readjust_fcc_table(); |
| 309 | return rc; |
| 310 | } |
| 311 | static struct kernel_param_ops bms_last_real_fcc_param_ops = { |
| 312 | .set = bms_last_real_fcc_set, |
| 313 | .get = param_get_int, |
| 314 | }; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 315 | module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops, |
| 316 | &last_real_fcc_mah, 0644); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 317 | |
| 318 | static int bms_last_real_fcc_batt_temp_set(const char *val, |
| 319 | const struct kernel_param *kp) |
| 320 | { |
| 321 | int rc = 0; |
| 322 | |
| 323 | if (last_real_fcc_batt_temp == -EINVAL) |
| 324 | rc = param_set_int(val, kp); |
| 325 | if (rc) { |
| 326 | pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc); |
| 327 | return rc; |
| 328 | } |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 329 | if (last_real_fcc_mah != -EINVAL) |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 330 | readjust_fcc_table(); |
| 331 | return rc; |
| 332 | } |
| 333 | |
| 334 | static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = { |
| 335 | .set = bms_last_real_fcc_batt_temp_set, |
| 336 | .get = param_get_int, |
| 337 | }; |
| 338 | module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops, |
| 339 | &last_real_fcc_batt_temp, 0644); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 340 | |
| 341 | static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id) |
| 342 | { |
| 343 | return pm8xxx_read_irq_stat(chip->dev->parent, |
| 344 | chip->pmic_bms_irq[irq_id]); |
| 345 | } |
| 346 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 347 | static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 348 | { |
| 349 | if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) { |
| 350 | dev_dbg(chip->dev, "%s %d\n", __func__, |
| 351 | chip->pmic_bms_irq[interrupt]); |
| 352 | enable_irq(chip->pmic_bms_irq[interrupt]); |
| 353 | } |
| 354 | } |
| 355 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 356 | static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 357 | { |
| 358 | if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 359 | pr_debug("%d\n", chip->pmic_bms_irq[interrupt]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 360 | disable_irq_nosync(chip->pmic_bms_irq[interrupt]); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr, |
| 365 | u8 mask, u8 val) |
| 366 | { |
| 367 | int rc; |
| 368 | u8 reg; |
| 369 | |
| 370 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 371 | if (rc) { |
| 372 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
| 373 | return rc; |
| 374 | } |
| 375 | reg &= ~mask; |
| 376 | reg |= val & mask; |
| 377 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 378 | if (rc) { |
| 379 | pr_err("write failed addr = %03X, rc = %d\n", addr, rc); |
| 380 | return rc; |
| 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
Abhijeet Dharmapurikar | 9e94898 | 2012-08-28 15:21:23 -0700 | [diff] [blame] | 385 | static int usb_chg_plugged_in(struct pm8921_bms_chip *chip) |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 386 | { |
Abhijeet Dharmapurikar | 3da6176 | 2012-07-12 15:56:58 -0700 | [diff] [blame] | 387 | int val = pm8921_is_usb_chg_plugged_in(); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 388 | |
Abhijeet Dharmapurikar | 9e94898 | 2012-08-28 15:21:23 -0700 | [diff] [blame] | 389 | /* if the charger driver was not initialized, use the restart reason */ |
| 390 | if (val == -EINVAL) { |
| 391 | if (pm8xxx_restart_reason(chip->dev->parent) |
| 392 | == PM8XXX_RESTART_CHG) |
| 393 | val = 1; |
| 394 | else |
| 395 | val = 0; |
| 396 | } |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 397 | |
Abhijeet Dharmapurikar | 3da6176 | 2012-07-12 15:56:58 -0700 | [diff] [blame] | 398 | return val; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 401 | static void pm8921_bms_low_voltage_config(struct pm8921_bms_chip *chip, |
| 402 | int time_ms) |
| 403 | { |
| 404 | int ms = 0; |
| 405 | |
| 406 | /* if work was pending and was cancelled, calculate SOC immediately */ |
| 407 | if (!cancel_delayed_work_sync(&chip->calculate_soc_delayed_work)) |
| 408 | ms = time_ms; |
| 409 | |
| 410 | chip->soc_calc_period = time_ms; |
| 411 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 412 | msecs_to_jiffies(ms)); |
| 413 | } |
| 414 | |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 415 | static int pm8921_bms_enable_batt_alarm(struct pm8921_bms_chip *chip) |
| 416 | { |
| 417 | int rc = 0; |
| 418 | |
| 419 | rc = pm8xxx_batt_alarm_enable(PM8XXX_BATT_ALARM_LOWER_COMPARATOR); |
| 420 | if (!rc) |
| 421 | rc = pm8xxx_batt_alarm_disable( |
| 422 | PM8XXX_BATT_ALARM_UPPER_COMPARATOR); |
| 423 | if (rc) { |
| 424 | pr_err("unable to set batt alarm state rc=%d\n", rc); |
| 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | return rc; |
| 429 | } |
| 430 | |
| 431 | static int pm8921_bms_configure_batt_alarm(struct pm8921_bms_chip *chip) |
| 432 | { |
| 433 | int rc = 0; |
| 434 | |
| 435 | rc = pm8xxx_batt_alarm_disable(PM8XXX_BATT_ALARM_UPPER_COMPARATOR); |
| 436 | if (!rc) |
| 437 | rc = pm8xxx_batt_alarm_disable( |
| 438 | PM8XXX_BATT_ALARM_LOWER_COMPARATOR); |
| 439 | if (rc) { |
| 440 | pr_err("unable to set batt alarm state rc=%d\n", rc); |
| 441 | return rc; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * The batt-alarm driver requires sane values for both min / max, |
| 446 | * regardless of whether they're both activated. |
| 447 | */ |
| 448 | rc = pm8xxx_batt_alarm_threshold_set( |
| 449 | PM8XXX_BATT_ALARM_LOWER_COMPARATOR, |
| 450 | chip->alarm_low_mv); |
| 451 | if (!rc) |
| 452 | rc = pm8xxx_batt_alarm_threshold_set( |
| 453 | PM8XXX_BATT_ALARM_UPPER_COMPARATOR, |
| 454 | chip->alarm_high_mv); |
| 455 | if (rc) { |
| 456 | pr_err("unable to set batt alarm threshold rc=%d\n", rc); |
| 457 | return rc; |
| 458 | } |
| 459 | |
| 460 | rc = pm8xxx_batt_alarm_hold_time_set( |
| 461 | PM8XXX_BATT_ALARM_HOLD_TIME_16_MS); |
| 462 | if (rc) { |
| 463 | pr_err("unable to set batt alarm hold time rc=%d\n", rc); |
| 464 | return rc; |
| 465 | } |
| 466 | |
| 467 | /* PWM enabled at 2Hz */ |
| 468 | rc = pm8xxx_batt_alarm_pwm_rate_set(1, 7, 4); |
| 469 | if (rc) { |
| 470 | pr_err("unable to set batt alarm pwm rate rc=%d\n", rc); |
| 471 | return rc; |
| 472 | } |
| 473 | |
| 474 | rc = pm8xxx_batt_alarm_register_notifier(&alarm_notifier); |
| 475 | if (rc) { |
| 476 | pr_err("unable to register alarm notifier rc=%d\n", rc); |
| 477 | return rc; |
| 478 | } |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
| 483 | static int pm8921_battery_gauge_alarm_notify(struct notifier_block *nb, |
| 484 | unsigned long status, void *unused) |
| 485 | { |
| 486 | int rc; |
| 487 | |
| 488 | if (!the_chip) { |
| 489 | pr_err("not initialized\n"); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | switch (status) { |
| 494 | case 0: |
| 495 | pr_debug("spurious interrupt\n"); |
| 496 | break; |
| 497 | case 1: |
| 498 | pr_debug("Low voltage alarm triggered\n"); |
| 499 | /* |
| 500 | * hold the low voltage wakelock until the soc |
| 501 | * work finds it appropriate to release it. |
| 502 | */ |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 503 | if (!wake_lock_active(&the_chip->low_voltage_wake_lock)) { |
| 504 | pr_debug("Holding low voltage wakelock\n"); |
| 505 | wake_lock(&the_chip->low_voltage_wake_lock); |
| 506 | pm8921_bms_low_voltage_config(the_chip, |
| 507 | the_chip->low_voltage_calc_ms); |
| 508 | } |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 509 | |
| 510 | rc = pm8xxx_batt_alarm_disable( |
| 511 | PM8XXX_BATT_ALARM_LOWER_COMPARATOR); |
| 512 | if (!rc) |
| 513 | rc = pm8xxx_batt_alarm_enable( |
| 514 | PM8XXX_BATT_ALARM_UPPER_COMPARATOR); |
| 515 | if (rc) |
| 516 | pr_err("unable to set alarm state rc=%d\n", rc); |
| 517 | break; |
| 518 | case 2: |
| 519 | rc = pm8xxx_batt_alarm_disable( |
| 520 | PM8XXX_BATT_ALARM_UPPER_COMPARATOR); |
| 521 | if (!rc) |
| 522 | rc = pm8xxx_batt_alarm_enable( |
| 523 | PM8XXX_BATT_ALARM_LOWER_COMPARATOR); |
| 524 | if (rc) |
| 525 | pr_err("unable to set alarm state rc=%d\n", rc); |
| 526 | |
| 527 | break; |
| 528 | default: |
| 529 | pr_err("error received\n"); |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | }; |
| 535 | |
| 536 | |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 537 | #define HOLD_OREG_DATA BIT(1) |
| 538 | static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip) |
| 539 | { |
| 540 | int rc; |
| 541 | |
| 542 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, |
| 543 | HOLD_OREG_DATA); |
| 544 | if (rc) { |
| 545 | pr_err("couldnt lock bms output rc = %d\n", rc); |
| 546 | return rc; |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip) |
| 552 | { |
| 553 | int rc; |
| 554 | |
| 555 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0); |
| 556 | if (rc) { |
| 557 | pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc); |
| 558 | return rc; |
| 559 | } |
| 560 | return 0; |
| 561 | } |
| 562 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 563 | #define SELECT_OUTPUT_DATA 0x1C |
| 564 | #define SELECT_OUTPUT_TYPE_SHIFT 2 |
| 565 | #define OCV_FOR_RBATT 0x0 |
| 566 | #define VSENSE_FOR_RBATT 0x1 |
| 567 | #define VBATT_FOR_RBATT 0x2 |
| 568 | #define CC_MSB 0x3 |
| 569 | #define CC_LSB 0x4 |
| 570 | #define LAST_GOOD_OCV_VALUE 0x5 |
| 571 | #define VSENSE_AVG 0x6 |
| 572 | #define VBATT_AVG 0x7 |
| 573 | |
| 574 | static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type, |
| 575 | int16_t *result) |
| 576 | { |
| 577 | int rc; |
| 578 | u8 reg; |
| 579 | |
| 580 | if (!result) { |
| 581 | pr_err("result pointer null\n"); |
| 582 | return -EINVAL; |
| 583 | } |
| 584 | *result = 0; |
| 585 | if (type < OCV_FOR_RBATT || type > VBATT_AVG) { |
| 586 | pr_err("invalid type %d asked to read\n", type); |
| 587 | return -EINVAL; |
| 588 | } |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 589 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 590 | rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA, |
| 591 | type << SELECT_OUTPUT_TYPE_SHIFT); |
| 592 | if (rc) { |
| 593 | pr_err("fail to select %d type in BMS_CONTROL rc = %d\n", |
| 594 | type, rc); |
| 595 | return rc; |
| 596 | } |
| 597 | |
| 598 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, ®); |
| 599 | if (rc) { |
| 600 | pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n", |
| 601 | type, rc); |
| 602 | return rc; |
| 603 | } |
| 604 | *result = reg; |
| 605 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, ®); |
| 606 | if (rc) { |
| 607 | pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n", |
| 608 | type, rc); |
| 609 | return rc; |
| 610 | } |
| 611 | *result |= reg << 8; |
| 612 | pr_debug("type %d result %x", type, *result); |
| 613 | return 0; |
| 614 | } |
| 615 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 616 | #define V_PER_BIT_MUL_FACTOR 97656 |
| 617 | #define V_PER_BIT_DIV_FACTOR 1000 |
| 618 | #define XOADC_INTRINSIC_OFFSET 0x6000 |
| 619 | static int xoadc_reading_to_microvolt(unsigned int a) |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 620 | { |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 621 | if (a <= XOADC_INTRINSIC_OFFSET) |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 624 | return (a - XOADC_INTRINSIC_OFFSET) |
| 625 | * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR; |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 626 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 627 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 628 | #define XOADC_CALIB_UV 625000 |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 629 | #define VBATT_MUL_FACTOR 3 |
| 630 | static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 631 | int usb_chg, unsigned int uv) |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 632 | { |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 633 | s64 numerator, denominator; |
| 634 | int local_delta; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 635 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 636 | if (uv == 0) |
| 637 | return 0; |
| 638 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 639 | /* dont adjust if not calibrated */ |
| 640 | if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) { |
| 641 | pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv); |
| 642 | return VBATT_MUL_FACTOR * uv; |
| 643 | } |
| 644 | |
| 645 | if (usb_chg) |
| 646 | local_delta = last_usb_cal_delta_uv; |
| 647 | else |
| 648 | local_delta = 0; |
| 649 | |
| 650 | pr_debug("using delta = %d\n", local_delta); |
| 651 | numerator = ((s64)uv - chip->xoadc_v0625 - local_delta) |
| 652 | * XOADC_CALIB_UV; |
| 653 | denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 654 | if (denominator == 0) |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 655 | return uv * VBATT_MUL_FACTOR; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 656 | return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator)) |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 657 | * VBATT_MUL_FACTOR; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Abhijeet Dharmapurikar | 5adb253 | 2012-06-21 16:15:43 -0700 | [diff] [blame] | 660 | #define CC_RESOLUTION_N 868056 |
| 661 | #define CC_RESOLUTION_D 10000 |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 662 | |
| 663 | static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc) |
| 664 | { |
Abhijeet Dharmapurikar | 5adb253 | 2012-06-21 16:15:43 -0700 | [diff] [blame] | 665 | return div_s64(cc * CC_RESOLUTION_N, CC_RESOLUTION_D); |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Abhijeet Dharmapurikar | 2feabe3 | 2012-07-23 21:42:17 -0700 | [diff] [blame] | 668 | #define CC_READING_TICKS 56 |
| 669 | #define SLEEP_CLK_HZ 32764 |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 670 | #define SECONDS_PER_HOUR 3600 |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 671 | /** |
Xiaozhe Shi | 35b82e4 | 2013-02-12 23:17:54 -0800 | [diff] [blame] | 672 | * ccmicrovolt_to_pvh - |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 673 | * @cc_uv: coulumb counter converted to uV |
| 674 | * |
Xiaozhe Shi | 35b82e4 | 2013-02-12 23:17:54 -0800 | [diff] [blame] | 675 | * RETURNS: coulumb counter based charge in pVh |
| 676 | * (pico Volt Hour) |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 677 | */ |
Xiaozhe Shi | 35b82e4 | 2013-02-12 23:17:54 -0800 | [diff] [blame] | 678 | static s64 ccmicrovolt_to_pvh(s64 cc_uv) |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 679 | { |
Xiaozhe Shi | 35b82e4 | 2013-02-12 23:17:54 -0800 | [diff] [blame] | 680 | return div_s64(cc_uv * CC_READING_TICKS * 1000000L, |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 681 | SLEEP_CLK_HZ * SECONDS_PER_HOUR); |
| 682 | } |
| 683 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 684 | /* returns the signed value read from the hardware */ |
| 685 | static int read_cc(struct pm8921_bms_chip *chip, int *result) |
| 686 | { |
| 687 | int rc; |
| 688 | uint16_t msw, lsw; |
| 689 | |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 690 | *result = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 691 | rc = pm_bms_read_output_data(chip, CC_LSB, &lsw); |
| 692 | if (rc) { |
| 693 | pr_err("fail to read CC_LSB rc = %d\n", rc); |
| 694 | return rc; |
| 695 | } |
| 696 | rc = pm_bms_read_output_data(chip, CC_MSB, &msw); |
| 697 | if (rc) { |
| 698 | pr_err("fail to read CC_MSB rc = %d\n", rc); |
| 699 | return rc; |
| 700 | } |
| 701 | *result = msw << 16 | lsw; |
| 702 | pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result); |
| 703 | return 0; |
| 704 | } |
| 705 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 706 | static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip, |
| 707 | int result) |
| 708 | { |
| 709 | int64_t numerator; |
| 710 | int64_t denominator; |
| 711 | |
| 712 | if (chip->amux_2_trim_delta == 0) |
| 713 | return result; |
| 714 | |
| 715 | numerator = (s64)result * 1000000; |
| 716 | denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta)); |
| 717 | return div_s64(numerator, denominator); |
| 718 | } |
| 719 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 720 | static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 721 | int usb_chg, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 722 | uint16_t reading, int *result) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 723 | { |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 724 | *result = xoadc_reading_to_microvolt(reading); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 725 | pr_debug("raw = %04x vbatt = %u\n", reading, *result); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 726 | *result = adjust_xo_vbatt_reading(chip, usb_chg, *result); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 727 | pr_debug("after adj vbatt = %u\n", *result); |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 728 | *result = adjust_xo_vbatt_reading_for_mbg(chip, *result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 732 | static int convert_vsense_to_uv(struct pm8921_bms_chip *chip, |
| 733 | int16_t reading, int *result) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 734 | { |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 735 | *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 736 | pr_debug("raw = %04x vsense = %d\n", reading, *result); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 737 | *result = pm8xxx_cc_adjust_for_gain(*result); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 738 | pr_debug("after adj vsense = %d\n", *result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 742 | static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result) |
| 743 | { |
| 744 | int rc; |
| 745 | int16_t reading; |
| 746 | |
| 747 | rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading); |
| 748 | if (rc) { |
| 749 | pr_err("fail to read VSENSE_AVG rc = %d\n", rc); |
| 750 | return rc; |
| 751 | } |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 752 | |
| 753 | convert_vsense_to_uv(chip, reading, result); |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 754 | return 0; |
| 755 | } |
| 756 | |
Abhijeet Dharmapurikar | a81e3a2 | 2013-01-14 18:19:18 -0800 | [diff] [blame] | 757 | static int get_batt_temp(struct pm8921_bms_chip *chip, int *batt_temp) |
| 758 | { |
| 759 | int rc; |
| 760 | struct pm8xxx_adc_chan_result result; |
| 761 | |
| 762 | rc = pm8xxx_adc_read(chip->batt_temp_channel, &result); |
| 763 | if (rc) { |
| 764 | pr_err("error reading batt_temp_channel = %d, rc = %d\n", |
| 765 | chip->batt_temp_channel, rc); |
| 766 | return rc; |
| 767 | } |
| 768 | *batt_temp = result.physical; |
| 769 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 770 | result.measurement); |
| 771 | return 0; |
| 772 | } |
| 773 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 774 | #define BMS_MODE_BIT BIT(6) |
| 775 | #define EN_VBAT_BIT BIT(5) |
| 776 | #define OVERRIDE_MODE_DELAY_MS 20 |
Abhijeet Dharmapurikar | b268013 | 2012-05-15 16:32:51 -0700 | [diff] [blame] | 777 | int override_mode_simultaneous_battery_voltage_and_current(int *ibat_ua, |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 778 | int *vbat_uv) |
| 779 | { |
| 780 | int16_t vsense_raw; |
| 781 | int16_t vbat_raw; |
| 782 | int vsense_uv; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 783 | int usb_chg; |
Abhijeet Dharmapurikar | a81e3a2 | 2013-01-14 18:19:18 -0800 | [diff] [blame] | 784 | int batt_temp; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 785 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 786 | mutex_lock(&the_chip->bms_output_lock); |
| 787 | |
| 788 | pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00); |
| 789 | pm_bms_masked_write(the_chip, BMS_CONTROL, |
| 790 | BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT); |
| 791 | |
| 792 | msleep(OVERRIDE_MODE_DELAY_MS); |
| 793 | |
| 794 | pm_bms_lock_output_data(the_chip); |
| 795 | pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw); |
| 796 | pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw); |
| 797 | pm_bms_unlock_output_data(the_chip); |
| 798 | pm_bms_masked_write(the_chip, BMS_CONTROL, |
Abhijeet Dharmapurikar | a81e3a2 | 2013-01-14 18:19:18 -0800 | [diff] [blame] | 799 | BMS_MODE_BIT | EN_VBAT_BIT, 0); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 800 | |
| 801 | pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B); |
| 802 | |
| 803 | mutex_unlock(&the_chip->bms_output_lock); |
| 804 | |
Abhijeet Dharmapurikar | a81e3a2 | 2013-01-14 18:19:18 -0800 | [diff] [blame] | 805 | get_batt_temp(the_chip, &batt_temp); |
Abhijeet Dharmapurikar | 9e94898 | 2012-08-28 15:21:23 -0700 | [diff] [blame] | 806 | usb_chg = usb_chg_plugged_in(the_chip); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 807 | |
| 808 | convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 809 | convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv); |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 810 | *ibat_ua = div_s64((s64)vsense_uv * 1000000LL, the_chip->r_sense_uohm); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 811 | |
| 812 | pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x" |
| 813 | " ibat_ua = %d vbat_uv = %d\n", |
| 814 | (uint16_t)vsense_raw, (uint16_t)vbat_raw, |
| 815 | *ibat_ua, *vbat_uv); |
| 816 | return 0; |
| 817 | } |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 818 | |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 819 | #define MBG_TRANSIENT_ERROR_UV 15000 |
| 820 | static void adjust_pon_ocv(struct pm8921_bms_chip *chip, int *uv) |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 821 | { |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 822 | /* |
| 823 | * In 8921 parts the PON ocv is taken when the MBG is not settled. |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 824 | * decrease the pon ocv by 15mV raw value to account for it |
| 825 | * Since a 1/3rd of vbatt is supplied to the adc the raw value |
| 826 | * needs to be adjusted by 5mV worth bits |
| 827 | */ |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 828 | if (*uv >= MBG_TRANSIENT_ERROR_UV) |
| 829 | *uv -= MBG_TRANSIENT_ERROR_UV; |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Abhijeet Dharmapurikar | ecdb5db | 2012-10-09 16:57:18 -0700 | [diff] [blame] | 832 | #define SEL_ALT_OREG_BIT BIT(2) |
| 833 | static int ocv_ir_compensation(struct pm8921_bms_chip *chip, int ocv) |
| 834 | { |
| 835 | int compensated_ocv; |
| 836 | int ibatt_ua; |
| 837 | int rbatt_mohm = chip->default_rbatt_mohm + chip->rconn_mohm; |
| 838 | |
| 839 | pm_bms_masked_write(chip, BMS_TEST1, |
| 840 | SEL_ALT_OREG_BIT, SEL_ALT_OREG_BIT); |
| 841 | |
| 842 | /* since the SEL_ALT_OREG_BIT is set this will give us VSENSE_OCV */ |
| 843 | pm8921_bms_get_battery_current(&ibatt_ua); |
| 844 | compensated_ocv = ocv + div_s64((s64)ibatt_ua * rbatt_mohm, 1000); |
| 845 | pr_debug("comp ocv = %d, ocv = %d, ibatt_ua = %d, rbatt_mohm = %d\n", |
| 846 | compensated_ocv, ocv, ibatt_ua, rbatt_mohm); |
| 847 | |
| 848 | pm_bms_masked_write(chip, BMS_TEST1, SEL_ALT_OREG_BIT, 0); |
| 849 | return compensated_ocv; |
| 850 | } |
| 851 | |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 852 | #define RESET_CC_BIT BIT(3) |
| 853 | static int reset_cc(struct pm8921_bms_chip *chip) |
| 854 | { |
| 855 | int rc; |
| 856 | |
| 857 | rc = pm_bms_masked_write(chip, BMS_TEST1, RESET_CC_BIT, RESET_CC_BIT); |
| 858 | if (rc < 0) { |
| 859 | pr_err("err setting cc reset rc = %d\n", rc); |
| 860 | return rc; |
| 861 | } |
| 862 | |
| 863 | /* sleep 100uS for the coulomb counter to reset */ |
| 864 | udelay(100); |
| 865 | |
| 866 | rc = pm_bms_masked_write(chip, BMS_TEST1, RESET_CC_BIT, 0); |
| 867 | if (rc < 0) |
| 868 | pr_err("err clearing cc reset rc = %d\n", rc); |
| 869 | return rc; |
| 870 | } |
| 871 | |
| 872 | static int estimate_ocv(struct pm8921_bms_chip *chip) |
| 873 | { |
| 874 | int ibat_ua, vbat_uv, ocv_est_uv; |
| 875 | int rc; |
Abhijeet Dharmapurikar | 03c6c23 | 2013-02-13 17:57:40 -0800 | [diff] [blame] | 876 | int rbatt_mohm = chip->default_rbatt_mohm + chip->rconn_mohm |
| 877 | + chip->rbatt_capacitive_mohm; |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 878 | |
| 879 | rc = pm8921_bms_get_simultaneous_battery_voltage_and_current( |
| 880 | &ibat_ua, |
| 881 | &vbat_uv); |
| 882 | if (rc) { |
| 883 | pr_err("simultaneous failed rc = %d\n", rc); |
| 884 | return rc; |
| 885 | } |
| 886 | |
| 887 | ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000; |
| 888 | pr_debug("estimated pon ocv = %d\n", ocv_est_uv); |
| 889 | return ocv_est_uv; |
| 890 | } |
| 891 | |
| 892 | static bool is_warm_restart(struct pm8921_bms_chip *chip) |
| 893 | { |
| 894 | u8 reg; |
| 895 | int rc; |
| 896 | |
| 897 | rc = pm8xxx_readb(chip->dev->parent, PON_CNTRL_6, ®); |
| 898 | if (rc) { |
| 899 | pr_err("err reading pon 6 rc = %d\n", rc); |
| 900 | return false; |
| 901 | } |
| 902 | return reg & WD_BIT; |
| 903 | } |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 904 | |
| 905 | #define IBAT_TOL_MASK 0x0F |
| 906 | #define OCV_TOL_MASK 0xF0 |
| 907 | #define IBAT_TOL_DEFAULT 0x03 |
| 908 | #define IBAT_TOL_NOCHG 0x0F |
| 909 | #define OCV_TOL_DEFAULT 0x20 |
| 910 | #define OCV_TOL_NO_OCV 0x00 |
| 911 | static int pm8921_bms_stop_ocv_updates(void) |
| 912 | { |
| 913 | if (!the_chip) { |
| 914 | pr_err("BMS driver has not been initialized yet!\n"); |
| 915 | return -EINVAL; |
| 916 | } |
| 917 | pr_debug("stopping ocv updates\n"); |
| 918 | return pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 919 | OCV_TOL_MASK, OCV_TOL_NO_OCV); |
| 920 | } |
| 921 | |
| 922 | static int pm8921_bms_start_ocv_updates(void) |
| 923 | { |
| 924 | if (!the_chip) { |
| 925 | pr_err("BMS driver has not been initialized yet!\n"); |
| 926 | return -EINVAL; |
| 927 | } |
Abhijeet Dharmapurikar | 1939ede | 2013-01-10 15:59:33 -0800 | [diff] [blame] | 928 | pr_debug("starting ocv updates\n"); |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 929 | return pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 930 | OCV_TOL_MASK, OCV_TOL_DEFAULT); |
| 931 | } |
| 932 | |
| 933 | static int reset_bms_for_test(void) |
| 934 | { |
| 935 | int ibat_ua, vbat_uv, rc; |
| 936 | int ocv_est_uv; |
| 937 | |
| 938 | if (!the_chip) { |
| 939 | pr_err("BMS driver has not been initialized yet!\n"); |
| 940 | return -EINVAL; |
| 941 | } |
| 942 | |
| 943 | rc = pm8921_bms_get_simultaneous_battery_voltage_and_current( |
| 944 | &ibat_ua, |
| 945 | &vbat_uv); |
Abhijeet Dharmapurikar | 03c6c23 | 2013-02-13 17:57:40 -0800 | [diff] [blame] | 946 | /* |
| 947 | * don't include rbatt and rbatt_capacitve since we expect this to |
| 948 | * be used with a fake battery which does not have internal resistnaces |
| 949 | */ |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 950 | ocv_est_uv = vbat_uv + (ibat_ua * the_chip->rconn_mohm) / 1000; |
| 951 | pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv); |
| 952 | the_chip->last_ocv_uv = ocv_est_uv; |
| 953 | last_soc = -EINVAL; |
| 954 | reset_cc(the_chip); |
| 955 | the_chip->last_cc_uah = 0; |
| 956 | pm8921_bms_stop_ocv_updates(); |
| 957 | |
| 958 | pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n", |
| 959 | the_chip->last_ocv_uv, vbat_uv, ibat_ua); |
| 960 | |
| 961 | return rc; |
| 962 | } |
| 963 | |
| 964 | static int bms_reset_set(const char *val, const struct kernel_param *kp) |
| 965 | { |
| 966 | int rc; |
| 967 | |
| 968 | rc = param_set_bool(val, kp); |
| 969 | if (rc) { |
| 970 | pr_err("Unable to set bms_reset: %d\n", rc); |
| 971 | return rc; |
| 972 | } |
| 973 | |
| 974 | if (*val == 'Y') { |
| 975 | rc = reset_bms_for_test(); |
| 976 | if (rc) { |
| 977 | pr_err("Unable to modify bms_reset: %d\n", rc); |
| 978 | return rc; |
| 979 | } |
| 980 | } |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static struct kernel_param_ops bms_reset_ops = { |
| 985 | .set = bms_reset_set, |
| 986 | .get = param_get_bool, |
| 987 | }; |
| 988 | |
| 989 | static bool bms_reset; |
| 990 | module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644); |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 991 | /* |
| 992 | * This reflects what should the CC readings should be for |
| 993 | * a 5mAh discharge. This value is dependent on |
| 994 | * CC_RESOLUTION_N, CC_RESOLUTION_D, CC_READING_TICKS |
| 995 | * and rsense |
| 996 | */ |
Abhijeet Dharmapurikar | 61275b9 | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 997 | #define CC_RAW_5MAH 0x00110000 |
| 998 | #define MIN_OCV_UV 2000000 |
| 999 | #define OCV_RAW_UNINITIALIZED 0xFFFF |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1000 | static int read_soc_params_raw(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1001 | struct pm8921_soc_params *raw, |
| 1002 | int batt_temp_decidegc) |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1003 | { |
| 1004 | int usb_chg; |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 1005 | int est_ocv_uv; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1006 | |
| 1007 | mutex_lock(&chip->bms_output_lock); |
| 1008 | pm_bms_lock_output_data(chip); |
| 1009 | |
| 1010 | pm_bms_read_output_data(chip, |
| 1011 | LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw); |
| 1012 | read_cc(chip, &raw->cc); |
| 1013 | |
| 1014 | pm_bms_unlock_output_data(chip); |
| 1015 | mutex_unlock(&chip->bms_output_lock); |
| 1016 | |
Abhijeet Dharmapurikar | 9e94898 | 2012-08-28 15:21:23 -0700 | [diff] [blame] | 1017 | usb_chg = usb_chg_plugged_in(chip); |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 1018 | |
Abhijeet Dharmapurikar | 61275b9 | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 1019 | if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) { |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 1020 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 1021 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 1022 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 1023 | raw->last_good_ocv_raw, &raw->last_good_ocv_uv); |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 1024 | adjust_pon_ocv(chip, &raw->last_good_ocv_uv); |
Abhijeet Dharmapurikar | ecdb5db | 2012-10-09 16:57:18 -0700 | [diff] [blame] | 1025 | raw->last_good_ocv_uv = ocv_ir_compensation(chip, |
| 1026 | raw->last_good_ocv_uv); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1027 | chip->last_ocv_uv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | 3ae2d41 | 2012-11-07 19:00:32 -0800 | [diff] [blame] | 1028 | |
| 1029 | if (is_warm_restart(chip) |
| 1030 | || raw->cc > CC_RAW_5MAH |
| 1031 | || (raw->last_good_ocv_uv < MIN_OCV_UV |
| 1032 | && raw->cc > 0)) { |
| 1033 | /* |
| 1034 | * The CC value is higher than 5mAh. |
| 1035 | * The phone started without going through a pon |
| 1036 | * sequence |
| 1037 | * OR |
| 1038 | * The ocv was very small and there was no |
| 1039 | * charging in the bootloader |
| 1040 | * - reset the CC and take an ocv again |
| 1041 | */ |
| 1042 | pr_debug("cc_raw = 0x%x may be > 5mAh(0x%x)\n", |
| 1043 | raw->cc, CC_RAW_5MAH); |
| 1044 | pr_debug("ocv_uv = %d ocv_raw = 0x%x may be < 2V\n", |
| 1045 | chip->last_ocv_uv, |
| 1046 | raw->last_good_ocv_raw); |
| 1047 | est_ocv_uv = estimate_ocv(chip); |
| 1048 | if (est_ocv_uv > 0) { |
| 1049 | raw->last_good_ocv_uv = est_ocv_uv; |
| 1050 | chip->last_ocv_uv = est_ocv_uv; |
| 1051 | reset_cc(chip); |
| 1052 | raw->cc = 0; |
| 1053 | } |
| 1054 | } |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1055 | chip->last_ocv_temp_decidegc = batt_temp_decidegc; |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 1056 | pr_debug("PON_OCV_UV = %d\n", chip->last_ocv_uv); |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 1057 | } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) { |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 1058 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 1059 | convert_vbatt_raw_to_uv(chip, usb_chg, |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1060 | raw->last_good_ocv_raw, &raw->last_good_ocv_uv); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1061 | chip->last_ocv_uv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1062 | chip->last_ocv_temp_decidegc = batt_temp_decidegc; |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1063 | /* forget the old cc value upon ocv */ |
| 1064 | chip->last_cc_uah = 0; |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 1065 | } else { |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1066 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 1067 | } |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1068 | |
Abhijeet Dharmapurikar | 079d6c5 | 2013-01-25 13:10:07 -0800 | [diff] [blame] | 1069 | /* stop faking 100% after an OCV event */ |
Abhijeet Dharmapurikar | 28d5137 | 2013-02-27 19:16:22 -0800 | [diff] [blame] | 1070 | if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) |
Abhijeet Dharmapurikar | 61275b9 | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 1071 | chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1072 | pr_debug("0p625 = %duV\n", chip->xoadc_v0625); |
| 1073 | pr_debug("1p25 = %duV\n", chip->xoadc_v125); |
Abhijeet Dharmapurikar | 1b8e829 | 2012-01-17 11:01:44 -0800 | [diff] [blame] | 1074 | pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n", |
| 1075 | raw->last_good_ocv_raw, raw->last_good_ocv_uv); |
| 1076 | pr_debug("cc_raw= 0x%x\n", raw->cc); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1077 | return 0; |
| 1078 | } |
| 1079 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1080 | static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp) |
| 1081 | { |
| 1082 | int rbatt, scalefactor; |
| 1083 | |
Abhijeet Dharmapurikar | 0711c52 | 2012-05-22 18:45:25 -0700 | [diff] [blame] | 1084 | rbatt = chip->default_rbatt_mohm; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1085 | pr_debug("rbatt before scaling = %d\n", rbatt); |
| 1086 | if (chip->rbatt_sf_lut == NULL) { |
| 1087 | pr_debug("RBATT = %d\n", rbatt); |
| 1088 | return rbatt; |
| 1089 | } |
Abhijeet Dharmapurikar | 12a891c | 2012-04-12 23:52:06 -0700 | [diff] [blame] | 1090 | /* Convert the batt_temp to DegC from deciDegC */ |
| 1091 | batt_temp = batt_temp / 10; |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1092 | scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1093 | batt_temp, soc_rbatt); |
| 1094 | pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n", |
| 1095 | scalefactor, batt_temp, soc_rbatt); |
| 1096 | rbatt = (rbatt * scalefactor) / 100; |
| 1097 | |
Abhijeet Dharmapurikar | baffba4 | 2012-03-22 14:41:10 -0700 | [diff] [blame] | 1098 | rbatt += the_chip->rconn_mohm; |
| 1099 | pr_debug("adding rconn_mohm = %d rbatt = %d\n", |
| 1100 | the_chip->rconn_mohm, rbatt); |
| 1101 | |
Abhijeet Dharmapurikar | 03c6c23 | 2013-02-13 17:57:40 -0800 | [diff] [blame] | 1102 | rbatt += the_chip->rbatt_capacitive_mohm; |
| 1103 | pr_debug("adding rbatt_capacitive_mohm = %d rbatt = %d\n", |
| 1104 | the_chip->rbatt_capacitive_mohm, rbatt); |
| 1105 | |
Abhijeet Dharmapurikar | ded189b | 2012-03-30 10:12:28 -0700 | [diff] [blame] | 1106 | if (is_between(20, 10, soc_rbatt)) |
| 1107 | rbatt = rbatt |
| 1108 | + ((20 - soc_rbatt) * chip->delta_rbatt_mohm) / 10; |
| 1109 | else |
| 1110 | if (is_between(10, 0, soc_rbatt)) |
| 1111 | rbatt = rbatt + chip->delta_rbatt_mohm; |
| 1112 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1113 | pr_debug("RBATT = %d\n", rbatt); |
| 1114 | return rbatt; |
| 1115 | } |
| 1116 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1117 | static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1118 | int chargecycles) |
| 1119 | { |
| 1120 | int initfcc, result, scalefactor = 0; |
| 1121 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1122 | if (chip->adjusted_fcc_temp_lut == NULL) { |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1123 | initfcc = interpolate_fcc(chip->fcc_temp_lut, batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1124 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1125 | scalefactor = interpolate_scalingfactor_fcc(chip->fcc_sf_lut, |
| 1126 | chargecycles); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1127 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1128 | /* Multiply the initial FCC value by the scale factor. */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1129 | result = (initfcc * scalefactor * 1000) / 100; |
| 1130 | pr_debug("fcc = %d uAh\n", result); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1131 | return result; |
| 1132 | } else { |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1133 | return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut, |
| 1134 | batt_temp); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1135 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1138 | static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts) |
| 1139 | { |
| 1140 | int rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1141 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1142 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1143 | rc = pm8xxx_adc_read(chip->vbat_channel, &result); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1144 | if (rc) { |
| 1145 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1146 | chip->vbat_channel, rc); |
| 1147 | return rc; |
| 1148 | } |
| 1149 | pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical, |
| 1150 | result.measurement); |
| 1151 | *uvolts = (int)result.physical; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv) |
| 1156 | { |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1157 | int vbatt, rbatt, ibatt_ua, rc; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1158 | |
| 1159 | rc = get_battery_uvolts(chip, &vbatt); |
| 1160 | if (rc) { |
| 1161 | pr_err("failed to read vbatt from adc rc = %d\n", rc); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1162 | return rc; |
| 1163 | } |
| 1164 | |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1165 | rc = pm8921_bms_get_battery_current(&ibatt_ua); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1166 | if (rc) { |
| 1167 | pr_err("failed to read batt current rc = %d\n", rc); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1168 | return rc; |
| 1169 | } |
| 1170 | |
Abhijeet Dharmapurikar | 0711c52 | 2012-05-22 18:45:25 -0700 | [diff] [blame] | 1171 | rbatt = chip->default_rbatt_mohm; |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1172 | *ocv = vbatt + (ibatt_ua * rbatt)/1000; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1176 | static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1177 | int batt_temp_decidegc, int chargecycles) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1178 | { |
| 1179 | int pc, scalefactor; |
| 1180 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1181 | pc = interpolate_pc(chip->pc_temp_ocv_lut, |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1182 | batt_temp_decidegc / 10, ocv_uv / 1000); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1183 | pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n", |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1184 | pc, ocv_uv, batt_temp_decidegc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1185 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1186 | scalefactor = interpolate_scalingfactor(chip->pc_sf_lut, |
| 1187 | chargecycles, pc); |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1188 | pr_debug("scalefactor = %u batt_temp = %d\n", |
| 1189 | scalefactor, batt_temp_decidegc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1190 | |
| 1191 | /* Multiply the initial FCC value by the scale factor. */ |
| 1192 | pc = (pc * scalefactor) / 100; |
| 1193 | return pc; |
| 1194 | } |
| 1195 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1196 | /** |
| 1197 | * calculate_cc_uah - |
| 1198 | * @chip: the bms chip pointer |
| 1199 | * @cc: the cc reading from bms h/w |
| 1200 | * @val: return value |
| 1201 | * @coulumb_counter: adjusted coulumb counter for 100% |
| 1202 | * |
| 1203 | * RETURNS: in val pointer coulumb counter based charger in uAh |
| 1204 | * (micro Amp hour) |
| 1205 | */ |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1206 | static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1207 | { |
Xiaozhe Shi | 35b82e4 | 2013-02-12 23:17:54 -0800 | [diff] [blame] | 1208 | int64_t cc_voltage_uv, cc_pvh, cc_uah; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1209 | |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1210 | cc_voltage_uv = cc; |
Abhijeet Dharmapurikar | 28d5137 | 2013-02-27 19:16:22 -0800 | [diff] [blame] | 1211 | pr_debug("cc = %d\n", cc); |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 1212 | cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 1213 | cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1214 | pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv); |
Xiaozhe Shi | 35b82e4 | 2013-02-12 23:17:54 -0800 | [diff] [blame] | 1215 | cc_pvh = ccmicrovolt_to_pvh(cc_voltage_uv); |
| 1216 | pr_debug("cc_pvh = %lld pico_volt_hour\n", cc_pvh); |
| 1217 | cc_uah = div_s64(cc_pvh, chip->r_sense_uohm); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1218 | *val = cc_uah; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Abhijeet Dharmapurikar | 4c78f46 | 2012-10-16 15:43:55 -0700 | [diff] [blame] | 1221 | int pm8921_bms_cc_uah(int *cc_uah) |
| 1222 | { |
| 1223 | int cc; |
| 1224 | |
| 1225 | *cc_uah = 0; |
| 1226 | |
| 1227 | if (!the_chip) |
| 1228 | return -EINVAL; |
| 1229 | |
| 1230 | read_cc(the_chip, &cc); |
| 1231 | calculate_cc_uah(the_chip, cc, cc_uah); |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | EXPORT_SYMBOL(pm8921_bms_cc_uah); |
| 1236 | |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1237 | static int calculate_termination_uuc(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1238 | int batt_temp, int chargecycles, |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1239 | int fcc_uah, int i_ma, |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1240 | int *ret_pc_unusable) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1241 | { |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1242 | int unusable_uv, pc_unusable, uuc; |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1243 | int i = 0; |
| 1244 | int ocv_mv; |
| 1245 | int batt_temp_degc = batt_temp / 10; |
| 1246 | int rbatt_mohm; |
| 1247 | int delta_uv; |
| 1248 | int prev_delta_uv = 0; |
| 1249 | int prev_rbatt_mohm = 0; |
| 1250 | int prev_ocv_mv = 0; |
| 1251 | int uuc_rbatt_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1252 | |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1253 | for (i = 0; i <= 100; i++) { |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1254 | ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, |
| 1255 | batt_temp_degc, i); |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1256 | rbatt_mohm = get_rbatt(chip, i, batt_temp); |
| 1257 | unusable_uv = (rbatt_mohm * i_ma) + (chip->v_cutoff * 1000); |
| 1258 | delta_uv = ocv_mv * 1000 - unusable_uv; |
| 1259 | |
| 1260 | pr_debug("soc = %d ocv = %d rbat = %d u_uv = %d delta_v = %d\n", |
| 1261 | i, ocv_mv, rbatt_mohm, unusable_uv, delta_uv); |
| 1262 | |
| 1263 | if (delta_uv > 0) |
| 1264 | break; |
| 1265 | |
| 1266 | prev_delta_uv = delta_uv; |
| 1267 | prev_rbatt_mohm = rbatt_mohm; |
| 1268 | prev_ocv_mv = ocv_mv; |
| 1269 | } |
| 1270 | |
| 1271 | uuc_rbatt_uv = linear_interpolate(rbatt_mohm, delta_uv, |
| 1272 | prev_rbatt_mohm, prev_delta_uv, |
| 1273 | 0); |
| 1274 | |
| 1275 | unusable_uv = (uuc_rbatt_uv * i_ma) + (chip->v_cutoff * 1000); |
| 1276 | |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1277 | pc_unusable = calculate_pc(chip, unusable_uv, batt_temp, chargecycles); |
| 1278 | uuc = (fcc_uah * pc_unusable) / 100; |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1279 | pr_debug("For i_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d uuc = %d\n", |
| 1280 | i_ma, uuc_rbatt_uv, unusable_uv, |
| 1281 | pc_unusable, uuc); |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1282 | *ret_pc_unusable = pc_unusable; |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1283 | return uuc; |
| 1284 | } |
| 1285 | |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1286 | #define TIME_PER_PERCENT_UUC 60 |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1287 | static int adjust_uuc(struct pm8921_bms_chip *chip, int fcc_uah, |
| 1288 | int new_pc_unusable, |
| 1289 | int new_uuc, |
| 1290 | int batt_temp, |
| 1291 | int rbatt, |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1292 | int *iavg_ma, |
| 1293 | int delta_time_s) |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1294 | { |
| 1295 | int new_unusable_mv; |
| 1296 | int batt_temp_degc = batt_temp / 10; |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1297 | int max_percent_change; |
| 1298 | |
| 1299 | max_percent_change = max(delta_time_s / TIME_PER_PERCENT_UUC, 1); |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1300 | |
| 1301 | if (chip->prev_pc_unusable == -EINVAL |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1302 | || abs(chip->prev_pc_unusable - new_pc_unusable) |
| 1303 | <= max_percent_change) { |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1304 | chip->prev_pc_unusable = new_pc_unusable; |
| 1305 | return new_uuc; |
| 1306 | } |
| 1307 | |
| 1308 | /* the uuc is trying to change more than 1% restrict it */ |
| 1309 | if (new_pc_unusable > chip->prev_pc_unusable) |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1310 | chip->prev_pc_unusable += max_percent_change; |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1311 | else |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1312 | chip->prev_pc_unusable -= max_percent_change; |
| 1313 | chip->prev_pc_unusable = clamp(chip->prev_pc_unusable, 0, 100); |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1314 | new_uuc = (fcc_uah * chip->prev_pc_unusable) / 100; |
| 1315 | |
| 1316 | /* also find update the iavg_ma accordingly */ |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1317 | new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut, |
| 1318 | batt_temp_degc, chip->prev_pc_unusable); |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1319 | if (new_unusable_mv < chip->v_cutoff) |
| 1320 | new_unusable_mv = chip->v_cutoff; |
| 1321 | |
| 1322 | *iavg_ma = (new_unusable_mv - chip->v_cutoff) * 1000 / rbatt; |
| 1323 | if (*iavg_ma == 0) |
| 1324 | *iavg_ma = 1; |
| 1325 | pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n", |
| 1326 | new_uuc, chip->prev_pc_unusable, |
| 1327 | new_unusable_mv, *iavg_ma); |
| 1328 | |
| 1329 | return new_uuc; |
| 1330 | } |
| 1331 | |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1332 | static int get_current_time(unsigned long *now_tm_sec) |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1333 | { |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1334 | struct rtc_time tm; |
| 1335 | struct rtc_device *rtc; |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1336 | int rc; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1337 | |
| 1338 | rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
| 1339 | if (rtc == NULL) { |
| 1340 | pr_err("%s: unable to open rtc device (%s)\n", |
| 1341 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1342 | return -EINVAL; |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1345 | rc = rtc_read_time(rtc, &tm); |
| 1346 | if (rc) { |
| 1347 | pr_err("Error reading rtc device (%s) : %d\n", |
| 1348 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1349 | return rc; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1350 | } |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1351 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1352 | rc = rtc_valid_tm(&tm); |
| 1353 | if (rc) { |
| 1354 | pr_err("Invalid RTC time (%s): %d\n", |
| 1355 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1356 | return rc; |
| 1357 | } |
| 1358 | rtc_tm_to_time(&tm, now_tm_sec); |
| 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1363 | static int calculate_delta_time(struct pm8921_bms_chip *chip, int *delta_time_s) |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1364 | { |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1365 | unsigned long now_tm_sec = 0; |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1366 | |
| 1367 | /* default to delta time = 0 if anything fails */ |
| 1368 | *delta_time_s = 0; |
| 1369 | |
| 1370 | get_current_time(&now_tm_sec); |
| 1371 | |
| 1372 | *delta_time_s = (now_tm_sec - chip->tm_sec); |
| 1373 | pr_debug("tm_sec = %ld, now_tm_sec = %ld delta_s = %d\n", |
| 1374 | chip->tm_sec, now_tm_sec, *delta_time_s); |
| 1375 | |
| 1376 | /* remember this time */ |
| 1377 | chip->tm_sec = now_tm_sec; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | static void calculate_iavg_ua(struct pm8921_bms_chip *chip, int cc_uah, |
| 1382 | int *iavg_ua, int delta_time_s) |
| 1383 | { |
| 1384 | int delta_cc_uah = 0; |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1385 | |
| 1386 | /* if anything fails report the previous iavg_ua */ |
| 1387 | *iavg_ua = chip->prev_iavg_ua; |
| 1388 | |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1389 | if (chip->last_cc_uah == INT_MIN) { |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1390 | pm8921_bms_get_battery_current(iavg_ua); |
| 1391 | goto out; |
| 1392 | } |
| 1393 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1394 | /* use the previous iavg if called within 15 seconds */ |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1395 | if (delta_time_s < 15) { |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1396 | *iavg_ua = chip->prev_iavg_ua; |
| 1397 | goto out; |
| 1398 | } |
| 1399 | |
| 1400 | delta_cc_uah = cc_uah - chip->last_cc_uah; |
| 1401 | |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1402 | *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s); |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1403 | |
| 1404 | out: |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1405 | pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua); |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1406 | /* remember the iavg */ |
| 1407 | chip->prev_iavg_ua = *iavg_ua; |
| 1408 | |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1409 | /* remember cc_uah */ |
| 1410 | chip->last_cc_uah = cc_uah; |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1411 | } |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1412 | |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1413 | #define IAVG_SAMPLES 16 |
Abhijeet Dharmapurikar | e6503da | 2012-12-20 18:33:56 -0800 | [diff] [blame] | 1414 | #define MIN_IAVG_MA 250 |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1415 | #define MIN_SECONDS_FOR_VALID_SAMPLE 20 |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1416 | static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip, |
| 1417 | int rbatt, int fcc_uah, int cc_uah, |
| 1418 | int soc_rbatt, int batt_temp, int chargecycles, |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1419 | int iavg_ua, int delta_time_s) |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1420 | { |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1421 | int uuc_uah_iavg; |
| 1422 | int i; |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1423 | int iavg_ma = iavg_ua / 1000; |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1424 | static int iavg_samples[IAVG_SAMPLES]; |
| 1425 | static int iavg_index; |
| 1426 | static int iavg_num_samples; |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 1427 | static int firsttime = 1; |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1428 | int pc_unusable; |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* |
| 1431 | * if we are called first time fill all the |
| 1432 | * samples with the the shutdown_iavg_ua |
| 1433 | */ |
| 1434 | if (firsttime && chip->shutdown_iavg_ua != 0) { |
Abhijeet Dharmapurikar | 6b4af16 | 2012-10-10 09:16:27 -0700 | [diff] [blame] | 1435 | pr_debug("Using shutdown_iavg_ua = %d in all samples\n", |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 1436 | chip->shutdown_iavg_ua); |
| 1437 | for (i = 0; i < IAVG_SAMPLES; i++) |
| 1438 | iavg_samples[i] = chip->shutdown_iavg_ua; |
| 1439 | |
| 1440 | iavg_index = 0; |
| 1441 | iavg_num_samples = IAVG_SAMPLES; |
| 1442 | } |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1443 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1444 | /* |
| 1445 | * if we are charging use a nominal avg current so that we keep |
| 1446 | * a reasonable UUC while charging |
| 1447 | */ |
Abhijeet Dharmapurikar | e6503da | 2012-12-20 18:33:56 -0800 | [diff] [blame] | 1448 | if (iavg_ma < MIN_IAVG_MA) |
| 1449 | iavg_ma = MIN_IAVG_MA; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1450 | iavg_samples[iavg_index] = iavg_ma; |
| 1451 | iavg_index = (iavg_index + 1) % IAVG_SAMPLES; |
| 1452 | iavg_num_samples++; |
| 1453 | if (iavg_num_samples >= IAVG_SAMPLES) |
| 1454 | iavg_num_samples = IAVG_SAMPLES; |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1455 | |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1456 | /* now that this sample is added calcualte the average */ |
| 1457 | iavg_ma = 0; |
| 1458 | if (iavg_num_samples != 0) { |
| 1459 | for (i = 0; i < iavg_num_samples; i++) { |
| 1460 | pr_debug("iavg_samples[%d] = %d\n", i, iavg_samples[i]); |
| 1461 | iavg_ma += iavg_samples[i]; |
| 1462 | } |
| 1463 | |
| 1464 | iavg_ma = DIV_ROUND_CLOSEST(iavg_ma, iavg_num_samples); |
| 1465 | } |
| 1466 | |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 1467 | /* |
| 1468 | * if we're in bms reset mode, force uuc to be 3% of fcc |
| 1469 | */ |
| 1470 | if (bms_reset) |
| 1471 | return (fcc_uah * 3) / 100; |
| 1472 | |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1473 | uuc_uah_iavg = calculate_termination_uuc(chip, |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1474 | batt_temp, chargecycles, |
Abhijeet Dharmapurikar | 5dc6d34 | 2012-08-04 13:58:53 -0700 | [diff] [blame] | 1475 | fcc_uah, iavg_ma, |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1476 | &pc_unusable); |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1477 | pr_debug("iavg = %d uuc_iavg = %d\n", iavg_ma, uuc_uah_iavg); |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1478 | |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1479 | /* restrict the uuc change to one percent per 60 seconds */ |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1480 | uuc_uah_iavg = adjust_uuc(chip, fcc_uah, pc_unusable, uuc_uah_iavg, |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1481 | batt_temp, rbatt, &iavg_ma, delta_time_s); |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 1482 | |
| 1483 | /* find out what the avg current should be for this uuc */ |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 1484 | chip->prev_uuc_iavg_ma = iavg_ma; |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1485 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 1486 | firsttime = 0; |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1487 | return uuc_uah_iavg; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1488 | } |
Abhijeet Dharmapurikar | 52aa968 | 2011-07-20 14:05:47 -0700 | [diff] [blame] | 1489 | |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1490 | /* calculate remainging charge at the time of ocv */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1491 | static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1492 | struct pm8921_soc_params *raw, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1493 | int fcc_uah, int batt_temp, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1494 | int chargecycles) |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1495 | { |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1496 | int ocv, pc, batt_temp_decidegc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1497 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1498 | ocv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1499 | batt_temp_decidegc = chip->last_ocv_temp_decidegc; |
| 1500 | pc = calculate_pc(chip, ocv, batt_temp_decidegc, chargecycles); |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1501 | pr_debug("ocv = %d pc = %d\n", ocv, pc); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1502 | return (fcc_uah * pc) / 100; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1505 | static void calculate_soc_params(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1506 | struct pm8921_soc_params *raw, |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1507 | int batt_temp, int chargecycles, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1508 | int *fcc_uah, |
| 1509 | int *unusable_charge_uah, |
| 1510 | int *remaining_charge_uah, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1511 | int *cc_uah, |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1512 | int *rbatt, |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1513 | int *iavg_ua) |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1514 | { |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1515 | int soc_rbatt; |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1516 | int delta_time_s; |
| 1517 | int rc; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1518 | |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1519 | rc = calculate_delta_time(chip, &delta_time_s); |
| 1520 | if (rc) { |
| 1521 | pr_err("Failed to get delta time from RTC: %d\n", rc); |
| 1522 | delta_time_s = 0; |
| 1523 | } |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1524 | *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles); |
| 1525 | pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n", |
| 1526 | *fcc_uah, batt_temp, chargecycles); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1527 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1528 | |
| 1529 | /* calculate remainging charge */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1530 | *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw, |
| 1531 | *fcc_uah, batt_temp, chargecycles); |
| 1532 | pr_debug("RC = %uuAh\n", *remaining_charge_uah); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1533 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1534 | /* calculate cc micro_volt_hour */ |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1535 | calculate_cc_uah(chip, raw->cc, cc_uah); |
Abhijeet Dharmapurikar | 28d5137 | 2013-02-27 19:16:22 -0800 | [diff] [blame] | 1536 | pr_debug("cc_uah = %duAh raw->cc = %x\n", *cc_uah, raw->cc); |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1537 | |
| 1538 | soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah; |
| 1539 | if (soc_rbatt < 0) |
| 1540 | soc_rbatt = 0; |
| 1541 | *rbatt = get_rbatt(chip, soc_rbatt, batt_temp); |
| 1542 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 1543 | calculate_iavg_ua(chip, *cc_uah, iavg_ua, delta_time_s); |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1544 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1545 | *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt, |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 1546 | *fcc_uah, *cc_uah, soc_rbatt, |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1547 | batt_temp, chargecycles, *iavg_ua, |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1548 | delta_time_s); |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1549 | pr_debug("UUC = %uuAh\n", *unusable_charge_uah); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1552 | static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1553 | struct pm8921_soc_params *raw, |
| 1554 | int batt_temp, int chargecycles, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1555 | int *ret_fcc_uah) |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1556 | { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1557 | int fcc_uah, unusable_charge_uah; |
| 1558 | int remaining_charge_uah; |
| 1559 | int cc_uah; |
| 1560 | int real_fcc_uah; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1561 | int rbatt; |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1562 | int iavg_ua; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1563 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1564 | calculate_soc_params(chip, raw, batt_temp, chargecycles, |
| 1565 | &fcc_uah, |
| 1566 | &unusable_charge_uah, |
| 1567 | &remaining_charge_uah, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1568 | &cc_uah, |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 1569 | &rbatt, |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 1570 | &iavg_ua); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1571 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1572 | real_fcc_uah = remaining_charge_uah - cc_uah; |
| 1573 | *ret_fcc_uah = fcc_uah; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1574 | pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n", |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1575 | real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah); |
| 1576 | return real_fcc_uah; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1577 | } |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1578 | |
Abhijeet Dharmapurikar | b268013 | 2012-05-15 16:32:51 -0700 | [diff] [blame] | 1579 | int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua, |
| 1580 | int *vbat_uv) |
| 1581 | { |
| 1582 | int rc; |
| 1583 | |
| 1584 | if (the_chip == NULL) { |
| 1585 | pr_err("Called too early\n"); |
| 1586 | return -EINVAL; |
| 1587 | } |
| 1588 | |
| 1589 | if (pm8921_is_batfet_closed()) { |
| 1590 | return override_mode_simultaneous_battery_voltage_and_current( |
| 1591 | ibat_ua, |
| 1592 | vbat_uv); |
| 1593 | } else { |
| 1594 | pr_debug("batfet is open using separate vbat and ibat meas\n"); |
| 1595 | rc = get_battery_uvolts(the_chip, vbat_uv); |
| 1596 | if (rc < 0) { |
| 1597 | pr_err("adc vbat failed err = %d\n", rc); |
| 1598 | return rc; |
| 1599 | } |
| 1600 | rc = pm8921_bms_get_battery_current(ibat_ua); |
| 1601 | if (rc < 0) { |
| 1602 | pr_err("bms ibat failed err = %d\n", rc); |
| 1603 | return rc; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | return 0; |
| 1608 | } |
| 1609 | EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current); |
| 1610 | |
Abhijeet Dharmapurikar | f1913b6 | 2013-02-13 16:47:14 -0800 | [diff] [blame] | 1611 | #define SIGN(x) ((x) < 0 ? -1 : 1) |
| 1612 | |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1613 | static void find_ocv_for_soc(struct pm8921_bms_chip *chip, |
| 1614 | int batt_temp, |
| 1615 | int chargecycles, |
| 1616 | int fcc_uah, |
| 1617 | int uuc_uah, |
| 1618 | int cc_uah, |
| 1619 | int shutdown_soc, |
| 1620 | int *rc_uah, |
| 1621 | int *ocv_uv) |
| 1622 | { |
| 1623 | s64 rc; |
| 1624 | int pc, new_pc; |
| 1625 | int batt_temp_degc = batt_temp / 10; |
| 1626 | int ocv; |
| 1627 | |
| 1628 | rc = (s64)shutdown_soc * (fcc_uah - uuc_uah); |
| 1629 | rc = div_s64(rc, 100) + cc_uah + uuc_uah; |
| 1630 | pc = DIV_ROUND_CLOSEST((int)rc * 100, fcc_uah); |
| 1631 | pc = clamp(pc, 0, 100); |
| 1632 | |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1633 | ocv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc); |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1634 | |
| 1635 | pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n", |
| 1636 | shutdown_soc, fcc_uah, uuc_uah, (int)rc, pc, ocv); |
Xiaozhe Shi | 5ee9519 | 2012-09-12 15:16:34 -0700 | [diff] [blame] | 1637 | new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv); |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1638 | pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv); |
| 1639 | |
Abhijeet Dharmapurikar | f1913b6 | 2013-02-13 16:47:14 -0800 | [diff] [blame] | 1640 | if (abs(new_pc - pc) > 0) { |
| 1641 | /* Maximum spins to make in while-loop when searching in |
| 1642 | * full resolution. |
| 1643 | */ |
| 1644 | const unsigned int max_spin_count = |
| 1645 | chip->max_voltage_uv / 1000 - chip->v_cutoff + 1; |
| 1646 | unsigned int count = 0; |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1647 | int delta_mv = 5; |
Abhijeet Dharmapurikar | f1913b6 | 2013-02-13 16:47:14 -0800 | [diff] [blame] | 1648 | int diff = abs(new_pc - pc); |
| 1649 | char sign = SIGN(new_pc - pc); |
| 1650 | char old_sign; |
| 1651 | int old_diff; |
| 1652 | int old_ocv; |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1653 | |
Abhijeet Dharmapurikar | f1913b6 | 2013-02-13 16:47:14 -0800 | [diff] [blame] | 1654 | do { |
| 1655 | count++; |
| 1656 | old_ocv = ocv; |
| 1657 | old_diff = diff; |
| 1658 | old_sign = sign; |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1659 | |
Abhijeet Dharmapurikar | f1913b6 | 2013-02-13 16:47:14 -0800 | [diff] [blame] | 1660 | if (new_pc > pc) |
| 1661 | ocv -= delta_mv; |
| 1662 | else |
| 1663 | ocv += delta_mv; |
| 1664 | |
| 1665 | new_pc = interpolate_pc(chip->pc_temp_ocv_lut, |
| 1666 | batt_temp_degc, ocv); |
| 1667 | pr_debug("test revlookup pc = %d for ocv = %d\n", |
| 1668 | new_pc, ocv); |
| 1669 | diff = abs(new_pc - pc); |
| 1670 | sign = SIGN(new_pc - pc); |
| 1671 | |
| 1672 | if (sign != old_sign) { |
| 1673 | if (delta_mv == 5) { |
| 1674 | /* |
| 1675 | * we crossed our desired PC probably |
| 1676 | * becuase we were overcorrecting |
| 1677 | */ |
| 1678 | delta_mv = 1; |
| 1679 | } else { |
| 1680 | /* we crossed our desired PC even with |
| 1681 | * 1mV steps, choose the best of two */ |
| 1682 | if (diff > old_diff) |
| 1683 | ocv = old_ocv; |
| 1684 | |
| 1685 | break; |
| 1686 | } |
| 1687 | } |
| 1688 | } while (count <= max_spin_count && diff > 0); |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | *ocv_uv = ocv * 1000; |
| 1692 | *rc_uah = (int)rc; |
| 1693 | } |
| 1694 | |
| 1695 | static void adjust_rc_and_uuc_for_specific_soc( |
| 1696 | struct pm8921_bms_chip *chip, |
| 1697 | int batt_temp, |
| 1698 | int chargecycles, |
| 1699 | int soc, |
| 1700 | int fcc_uah, |
| 1701 | int uuc_uah, |
| 1702 | int cc_uah, |
| 1703 | int rc_uah, |
| 1704 | int rbatt, |
| 1705 | int *ret_ocv, |
| 1706 | int *ret_rc, |
| 1707 | int *ret_uuc, |
| 1708 | int *ret_rbatt) |
| 1709 | { |
| 1710 | int ocv_uv; |
| 1711 | |
| 1712 | find_ocv_for_soc(chip, batt_temp, chargecycles, |
| 1713 | fcc_uah, uuc_uah, cc_uah, |
| 1714 | soc, |
| 1715 | &rc_uah, &ocv_uv); |
| 1716 | |
| 1717 | *ret_ocv = ocv_uv; |
| 1718 | *ret_rbatt = rbatt; |
| 1719 | *ret_rc = rc_uah; |
| 1720 | *ret_uuc = uuc_uah; |
| 1721 | } |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 1722 | |
| 1723 | static void calc_current_max(struct pm8921_bms_chip *chip, int ocv_uv, |
| 1724 | int rbatt_mohm) |
| 1725 | { |
| 1726 | chip->imax_ua = 1000 * (ocv_uv - chip->v_cutoff * 1000) / rbatt_mohm; |
| 1727 | } |
| 1728 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1729 | static int bound_soc(int soc) |
| 1730 | { |
| 1731 | soc = max(0, soc); |
| 1732 | soc = min(100, soc); |
| 1733 | return soc; |
| 1734 | } |
| 1735 | |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1736 | static int charging_adjustments(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1737 | int soc, int vbat_uv, int ibat_ua, |
| 1738 | int batt_temp, int chargecycles, |
| 1739 | int fcc_uah, int cc_uah, int uuc_uah) |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1740 | { |
| 1741 | int chg_soc; |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1742 | int vbat_batt_terminal_uv = vbat_uv |
| 1743 | + (ibat_ua * chip->rconn_mohm) / 1000; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1744 | |
| 1745 | if (chip->soc_at_cv == -EINVAL) { |
| 1746 | /* In constant current charging return the calc soc */ |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1747 | if (vbat_batt_terminal_uv <= chip->max_voltage_uv) |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1748 | pr_debug("CC CHG SOC %d\n", soc); |
| 1749 | |
| 1750 | /* Note the CC to CV point */ |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1751 | if (vbat_batt_terminal_uv >= chip->max_voltage_uv) { |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1752 | chip->soc_at_cv = soc; |
| 1753 | chip->prev_chg_soc = soc; |
| 1754 | chip->ibat_at_cv_ua = ibat_ua; |
| 1755 | pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n", |
| 1756 | ibat_ua, soc); |
| 1757 | } |
Abhijeet Dharmapurikar | cbde3db | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1758 | |
| 1759 | chip->prev_vbat_batt_terminal_uv = vbat_batt_terminal_uv; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1760 | return soc; |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * battery is in CV phase - begin liner inerpolation of soc based on |
| 1765 | * battery charge current |
| 1766 | */ |
| 1767 | |
| 1768 | /* |
Abhijeet Dharmapurikar | cbde3db | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1769 | * if the battery terminal voltage lessened (possibly because of |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1770 | * a sudden increase in system load) keep reporting the prev chg soc |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1771 | */ |
Abhijeet Dharmapurikar | cbde3db | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1772 | if (vbat_batt_terminal_uv < chip->prev_vbat_batt_terminal_uv) { |
| 1773 | pr_debug("vbat_terminals %d < prev = %d CC CHG SOC %d\n", |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1774 | vbat_batt_terminal_uv, |
Abhijeet Dharmapurikar | cbde3db | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1775 | chip->prev_vbat_batt_terminal_uv, |
| 1776 | chip->prev_chg_soc); |
| 1777 | chip->prev_vbat_batt_terminal_uv = vbat_batt_terminal_uv; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1778 | return chip->prev_chg_soc; |
| 1779 | } |
| 1780 | |
| 1781 | chg_soc = linear_interpolate(chip->soc_at_cv, chip->ibat_at_cv_ua, |
Abhijeet Dharmapurikar | a82a00a | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1782 | 100, -1 * chip->chg_term_ua, |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1783 | ibat_ua); |
Xiaozhe Shi | 33985a9 | 2012-12-10 13:02:14 -0800 | [diff] [blame] | 1784 | chg_soc = bound_soc(chg_soc); |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1785 | |
| 1786 | /* always report a higher soc */ |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1787 | if (chg_soc > chip->prev_chg_soc) { |
| 1788 | int new_ocv_uv; |
| 1789 | int new_rc; |
| 1790 | |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1791 | chip->prev_chg_soc = chg_soc; |
| 1792 | |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1793 | find_ocv_for_soc(chip, batt_temp, chargecycles, |
| 1794 | fcc_uah, uuc_uah, cc_uah, |
| 1795 | chg_soc, |
| 1796 | &new_rc, &new_ocv_uv); |
| 1797 | the_chip->last_ocv_uv = new_ocv_uv; |
| 1798 | pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n", |
| 1799 | new_ocv_uv, |
| 1800 | chip->prev_chg_soc); |
| 1801 | } |
| 1802 | |
Abhijeet Dharmapurikar | d3c435e | 2012-08-16 19:23:18 -0700 | [diff] [blame] | 1803 | pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc); |
Abhijeet Dharmapurikar | cbde3db | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1804 | chip->prev_vbat_batt_terminal_uv = vbat_batt_terminal_uv; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1805 | return chip->prev_chg_soc; |
| 1806 | } |
| 1807 | |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1808 | static void very_low_voltage_check(struct pm8921_bms_chip *chip, |
| 1809 | int ibat_ua, int vbat_uv) |
| 1810 | { |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 1811 | int rc; |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1812 | /* |
| 1813 | * if battery is very low (v_cutoff voltage + 20mv) hold |
| 1814 | * a wakelock untill soc = 0% |
| 1815 | */ |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 1816 | if (vbat_uv <= (chip->alarm_low_mv + 20) * 1000 && |
| 1817 | !wake_lock_active(&the_chip->low_voltage_wake_lock)) { |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1818 | pr_debug("voltage = %d low holding wakelock\n", vbat_uv); |
| 1819 | wake_lock(&chip->low_voltage_wake_lock); |
Abhijeet Dharmapurikar | 683472d | 2012-10-30 16:21:58 -0700 | [diff] [blame] | 1820 | chip->soc_calc_period = chip->low_voltage_calc_ms; |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 1823 | if (vbat_uv > (chip->alarm_low_mv + 20 + BATT_ALARM_ACCURACY) * 1000 |
| 1824 | && wake_lock_active(&the_chip->low_voltage_wake_lock)) { |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1825 | pr_debug("voltage = %d releasing wakelock\n", vbat_uv); |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 1826 | chip->vbatt_cutoff_count = 0; |
Abhijeet Dharmapurikar | 683472d | 2012-10-30 16:21:58 -0700 | [diff] [blame] | 1827 | chip->soc_calc_period = chip->normal_voltage_calc_ms; |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 1828 | rc = pm8921_bms_enable_batt_alarm(chip); |
| 1829 | if (rc) |
| 1830 | pr_err("Unable to enable batt alarm\n"); |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 1831 | wake_unlock(&chip->low_voltage_wake_lock); |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 1835 | static bool is_voltage_below_cutoff_window(struct pm8921_bms_chip *chip, |
| 1836 | int ibat_ua, int vbat_uv) |
| 1837 | { |
| 1838 | if (vbat_uv < (chip->v_cutoff * 1000) && ibat_ua > 0) { |
| 1839 | chip->vbatt_cutoff_count++; |
| 1840 | if (chip->vbatt_cutoff_count >= chip->vbatt_cutoff_retries) { |
| 1841 | pr_debug("cutoff_count >= %d\n", |
| 1842 | chip->vbatt_cutoff_retries); |
| 1843 | return true; |
| 1844 | } |
| 1845 | } else { |
| 1846 | chip->vbatt_cutoff_count = 0; |
| 1847 | } |
| 1848 | |
| 1849 | return false; |
| 1850 | } |
| 1851 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1852 | static int last_soc_est = -EINVAL; |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1853 | static int adjust_soc(struct pm8921_bms_chip *chip, int soc, |
| 1854 | int batt_temp, int chargecycles, |
| 1855 | int rbatt, int fcc_uah, int uuc_uah, int cc_uah) |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1856 | { |
| 1857 | int ibat_ua = 0, vbat_uv = 0; |
| 1858 | int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0; |
| 1859 | int delta_ocv_uv = 0; |
| 1860 | int n = 0; |
| 1861 | int rc_new_uah = 0; |
| 1862 | int pc_new = 0; |
| 1863 | int soc_new = 0; |
| 1864 | int m = 0; |
Abhijeet Dharmapurikar | b268013 | 2012-05-15 16:32:51 -0700 | [diff] [blame] | 1865 | int rc = 0; |
Abhijeet Dharmapurikar | 720b00e | 2012-08-01 21:33:09 -0700 | [diff] [blame] | 1866 | int delta_ocv_uv_limit = 0; |
Abhijeet Dharmapurikar | 4870761 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1867 | int correction_limit_uv = 0; |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1868 | |
Abhijeet Dharmapurikar | b268013 | 2012-05-15 16:32:51 -0700 | [diff] [blame] | 1869 | rc = pm8921_bms_get_simultaneous_battery_voltage_and_current( |
| 1870 | &ibat_ua, |
| 1871 | &vbat_uv); |
| 1872 | if (rc < 0) { |
| 1873 | pr_err("simultaneous vbat ibat failed err = %d\n", rc); |
| 1874 | goto out; |
| 1875 | } |
| 1876 | |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 1877 | very_low_voltage_check(chip, ibat_ua, vbat_uv); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1878 | |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 1879 | if (chip->low_voltage_detect && |
| 1880 | wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 1881 | if (is_voltage_below_cutoff_window(chip, ibat_ua, vbat_uv)) { |
| 1882 | soc = 0; |
| 1883 | pr_info("Voltage below cutoff, setting soc to 0\n"); |
| 1884 | goto out; |
| 1885 | } |
| 1886 | } |
| 1887 | |
Abhijeet Dharmapurikar | 720b00e | 2012-08-01 21:33:09 -0700 | [diff] [blame] | 1888 | delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000); |
| 1889 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1890 | ocv_est_uv = vbat_uv + (ibat_ua * rbatt)/1000; |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 1891 | calc_current_max(chip, ocv_est_uv, rbatt); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1892 | pc_est = calculate_pc(chip, ocv_est_uv, batt_temp, last_chargecycles); |
| 1893 | soc_est = div_s64((s64)fcc_uah * pc_est - uuc_uah*100, |
| 1894 | (s64)fcc_uah - uuc_uah); |
| 1895 | soc_est = bound_soc(soc_est); |
| 1896 | |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 1897 | /* never adjust during bms reset mode */ |
| 1898 | if (bms_reset) { |
| 1899 | pr_debug("bms reset mode, SOC adjustment skipped\n"); |
| 1900 | goto out; |
| 1901 | } |
| 1902 | |
Abhijeet Dharmapurikar | 552df98 | 2012-10-23 19:13:45 -0700 | [diff] [blame] | 1903 | if (ibat_ua < 0 && pm8921_is_batfet_closed()) { |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 1904 | soc = charging_adjustments(chip, soc, vbat_uv, ibat_ua, |
| 1905 | batt_temp, chargecycles, |
| 1906 | fcc_uah, cc_uah, uuc_uah); |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 1907 | goto out; |
| 1908 | } |
| 1909 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1910 | /* |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1911 | * do not adjust |
Abhijeet Dharmapurikar | 321bcb1 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 1912 | * if soc_est is same as what bms calculated |
| 1913 | * OR if soc_est > 15 |
| 1914 | * OR if soc it is above 90 because we might pull it low |
Abhijeet Dharmapurikar | 52daf65 | 2012-07-19 11:50:47 -0700 | [diff] [blame] | 1915 | * and cause a bad user experience |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1916 | */ |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 1917 | if (soc_est == soc |
Abhijeet Dharmapurikar | 321bcb1 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 1918 | || soc_est > 15 |
Abhijeet Dharmapurikar | 52daf65 | 2012-07-19 11:50:47 -0700 | [diff] [blame] | 1919 | || soc >= 90) |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1920 | goto out; |
| 1921 | |
| 1922 | if (last_soc_est == -EINVAL) |
| 1923 | last_soc_est = soc; |
| 1924 | |
| 1925 | n = min(200, max(1 , soc + soc_est + last_soc_est)); |
| 1926 | /* remember the last soc_est in last_soc_est */ |
| 1927 | last_soc_est = soc_est; |
| 1928 | |
| 1929 | pc = calculate_pc(chip, chip->last_ocv_uv, |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1930 | chip->last_ocv_temp_decidegc, last_chargecycles); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1931 | if (pc > 0) { |
| 1932 | pc_new = calculate_pc(chip, chip->last_ocv_uv - (++m * 1000), |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1933 | chip->last_ocv_temp_decidegc, |
| 1934 | last_chargecycles); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1935 | while (pc_new == pc) { |
| 1936 | /* start taking 10mV steps */ |
| 1937 | m = m + 10; |
| 1938 | pc_new = calculate_pc(chip, |
| 1939 | chip->last_ocv_uv - (m * 1000), |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1940 | chip->last_ocv_temp_decidegc, |
| 1941 | last_chargecycles); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1942 | } |
| 1943 | } else { |
| 1944 | /* |
| 1945 | * pc is already at the lowest point, |
| 1946 | * assume 1 millivolt translates to 1% pc |
| 1947 | */ |
| 1948 | pc = 1; |
| 1949 | pc_new = 0; |
| 1950 | m = 1; |
| 1951 | } |
| 1952 | |
| 1953 | delta_ocv_uv = div_s64((soc - soc_est) * (s64)m * 1000, |
| 1954 | n * (pc - pc_new)); |
Abhijeet Dharmapurikar | 720b00e | 2012-08-01 21:33:09 -0700 | [diff] [blame] | 1955 | |
| 1956 | if (abs(delta_ocv_uv) > delta_ocv_uv_limit) { |
| 1957 | pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv, |
| 1958 | delta_ocv_uv_limit); |
| 1959 | |
| 1960 | if (delta_ocv_uv > 0) |
| 1961 | delta_ocv_uv = delta_ocv_uv_limit; |
| 1962 | else |
| 1963 | delta_ocv_uv = -1 * delta_ocv_uv_limit; |
| 1964 | pr_debug("new delta ocv = %d\n", delta_ocv_uv); |
| 1965 | } |
| 1966 | |
Abhijeet Dharmapurikar | 4870761 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1967 | if (chip->last_ocv_uv > 3800000) |
| 1968 | correction_limit_uv = the_chip->high_ocv_correction_limit_uv; |
| 1969 | else |
| 1970 | correction_limit_uv = the_chip->low_ocv_correction_limit_uv; |
| 1971 | |
| 1972 | if (abs(delta_ocv_uv) > correction_limit_uv) { |
| 1973 | pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv, |
| 1974 | correction_limit_uv); |
| 1975 | |
| 1976 | if (delta_ocv_uv > 0) |
| 1977 | delta_ocv_uv = correction_limit_uv; |
| 1978 | else |
| 1979 | delta_ocv_uv = -1 * correction_limit_uv; |
| 1980 | pr_debug("new delta ocv = %d\n", delta_ocv_uv); |
| 1981 | } |
| 1982 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1983 | chip->last_ocv_uv -= delta_ocv_uv; |
| 1984 | |
| 1985 | if (chip->last_ocv_uv >= chip->max_voltage_uv) |
| 1986 | chip->last_ocv_uv = chip->max_voltage_uv; |
| 1987 | |
| 1988 | /* calculate the soc based on this new ocv */ |
| 1989 | pc_new = calculate_pc(chip, chip->last_ocv_uv, |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1990 | chip->last_ocv_temp_decidegc, last_chargecycles); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 1991 | rc_new_uah = (fcc_uah * pc_new) / 100; |
| 1992 | soc_new = (rc_new_uah - cc_uah - uuc_uah)*100 / (fcc_uah - uuc_uah); |
| 1993 | soc_new = bound_soc(soc_new); |
| 1994 | |
| 1995 | /* |
| 1996 | * if soc_new is ZERO force it higher so that phone doesnt report soc=0 |
| 1997 | * soc = 0 should happen only when soc_est == 0 |
| 1998 | */ |
Abhijeet Dharmapurikar | 4870761 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1999 | if (soc_new == 0 && soc_est >= the_chip->hold_soc_est) |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2000 | soc_new = 1; |
| 2001 | |
| 2002 | soc = soc_new; |
| 2003 | |
| 2004 | out: |
| 2005 | pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, " |
| 2006 | "soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, " |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 2007 | "pc_new = %d, soc_new = %d, rbatt = %d, m = %d\n", |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2008 | ibat_ua, vbat_uv, ocv_est_uv, pc_est, |
| 2009 | soc_est, n, delta_ocv_uv, chip->last_ocv_uv, |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 2010 | pc_new, soc_new, rbatt, m); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2011 | |
| 2012 | return soc; |
| 2013 | } |
| 2014 | |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 2015 | #define IGNORE_SOC_TEMP_DECIDEG 50 |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2016 | #define IAVG_STEP_SIZE_MA 50 |
| 2017 | #define IAVG_START 600 |
| 2018 | #define SOC_ZERO 0xFF |
| 2019 | static void backup_soc_and_iavg(struct pm8921_bms_chip *chip, int batt_temp, |
| 2020 | int soc) |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2021 | { |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2022 | u8 temp; |
| 2023 | int iavg_ma = chip->prev_uuc_iavg_ma; |
| 2024 | |
| 2025 | if (iavg_ma > IAVG_START) |
| 2026 | temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA; |
| 2027 | else |
| 2028 | temp = 0; |
| 2029 | |
| 2030 | pm_bms_masked_write(chip, TEMP_IAVG_STORAGE, |
| 2031 | TEMP_IAVG_STORAGE_USE_MASK, temp); |
| 2032 | |
| 2033 | /* since only 6 bits are available for SOC, we store half the soc */ |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 2034 | if (soc == 0) |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2035 | temp = SOC_ZERO; |
| 2036 | else |
| 2037 | temp = soc; |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2038 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2039 | /* don't store soc if temperature is below 5degC */ |
| 2040 | if (batt_temp > IGNORE_SOC_TEMP_DECIDEG) |
| 2041 | pm8xxx_writeb(the_chip->dev->parent, TEMP_SOC_STORAGE, temp); |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2044 | static void read_shutdown_soc_and_iavg(struct pm8921_bms_chip *chip) |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2045 | { |
| 2046 | int rc; |
| 2047 | u8 temp; |
| 2048 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2049 | rc = pm8xxx_readb(chip->dev->parent, TEMP_IAVG_STORAGE, &temp); |
| 2050 | if (rc) { |
| 2051 | pr_err("failed to read addr = %d %d assuming %d\n", |
| 2052 | TEMP_IAVG_STORAGE, rc, IAVG_START); |
| 2053 | chip->shutdown_iavg_ua = IAVG_START; |
| 2054 | } else { |
| 2055 | temp &= TEMP_IAVG_STORAGE_USE_MASK; |
| 2056 | |
| 2057 | if (temp == 0) { |
| 2058 | chip->shutdown_iavg_ua = IAVG_START; |
| 2059 | } else { |
| 2060 | chip->shutdown_iavg_ua = IAVG_START |
| 2061 | + IAVG_STEP_SIZE_MA * (temp + 1); |
| 2062 | } |
| 2063 | } |
| 2064 | |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2065 | rc = pm8xxx_readb(chip->dev->parent, TEMP_SOC_STORAGE, &temp); |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2066 | if (rc) { |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2067 | pr_err("failed to read addr = %d %d\n", TEMP_SOC_STORAGE, rc); |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2068 | } else { |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2069 | chip->shutdown_soc = temp; |
| 2070 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2071 | if (chip->shutdown_soc == 0) { |
| 2072 | pr_debug("No shutdown soc available\n"); |
| 2073 | shutdown_soc_invalid = 1; |
| 2074 | chip->shutdown_iavg_ua = 0; |
| 2075 | } else if (chip->shutdown_soc == SOC_ZERO) { |
| 2076 | chip->shutdown_soc = 0; |
| 2077 | } |
| 2078 | } |
Abhijeet Dharmapurikar | 1f4cc51 | 2012-07-21 00:10:39 -0700 | [diff] [blame] | 2079 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2080 | if (chip->ignore_shutdown_soc) { |
| 2081 | shutdown_soc_invalid = 1; |
| 2082 | chip->shutdown_soc = 0; |
| 2083 | chip->shutdown_iavg_ua = 0; |
| 2084 | } |
| 2085 | |
| 2086 | pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d\n", |
| 2087 | chip->shutdown_soc, |
| 2088 | chip->shutdown_iavg_ua, |
| 2089 | shutdown_soc_invalid); |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2092 | #define SOC_CATCHUP_SEC_MAX 600 |
| 2093 | #define SOC_CATCHUP_SEC_PER_PERCENT 60 |
| 2094 | #define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX/SOC_CATCHUP_SEC_PER_PERCENT) |
| 2095 | static int scale_soc_while_chg(struct pm8921_bms_chip *chip, |
| 2096 | int delta_time_us, int new_soc, int prev_soc) |
| 2097 | { |
| 2098 | int chg_time_sec; |
| 2099 | int catch_up_sec; |
| 2100 | int scaled_soc; |
| 2101 | int numerator; |
| 2102 | |
| 2103 | /* |
| 2104 | * The device must be charging for reporting a higher soc, if |
| 2105 | * not ignore this soc and continue reporting the prev_soc. |
| 2106 | * Also don't report a high value immediately slowly scale the |
| 2107 | * value from prev_soc to the new soc based on a charge time |
| 2108 | * weighted average |
| 2109 | */ |
| 2110 | |
| 2111 | /* if we are not charging return last soc */ |
| 2112 | if (the_chip->start_percent == -EINVAL) |
| 2113 | return prev_soc; |
| 2114 | |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2115 | chg_time_sec = DIV_ROUND_UP(the_chip->charge_time_us, USEC_PER_SEC); |
| 2116 | catch_up_sec = DIV_ROUND_UP(the_chip->catch_up_time_us, USEC_PER_SEC); |
Xiaozhe Shi | d3ce986 | 2012-12-17 19:34:50 -0800 | [diff] [blame] | 2117 | if (catch_up_sec == 0) |
| 2118 | return new_soc; |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2119 | pr_debug("cts= %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec); |
| 2120 | |
| 2121 | /* |
| 2122 | * if we have been charging for more than catch_up time simply return |
| 2123 | * new soc |
| 2124 | */ |
| 2125 | if (chg_time_sec > catch_up_sec) |
| 2126 | return new_soc; |
| 2127 | |
| 2128 | numerator = (catch_up_sec - chg_time_sec) * prev_soc |
| 2129 | + chg_time_sec * new_soc; |
| 2130 | scaled_soc = numerator / catch_up_sec; |
| 2131 | |
| 2132 | pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n", |
| 2133 | chg_time_sec, new_soc, prev_soc, scaled_soc); |
| 2134 | |
| 2135 | return scaled_soc; |
| 2136 | } |
| 2137 | |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2138 | static bool is_shutdown_soc_within_limits(struct pm8921_bms_chip *chip, int soc) |
| 2139 | { |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2140 | if (shutdown_soc_invalid) { |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2141 | pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc); |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2145 | if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) { |
| 2146 | pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n", |
| 2147 | chip->shutdown_soc, soc, |
| 2148 | chip->shutdown_soc_valid_limit); |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2149 | shutdown_soc_invalid = 1; |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | return 1; |
| 2154 | } |
Abhijeet Dharmapurikar | 0622a43 | 2012-08-28 16:34:18 -0700 | [diff] [blame] | 2155 | |
| 2156 | static void update_power_supply(struct pm8921_bms_chip *chip) |
| 2157 | { |
| 2158 | if (chip->batt_psy == NULL || chip->batt_psy < 0) |
| 2159 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 2160 | |
| 2161 | if (chip->batt_psy > 0) |
| 2162 | power_supply_changed(chip->batt_psy); |
| 2163 | } |
| 2164 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2165 | #define MIN_DELTA_625_UV 1000 |
| 2166 | static void calib_hkadc(struct pm8921_bms_chip *chip) |
| 2167 | { |
| 2168 | int voltage, rc; |
| 2169 | struct pm8xxx_adc_chan_result result; |
| 2170 | int usb_chg; |
| 2171 | int this_delta; |
| 2172 | |
| 2173 | mutex_lock(&chip->calib_mutex); |
| 2174 | rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result); |
| 2175 | if (rc) { |
| 2176 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 2177 | goto out; |
| 2178 | } |
| 2179 | voltage = xoadc_reading_to_microvolt(result.adc_code); |
| 2180 | |
| 2181 | pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n", |
| 2182 | result.adc_code, voltage, result.measurement); |
| 2183 | |
| 2184 | chip->xoadc_v125 = voltage; |
| 2185 | |
| 2186 | rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result); |
| 2187 | if (rc) { |
| 2188 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 2189 | goto out; |
| 2190 | } |
| 2191 | voltage = xoadc_reading_to_microvolt(result.adc_code); |
| 2192 | |
| 2193 | usb_chg = usb_chg_plugged_in(chip); |
| 2194 | pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld usb_chg = %d\n", |
| 2195 | result.adc_code, voltage, result.measurement, |
| 2196 | usb_chg); |
| 2197 | |
| 2198 | if (usb_chg) |
| 2199 | chip->xoadc_v0625_usb_present = voltage; |
| 2200 | else |
| 2201 | chip->xoadc_v0625_usb_absent = voltage; |
| 2202 | |
| 2203 | chip->xoadc_v0625 = voltage; |
| 2204 | if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) { |
| 2205 | this_delta = chip->xoadc_v0625_usb_present |
| 2206 | - chip->xoadc_v0625_usb_absent; |
| 2207 | pr_debug("this_delta= %duV\n", this_delta); |
| 2208 | if (this_delta > MIN_DELTA_625_UV) |
| 2209 | last_usb_cal_delta_uv = this_delta; |
| 2210 | pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n", |
| 2211 | chip->xoadc_v0625_usb_present, |
| 2212 | chip->xoadc_v0625_usb_absent, |
| 2213 | last_usb_cal_delta_uv); |
| 2214 | } |
| 2215 | pr_debug("calibration batt_temp = %d\n", chip->last_calib_temp); |
| 2216 | out: |
| 2217 | mutex_unlock(&chip->calib_mutex); |
| 2218 | } |
| 2219 | |
| 2220 | #define HKADC_CALIB_DELAY_S 600 |
| 2221 | #define HKADC_CALIB_DELTA_TEMP 20 |
| 2222 | static void calib_hkadc_check(struct pm8921_bms_chip *chip, int batt_temp) |
| 2223 | { |
| 2224 | unsigned long time_since_last_calib; |
| 2225 | unsigned long tm_now_sec; |
| 2226 | int delta_temp; |
| 2227 | int rc; |
| 2228 | |
| 2229 | rc = get_current_time(&tm_now_sec); |
| 2230 | if (rc) { |
| 2231 | pr_err("Could not read current time: %d\n", rc); |
| 2232 | return; |
| 2233 | } |
| 2234 | if (tm_now_sec > chip->last_calib_time) { |
| 2235 | time_since_last_calib = tm_now_sec - chip->last_calib_time; |
| 2236 | delta_temp = abs(chip->last_calib_temp - batt_temp); |
| 2237 | pr_debug("time since last calib: %lu, temp diff = %d\n", |
| 2238 | time_since_last_calib, delta_temp); |
| 2239 | if (time_since_last_calib >= HKADC_CALIB_DELAY_S |
| 2240 | || delta_temp > HKADC_CALIB_DELTA_TEMP) { |
| 2241 | chip->last_calib_temp = batt_temp; |
| 2242 | chip->last_calib_time = tm_now_sec; |
| 2243 | calib_hkadc(chip); |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 2248 | /* |
| 2249 | * Remaining Usable Charge = remaining_charge (charge at ocv instance) |
| 2250 | * - coloumb counter charge |
| 2251 | * - unusable charge (due to battery resistance) |
| 2252 | * SOC% = (remaining usable charge/ fcc - usable_charge); |
| 2253 | */ |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 2254 | static int calculate_state_of_charge(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2255 | struct pm8921_soc_params *raw, |
| 2256 | int batt_temp, int chargecycles) |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 2257 | { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2258 | int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah; |
| 2259 | int remaining_charge_uah, soc; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2260 | int cc_uah; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2261 | int rbatt; |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 2262 | int iavg_ua; |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2263 | int new_ocv; |
Abhijeet Dharmapurikar | 6f42a23 | 2012-07-17 22:11:13 -0700 | [diff] [blame] | 2264 | int new_rc_uah; |
| 2265 | int new_ucc_uah; |
| 2266 | int new_rbatt; |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2267 | int shutdown_soc; |
Abhijeet Dharmapurikar | 0622a43 | 2012-08-28 16:34:18 -0700 | [diff] [blame] | 2268 | int new_calculated_soc; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 2269 | static int firsttime = 1; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 2270 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2271 | calib_hkadc_check(chip, batt_temp); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2272 | calculate_soc_params(chip, raw, batt_temp, chargecycles, |
| 2273 | &fcc_uah, |
| 2274 | &unusable_charge_uah, |
| 2275 | &remaining_charge_uah, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2276 | &cc_uah, |
Abhijeet Dharmapurikar | 30f30f7 | 2012-04-26 17:34:18 -0700 | [diff] [blame] | 2277 | &rbatt, |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 2278 | &iavg_ua); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2279 | |
| 2280 | /* calculate remaining usable charge */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2281 | remaining_usable_charge_uah = remaining_charge_uah |
| 2282 | - cc_uah |
| 2283 | - unusable_charge_uah; |
| 2284 | |
| 2285 | pr_debug("RUC = %duAh\n", remaining_usable_charge_uah); |
Abhijeet Dharmapurikar | bbae8831 | 2012-02-14 18:56:42 -0800 | [diff] [blame] | 2286 | if (fcc_uah - unusable_charge_uah <= 0) { |
Xiaozhe Shi | eec91df | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 2287 | pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n", |
Abhijeet Dharmapurikar | bbae8831 | 2012-02-14 18:56:42 -0800 | [diff] [blame] | 2288 | fcc_uah, unusable_charge_uah); |
| 2289 | soc = 0; |
| 2290 | } else { |
Abhijeet Dharmapurikar | 4ff213c | 2012-07-11 21:28:29 -0700 | [diff] [blame] | 2291 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 2292 | (fcc_uah - unusable_charge_uah)); |
Abhijeet Dharmapurikar | bbae8831 | 2012-02-14 18:56:42 -0800 | [diff] [blame] | 2293 | } |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2294 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 2295 | if (firsttime && soc < 0) { |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2296 | /* |
| 2297 | * first time calcualtion and the pon ocv is too low resulting |
| 2298 | * in a bad soc. Adjust ocv such that we get 0 soc |
| 2299 | */ |
| 2300 | pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc); |
| 2301 | adjust_rc_and_uuc_for_specific_soc( |
| 2302 | chip, |
| 2303 | batt_temp, chargecycles, |
| 2304 | 0, |
| 2305 | fcc_uah, unusable_charge_uah, |
| 2306 | cc_uah, remaining_charge_uah, |
| 2307 | rbatt, |
| 2308 | &new_ocv, |
| 2309 | &new_rc_uah, &new_ucc_uah, |
| 2310 | &new_rbatt); |
| 2311 | chip->last_ocv_uv = new_ocv; |
| 2312 | remaining_charge_uah = new_rc_uah; |
| 2313 | unusable_charge_uah = new_ucc_uah; |
| 2314 | rbatt = new_rbatt; |
| 2315 | |
| 2316 | remaining_usable_charge_uah = remaining_charge_uah |
| 2317 | - cc_uah |
| 2318 | - unusable_charge_uah; |
| 2319 | |
| 2320 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 2321 | (fcc_uah - unusable_charge_uah)); |
| 2322 | pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n", |
| 2323 | soc, chip->last_ocv_uv); |
| 2324 | } |
| 2325 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 2326 | if (soc > 100) |
| 2327 | soc = 100; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 2328 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 2329 | if (soc < 0) { |
Xiaozhe Shi | eec91df | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 2330 | pr_debug("bad rem_usb_chg = %d rem_chg %d," |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2331 | "cc_uah %d, unusb_chg %d\n", |
| 2332 | remaining_usable_charge_uah, |
| 2333 | remaining_charge_uah, |
| 2334 | cc_uah, unusable_charge_uah); |
| 2335 | |
Xiaozhe Shi | eec91df | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 2336 | pr_debug("for bad rem_usb_chg last_ocv_uv = %d" |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 2337 | "chargecycles = %d, batt_temp = %d" |
| 2338 | "fcc = %d soc =%d\n", |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2339 | chip->last_ocv_uv, chargecycles, batt_temp, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2340 | fcc_uah, soc); |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 2341 | soc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2344 | mutex_lock(&soc_invalidation_mutex); |
| 2345 | shutdown_soc = chip->shutdown_soc; |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2346 | |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 2347 | if (firsttime && soc != shutdown_soc |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2348 | && is_shutdown_soc_within_limits(chip, soc)) { |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2349 | /* |
| 2350 | * soc for the first time - use shutdown soc |
| 2351 | * to adjust pon ocv since it is a small percent away from |
| 2352 | * the real soc |
| 2353 | */ |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2354 | pr_debug("soc = %d before forcing shutdown_soc = %d\n", |
| 2355 | soc, shutdown_soc); |
| 2356 | adjust_rc_and_uuc_for_specific_soc( |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2357 | chip, |
| 2358 | batt_temp, chargecycles, |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2359 | shutdown_soc, |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2360 | fcc_uah, unusable_charge_uah, |
| 2361 | cc_uah, remaining_charge_uah, |
| 2362 | rbatt, |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2363 | &new_ocv, |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2364 | &new_rc_uah, &new_ucc_uah, |
| 2365 | &new_rbatt); |
| 2366 | |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2367 | chip->pon_ocv_uv = chip->last_ocv_uv; |
| 2368 | chip->last_ocv_uv = new_ocv; |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2369 | remaining_charge_uah = new_rc_uah; |
| 2370 | unusable_charge_uah = new_ucc_uah; |
| 2371 | rbatt = new_rbatt; |
| 2372 | |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2373 | remaining_usable_charge_uah = remaining_charge_uah |
| 2374 | - cc_uah |
| 2375 | - unusable_charge_uah; |
| 2376 | |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2377 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 2378 | (fcc_uah - unusable_charge_uah)); |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2379 | |
| 2380 | pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n", |
| 2381 | shutdown_soc, soc, chip->last_ocv_uv); |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2382 | } |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2383 | mutex_unlock(&soc_invalidation_mutex); |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2384 | |
Abhijeet Dharmapurikar | 0eb50ee | 2012-08-14 13:26:48 -0700 | [diff] [blame] | 2385 | pr_debug("SOC before adjustment = %d\n", soc); |
Abhijeet Dharmapurikar | 0622a43 | 2012-08-28 16:34:18 -0700 | [diff] [blame] | 2386 | new_calculated_soc = adjust_soc(chip, soc, batt_temp, chargecycles, |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 2387 | rbatt, fcc_uah, unusable_charge_uah, cc_uah); |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 2388 | |
Abhijeet Dharmapurikar | 0622a43 | 2012-08-28 16:34:18 -0700 | [diff] [blame] | 2389 | pr_debug("calculated SOC = %d\n", new_calculated_soc); |
| 2390 | if (new_calculated_soc != calculated_soc) |
| 2391 | update_power_supply(chip); |
| 2392 | |
| 2393 | calculated_soc = new_calculated_soc; |
Abhijeet Dharmapurikar | 1658215 | 2012-07-14 19:55:17 -0700 | [diff] [blame] | 2394 | firsttime = 0; |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2395 | get_current_time(&chip->last_recalc_time); |
Abhijeet Dharmapurikar | 1939ede | 2013-01-10 15:59:33 -0800 | [diff] [blame] | 2396 | |
| 2397 | if (chip->disable_flat_portion_ocv) { |
| 2398 | if (is_between(chip->ocv_dis_high_soc, chip->ocv_dis_low_soc, |
| 2399 | calculated_soc)) { |
| 2400 | pm8921_bms_stop_ocv_updates(); |
| 2401 | } else { |
| 2402 | pm8921_bms_start_ocv_updates(); |
| 2403 | } |
| 2404 | } |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2405 | return calculated_soc; |
| 2406 | } |
| 2407 | |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2408 | static int recalculate_soc(struct pm8921_bms_chip *chip) |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2409 | { |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2410 | int batt_temp; |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2411 | struct pm8921_soc_params raw; |
| 2412 | int soc; |
| 2413 | |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2414 | wake_lock(&the_chip->soc_wake_lock); |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2415 | get_batt_temp(chip, &batt_temp); |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2416 | |
| 2417 | mutex_lock(&chip->last_ocv_uv_mutex); |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2418 | read_soc_params_raw(chip, &raw, batt_temp); |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2419 | |
| 2420 | soc = calculate_state_of_charge(chip, &raw, |
| 2421 | batt_temp, last_chargecycles); |
| 2422 | mutex_unlock(&chip->last_ocv_uv_mutex); |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2423 | wake_unlock(&the_chip->soc_wake_lock); |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2424 | return soc; |
| 2425 | } |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2426 | |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2427 | static void calculate_soc_work(struct work_struct *work) |
| 2428 | { |
Willie Ruan | 38760d3 | 2013-01-14 15:46:01 -0800 | [diff] [blame] | 2429 | struct delayed_work *dwork = to_delayed_work(work); |
| 2430 | struct pm8921_bms_chip *chip = container_of(dwork, |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2431 | struct pm8921_bms_chip, |
Willie Ruan | 38760d3 | 2013-01-14 15:46:01 -0800 | [diff] [blame] | 2432 | calculate_soc_delayed_work); |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2433 | |
| 2434 | recalculate_soc(chip); |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2435 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 2436 | round_jiffies_relative(msecs_to_jiffies |
Abhijeet Dharmapurikar | 683472d | 2012-10-30 16:21:58 -0700 | [diff] [blame] | 2437 | (chip->soc_calc_period))); |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | static int report_state_of_charge(struct pm8921_bms_chip *chip) |
| 2441 | { |
| 2442 | int soc = calculated_soc; |
| 2443 | int delta_time_us; |
| 2444 | struct timespec now; |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2445 | int batt_temp; |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2446 | |
Abhijeet Dharmapurikar | d02bd88 | 2012-08-22 17:48:16 -0700 | [diff] [blame] | 2447 | if (bms_fake_battery != -EINVAL) { |
| 2448 | pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery); |
| 2449 | return bms_fake_battery; |
| 2450 | } |
| 2451 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2452 | get_batt_temp(chip, &batt_temp); |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2453 | |
| 2454 | do_posix_clock_monotonic_gettime(&now); |
| 2455 | if (chip->t_soc_queried.tv_sec != 0) { |
| 2456 | delta_time_us |
| 2457 | = (now.tv_sec - chip->t_soc_queried.tv_sec) * USEC_PER_SEC |
| 2458 | + (now.tv_nsec - chip->t_soc_queried.tv_nsec) / 1000; |
| 2459 | } else { |
| 2460 | /* calculation for the first time */ |
| 2461 | delta_time_us = 0; |
| 2462 | } |
| 2463 | |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2464 | /* |
| 2465 | * account for charge time - limit it to SOC_CATCHUP_SEC to |
| 2466 | * avoid overflows when charging continues for extended periods |
| 2467 | */ |
| 2468 | if (the_chip->start_percent != -EINVAL) { |
| 2469 | if (the_chip->charge_time_us == 0) { |
| 2470 | /* |
| 2471 | * calculating soc for the first time |
| 2472 | * after start of chg. Initialize catchup time |
| 2473 | */ |
| 2474 | if (abs(soc - last_soc) < MAX_CATCHUP_SOC) |
| 2475 | the_chip->catch_up_time_us = |
| 2476 | (soc - last_soc) * SOC_CATCHUP_SEC_PER_PERCENT |
| 2477 | * USEC_PER_SEC; |
| 2478 | else |
| 2479 | the_chip->catch_up_time_us = |
| 2480 | SOC_CATCHUP_SEC_MAX * USEC_PER_SEC; |
| 2481 | |
| 2482 | if (the_chip->catch_up_time_us < 0) |
| 2483 | the_chip->catch_up_time_us = 0; |
| 2484 | } |
| 2485 | |
| 2486 | /* add charge time */ |
| 2487 | if (the_chip->charge_time_us |
| 2488 | < SOC_CATCHUP_SEC_MAX * USEC_PER_SEC) |
| 2489 | chip->charge_time_us += delta_time_us; |
| 2490 | |
| 2491 | /* end catchup if calculated soc and last soc are same */ |
| 2492 | if (last_soc == soc) |
| 2493 | the_chip->catch_up_time_us = 0; |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2496 | /* last_soc < soc ... scale and catch up */ |
Xiaozhe Shi | d3ce986 | 2012-12-17 19:34:50 -0800 | [diff] [blame] | 2497 | if (last_soc != -EINVAL && last_soc < soc && soc != 100) |
Abhijeet Dharmapurikar | 1ed9418 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 2498 | soc = scale_soc_while_chg(chip, delta_time_us, |
| 2499 | soc, last_soc); |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2500 | |
Abhijeet Dharmapurikar | 1ed9418 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 2501 | /* restrict soc to 1% change */ |
| 2502 | if (last_soc != -EINVAL) { |
| 2503 | if (soc < last_soc && soc != 0) |
| 2504 | soc = last_soc - 1; |
| 2505 | if (soc > last_soc && soc != 100) |
| 2506 | soc = last_soc + 1; |
| 2507 | } |
| 2508 | |
| 2509 | last_soc = bound_soc(soc); |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2510 | backup_soc_and_iavg(chip, batt_temp, last_soc); |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 2511 | pr_debug("Reported SOC = %d\n", last_soc); |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2512 | chip->t_soc_queried = now; |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 2513 | |
| 2514 | return last_soc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2515 | } |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 2516 | |
Abhijeet Dharmapurikar | bd5e508 | 2013-03-25 14:21:27 -0700 | [diff] [blame] | 2517 | void pm8921_bms_battery_removed(void) |
| 2518 | { |
| 2519 | if (!the_chip) { |
| 2520 | pr_err("called before initialization\n"); |
| 2521 | return; |
| 2522 | } |
| 2523 | pr_info("Battery Removed Cleaning up\n"); |
| 2524 | |
| 2525 | cancel_delayed_work_sync(&the_chip->calculate_soc_delayed_work); |
| 2526 | calculated_soc = 0; |
| 2527 | the_chip->start_percent = -EINVAL; |
| 2528 | the_chip->end_percent = -EINVAL; |
| 2529 | /* cleanup for charge time catchup */ |
| 2530 | the_chip->charge_time_us = 0; |
| 2531 | the_chip->catch_up_time_us = 0; |
| 2532 | /* cleanup for charge time adjusting */ |
| 2533 | the_chip->soc_at_cv = -EINVAL; |
| 2534 | the_chip->soc_at_cv = -EINVAL; |
| 2535 | the_chip->prev_chg_soc = -EINVAL; |
| 2536 | the_chip->ibat_at_cv_ua = 0; |
| 2537 | the_chip->prev_vbat_batt_terminal_uv = 0; |
| 2538 | /* ocv cleanups */ |
| 2539 | the_chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
| 2540 | the_chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED; |
| 2541 | the_chip->last_ocv_temp_decidegc = -EINVAL; |
| 2542 | |
| 2543 | /* cleanup delta time */ |
| 2544 | the_chip->tm_sec = 0; |
| 2545 | |
| 2546 | /* cc and avg current cleanups */ |
| 2547 | the_chip->prev_iavg_ua = 0; |
| 2548 | the_chip->last_cc_uah = INT_MIN; |
| 2549 | |
| 2550 | /* report SOC cleanups */ |
| 2551 | the_chip->t_soc_queried.tv_sec = 0; |
| 2552 | the_chip->t_soc_queried.tv_nsec = 0; |
| 2553 | |
| 2554 | last_soc = -EINVAL; |
| 2555 | /* store invalid soc */ |
| 2556 | pm8xxx_writeb(the_chip->dev->parent, TEMP_SOC_STORAGE, 0); |
| 2557 | |
| 2558 | /* UUC related data is left as is - use the same historical load avg */ |
| 2559 | update_power_supply(the_chip); |
| 2560 | } |
| 2561 | EXPORT_SYMBOL(pm8921_bms_battery_removed); |
| 2562 | |
| 2563 | void pm8921_bms_battery_inserted(void) |
| 2564 | { |
| 2565 | if (!the_chip) { |
| 2566 | pr_err("called before initialization\n"); |
| 2567 | return; |
| 2568 | } |
| 2569 | |
| 2570 | pr_info("Battery Inserted\n"); |
| 2571 | the_chip->last_ocv_uv = estimate_ocv(the_chip); |
| 2572 | schedule_delayed_work(&the_chip->calculate_soc_delayed_work, 0); |
| 2573 | } |
| 2574 | EXPORT_SYMBOL(pm8921_bms_battery_inserted); |
| 2575 | |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2576 | void pm8921_bms_invalidate_shutdown_soc(void) |
| 2577 | { |
| 2578 | int calculate_soc = 0; |
| 2579 | struct pm8921_bms_chip *chip = the_chip; |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2580 | |
| 2581 | pr_debug("Invalidating shutdown soc - the battery was removed\n"); |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2582 | if (shutdown_soc_invalid) |
| 2583 | return; |
| 2584 | |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2585 | mutex_lock(&soc_invalidation_mutex); |
| 2586 | shutdown_soc_invalid = 1; |
| 2587 | last_soc = -EINVAL; |
| 2588 | if (the_chip) { |
| 2589 | /* reset to pon ocv undoing what the adjusting did */ |
| 2590 | if (the_chip->pon_ocv_uv) { |
| 2591 | the_chip->last_ocv_uv = the_chip->pon_ocv_uv; |
| 2592 | calculate_soc = 1; |
| 2593 | pr_debug("resetting ocv to pon_ocv = %d\n", |
| 2594 | the_chip->pon_ocv_uv); |
| 2595 | } |
| 2596 | } |
| 2597 | mutex_unlock(&soc_invalidation_mutex); |
| 2598 | if (!calculate_soc) |
| 2599 | return; |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 2600 | recalculate_soc(chip); |
Abhijeet Dharmapurikar | d74be6b | 2012-07-21 00:02:42 -0700 | [diff] [blame] | 2601 | } |
| 2602 | EXPORT_SYMBOL(pm8921_bms_invalidate_shutdown_soc); |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 2603 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2604 | static void calibrate_hkadc_work(struct work_struct *work) |
| 2605 | { |
| 2606 | struct pm8921_bms_chip *chip = container_of(work, |
| 2607 | struct pm8921_bms_chip, calib_hkadc_work); |
| 2608 | |
| 2609 | calib_hkadc(chip); |
| 2610 | } |
| 2611 | |
Abhijeet Dharmapurikar | 1b8e829 | 2012-01-17 11:01:44 -0800 | [diff] [blame] | 2612 | void pm8921_bms_calibrate_hkadc(void) |
| 2613 | { |
| 2614 | schedule_work(&the_chip->calib_hkadc_work); |
| 2615 | } |
| 2616 | |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 2617 | int pm8921_bms_get_vsense_avg(int *result) |
| 2618 | { |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2619 | int rc = -EINVAL; |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2620 | |
| 2621 | if (the_chip) { |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2622 | mutex_lock(&the_chip->bms_output_lock); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2623 | pm_bms_lock_output_data(the_chip); |
| 2624 | rc = read_vsense_avg(the_chip, result); |
| 2625 | pm_bms_unlock_output_data(the_chip); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2626 | mutex_unlock(&the_chip->bms_output_lock); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2627 | } |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 2628 | |
| 2629 | pr_err("called before initialization\n"); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2630 | return rc; |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 2631 | } |
| 2632 | EXPORT_SYMBOL(pm8921_bms_get_vsense_avg); |
| 2633 | |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 2634 | int pm8921_bms_get_battery_current(int *result_ua) |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 2635 | { |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2636 | int vsense_uv; |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 2637 | int rc = 0; |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2638 | |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 2639 | *result_ua = 0; |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 2640 | if (!the_chip) { |
| 2641 | pr_err("called before initialization\n"); |
| 2642 | return -EINVAL; |
| 2643 | } |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2644 | if (the_chip->r_sense_uohm == 0) { |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 2645 | pr_err("r_sense is zero\n"); |
| 2646 | return -EINVAL; |
| 2647 | } |
| 2648 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2649 | mutex_lock(&the_chip->bms_output_lock); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2650 | pm_bms_lock_output_data(the_chip); |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 2651 | rc = read_vsense_avg(the_chip, &vsense_uv); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 2652 | pm_bms_unlock_output_data(the_chip); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2653 | mutex_unlock(&the_chip->bms_output_lock); |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 2654 | if (rc) { |
| 2655 | pr_err("Unable to read vsense average\n"); |
| 2656 | goto error_vsense; |
| 2657 | } |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2658 | pr_debug("vsense=%duV\n", vsense_uv); |
Abhijeet Dharmapurikar | a7a1c6b | 2011-08-17 10:04:58 -0700 | [diff] [blame] | 2659 | /* cast for signed division */ |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2660 | *result_ua = div_s64(vsense_uv * 1000000LL, the_chip->r_sense_uohm); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2661 | pr_debug("ibat=%duA\n", *result_ua); |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 2662 | |
| 2663 | error_vsense: |
| 2664 | return rc; |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 2665 | } |
| 2666 | EXPORT_SYMBOL(pm8921_bms_get_battery_current); |
| 2667 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2668 | int pm8921_bms_get_percent_charge(void) |
| 2669 | { |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 2670 | if (!the_chip) { |
| 2671 | pr_err("called before initialization\n"); |
| 2672 | return -EINVAL; |
| 2673 | } |
| 2674 | |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2675 | return report_state_of_charge(the_chip); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2676 | } |
| 2677 | EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge); |
| 2678 | |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 2679 | int pm8921_bms_get_current_max(void) |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2680 | { |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2681 | if (!the_chip) { |
| 2682 | pr_err("called before initialization\n"); |
| 2683 | return -EINVAL; |
| 2684 | } |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 2685 | return the_chip->imax_ua; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2686 | } |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 2687 | EXPORT_SYMBOL_GPL(pm8921_bms_get_current_max); |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2688 | |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 2689 | int pm8921_bms_get_fcc(void) |
| 2690 | { |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2691 | int batt_temp; |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 2692 | |
| 2693 | if (!the_chip) { |
| 2694 | pr_err("called before initialization\n"); |
| 2695 | return -EINVAL; |
| 2696 | } |
| 2697 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2698 | get_batt_temp(the_chip, &batt_temp); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2699 | return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles); |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 2700 | } |
| 2701 | EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2702 | void pm8921_bms_charging_began(void) |
| 2703 | { |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 2704 | struct pm8921_soc_params raw; |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2705 | int batt_temp; |
| 2706 | |
| 2707 | get_batt_temp(the_chip, &batt_temp); |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 2708 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2709 | mutex_lock(&the_chip->last_ocv_uv_mutex); |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2710 | read_soc_params_raw(the_chip, &raw, batt_temp); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2711 | mutex_unlock(&the_chip->last_ocv_uv_mutex); |
| 2712 | |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 2713 | the_chip->start_percent = report_state_of_charge(the_chip); |
| 2714 | |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 2715 | bms_start_percent = the_chip->start_percent; |
| 2716 | bms_start_ocv_uv = raw.last_good_ocv_uv; |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 2717 | calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah); |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 2718 | pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 2719 | IBAT_TOL_MASK, IBAT_TOL_DEFAULT); |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2720 | the_chip->charge_time_us = 0; |
| 2721 | the_chip->catch_up_time_us = 0; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 2722 | |
| 2723 | the_chip->soc_at_cv = -EINVAL; |
| 2724 | the_chip->prev_chg_soc = -EINVAL; |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 2725 | pr_debug("start_percent = %u%%\n", the_chip->start_percent); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2726 | } |
| 2727 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_began); |
| 2728 | |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 2729 | #define DELTA_FCC_PERCENT 3 |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2730 | #define MIN_START_PERCENT_FOR_LEARNING 30 |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 2731 | void pm8921_bms_charging_end(int is_battery_full) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2732 | { |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2733 | int batt_temp; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2734 | struct pm8921_soc_params raw; |
| 2735 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2736 | if (the_chip == NULL) |
| 2737 | return; |
| 2738 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 2739 | get_batt_temp(the_chip, &batt_temp); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2740 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2741 | mutex_lock(&the_chip->last_ocv_uv_mutex); |
| 2742 | |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 2743 | read_soc_params_raw(the_chip, &raw, batt_temp); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2744 | |
Abhijeet Dharmapurikar | 57aeb92 | 2012-01-25 13:22:26 -0800 | [diff] [blame] | 2745 | calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah); |
| 2746 | |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2747 | bms_end_ocv_uv = raw.last_good_ocv_uv; |
| 2748 | |
Abhijeet Dharmapurikar | e88f246 | 2012-04-25 18:22:38 -0700 | [diff] [blame] | 2749 | if (is_battery_full && the_chip->enable_fcc_learning |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2750 | && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2751 | int fcc_uah, new_fcc_uah, delta_fcc_uah; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 2752 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2753 | new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw, |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 2754 | batt_temp, last_chargecycles, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2755 | &fcc_uah); |
| 2756 | delta_fcc_uah = new_fcc_uah - fcc_uah; |
| 2757 | if (delta_fcc_uah < 0) |
| 2758 | delta_fcc_uah = -delta_fcc_uah; |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 2759 | |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2760 | if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) { |
| 2761 | /* new_fcc_uah is outside the scope limit it */ |
| 2762 | if (new_fcc_uah > fcc_uah) |
| 2763 | new_fcc_uah |
Abhijeet Dharmapurikar | 873ffd1 | 2012-04-17 00:06:15 -0700 | [diff] [blame] | 2764 | = (fcc_uah + |
| 2765 | (DELTA_FCC_PERCENT * fcc_uah) / 100); |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2766 | else |
| 2767 | new_fcc_uah |
Abhijeet Dharmapurikar | 873ffd1 | 2012-04-17 00:06:15 -0700 | [diff] [blame] | 2768 | = (fcc_uah - |
| 2769 | (DELTA_FCC_PERCENT * fcc_uah) / 100); |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2770 | |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 2771 | pr_debug("delta_fcc=%d > %d percent of fcc=%d" |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2772 | "restring it to %d\n", |
| 2773 | delta_fcc_uah, DELTA_FCC_PERCENT, |
| 2774 | fcc_uah, new_fcc_uah); |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 2775 | } |
Abhijeet Dharmapurikar | 57b06b5 | 2011-11-07 19:14:08 -0800 | [diff] [blame] | 2776 | |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 2777 | last_real_fcc_mah = new_fcc_uah/1000; |
| 2778 | last_real_fcc_batt_temp = batt_temp; |
| 2779 | readjust_fcc_table(); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | if (is_battery_full) { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2783 | the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw; |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2784 | |
| 2785 | the_chip->last_ocv_uv = the_chip->max_voltage_uv; |
| 2786 | raw.last_good_ocv_uv = the_chip->max_voltage_uv; |
Abhijeet Dharmapurikar | 28d5137 | 2013-02-27 19:16:22 -0800 | [diff] [blame] | 2787 | raw.cc = 0; |
| 2788 | /* reset the cc in h/w */ |
| 2789 | reset_cc(the_chip); |
Abhijeet Dharmapurikar | 079d6c5 | 2013-01-25 13:10:07 -0800 | [diff] [blame] | 2790 | the_chip->last_ocv_temp_decidegc = batt_temp; |
Abhijeet Dharmapurikar | b40b96f | 2012-04-24 13:19:25 -0700 | [diff] [blame] | 2791 | /* |
| 2792 | * since we are treating this as an ocv event |
| 2793 | * forget the old cc value |
| 2794 | */ |
| 2795 | the_chip->last_cc_uah = 0; |
Abhijeet Dharmapurikar | 28d5137 | 2013-02-27 19:16:22 -0800 | [diff] [blame] | 2796 | pr_debug("EOC BATT_FULL ocv_reading = 0x%x\n", |
| 2797 | the_chip->ocv_reading_at_100); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2800 | the_chip->end_percent = calculate_state_of_charge(the_chip, &raw, |
| 2801 | batt_temp, last_chargecycles); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2802 | mutex_unlock(&the_chip->last_ocv_uv_mutex); |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 2803 | |
| 2804 | bms_end_percent = the_chip->end_percent; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 2805 | |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 2806 | if (the_chip->end_percent > the_chip->start_percent) { |
Abhijeet Dharmapurikar | 8a113f8 | 2012-01-19 10:58:45 -0800 | [diff] [blame] | 2807 | last_charge_increase += |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 2808 | the_chip->end_percent - the_chip->start_percent; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2809 | if (last_charge_increase > 100) { |
| 2810 | last_chargecycles++; |
| 2811 | last_charge_increase = last_charge_increase % 100; |
| 2812 | } |
| 2813 | } |
| 2814 | pr_debug("end_percent = %u%% last_charge_increase = %d" |
| 2815 | "last_chargecycles = %d\n", |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 2816 | the_chip->end_percent, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2817 | last_charge_increase, |
| 2818 | last_chargecycles); |
Abhijeet Dharmapurikar | dba91f4 | 2011-12-21 16:56:13 -0800 | [diff] [blame] | 2819 | the_chip->start_percent = -EINVAL; |
| 2820 | the_chip->end_percent = -EINVAL; |
Abhijeet Dharmapurikar | 647d0f8 | 2012-08-13 18:39:15 -0700 | [diff] [blame] | 2821 | the_chip->charge_time_us = 0; |
| 2822 | the_chip->catch_up_time_us = 0; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 2823 | the_chip->soc_at_cv = -EINVAL; |
| 2824 | the_chip->prev_chg_soc = -EINVAL; |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 2825 | pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 2826 | IBAT_TOL_MASK, IBAT_TOL_NOCHG); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2827 | } |
| 2828 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_end); |
| 2829 | |
| 2830 | static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data) |
| 2831 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2832 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2833 | return IRQ_HANDLED; |
| 2834 | } |
| 2835 | |
| 2836 | static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data) |
| 2837 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2838 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2839 | return IRQ_HANDLED; |
| 2840 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2841 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2842 | static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data) |
| 2843 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2844 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2845 | return IRQ_HANDLED; |
| 2846 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2847 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2848 | static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data) |
| 2849 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2850 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2851 | return IRQ_HANDLED; |
| 2852 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2853 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2854 | static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data) |
| 2855 | { |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2856 | struct pm8921_bms_chip *chip = data; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2857 | |
| 2858 | pr_debug("irq = %d triggered", irq); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2859 | schedule_work(&chip->calib_hkadc_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2860 | return IRQ_HANDLED; |
| 2861 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2862 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2863 | static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data) |
| 2864 | { |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2865 | struct pm8921_bms_chip *chip = data; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2866 | |
| 2867 | pr_debug("irq = %d triggered", irq); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2868 | schedule_work(&chip->calib_hkadc_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2869 | return IRQ_HANDLED; |
| 2870 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2871 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2872 | struct pm_bms_irq_init_data { |
| 2873 | unsigned int irq_id; |
| 2874 | char *name; |
| 2875 | unsigned long flags; |
| 2876 | irqreturn_t (*handler)(int, void *); |
| 2877 | }; |
| 2878 | |
| 2879 | #define BMS_IRQ(_id, _flags, _handler) \ |
| 2880 | { \ |
| 2881 | .irq_id = _id, \ |
| 2882 | .name = #_id, \ |
| 2883 | .flags = _flags, \ |
| 2884 | .handler = _handler, \ |
| 2885 | } |
| 2886 | |
| 2887 | struct pm_bms_irq_init_data bms_irq_data[] = { |
| 2888 | BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING, |
| 2889 | pm8921_bms_sbi_write_ok_handler), |
| 2890 | BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING, |
| 2891 | pm8921_bms_cc_thr_handler), |
| 2892 | BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING, |
| 2893 | pm8921_bms_vsense_thr_handler), |
| 2894 | BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING, |
| 2895 | pm8921_bms_vsense_for_r_handler), |
| 2896 | BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING, |
| 2897 | pm8921_bms_ocv_for_r_handler), |
| 2898 | BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING, |
| 2899 | pm8921_bms_good_ocv_handler), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2900 | }; |
| 2901 | |
| 2902 | static void free_irqs(struct pm8921_bms_chip *chip) |
| 2903 | { |
| 2904 | int i; |
| 2905 | |
| 2906 | for (i = 0; i < PM_BMS_MAX_INTS; i++) |
| 2907 | if (chip->pmic_bms_irq[i]) { |
| 2908 | free_irq(chip->pmic_bms_irq[i], NULL); |
| 2909 | chip->pmic_bms_irq[i] = 0; |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | static int __devinit request_irqs(struct pm8921_bms_chip *chip, |
| 2914 | struct platform_device *pdev) |
| 2915 | { |
| 2916 | struct resource *res; |
| 2917 | int ret, i; |
| 2918 | |
| 2919 | ret = 0; |
| 2920 | bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS); |
| 2921 | |
| 2922 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 2923 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 2924 | bms_irq_data[i].name); |
| 2925 | if (res == NULL) { |
| 2926 | pr_err("couldn't find %s\n", bms_irq_data[i].name); |
| 2927 | goto err_out; |
| 2928 | } |
| 2929 | ret = request_irq(res->start, bms_irq_data[i].handler, |
| 2930 | bms_irq_data[i].flags, |
| 2931 | bms_irq_data[i].name, chip); |
| 2932 | if (ret < 0) { |
| 2933 | pr_err("couldn't request %d (%s) %d\n", res->start, |
| 2934 | bms_irq_data[i].name, ret); |
| 2935 | goto err_out; |
| 2936 | } |
| 2937 | chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start; |
| 2938 | pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id); |
| 2939 | } |
| 2940 | return 0; |
| 2941 | |
| 2942 | err_out: |
| 2943 | free_irqs(chip); |
| 2944 | return -EINVAL; |
| 2945 | } |
| 2946 | |
| 2947 | #define EN_BMS_BIT BIT(7) |
| 2948 | #define EN_PON_HS_BIT BIT(0) |
| 2949 | static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip) |
| 2950 | { |
| 2951 | int rc; |
| 2952 | |
| 2953 | rc = pm_bms_masked_write(chip, BMS_CONTROL, |
| 2954 | EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT); |
| 2955 | if (rc) { |
| 2956 | pr_err("failed to enable pon and bms addr = %d %d", |
| 2957 | BMS_CONTROL, rc); |
| 2958 | } |
| 2959 | |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 2960 | /* The charger will call start charge later if usb is present */ |
| 2961 | pm_bms_masked_write(chip, BMS_TOLERANCES, |
| 2962 | IBAT_TOL_MASK, IBAT_TOL_NOCHG); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2963 | return 0; |
| 2964 | } |
| 2965 | |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2966 | static void check_initial_ocv(struct pm8921_bms_chip *chip) |
| 2967 | { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2968 | int ocv_uv, rc; |
| 2969 | int16_t ocv_raw; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 2970 | int usb_chg; |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2971 | |
| 2972 | /* |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 2973 | * Check if a ocv is available in bms hw, |
| 2974 | * if not compute it here at boot time and save it |
| 2975 | * in the last_ocv_uv. |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2976 | */ |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2977 | ocv_uv = 0; |
| 2978 | pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw); |
Abhijeet Dharmapurikar | 9e94898 | 2012-08-28 15:21:23 -0700 | [diff] [blame] | 2979 | usb_chg = usb_chg_plugged_in(chip); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 2980 | rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2981 | if (rc || ocv_uv == 0) { |
| 2982 | rc = adc_based_ocv(chip, &ocv_uv); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 2983 | if (rc) { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2984 | pr_err("failed to read adc based ocv_uv rc = %d\n", rc); |
| 2985 | ocv_uv = DEFAULT_OCV_MICROVOLTS; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 2986 | } |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2987 | } |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 2988 | chip->last_ocv_uv = ocv_uv; |
| 2989 | pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, chip->last_ocv_uv); |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 2992 | static int64_t read_battery_id(struct pm8921_bms_chip *chip) |
| 2993 | { |
| 2994 | int rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 2995 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 2996 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 2997 | rc = pm8xxx_adc_read(chip->batt_id_channel, &result); |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 2998 | if (rc) { |
| 2999 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 3000 | chip->vbat_channel, rc); |
| 3001 | return rc; |
| 3002 | } |
| 3003 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 3004 | result.measurement); |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 3005 | return result.adc_code; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 3008 | #define PALLADIUM_ID_MIN 0x7F40 |
| 3009 | #define PALLADIUM_ID_MAX 0x7F5A |
| 3010 | #define DESAY_5200_ID_MIN 0x7F7F |
| 3011 | #define DESAY_5200_ID_MAX 0x802F |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3012 | static int set_battery_data(struct pm8921_bms_chip *chip) |
| 3013 | { |
| 3014 | int64_t battery_id; |
| 3015 | |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 3016 | if (chip->batt_type == BATT_DESAY) |
| 3017 | goto desay; |
| 3018 | else if (chip->batt_type == BATT_PALLADIUM) |
| 3019 | goto palladium; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3020 | |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 3021 | battery_id = read_battery_id(chip); |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3022 | if (battery_id < 0) { |
| 3023 | pr_err("cannot read battery id err = %lld\n", battery_id); |
| 3024 | return battery_id; |
| 3025 | } |
| 3026 | |
| 3027 | if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) { |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 3028 | goto palladium; |
| 3029 | } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX, |
| 3030 | battery_id)) { |
| 3031 | goto desay; |
| 3032 | } else { |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 3033 | pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n", |
| 3034 | battery_id); |
| 3035 | goto palladium; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | palladium: |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3039 | chip->fcc = palladium_1500_data.fcc; |
| 3040 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 3041 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 3042 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 3043 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 3044 | chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut; |
| 3045 | chip->default_rbatt_mohm |
| 3046 | = palladium_1500_data.default_rbatt_mohm; |
Abhijeet Dharmapurikar | ded189b | 2012-03-30 10:12:28 -0700 | [diff] [blame] | 3047 | chip->delta_rbatt_mohm = palladium_1500_data.delta_rbatt_mohm; |
Abhijeet Dharmapurikar | 03c6c23 | 2013-02-13 17:57:40 -0800 | [diff] [blame] | 3048 | chip->rbatt_capacitive_mohm |
| 3049 | = palladium_1500_data.rbatt_capacitive_mohm; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3050 | return 0; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 3051 | desay: |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 3052 | chip->fcc = desay_5200_data.fcc; |
| 3053 | chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut; |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 3054 | chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut; |
| 3055 | chip->pc_sf_lut = desay_5200_data.pc_sf_lut; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 3056 | chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut; |
| 3057 | chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm; |
Abhijeet Dharmapurikar | ded189b | 2012-03-30 10:12:28 -0700 | [diff] [blame] | 3058 | chip->delta_rbatt_mohm = desay_5200_data.delta_rbatt_mohm; |
Abhijeet Dharmapurikar | 03c6c23 | 2013-02-13 17:57:40 -0800 | [diff] [blame] | 3059 | chip->rbatt_capacitive_mohm |
| 3060 | = desay_5200_data.rbatt_capacitive_mohm; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3061 | return 0; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3062 | } |
| 3063 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3064 | enum bms_request_operation { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3065 | CALC_FCC, |
| 3066 | CALC_PC, |
| 3067 | CALC_SOC, |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 3068 | CALIB_HKADC, |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3069 | CALIB_CCADC, |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3070 | GET_VBAT_VSENSE_SIMULTANEOUS, |
Abhijeet Dharmapurikar | c99f586 | 2012-03-14 19:33:16 -0700 | [diff] [blame] | 3071 | STOP_OCV, |
| 3072 | START_OCV, |
Abhijeet Dharmapurikar | 1ed9418 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 3073 | SET_OCV, |
Abhijeet Dharmapurikar | bd5e508 | 2013-03-25 14:21:27 -0700 | [diff] [blame] | 3074 | BATT_PRESENT, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3075 | }; |
| 3076 | |
| 3077 | static int test_batt_temp = 5; |
| 3078 | static int test_chargecycle = 150; |
| 3079 | static int test_ocv = 3900000; |
| 3080 | enum { |
| 3081 | TEST_BATT_TEMP, |
| 3082 | TEST_CHARGE_CYCLE, |
| 3083 | TEST_OCV, |
| 3084 | }; |
| 3085 | static int get_test_param(void *data, u64 * val) |
| 3086 | { |
| 3087 | switch ((int)data) { |
| 3088 | case TEST_BATT_TEMP: |
| 3089 | *val = test_batt_temp; |
| 3090 | break; |
| 3091 | case TEST_CHARGE_CYCLE: |
| 3092 | *val = test_chargecycle; |
| 3093 | break; |
| 3094 | case TEST_OCV: |
| 3095 | *val = test_ocv; |
| 3096 | break; |
| 3097 | default: |
| 3098 | return -EINVAL; |
| 3099 | } |
| 3100 | return 0; |
| 3101 | } |
| 3102 | static int set_test_param(void *data, u64 val) |
| 3103 | { |
| 3104 | switch ((int)data) { |
| 3105 | case TEST_BATT_TEMP: |
| 3106 | test_batt_temp = (int)val; |
| 3107 | break; |
| 3108 | case TEST_CHARGE_CYCLE: |
| 3109 | test_chargecycle = (int)val; |
| 3110 | break; |
| 3111 | case TEST_OCV: |
| 3112 | test_ocv = (int)val; |
| 3113 | break; |
| 3114 | default: |
| 3115 | return -EINVAL; |
| 3116 | } |
| 3117 | return 0; |
| 3118 | } |
| 3119 | DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n"); |
| 3120 | |
| 3121 | static int get_calc(void *data, u64 * val) |
| 3122 | { |
| 3123 | int param = (int)data; |
| 3124 | int ret = 0; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3125 | int ibat_ua, vbat_uv; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 3126 | struct pm8921_soc_params raw; |
| 3127 | |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 3128 | read_soc_params_raw(the_chip, &raw, 300); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3129 | |
| 3130 | *val = 0; |
| 3131 | |
| 3132 | /* global irq number passed in via data */ |
| 3133 | switch (param) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3134 | case CALC_FCC: |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 3135 | *val = calculate_fcc_uah(the_chip, test_batt_temp, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3136 | test_chargecycle); |
| 3137 | break; |
| 3138 | case CALC_PC: |
| 3139 | *val = calculate_pc(the_chip, test_ocv, test_batt_temp, |
| 3140 | test_chargecycle); |
| 3141 | break; |
| 3142 | case CALC_SOC: |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 3143 | *val = calculate_state_of_charge(the_chip, &raw, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3144 | test_batt_temp, test_chargecycle); |
| 3145 | break; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 3146 | case CALIB_HKADC: |
| 3147 | /* reading this will trigger calibration */ |
| 3148 | *val = 0; |
| 3149 | calib_hkadc(the_chip); |
| 3150 | break; |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3151 | case CALIB_CCADC: |
| 3152 | /* reading this will trigger calibration */ |
| 3153 | *val = 0; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 3154 | pm8xxx_calib_ccadc(); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3155 | break; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3156 | case GET_VBAT_VSENSE_SIMULTANEOUS: |
| 3157 | /* reading this will call simultaneous vbat and vsense */ |
| 3158 | *val = |
| 3159 | pm8921_bms_get_simultaneous_battery_voltage_and_current( |
| 3160 | &ibat_ua, |
| 3161 | &vbat_uv); |
Abhijeet Dharmapurikar | c99f586 | 2012-03-14 19:33:16 -0700 | [diff] [blame] | 3162 | default: |
| 3163 | ret = -EINVAL; |
| 3164 | } |
| 3165 | return ret; |
| 3166 | } |
| 3167 | |
| 3168 | static int set_calc(void *data, u64 val) |
| 3169 | { |
| 3170 | int param = (int)data; |
| 3171 | int ret = 0; |
| 3172 | |
| 3173 | switch (param) { |
| 3174 | case STOP_OCV: |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 3175 | pm8921_bms_stop_ocv_updates(); |
Abhijeet Dharmapurikar | c99f586 | 2012-03-14 19:33:16 -0700 | [diff] [blame] | 3176 | break; |
| 3177 | case START_OCV: |
Abhijeet Dharmapurikar | 7df608c | 2012-12-26 21:28:43 -0800 | [diff] [blame] | 3178 | pm8921_bms_start_ocv_updates(); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3179 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3180 | default: |
| 3181 | ret = -EINVAL; |
| 3182 | } |
| 3183 | return ret; |
| 3184 | } |
Abhijeet Dharmapurikar | c99f586 | 2012-03-14 19:33:16 -0700 | [diff] [blame] | 3185 | DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, set_calc, "%llu\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3186 | |
| 3187 | static int get_reading(void *data, u64 * val) |
| 3188 | { |
| 3189 | int param = (int)data; |
| 3190 | int ret = 0; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 3191 | struct pm8921_soc_params raw; |
| 3192 | |
Abhijeet Dharmapurikar | a937bc4 | 2013-02-13 19:51:56 -0800 | [diff] [blame] | 3193 | mutex_lock(&the_chip->last_ocv_uv_mutex); |
Abhijeet Dharmapurikar | be2271f | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 3194 | read_soc_params_raw(the_chip, &raw, 300); |
Abhijeet Dharmapurikar | a937bc4 | 2013-02-13 19:51:56 -0800 | [diff] [blame] | 3195 | mutex_lock(&the_chip->last_ocv_uv_mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3196 | |
| 3197 | *val = 0; |
| 3198 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3199 | switch (param) { |
| 3200 | case CC_MSB: |
| 3201 | case CC_LSB: |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 3202 | *val = raw.cc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3203 | break; |
| 3204 | case LAST_GOOD_OCV_VALUE: |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 3205 | *val = raw.last_good_ocv_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3206 | break; |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 3207 | case VSENSE_AVG: |
| 3208 | read_vsense_avg(the_chip, (uint *)val); |
| 3209 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3210 | default: |
| 3211 | ret = -EINVAL; |
| 3212 | } |
| 3213 | return ret; |
| 3214 | } |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 3215 | DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3216 | |
| 3217 | static int get_rt_status(void *data, u64 * val) |
| 3218 | { |
| 3219 | int i = (int)data; |
| 3220 | int ret; |
| 3221 | |
| 3222 | /* global irq number passed in via data */ |
| 3223 | ret = pm_bms_get_rt_status(the_chip, i); |
| 3224 | *val = ret; |
| 3225 | return 0; |
| 3226 | } |
| 3227 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 3228 | |
| 3229 | static int get_reg(void *data, u64 * val) |
| 3230 | { |
| 3231 | int addr = (int)data; |
| 3232 | int ret; |
| 3233 | u8 temp; |
| 3234 | |
| 3235 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 3236 | if (ret) { |
| 3237 | pr_err("pm8xxx_readb to %x value = %d errored = %d\n", |
| 3238 | addr, temp, ret); |
| 3239 | return -EAGAIN; |
| 3240 | } |
| 3241 | *val = temp; |
| 3242 | return 0; |
| 3243 | } |
| 3244 | |
| 3245 | static int set_reg(void *data, u64 val) |
| 3246 | { |
| 3247 | int addr = (int)data; |
| 3248 | int ret; |
| 3249 | u8 temp; |
| 3250 | |
| 3251 | temp = (u8) val; |
| 3252 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 3253 | if (ret) { |
| 3254 | pr_err("pm8xxx_writeb to %x value = %d errored = %d\n", |
| 3255 | addr, temp, ret); |
| 3256 | return -EAGAIN; |
| 3257 | } |
| 3258 | return 0; |
| 3259 | } |
| 3260 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 3261 | |
| 3262 | static void create_debugfs_entries(struct pm8921_bms_chip *chip) |
| 3263 | { |
| 3264 | int i; |
| 3265 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 3266 | chip->dent = debugfs_create_dir("pm8921-bms", NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3267 | |
| 3268 | if (IS_ERR(chip->dent)) { |
| 3269 | pr_err("pmic bms couldnt create debugfs dir\n"); |
| 3270 | return; |
| 3271 | } |
| 3272 | |
| 3273 | debugfs_create_file("BMS_CONTROL", 0644, chip->dent, |
| 3274 | (void *)BMS_CONTROL, ®_fops); |
| 3275 | debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent, |
| 3276 | (void *)BMS_OUTPUT0, ®_fops); |
| 3277 | debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent, |
| 3278 | (void *)BMS_OUTPUT1, ®_fops); |
| 3279 | debugfs_create_file("BMS_TEST1", 0644, chip->dent, |
| 3280 | (void *)BMS_TEST1, ®_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3281 | |
| 3282 | debugfs_create_file("test_batt_temp", 0644, chip->dent, |
| 3283 | (void *)TEST_BATT_TEMP, &temp_fops); |
| 3284 | debugfs_create_file("test_chargecycle", 0644, chip->dent, |
| 3285 | (void *)TEST_CHARGE_CYCLE, &temp_fops); |
| 3286 | debugfs_create_file("test_ocv", 0644, chip->dent, |
| 3287 | (void *)TEST_OCV, &temp_fops); |
| 3288 | |
| 3289 | debugfs_create_file("read_cc", 0644, chip->dent, |
| 3290 | (void *)CC_MSB, &reading_fops); |
| 3291 | debugfs_create_file("read_last_good_ocv", 0644, chip->dent, |
| 3292 | (void *)LAST_GOOD_OCV_VALUE, &reading_fops); |
| 3293 | debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent, |
| 3294 | (void *)VBATT_FOR_RBATT, &reading_fops); |
| 3295 | debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent, |
| 3296 | (void *)VSENSE_FOR_RBATT, &reading_fops); |
| 3297 | debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent, |
| 3298 | (void *)OCV_FOR_RBATT, &reading_fops); |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 3299 | debugfs_create_file("read_vsense_avg", 0644, chip->dent, |
| 3300 | (void *)VSENSE_AVG, &reading_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3301 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3302 | debugfs_create_file("show_fcc", 0644, chip->dent, |
| 3303 | (void *)CALC_FCC, &calc_fops); |
| 3304 | debugfs_create_file("show_pc", 0644, chip->dent, |
| 3305 | (void *)CALC_PC, &calc_fops); |
| 3306 | debugfs_create_file("show_soc", 0644, chip->dent, |
| 3307 | (void *)CALC_SOC, &calc_fops); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 3308 | debugfs_create_file("calib_hkadc", 0644, chip->dent, |
| 3309 | (void *)CALIB_HKADC, &calc_fops); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3310 | debugfs_create_file("calib_ccadc", 0644, chip->dent, |
| 3311 | (void *)CALIB_CCADC, &calc_fops); |
Abhijeet Dharmapurikar | c99f586 | 2012-03-14 19:33:16 -0700 | [diff] [blame] | 3312 | debugfs_create_file("stop_ocv", 0644, chip->dent, |
| 3313 | (void *)STOP_OCV, &calc_fops); |
| 3314 | debugfs_create_file("start_ocv", 0644, chip->dent, |
| 3315 | (void *)START_OCV, &calc_fops); |
Abhijeet Dharmapurikar | 1ed9418 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 3316 | debugfs_create_file("set_ocv", 0644, chip->dent, |
| 3317 | (void *)SET_OCV, &calc_fops); |
Abhijeet Dharmapurikar | bd5e508 | 2013-03-25 14:21:27 -0700 | [diff] [blame] | 3318 | debugfs_create_file("batt_present", 0644, chip->dent, |
| 3319 | (void *)BATT_PRESENT, &calc_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3320 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3321 | debugfs_create_file("simultaneous", 0644, chip->dent, |
| 3322 | (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops); |
| 3323 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3324 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 3325 | if (chip->pmic_bms_irq[bms_irq_data[i].irq_id]) |
| 3326 | debugfs_create_file(bms_irq_data[i].name, 0444, |
| 3327 | chip->dent, |
| 3328 | (void *)bms_irq_data[i].irq_id, |
| 3329 | &rt_fops); |
| 3330 | } |
| 3331 | } |
| 3332 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 3333 | #define REG_SBI_CONFIG 0x04F |
| 3334 | #define PAGE3_ENABLE_MASK 0x6 |
| 3335 | #define PROGRAM_REV_MASK 0x0F |
| 3336 | #define PROGRAM_REV 0x9 |
| 3337 | static int read_ocv_trim(struct pm8921_bms_chip *chip) |
| 3338 | { |
| 3339 | int rc; |
| 3340 | u8 reg, sbi_config; |
| 3341 | |
| 3342 | rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config); |
| 3343 | if (rc) { |
| 3344 | pr_err("error = %d reading sbi config reg\n", rc); |
| 3345 | return rc; |
| 3346 | } |
| 3347 | |
| 3348 | reg = sbi_config | PAGE3_ENABLE_MASK; |
| 3349 | rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg); |
| 3350 | if (rc) { |
| 3351 | pr_err("error = %d writing sbi config reg\n", rc); |
| 3352 | return rc; |
| 3353 | } |
| 3354 | |
| 3355 | rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, ®); |
| 3356 | if (rc) |
| 3357 | pr_err("Error %d reading %d addr %d\n", |
| 3358 | rc, reg, TEST_PROGRAM_REV); |
| 3359 | pr_err("program rev reg is 0x%x\n", reg); |
| 3360 | reg &= PROGRAM_REV_MASK; |
| 3361 | |
| 3362 | /* If the revision is equal or higher do not adjust trim delta */ |
| 3363 | if (reg >= PROGRAM_REV) { |
| 3364 | chip->amux_2_trim_delta = 0; |
| 3365 | goto restore_sbi_config; |
| 3366 | } |
| 3367 | |
| 3368 | rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, ®); |
| 3369 | if (rc) { |
| 3370 | pr_err("error = %d reading trim reg\n", rc); |
| 3371 | return rc; |
| 3372 | } |
| 3373 | |
| 3374 | pr_err("trim reg is 0x%x\n", reg); |
| 3375 | chip->amux_2_trim_delta = abs(0x49 - reg); |
| 3376 | pr_err("trim delta is %d\n", chip->amux_2_trim_delta); |
| 3377 | |
| 3378 | restore_sbi_config: |
| 3379 | rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config); |
| 3380 | if (rc) { |
| 3381 | pr_err("error = %d writing sbi config reg\n", rc); |
| 3382 | return rc; |
| 3383 | } |
| 3384 | |
| 3385 | return 0; |
| 3386 | } |
| 3387 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3388 | static int __devinit pm8921_bms_probe(struct platform_device *pdev) |
| 3389 | { |
| 3390 | int rc = 0; |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 3391 | int vbatt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3392 | struct pm8921_bms_chip *chip; |
| 3393 | const struct pm8921_bms_platform_data *pdata |
| 3394 | = pdev->dev.platform_data; |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 3395 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3396 | if (!pdata) { |
| 3397 | pr_err("missing platform data\n"); |
| 3398 | return -EINVAL; |
| 3399 | } |
| 3400 | |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 3401 | chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3402 | if (!chip) { |
| 3403 | pr_err("Cannot allocate pm_bms_chip\n"); |
| 3404 | return -ENOMEM; |
| 3405 | } |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 3406 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 3407 | mutex_init(&chip->bms_output_lock); |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 3408 | mutex_init(&chip->last_ocv_uv_mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3409 | chip->dev = &pdev->dev; |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 3410 | chip->r_sense_uohm = pdata->r_sense_uohm; |
Abhijeet Dharmapurikar | a51c87b | 2012-07-13 20:13:14 -0700 | [diff] [blame] | 3411 | chip->v_cutoff = pdata->v_cutoff; |
Abhijeet Dharmapurikar | a93ede8 | 2011-11-17 12:20:03 -0800 | [diff] [blame] | 3412 | chip->max_voltage_uv = pdata->max_voltage_uv; |
Abhijeet Dharmapurikar | d6d66c7 | 2012-08-06 14:02:51 -0700 | [diff] [blame] | 3413 | chip->chg_term_ua = pdata->chg_term_ua; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 3414 | chip->batt_type = pdata->battery_type; |
Abhijeet Dharmapurikar | baffba4 | 2012-03-22 14:41:10 -0700 | [diff] [blame] | 3415 | chip->rconn_mohm = pdata->rconn_mohm; |
Abhijeet Dharmapurikar | dba91f4 | 2011-12-21 16:56:13 -0800 | [diff] [blame] | 3416 | chip->start_percent = -EINVAL; |
| 3417 | chip->end_percent = -EINVAL; |
Xiaozhe Shi | d28e497 | 2012-12-04 15:13:32 -0800 | [diff] [blame] | 3418 | chip->last_cc_uah = INT_MIN; |
Abhijeet Dharmapurikar | 61275b9 | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 3419 | chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
| 3420 | chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED; |
Abhijeet Dharmapurikar | 602dc27 | 2012-07-19 21:34:56 -0700 | [diff] [blame] | 3421 | chip->shutdown_soc_valid_limit = pdata->shutdown_soc_valid_limit; |
Abhijeet Dharmapurikar | 720b00e | 2012-08-01 21:33:09 -0700 | [diff] [blame] | 3422 | chip->adjust_soc_low_threshold = pdata->adjust_soc_low_threshold; |
Abhijeet Dharmapurikar | 683472d | 2012-10-30 16:21:58 -0700 | [diff] [blame] | 3423 | |
| 3424 | chip->normal_voltage_calc_ms = pdata->normal_voltage_calc_ms; |
| 3425 | chip->low_voltage_calc_ms = pdata->low_voltage_calc_ms; |
| 3426 | |
| 3427 | chip->soc_calc_period = pdata->normal_voltage_calc_ms; |
| 3428 | |
Abhijeet Dharmapurikar | 720b00e | 2012-08-01 21:33:09 -0700 | [diff] [blame] | 3429 | if (chip->adjust_soc_low_threshold >= 45) |
| 3430 | chip->adjust_soc_low_threshold = 45; |
| 3431 | |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 3432 | chip->prev_pc_unusable = -EINVAL; |
Abhijeet Dharmapurikar | d3c435e | 2012-08-16 19:23:18 -0700 | [diff] [blame] | 3433 | chip->soc_at_cv = -EINVAL; |
Abhijeet Dharmapurikar | eca7b3e | 2012-12-26 21:30:29 -0800 | [diff] [blame] | 3434 | chip->imax_ua = -EINVAL; |
Abhijeet Dharmapurikar | 2f7f6fc | 2012-08-03 15:31:34 -0700 | [diff] [blame] | 3435 | |
Abhijeet Dharmapurikar | 1f4cc51 | 2012-07-21 00:10:39 -0700 | [diff] [blame] | 3436 | chip->ignore_shutdown_soc = pdata->ignore_shutdown_soc; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3437 | rc = set_battery_data(chip); |
| 3438 | if (rc) { |
| 3439 | pr_err("%s bad battery data %d\n", __func__, rc); |
Abhijeet Dharmapurikar | 4a4456e | 2011-09-27 15:10:38 -0700 | [diff] [blame] | 3440 | goto free_chip; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3441 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3442 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 3443 | if (chip->pc_temp_ocv_lut == NULL) { |
| 3444 | pr_err("temp ocv lut table is NULL\n"); |
| 3445 | rc = -EINVAL; |
| 3446 | goto free_chip; |
| 3447 | } |
| 3448 | |
| 3449 | /* set defaults in the battery data */ |
| 3450 | if (chip->default_rbatt_mohm <= 0) |
| 3451 | chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS; |
| 3452 | |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 3453 | chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel; |
| 3454 | chip->vbat_channel = pdata->bms_cdata.vbat_channel; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 3455 | chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel; |
| 3456 | chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 3457 | chip->batt_id_channel = pdata->bms_cdata.batt_id_channel; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 3458 | chip->revision = pm8xxx_get_revision(chip->dev->parent); |
Abhijeet Dharmapurikar | e88f246 | 2012-04-25 18:22:38 -0700 | [diff] [blame] | 3459 | chip->enable_fcc_learning = pdata->enable_fcc_learning; |
Abhijeet Dharmapurikar | 9c0e604 | 2012-07-12 14:55:13 -0700 | [diff] [blame] | 3460 | |
Abhijeet Dharmapurikar | 1939ede | 2013-01-10 15:59:33 -0800 | [diff] [blame] | 3461 | chip->disable_flat_portion_ocv = pdata->disable_flat_portion_ocv; |
| 3462 | chip->ocv_dis_high_soc = pdata->ocv_dis_high_soc; |
| 3463 | chip->ocv_dis_low_soc = pdata->ocv_dis_low_soc; |
| 3464 | |
Abhijeet Dharmapurikar | 4870761 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 3465 | chip->high_ocv_correction_limit_uv |
| 3466 | = pdata->high_ocv_correction_limit_uv; |
| 3467 | chip->low_ocv_correction_limit_uv = pdata->low_ocv_correction_limit_uv; |
| 3468 | chip->hold_soc_est = pdata->hold_soc_est; |
| 3469 | |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 3470 | chip->alarm_low_mv = pdata->alarm_low_mv; |
| 3471 | chip->alarm_high_mv = pdata->alarm_high_mv; |
Anirudh Ghayal | 0c7c2bc | 2013-03-22 12:57:50 +0530 | [diff] [blame] | 3472 | chip->low_voltage_detect = pdata->low_voltage_detect; |
| 3473 | chip->vbatt_cutoff_retries = pdata->vbatt_cutoff_retries; |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 3474 | |
Abhijeet Dharmapurikar | 9c0e604 | 2012-07-12 14:55:13 -0700 | [diff] [blame] | 3475 | mutex_init(&chip->calib_mutex); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 3476 | INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work); |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 3477 | |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 3478 | INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work, |
| 3479 | calculate_soc_work); |
| 3480 | |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 3481 | wake_lock_init(&chip->soc_wake_lock, |
| 3482 | WAKE_LOCK_SUSPEND, "pm8921_soc_lock"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3483 | rc = request_irqs(chip, pdev); |
| 3484 | if (rc) { |
| 3485 | pr_err("couldn't register interrupts rc = %d\n", rc); |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 3486 | goto destroy_soc_wl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3489 | wake_lock_init(&chip->low_voltage_wake_lock, |
| 3490 | WAKE_LOCK_SUSPEND, "pm8921_bms_low"); |
| 3491 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3492 | rc = pm8921_bms_hw_init(chip); |
| 3493 | if (rc) { |
| 3494 | pr_err("couldn't init hardware rc = %d\n", rc); |
| 3495 | goto free_irqs; |
| 3496 | } |
| 3497 | |
Abhijeet Dharmapurikar | a7daba5 | 2012-07-27 20:40:51 -0700 | [diff] [blame] | 3498 | read_shutdown_soc_and_iavg(chip); |
Abhijeet Dharmapurikar | ed2d87e | 2012-06-26 20:15:36 -0700 | [diff] [blame] | 3499 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3500 | platform_set_drvdata(pdev, chip); |
| 3501 | the_chip = chip; |
| 3502 | create_debugfs_entries(chip); |
| 3503 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame] | 3504 | rc = read_ocv_trim(chip); |
| 3505 | if (rc) { |
| 3506 | pr_err("couldn't adjust ocv_trim rc= %d\n", rc); |
| 3507 | goto free_irqs; |
| 3508 | } |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 3509 | check_initial_ocv(chip); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3510 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 3511 | /* enable the vbatt reading interrupts for scheduling hkadc calib */ |
| 3512 | pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV); |
| 3513 | pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R); |
Abhijeet Dharmapurikar | 21ebadd | 2011-09-28 21:14:16 -0700 | [diff] [blame] | 3514 | |
Anirudh Ghayal | 6fe4ffa | 2013-02-01 11:44:33 +0530 | [diff] [blame] | 3515 | rc = pm8921_bms_configure_batt_alarm(chip); |
| 3516 | if (rc) { |
| 3517 | pr_err("Couldn't configure battery alarm! rc=%d\n", rc); |
| 3518 | goto free_irqs; |
| 3519 | } |
| 3520 | |
| 3521 | rc = pm8921_bms_enable_batt_alarm(chip); |
| 3522 | if (rc) { |
| 3523 | pr_err("Couldn't enable battery alarm! rc=%d\n", rc); |
| 3524 | goto free_irqs; |
| 3525 | } |
| 3526 | |
Abhijeet Dharmapurikar | 41b46cd | 2012-07-18 19:38:43 -0700 | [diff] [blame] | 3527 | calculate_soc_work(&(chip->calculate_soc_delayed_work.work)); |
| 3528 | |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 3529 | rc = get_battery_uvolts(chip, &vbatt); |
| 3530 | if (!rc) |
| 3531 | pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n", |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 3532 | pm8921_bms_get_percent_charge(), |
Abhijeet Dharmapurikar | 10bc0fd | 2012-04-18 21:03:50 -0700 | [diff] [blame] | 3533 | vbatt, chip->last_ocv_uv); |
Anirudh Ghayal | 3a38a38 | 2012-12-05 10:42:17 +0530 | [diff] [blame] | 3534 | else |
| 3535 | pr_info("Unable to read battery voltage at boot\n"); |
Abhijeet Dharmapurikar | 807249a | 2012-07-12 20:26:27 -0700 | [diff] [blame] | 3536 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3537 | return 0; |
| 3538 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3539 | free_irqs: |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 3540 | wake_lock_destroy(&chip->low_voltage_wake_lock); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 3541 | free_irqs(chip); |
Abhijeet Dharmapurikar | b2aeb7e | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 3542 | destroy_soc_wl: |
| 3543 | wake_lock_destroy(&chip->soc_wake_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3544 | free_chip: |
| 3545 | kfree(chip); |
| 3546 | return rc; |
| 3547 | } |
| 3548 | |
| 3549 | static int __devexit pm8921_bms_remove(struct platform_device *pdev) |
| 3550 | { |
| 3551 | struct pm8921_bms_chip *chip = platform_get_drvdata(pdev); |
| 3552 | |
| 3553 | free_irqs(chip); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 3554 | kfree(chip->adjusted_fcc_temp_lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3555 | platform_set_drvdata(pdev, NULL); |
| 3556 | the_chip = NULL; |
| 3557 | kfree(chip); |
| 3558 | return 0; |
| 3559 | } |
| 3560 | |
Abhijeet Dharmapurikar | 1ed9418 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 3561 | static int pm8921_bms_suspend(struct device *dev) |
| 3562 | { |
| 3563 | /* |
| 3564 | * set the last reported soc to invalid, so that |
| 3565 | * next time we resume we don't want to restrict |
| 3566 | * the decrease of soc by only 1% |
| 3567 | */ |
| 3568 | last_soc = -EINVAL; |
| 3569 | |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3573 | static int pm8921_bms_resume(struct device *dev) |
| 3574 | { |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3575 | int rc; |
| 3576 | unsigned long time_since_last_recalc; |
| 3577 | unsigned long tm_now_sec; |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3578 | |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3579 | rc = get_current_time(&tm_now_sec); |
| 3580 | if (rc) { |
| 3581 | pr_err("Could not read current time: %d\n", rc); |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3582 | return 0; |
| 3583 | } |
Xiaozhe Shi | b2acb9f | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3584 | if (tm_now_sec > the_chip->last_recalc_time) { |
| 3585 | time_since_last_recalc = tm_now_sec - |
| 3586 | the_chip->last_recalc_time; |
| 3587 | pr_debug("Time since last recalc: %lu\n", |
| 3588 | time_since_last_recalc); |
| 3589 | if (time_since_last_recalc >= the_chip->soc_calc_period) { |
| 3590 | the_chip->last_recalc_time = tm_now_sec; |
| 3591 | recalculate_soc(the_chip); |
| 3592 | } |
| 3593 | } |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3594 | |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3595 | return 0; |
| 3596 | } |
| 3597 | |
| 3598 | static const struct dev_pm_ops pm8921_bms_pm_ops = { |
| 3599 | .resume = pm8921_bms_resume, |
Abhijeet Dharmapurikar | 1ed9418 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 3600 | .suspend = pm8921_bms_suspend, |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3601 | }; |
| 3602 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3603 | static struct platform_driver pm8921_bms_driver = { |
| 3604 | .probe = pm8921_bms_probe, |
| 3605 | .remove = __devexit_p(pm8921_bms_remove), |
| 3606 | .driver = { |
| 3607 | .name = PM8921_BMS_DEV_NAME, |
| 3608 | .owner = THIS_MODULE, |
Abhijeet Dharmapurikar | d1c761d | 2012-08-14 19:15:53 -0700 | [diff] [blame] | 3609 | .pm = &pm8921_bms_pm_ops, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3610 | }, |
| 3611 | }; |
| 3612 | |
| 3613 | static int __init pm8921_bms_init(void) |
| 3614 | { |
| 3615 | return platform_driver_register(&pm8921_bms_driver); |
| 3616 | } |
| 3617 | |
| 3618 | static void __exit pm8921_bms_exit(void) |
| 3619 | { |
| 3620 | platform_driver_unregister(&pm8921_bms_driver); |
| 3621 | } |
| 3622 | |
| 3623 | late_initcall(pm8921_bms_init); |
| 3624 | module_exit(pm8921_bms_exit); |
| 3625 | |
| 3626 | MODULE_LICENSE("GPL v2"); |
| 3627 | MODULE_DESCRIPTION("PMIC8921 bms driver"); |
| 3628 | MODULE_VERSION("1.0"); |
| 3629 | MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME); |