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