Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. 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 | */ |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 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 | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 23 | #include <linux/mfd/pm8xxx/ccadc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/bitops.h> |
| 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/slab.h> |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 28 | #include <linux/delay.h> |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 30 | |
| 31 | #define BMS_CONTROL 0x224 |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 32 | #define BMS_S1_DELAY 0x225 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 33 | #define BMS_OUTPUT0 0x230 |
| 34 | #define BMS_OUTPUT1 0x231 |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 35 | #define BMS_TOLERANCES 0x232 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | #define BMS_TEST1 0x237 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 38 | #define ADC_ARB_SECP_CNTRL 0x190 |
| 39 | #define ADC_ARB_SECP_AMUX_CNTRL 0x191 |
| 40 | #define ADC_ARB_SECP_ANA_PARAM 0x192 |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 41 | #define ADC_ARB_SECP_DIG_PARAM 0x193 |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 42 | #define ADC_ARB_SECP_RSV 0x194 |
| 43 | #define ADC_ARB_SECP_DATA1 0x195 |
| 44 | #define ADC_ARB_SECP_DATA0 0x196 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 46 | #define ADC_ARB_BMS_CNTRL 0x18D |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 47 | #define AMUX_TRIM_2 0x322 |
| 48 | #define TEST_PROGRAM_REV 0x339 |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 49 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | enum pmic_bms_interrupts { |
| 51 | PM8921_BMS_SBI_WRITE_OK, |
| 52 | PM8921_BMS_CC_THR, |
| 53 | PM8921_BMS_VSENSE_THR, |
| 54 | PM8921_BMS_VSENSE_FOR_R, |
| 55 | PM8921_BMS_OCV_FOR_R, |
| 56 | PM8921_BMS_GOOD_OCV, |
| 57 | PM8921_BMS_VSENSE_AVG, |
| 58 | PM_BMS_MAX_INTS, |
| 59 | }; |
| 60 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 61 | struct pm8921_soc_params { |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 62 | uint16_t last_good_ocv_raw; |
| 63 | int cc; |
| 64 | |
| 65 | int last_good_ocv_uv; |
| 66 | }; |
| 67 | |
| 68 | struct pm8921_rbatt_params { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 69 | uint16_t ocv_for_rbatt_raw; |
| 70 | uint16_t vsense_for_rbatt_raw; |
| 71 | uint16_t vbatt_for_rbatt_raw; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 72 | |
| 73 | int ocv_for_rbatt_uv; |
| 74 | int vsense_for_rbatt_uv; |
| 75 | int vbatt_for_rbatt_uv; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * struct pm8921_bms_chip - |
| 80 | * @bms_output_lock: lock to prevent concurrent bms reads |
| 81 | * @bms_100_lock: lock to prevent concurrent updates to values that force |
| 82 | * 100% charge |
| 83 | * |
| 84 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 85 | struct pm8921_bms_chip { |
| 86 | struct device *dev; |
| 87 | struct dentry *dent; |
| 88 | unsigned int r_sense; |
| 89 | unsigned int i_test; |
| 90 | unsigned int v_failure; |
| 91 | unsigned int fcc; |
| 92 | struct single_row_lut *fcc_temp_lut; |
| 93 | struct single_row_lut *fcc_sf_lut; |
| 94 | struct pc_temp_ocv_lut *pc_temp_ocv_lut; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 95 | struct sf_lut *pc_sf_lut; |
| 96 | struct sf_lut *rbatt_sf_lut; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 97 | struct work_struct calib_hkadc_work; |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 98 | struct delayed_work calib_ccadc_work; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | unsigned int calib_delay_ms; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 100 | unsigned int revision; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 101 | unsigned int xoadc_v0625_usb_present; |
| 102 | unsigned int xoadc_v0625_usb_absent; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 103 | unsigned int xoadc_v0625; |
| 104 | unsigned int xoadc_v125; |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 105 | unsigned int batt_temp_channel; |
| 106 | unsigned int vbat_channel; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 107 | unsigned int ref625mv_channel; |
| 108 | unsigned int ref1p25v_channel; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 109 | unsigned int batt_id_channel; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 110 | unsigned int pmic_bms_irq[PM_BMS_MAX_INTS]; |
| 111 | DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 112 | struct mutex bms_output_lock; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 113 | spinlock_t bms_100_lock; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 114 | struct single_row_lut *adjusted_fcc_temp_lut; |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 115 | unsigned int charging_began; |
| 116 | unsigned int start_percent; |
| 117 | unsigned int end_percent; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 118 | enum battery_type batt_type; |
Abhijeet Dharmapurikar | 57b06b5 | 2011-11-07 19:14:08 -0800 | [diff] [blame] | 119 | uint16_t ocv_reading_at_100; |
| 120 | int cc_reading_at_100; |
Abhijeet Dharmapurikar | a93ede8 | 2011-11-17 12:20:03 -0800 | [diff] [blame] | 121 | int max_voltage_uv; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 122 | |
| 123 | int batt_temp_suspend; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 124 | int soc_rbatt_suspend; |
| 125 | int default_rbatt_mohm; |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 126 | int amux_2_trim_delta; |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 127 | uint16_t prev_last_good_ocv_raw; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static struct pm8921_bms_chip *the_chip; |
| 131 | |
| 132 | #define DEFAULT_RBATT_MOHMS 128 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | #define DEFAULT_OCV_MICROVOLTS 3900000 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | #define DEFAULT_CHARGE_CYCLES 0 |
| 135 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 136 | static int last_usb_cal_delta_uv = 1800; |
| 137 | module_param(last_usb_cal_delta_uv, int, 0644); |
| 138 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 139 | static int last_chargecycles = DEFAULT_CHARGE_CYCLES; |
| 140 | static int last_charge_increase; |
| 141 | module_param(last_chargecycles, int, 0644); |
| 142 | module_param(last_charge_increase, int, 0644); |
| 143 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | static int last_rbatt = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | static int last_ocv_uv = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | static int last_soc = -EINVAL; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 147 | static int last_real_fcc_mah = -EINVAL; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 148 | static int last_real_fcc_batt_temp = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 149 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 150 | static int bms_ops_set(const char *val, const struct kernel_param *kp) |
| 151 | { |
| 152 | if (*(int *)kp->arg == -EINVAL) |
| 153 | return param_set_int(val, kp); |
| 154 | else |
| 155 | return 0; |
| 156 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 158 | static struct kernel_param_ops bms_param_ops = { |
| 159 | .set = bms_ops_set, |
| 160 | .get = param_get_int, |
| 161 | }; |
| 162 | |
| 163 | module_param_cb(last_rbatt, &bms_param_ops, &last_rbatt, 0644); |
| 164 | module_param_cb(last_ocv_uv, &bms_param_ops, &last_ocv_uv, 0644); |
| 165 | module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644); |
| 166 | |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 167 | /* |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 168 | * bms_fake_battery is set in setups where a battery emulator is used instead |
| 169 | * of a real battery. This makes the bms driver report a different/fake value |
| 170 | * regardless of the calculated state of charge. |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 171 | */ |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 172 | static int bms_fake_battery = -EINVAL; |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 173 | module_param(bms_fake_battery, int, 0644); |
| 174 | |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 175 | /* bms_start_XXX and bms_end_XXX are read only */ |
| 176 | static int bms_start_percent; |
| 177 | static int bms_start_ocv_uv; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 178 | static int bms_start_cc_uah; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 179 | static int bms_end_percent; |
| 180 | static int bms_end_ocv_uv; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 181 | static int bms_end_cc_uah; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 182 | |
| 183 | static int bms_ro_ops_set(const char *val, const struct kernel_param *kp) |
| 184 | { |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | static struct kernel_param_ops bms_ro_param_ops = { |
| 189 | .set = bms_ro_ops_set, |
| 190 | .get = param_get_int, |
| 191 | }; |
| 192 | module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644); |
| 193 | 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] | 194 | 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] | 195 | |
| 196 | module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644); |
| 197 | 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] | 198 | 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] | 199 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 200 | static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp); |
| 201 | static void readjust_fcc_table(void) |
| 202 | { |
| 203 | struct single_row_lut *temp, *old; |
| 204 | int i, fcc, ratio; |
| 205 | |
| 206 | if (!the_chip->fcc_temp_lut) { |
| 207 | pr_err("The static fcc lut table is NULL\n"); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL); |
| 212 | if (!temp) { |
| 213 | pr_err("Cannot allocate memory for adjusted fcc table\n"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp); |
| 218 | |
| 219 | temp->cols = the_chip->fcc_temp_lut->cols; |
| 220 | for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) { |
| 221 | temp->x[i] = the_chip->fcc_temp_lut->x[i]; |
| 222 | ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 223 | temp->y[i] = (ratio * last_real_fcc_mah); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 224 | temp->y[i] /= 1000; |
| 225 | pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n", |
| 226 | temp->x[i], the_chip->fcc_temp_lut->y[i], |
| 227 | temp->y[i], ratio); |
| 228 | } |
| 229 | |
| 230 | old = the_chip->adjusted_fcc_temp_lut; |
| 231 | the_chip->adjusted_fcc_temp_lut = temp; |
| 232 | kfree(old); |
| 233 | } |
| 234 | |
| 235 | static int bms_last_real_fcc_set(const char *val, |
| 236 | const struct kernel_param *kp) |
| 237 | { |
| 238 | int rc = 0; |
| 239 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 240 | if (last_real_fcc_mah == -EINVAL) |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 241 | rc = param_set_int(val, kp); |
| 242 | if (rc) { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 243 | 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] | 244 | return rc; |
| 245 | } |
| 246 | if (last_real_fcc_batt_temp != -EINVAL) |
| 247 | readjust_fcc_table(); |
| 248 | return rc; |
| 249 | } |
| 250 | static struct kernel_param_ops bms_last_real_fcc_param_ops = { |
| 251 | .set = bms_last_real_fcc_set, |
| 252 | .get = param_get_int, |
| 253 | }; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 254 | module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops, |
| 255 | &last_real_fcc_mah, 0644); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 256 | |
| 257 | static int bms_last_real_fcc_batt_temp_set(const char *val, |
| 258 | const struct kernel_param *kp) |
| 259 | { |
| 260 | int rc = 0; |
| 261 | |
| 262 | if (last_real_fcc_batt_temp == -EINVAL) |
| 263 | rc = param_set_int(val, kp); |
| 264 | if (rc) { |
| 265 | pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc); |
| 266 | return rc; |
| 267 | } |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 268 | if (last_real_fcc_mah != -EINVAL) |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 269 | readjust_fcc_table(); |
| 270 | return rc; |
| 271 | } |
| 272 | |
| 273 | static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = { |
| 274 | .set = bms_last_real_fcc_batt_temp_set, |
| 275 | .get = param_get_int, |
| 276 | }; |
| 277 | module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops, |
| 278 | &last_real_fcc_batt_temp, 0644); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 279 | |
| 280 | static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id) |
| 281 | { |
| 282 | return pm8xxx_read_irq_stat(chip->dev->parent, |
| 283 | chip->pmic_bms_irq[irq_id]); |
| 284 | } |
| 285 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 286 | static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 287 | { |
| 288 | if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) { |
| 289 | dev_dbg(chip->dev, "%s %d\n", __func__, |
| 290 | chip->pmic_bms_irq[interrupt]); |
| 291 | enable_irq(chip->pmic_bms_irq[interrupt]); |
| 292 | } |
| 293 | } |
| 294 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 295 | static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 296 | { |
| 297 | if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 298 | pr_debug("%d\n", chip->pmic_bms_irq[interrupt]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | disable_irq_nosync(chip->pmic_bms_irq[interrupt]); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr, |
| 304 | u8 mask, u8 val) |
| 305 | { |
| 306 | int rc; |
| 307 | u8 reg; |
| 308 | |
| 309 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 310 | if (rc) { |
| 311 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
| 312 | return rc; |
| 313 | } |
| 314 | reg &= ~mask; |
| 315 | reg |= val & mask; |
| 316 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 317 | if (rc) { |
| 318 | pr_err("write failed addr = %03X, rc = %d\n", addr, rc); |
| 319 | return rc; |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 324 | static int usb_chg_plugged_in(void) |
| 325 | { |
| 326 | union power_supply_propval ret = {0,}; |
| 327 | static struct power_supply *psy; |
| 328 | |
| 329 | if (psy == NULL) { |
| 330 | psy = power_supply_get_by_name("usb"); |
| 331 | if (psy == NULL) |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 336 | return 0; |
| 337 | |
| 338 | return ret.intval; |
| 339 | } |
| 340 | |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 341 | #define HOLD_OREG_DATA BIT(1) |
| 342 | static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip) |
| 343 | { |
| 344 | int rc; |
| 345 | |
| 346 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, |
| 347 | HOLD_OREG_DATA); |
| 348 | if (rc) { |
| 349 | pr_err("couldnt lock bms output rc = %d\n", rc); |
| 350 | return rc; |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip) |
| 356 | { |
| 357 | int rc; |
| 358 | |
| 359 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0); |
| 360 | if (rc) { |
| 361 | pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc); |
| 362 | return rc; |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 367 | #define SELECT_OUTPUT_DATA 0x1C |
| 368 | #define SELECT_OUTPUT_TYPE_SHIFT 2 |
| 369 | #define OCV_FOR_RBATT 0x0 |
| 370 | #define VSENSE_FOR_RBATT 0x1 |
| 371 | #define VBATT_FOR_RBATT 0x2 |
| 372 | #define CC_MSB 0x3 |
| 373 | #define CC_LSB 0x4 |
| 374 | #define LAST_GOOD_OCV_VALUE 0x5 |
| 375 | #define VSENSE_AVG 0x6 |
| 376 | #define VBATT_AVG 0x7 |
| 377 | |
| 378 | static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type, |
| 379 | int16_t *result) |
| 380 | { |
| 381 | int rc; |
| 382 | u8 reg; |
| 383 | |
| 384 | if (!result) { |
| 385 | pr_err("result pointer null\n"); |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | *result = 0; |
| 389 | if (type < OCV_FOR_RBATT || type > VBATT_AVG) { |
| 390 | pr_err("invalid type %d asked to read\n", type); |
| 391 | return -EINVAL; |
| 392 | } |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 393 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 394 | rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA, |
| 395 | type << SELECT_OUTPUT_TYPE_SHIFT); |
| 396 | if (rc) { |
| 397 | pr_err("fail to select %d type in BMS_CONTROL rc = %d\n", |
| 398 | type, rc); |
| 399 | return rc; |
| 400 | } |
| 401 | |
| 402 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, ®); |
| 403 | if (rc) { |
| 404 | pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n", |
| 405 | type, rc); |
| 406 | return rc; |
| 407 | } |
| 408 | *result = reg; |
| 409 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, ®); |
| 410 | if (rc) { |
| 411 | pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n", |
| 412 | type, rc); |
| 413 | return rc; |
| 414 | } |
| 415 | *result |= reg << 8; |
| 416 | pr_debug("type %d result %x", type, *result); |
| 417 | return 0; |
| 418 | } |
| 419 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 420 | #define V_PER_BIT_MUL_FACTOR 97656 |
| 421 | #define V_PER_BIT_DIV_FACTOR 1000 |
| 422 | #define XOADC_INTRINSIC_OFFSET 0x6000 |
| 423 | static int xoadc_reading_to_microvolt(unsigned int a) |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 424 | { |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 425 | if (a <= XOADC_INTRINSIC_OFFSET) |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 426 | return 0; |
| 427 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 428 | return (a - XOADC_INTRINSIC_OFFSET) |
| 429 | * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR; |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 430 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 431 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 432 | #define XOADC_CALIB_UV 625000 |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 433 | #define VBATT_MUL_FACTOR 3 |
| 434 | static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 435 | int usb_chg, unsigned int uv) |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 436 | { |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 437 | s64 numerator, denominator; |
| 438 | int local_delta; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 439 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 440 | if (uv == 0) |
| 441 | return 0; |
| 442 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 443 | /* dont adjust if not calibrated */ |
| 444 | if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) { |
| 445 | pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv); |
| 446 | return VBATT_MUL_FACTOR * uv; |
| 447 | } |
| 448 | |
| 449 | if (usb_chg) |
| 450 | local_delta = last_usb_cal_delta_uv; |
| 451 | else |
| 452 | local_delta = 0; |
| 453 | |
| 454 | pr_debug("using delta = %d\n", local_delta); |
| 455 | numerator = ((s64)uv - chip->xoadc_v0625 - local_delta) |
| 456 | * XOADC_CALIB_UV; |
| 457 | denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 458 | if (denominator == 0) |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 459 | return uv * VBATT_MUL_FACTOR; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 460 | return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator)) |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 461 | * VBATT_MUL_FACTOR; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 464 | #define CC_RESOLUTION_N_V1 1085069 |
| 465 | #define CC_RESOLUTION_D_V1 100000 |
| 466 | #define CC_RESOLUTION_N_V2 868056 |
| 467 | #define CC_RESOLUTION_D_V2 10000 |
| 468 | static s64 cc_to_microvolt_v1(s64 cc) |
| 469 | { |
| 470 | return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1); |
| 471 | } |
| 472 | |
| 473 | static s64 cc_to_microvolt_v2(s64 cc) |
| 474 | { |
| 475 | return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2); |
| 476 | } |
| 477 | |
| 478 | static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc) |
| 479 | { |
| 480 | /* |
| 481 | * resolution (the value of a single bit) was changed after revision 2.0 |
| 482 | * for more accurate readings |
| 483 | */ |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 484 | return (chip->revision < PM8XXX_REVISION_8921_2p0) ? |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 485 | cc_to_microvolt_v1((s64)cc) : |
| 486 | cc_to_microvolt_v2((s64)cc); |
| 487 | } |
| 488 | |
| 489 | #define CC_READING_TICKS 55 |
| 490 | #define SLEEP_CLK_HZ 32768 |
| 491 | #define SECONDS_PER_HOUR 3600 |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 492 | /** |
| 493 | * ccmicrovolt_to_nvh - |
| 494 | * @cc_uv: coulumb counter converted to uV |
| 495 | * |
| 496 | * RETURNS: coulumb counter based charge in nVh |
| 497 | * (nano Volt Hour) |
| 498 | */ |
| 499 | static s64 ccmicrovolt_to_nvh(s64 cc_uv) |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 500 | { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 501 | return div_s64(cc_uv * CC_READING_TICKS * 1000, |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 502 | SLEEP_CLK_HZ * SECONDS_PER_HOUR); |
| 503 | } |
| 504 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | /* returns the signed value read from the hardware */ |
| 506 | static int read_cc(struct pm8921_bms_chip *chip, int *result) |
| 507 | { |
| 508 | int rc; |
| 509 | uint16_t msw, lsw; |
| 510 | |
| 511 | rc = pm_bms_read_output_data(chip, CC_LSB, &lsw); |
| 512 | if (rc) { |
| 513 | pr_err("fail to read CC_LSB rc = %d\n", rc); |
| 514 | return rc; |
| 515 | } |
| 516 | rc = pm_bms_read_output_data(chip, CC_MSB, &msw); |
| 517 | if (rc) { |
| 518 | pr_err("fail to read CC_MSB rc = %d\n", rc); |
| 519 | return rc; |
| 520 | } |
| 521 | *result = msw << 16 | lsw; |
| 522 | pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result); |
| 523 | return 0; |
| 524 | } |
| 525 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 526 | static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip, |
| 527 | int result) |
| 528 | { |
| 529 | int64_t numerator; |
| 530 | int64_t denominator; |
| 531 | |
| 532 | if (chip->amux_2_trim_delta == 0) |
| 533 | return result; |
| 534 | |
| 535 | numerator = (s64)result * 1000000; |
| 536 | denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta)); |
| 537 | return div_s64(numerator, denominator); |
| 538 | } |
| 539 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 540 | static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 541 | int usb_chg, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 542 | uint16_t reading, int *result) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 543 | { |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 544 | *result = xoadc_reading_to_microvolt(reading); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 545 | pr_debug("raw = %04x vbatt = %u\n", reading, *result); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 546 | *result = adjust_xo_vbatt_reading(chip, usb_chg, *result); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 547 | pr_debug("after adj vbatt = %u\n", *result); |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 548 | *result = adjust_xo_vbatt_reading_for_mbg(chip, *result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 552 | static int convert_vsense_to_uv(struct pm8921_bms_chip *chip, |
| 553 | int16_t reading, int *result) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 554 | { |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 555 | *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 556 | pr_debug("raw = %04x vsense = %d\n", reading, *result); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 557 | *result = pm8xxx_cc_adjust_for_gain(*result); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 558 | pr_debug("after adj vsense = %d\n", *result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 562 | static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result) |
| 563 | { |
| 564 | int rc; |
| 565 | int16_t reading; |
| 566 | |
| 567 | rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading); |
| 568 | if (rc) { |
| 569 | pr_err("fail to read VSENSE_AVG rc = %d\n", rc); |
| 570 | return rc; |
| 571 | } |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 572 | |
| 573 | convert_vsense_to_uv(chip, reading, result); |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 577 | static int linear_interpolate(int y0, int x0, int y1, int x1, int x) |
| 578 | { |
| 579 | if (y0 == y1 || x == x0) |
| 580 | return y0; |
| 581 | if (x1 == x0 || x == x1) |
| 582 | return y1; |
| 583 | |
| 584 | return y0 + ((y1 - y0) * (x - x0) / (x1 - x0)); |
| 585 | } |
| 586 | |
| 587 | static int interpolate_single_lut(struct single_row_lut *lut, int x) |
| 588 | { |
| 589 | int i, result; |
| 590 | |
| 591 | if (x < lut->x[0]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 592 | pr_debug("x %d less than known range return y = %d lut = %pS\n", |
| 593 | x, lut->y[0], lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 594 | return lut->y[0]; |
| 595 | } |
| 596 | if (x > lut->x[lut->cols - 1]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 597 | pr_debug("x %d more than known range return y = %d lut = %pS\n", |
| 598 | x, lut->y[lut->cols - 1], lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 599 | return lut->y[lut->cols - 1]; |
| 600 | } |
| 601 | |
| 602 | for (i = 0; i < lut->cols; i++) |
| 603 | if (x <= lut->x[i]) |
| 604 | break; |
| 605 | if (x == lut->x[i]) { |
| 606 | result = lut->y[i]; |
| 607 | } else { |
| 608 | result = linear_interpolate( |
| 609 | lut->y[i - 1], |
| 610 | lut->x[i - 1], |
| 611 | lut->y[i], |
| 612 | lut->x[i], |
| 613 | x); |
| 614 | } |
| 615 | return result; |
| 616 | } |
| 617 | |
| 618 | static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp) |
| 619 | { |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 620 | /* batt_temp is in tenths of degC - convert it to degC for lookups */ |
| 621 | batt_temp = batt_temp/10; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 622 | return interpolate_single_lut(chip->fcc_temp_lut, batt_temp); |
| 623 | } |
| 624 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 625 | static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp) |
| 626 | { |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 627 | /* batt_temp is in tenths of degC - convert it to degC for lookups */ |
| 628 | batt_temp = batt_temp/10; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 629 | return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp); |
| 630 | } |
| 631 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 632 | static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip, |
| 633 | int cycles) |
| 634 | { |
Abhijeet Dharmapurikar | 205bbc0 | 2011-11-04 14:47:36 -0700 | [diff] [blame] | 635 | /* |
| 636 | * sf table could be null when no battery aging data is available, in |
| 637 | * that case return 100% |
| 638 | */ |
| 639 | if (chip->fcc_sf_lut) |
| 640 | return interpolate_single_lut(chip->fcc_sf_lut, cycles); |
| 641 | else |
| 642 | return 100; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 645 | static int interpolate_scalingfactor(struct pm8921_bms_chip *chip, |
| 646 | struct sf_lut *sf_lut, |
| 647 | int row_entry, int pc) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 648 | { |
Abhijeet Dharmapurikar | 4a4456e | 2011-09-27 15:10:38 -0700 | [diff] [blame] | 649 | int i, scalefactorrow1, scalefactorrow2, scalefactor; |
Abhijeet Dharmapurikar | 205bbc0 | 2011-11-04 14:47:36 -0700 | [diff] [blame] | 650 | int rows, cols; |
Abhijeet Dharmapurikar | 4a4456e | 2011-09-27 15:10:38 -0700 | [diff] [blame] | 651 | int row1 = 0; |
| 652 | int row2 = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 653 | |
Abhijeet Dharmapurikar | 205bbc0 | 2011-11-04 14:47:36 -0700 | [diff] [blame] | 654 | /* |
| 655 | * sf table could be null when no battery aging data is available, in |
| 656 | * that case return 100% |
| 657 | */ |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 658 | if (!sf_lut) |
Abhijeet Dharmapurikar | 205bbc0 | 2011-11-04 14:47:36 -0700 | [diff] [blame] | 659 | return 100; |
| 660 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 661 | rows = sf_lut->rows; |
| 662 | cols = sf_lut->cols; |
| 663 | if (pc > sf_lut->percent[0]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 664 | pr_debug("pc %d greater than known pc ranges for sfd\n", pc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 665 | row1 = 0; |
| 666 | row2 = 0; |
| 667 | } |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 668 | if (pc < sf_lut->percent[rows - 1]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 669 | pr_debug("pc %d less than known pc ranges for sf", pc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 670 | row1 = rows - 1; |
| 671 | row2 = rows - 1; |
| 672 | } |
| 673 | for (i = 0; i < rows; i++) { |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 674 | if (pc == sf_lut->percent[i]) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 675 | row1 = i; |
| 676 | row2 = i; |
| 677 | break; |
| 678 | } |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 679 | if (pc > sf_lut->percent[i]) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 680 | row1 = i - 1; |
| 681 | row2 = i; |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 686 | if (row_entry < sf_lut->row_entries[0]) |
| 687 | row_entry = sf_lut->row_entries[0]; |
| 688 | if (row_entry > sf_lut->row_entries[cols - 1]) |
| 689 | row_entry = sf_lut->row_entries[cols - 1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 690 | |
| 691 | for (i = 0; i < cols; i++) |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 692 | if (row_entry <= sf_lut->row_entries[i]) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 693 | break; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 694 | if (row_entry == sf_lut->row_entries[i]) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 695 | scalefactor = linear_interpolate( |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 696 | sf_lut->sf[row1][i], |
| 697 | sf_lut->percent[row1], |
| 698 | sf_lut->sf[row2][i], |
| 699 | sf_lut->percent[row2], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 700 | pc); |
| 701 | return scalefactor; |
| 702 | } |
| 703 | |
| 704 | scalefactorrow1 = linear_interpolate( |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 705 | sf_lut->sf[row1][i - 1], |
| 706 | sf_lut->row_entries[i - 1], |
| 707 | sf_lut->sf[row1][i], |
| 708 | sf_lut->row_entries[i], |
| 709 | row_entry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 710 | |
| 711 | scalefactorrow2 = linear_interpolate( |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 712 | sf_lut->sf[row2][i - 1], |
| 713 | sf_lut->row_entries[i - 1], |
| 714 | sf_lut->sf[row2][i], |
| 715 | sf_lut->row_entries[i], |
| 716 | row_entry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 717 | |
| 718 | scalefactor = linear_interpolate( |
| 719 | scalefactorrow1, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 720 | sf_lut->percent[row1], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 721 | scalefactorrow2, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 722 | sf_lut->percent[row2], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 723 | pc); |
| 724 | |
| 725 | return scalefactor; |
| 726 | } |
| 727 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 728 | static int is_between(int left, int right, int value) |
| 729 | { |
| 730 | if (left >= right && left >= value && value >= right) |
| 731 | return 1; |
| 732 | if (left <= right && left <= value && value <= right) |
| 733 | return 1; |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 738 | static int interpolate_pc(struct pm8921_bms_chip *chip, |
| 739 | int batt_temp, int ocv) |
| 740 | { |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 741 | int i, j, pcj, pcj_minus_one, pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 742 | int rows = chip->pc_temp_ocv_lut->rows; |
| 743 | int cols = chip->pc_temp_ocv_lut->cols; |
| 744 | |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 745 | /* batt_temp is in tenths of degC - convert it to degC for lookups */ |
| 746 | batt_temp = batt_temp/10; |
| 747 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 748 | if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 749 | pr_debug("batt_temp %d < known temp range for pc\n", batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 750 | batt_temp = chip->pc_temp_ocv_lut->temp[0]; |
| 751 | } |
| 752 | if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 753 | pr_debug("batt_temp %d > known temp range for pc\n", batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 754 | batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1]; |
| 755 | } |
| 756 | |
| 757 | for (j = 0; j < cols; j++) |
| 758 | if (batt_temp <= chip->pc_temp_ocv_lut->temp[j]) |
| 759 | break; |
| 760 | if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) { |
| 761 | /* found an exact match for temp in the table */ |
| 762 | if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j]) |
| 763 | return chip->pc_temp_ocv_lut->percent[0]; |
| 764 | if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j]) |
| 765 | return chip->pc_temp_ocv_lut->percent[rows - 1]; |
| 766 | for (i = 0; i < rows; i++) { |
| 767 | if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) { |
| 768 | if (ocv == chip->pc_temp_ocv_lut->ocv[i][j]) |
| 769 | return |
| 770 | chip->pc_temp_ocv_lut->percent[i]; |
| 771 | pc = linear_interpolate( |
| 772 | chip->pc_temp_ocv_lut->percent[i], |
| 773 | chip->pc_temp_ocv_lut->ocv[i][j], |
| 774 | chip->pc_temp_ocv_lut->percent[i - 1], |
| 775 | chip->pc_temp_ocv_lut->ocv[i - 1][j], |
| 776 | ocv); |
| 777 | return pc; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 782 | /* |
| 783 | * batt_temp is within temperature for |
| 784 | * column j-1 and j |
| 785 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 786 | if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j]) |
| 787 | return chip->pc_temp_ocv_lut->percent[0]; |
| 788 | if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1]) |
| 789 | return chip->pc_temp_ocv_lut->percent[rows - 1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 790 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 791 | pcj_minus_one = 0; |
| 792 | pcj = 0; |
| 793 | for (i = 0; i < rows-1; i++) { |
| 794 | if (pcj == 0 |
| 795 | && is_between(chip->pc_temp_ocv_lut->ocv[i][j], |
| 796 | chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) { |
| 797 | pcj = linear_interpolate( |
| 798 | chip->pc_temp_ocv_lut->percent[i], |
| 799 | chip->pc_temp_ocv_lut->ocv[i][j], |
| 800 | chip->pc_temp_ocv_lut->percent[i + 1], |
| 801 | chip->pc_temp_ocv_lut->ocv[i+1][j], |
| 802 | ocv); |
| 803 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 804 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 805 | if (pcj_minus_one == 0 |
| 806 | && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1], |
| 807 | chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 808 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 809 | pcj_minus_one = linear_interpolate( |
| 810 | chip->pc_temp_ocv_lut->percent[i], |
| 811 | chip->pc_temp_ocv_lut->ocv[i][j-1], |
| 812 | chip->pc_temp_ocv_lut->percent[i + 1], |
| 813 | chip->pc_temp_ocv_lut->ocv[i+1][j-1], |
| 814 | ocv); |
| 815 | } |
| 816 | |
| 817 | if (pcj && pcj_minus_one) { |
| 818 | pc = linear_interpolate( |
| 819 | pcj_minus_one, |
| 820 | chip->pc_temp_ocv_lut->temp[j-1], |
| 821 | pcj, |
| 822 | chip->pc_temp_ocv_lut->temp[j], |
| 823 | batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 824 | return pc; |
| 825 | } |
| 826 | } |
| 827 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 828 | if (pcj) |
| 829 | return pcj; |
| 830 | |
| 831 | if (pcj_minus_one) |
| 832 | return pcj_minus_one; |
| 833 | |
| 834 | pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%", |
| 835 | ocv, batt_temp); |
| 836 | return 100; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 839 | #define BMS_MODE_BIT BIT(6) |
| 840 | #define EN_VBAT_BIT BIT(5) |
| 841 | #define OVERRIDE_MODE_DELAY_MS 20 |
| 842 | int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua, |
| 843 | int *vbat_uv) |
| 844 | { |
| 845 | int16_t vsense_raw; |
| 846 | int16_t vbat_raw; |
| 847 | int vsense_uv; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 848 | int usb_chg; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 849 | |
| 850 | if (the_chip == NULL) { |
| 851 | pr_err("Called to early\n"); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | |
| 855 | mutex_lock(&the_chip->bms_output_lock); |
| 856 | |
| 857 | pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00); |
| 858 | pm_bms_masked_write(the_chip, BMS_CONTROL, |
| 859 | BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT); |
| 860 | |
| 861 | msleep(OVERRIDE_MODE_DELAY_MS); |
| 862 | |
| 863 | pm_bms_lock_output_data(the_chip); |
| 864 | pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw); |
| 865 | pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw); |
| 866 | pm_bms_unlock_output_data(the_chip); |
| 867 | pm_bms_masked_write(the_chip, BMS_CONTROL, |
| 868 | BMS_MODE_BIT | EN_VBAT_BIT, 0); |
| 869 | |
| 870 | pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B); |
| 871 | |
| 872 | mutex_unlock(&the_chip->bms_output_lock); |
| 873 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 874 | usb_chg = usb_chg_plugged_in(); |
| 875 | |
| 876 | 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] | 877 | convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv); |
| 878 | *ibat_ua = vsense_uv * 1000 / (int)the_chip->r_sense; |
| 879 | |
| 880 | pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x" |
| 881 | " ibat_ua = %d vbat_uv = %d\n", |
| 882 | (uint16_t)vsense_raw, (uint16_t)vbat_raw, |
| 883 | *ibat_ua, *vbat_uv); |
| 884 | return 0; |
| 885 | } |
| 886 | EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current); |
| 887 | |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 888 | static int read_rbatt_params_raw(struct pm8921_bms_chip *chip, |
| 889 | struct pm8921_rbatt_params *raw) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 890 | { |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 891 | int usb_chg; |
| 892 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 893 | mutex_lock(&chip->bms_output_lock); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 894 | pm_bms_lock_output_data(chip); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 895 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 896 | pm_bms_read_output_data(chip, |
| 897 | OCV_FOR_RBATT, &raw->ocv_for_rbatt_raw); |
| 898 | pm_bms_read_output_data(chip, |
| 899 | VBATT_FOR_RBATT, &raw->vbatt_for_rbatt_raw); |
| 900 | pm_bms_read_output_data(chip, |
| 901 | VSENSE_FOR_RBATT, &raw->vsense_for_rbatt_raw); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 902 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 903 | pm_bms_unlock_output_data(chip); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 904 | mutex_unlock(&chip->bms_output_lock); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 905 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 906 | usb_chg = usb_chg_plugged_in(); |
| 907 | convert_vbatt_raw_to_uv(chip, usb_chg, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 908 | raw->vbatt_for_rbatt_raw, &raw->vbatt_for_rbatt_uv); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 909 | convert_vbatt_raw_to_uv(chip, usb_chg, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 910 | raw->ocv_for_rbatt_raw, &raw->ocv_for_rbatt_uv); |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 911 | convert_vsense_to_uv(chip, raw->vsense_for_rbatt_raw, |
| 912 | &raw->vsense_for_rbatt_uv); |
Abhijeet Dharmapurikar | 1b8e829 | 2012-01-17 11:01:44 -0800 | [diff] [blame] | 913 | |
| 914 | pr_debug("vbatt_for_rbatt_raw = 0x%x, vbatt_for_rbatt= %duV\n", |
| 915 | raw->vbatt_for_rbatt_raw, raw->vbatt_for_rbatt_uv); |
| 916 | pr_debug("ocv_for_rbatt_raw = 0x%x, ocv_for_rbatt= %duV\n", |
| 917 | raw->ocv_for_rbatt_raw, raw->ocv_for_rbatt_uv); |
| 918 | pr_debug("vsense_for_rbatt_raw = 0x%x, vsense_for_rbatt= %duV\n", |
| 919 | raw->vsense_for_rbatt_raw, raw->vsense_for_rbatt_uv); |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 923 | #define MBG_TRANSIENT_ERROR_RAW 51 |
| 924 | static void adjust_pon_ocv_raw(struct pm8921_bms_chip *chip, |
| 925 | struct pm8921_soc_params *raw) |
| 926 | { |
| 927 | /* in 8921 parts the PON ocv is taken when the MBG is not settled. |
| 928 | * decrease the pon ocv by 15mV raw value to account for it |
| 929 | * Since a 1/3rd of vbatt is supplied to the adc the raw value |
| 930 | * needs to be adjusted by 5mV worth bits |
| 931 | */ |
| 932 | if (raw->last_good_ocv_raw >= MBG_TRANSIENT_ERROR_RAW) |
| 933 | raw->last_good_ocv_raw -= MBG_TRANSIENT_ERROR_RAW; |
| 934 | } |
| 935 | |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 936 | static int read_soc_params_raw(struct pm8921_bms_chip *chip, |
| 937 | struct pm8921_soc_params *raw) |
| 938 | { |
| 939 | int usb_chg; |
| 940 | |
| 941 | mutex_lock(&chip->bms_output_lock); |
| 942 | pm_bms_lock_output_data(chip); |
| 943 | |
| 944 | pm_bms_read_output_data(chip, |
| 945 | LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw); |
| 946 | read_cc(chip, &raw->cc); |
| 947 | |
| 948 | pm_bms_unlock_output_data(chip); |
| 949 | mutex_unlock(&chip->bms_output_lock); |
| 950 | |
| 951 | usb_chg = usb_chg_plugged_in(); |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 952 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 953 | if (chip->prev_last_good_ocv_raw == 0) { |
| 954 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 955 | adjust_pon_ocv_raw(chip, raw); |
| 956 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 957 | raw->last_good_ocv_raw, &raw->last_good_ocv_uv); |
| 958 | last_ocv_uv = raw->last_good_ocv_uv; |
| 959 | } 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] | 960 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 961 | convert_vbatt_raw_to_uv(chip, usb_chg, |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 962 | raw->last_good_ocv_raw, &raw->last_good_ocv_uv); |
Abhijeet Dharmapurikar | b7092f1 | 2012-03-14 21:19:50 -0700 | [diff] [blame] | 963 | last_ocv_uv = raw->last_good_ocv_uv; |
| 964 | } else { |
| 965 | raw->last_good_ocv_uv = last_ocv_uv; |
| 966 | } |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 967 | |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 968 | pr_debug("0p625 = %duV\n", chip->xoadc_v0625); |
| 969 | pr_debug("1p25 = %duV\n", chip->xoadc_v125); |
Abhijeet Dharmapurikar | 1b8e829 | 2012-01-17 11:01:44 -0800 | [diff] [blame] | 970 | pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n", |
| 971 | raw->last_good_ocv_raw, raw->last_good_ocv_uv); |
| 972 | pr_debug("cc_raw= 0x%x\n", raw->cc); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 976 | static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp) |
| 977 | { |
| 978 | int rbatt, scalefactor; |
| 979 | |
| 980 | rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt; |
| 981 | pr_debug("rbatt before scaling = %d\n", rbatt); |
| 982 | if (chip->rbatt_sf_lut == NULL) { |
| 983 | pr_debug("RBATT = %d\n", rbatt); |
| 984 | return rbatt; |
| 985 | } |
| 986 | |
| 987 | scalefactor = interpolate_scalingfactor(chip, chip->rbatt_sf_lut, |
| 988 | batt_temp, soc_rbatt); |
| 989 | pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n", |
| 990 | scalefactor, batt_temp, soc_rbatt); |
| 991 | rbatt = (rbatt * scalefactor) / 100; |
| 992 | |
| 993 | pr_debug("RBATT = %d\n", rbatt); |
| 994 | return rbatt; |
| 995 | } |
| 996 | |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 997 | static int calculate_rbatt_resume(struct pm8921_bms_chip *chip, |
| 998 | struct pm8921_rbatt_params *raw) |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 999 | { |
| 1000 | unsigned int r_batt; |
| 1001 | |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1002 | if (raw->ocv_for_rbatt_uv <= 0 |
| 1003 | || raw->ocv_for_rbatt_uv <= raw->vbatt_for_rbatt_uv |
| 1004 | || raw->vsense_for_rbatt_raw <= 0) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1005 | pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d," |
| 1006 | "vsen = %d\n", |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1007 | raw->ocv_for_rbatt_uv, |
| 1008 | raw->vbatt_for_rbatt_uv, |
| 1009 | raw->vsense_for_rbatt_raw); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1010 | return -EINVAL; |
| 1011 | } |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1012 | r_batt = ((raw->ocv_for_rbatt_uv - raw->vbatt_for_rbatt_uv) |
| 1013 | * chip->r_sense) / raw->vsense_for_rbatt_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1014 | pr_debug("r_batt = %umilliOhms", r_batt); |
| 1015 | return r_batt; |
| 1016 | } |
| 1017 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1018 | 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] | 1019 | int chargecycles) |
| 1020 | { |
| 1021 | int initfcc, result, scalefactor = 0; |
| 1022 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1023 | if (chip->adjusted_fcc_temp_lut == NULL) { |
| 1024 | initfcc = interpolate_fcc(chip, batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1025 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1026 | scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1027 | |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1028 | /* Multiply the initial FCC value by the scale factor. */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1029 | result = (initfcc * scalefactor * 1000) / 100; |
| 1030 | pr_debug("fcc = %d uAh\n", result); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1031 | return result; |
| 1032 | } else { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1033 | return 1000 * interpolate_fcc_adjusted(chip, batt_temp); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1034 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1037 | static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts) |
| 1038 | { |
| 1039 | int rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1040 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1041 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1042 | rc = pm8xxx_adc_read(chip->vbat_channel, &result); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1043 | if (rc) { |
| 1044 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1045 | chip->vbat_channel, rc); |
| 1046 | return rc; |
| 1047 | } |
| 1048 | pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical, |
| 1049 | result.measurement); |
| 1050 | *uvolts = (int)result.physical; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv) |
| 1055 | { |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1056 | int vbatt, rbatt, ibatt_ua, rc; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1057 | |
| 1058 | rc = get_battery_uvolts(chip, &vbatt); |
| 1059 | if (rc) { |
| 1060 | pr_err("failed to read vbatt from adc rc = %d\n", rc); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1061 | return rc; |
| 1062 | } |
| 1063 | |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1064 | rc = pm8921_bms_get_battery_current(&ibatt_ua); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1065 | if (rc) { |
| 1066 | pr_err("failed to read batt current rc = %d\n", rc); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1067 | return rc; |
| 1068 | } |
| 1069 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1070 | rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt; |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1071 | *ocv = vbatt + (ibatt_ua * rbatt)/1000; |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1075 | static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp, |
| 1076 | int chargecycles) |
| 1077 | { |
| 1078 | int pc, scalefactor; |
| 1079 | |
| 1080 | pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000); |
| 1081 | pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n", |
| 1082 | pc, ocv_uv, batt_temp); |
| 1083 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1084 | scalefactor = interpolate_scalingfactor(chip, |
| 1085 | chip->pc_sf_lut, chargecycles, pc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1086 | pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp); |
| 1087 | |
| 1088 | /* Multiply the initial FCC value by the scale factor. */ |
| 1089 | pc = (pc * scalefactor) / 100; |
| 1090 | return pc; |
| 1091 | } |
| 1092 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1093 | /** |
| 1094 | * calculate_cc_uah - |
| 1095 | * @chip: the bms chip pointer |
| 1096 | * @cc: the cc reading from bms h/w |
| 1097 | * @val: return value |
| 1098 | * @coulumb_counter: adjusted coulumb counter for 100% |
| 1099 | * |
| 1100 | * RETURNS: in val pointer coulumb counter based charger in uAh |
| 1101 | * (micro Amp hour) |
| 1102 | */ |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1103 | 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] | 1104 | { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1105 | int64_t cc_voltage_uv, cc_nvh, cc_uah; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1106 | |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1107 | cc_voltage_uv = cc; |
| 1108 | cc_voltage_uv -= chip->cc_reading_at_100; |
| 1109 | pr_debug("cc = %d. after subtracting %d cc = %lld\n", |
| 1110 | cc, chip->cc_reading_at_100, |
| 1111 | cc_voltage_uv); |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 1112 | cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 1113 | cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1114 | pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1115 | cc_nvh = ccmicrovolt_to_nvh(cc_voltage_uv); |
| 1116 | pr_debug("cc_nvh = %lld nano_volt_hour\n", cc_nvh); |
| 1117 | cc_uah = div_s64(cc_nvh, chip->r_sense); |
| 1118 | *val = cc_uah; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1121 | static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1122 | int rbatt, int fcc_uah, |
| 1123 | int batt_temp, int chargecycles) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1124 | { |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1125 | int voltage_unusable_uv, pc_unusable; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1126 | |
| 1127 | /* calculate unusable charge */ |
| 1128 | voltage_unusable_uv = (rbatt * chip->i_test) |
| 1129 | + (chip->v_failure * 1000); |
| 1130 | pc_unusable = calculate_pc(chip, voltage_unusable_uv, |
| 1131 | batt_temp, chargecycles); |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1132 | pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n", |
| 1133 | rbatt, voltage_unusable_uv, pc_unusable); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1134 | return (fcc_uah * pc_unusable) / 100; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1135 | } |
Abhijeet Dharmapurikar | 52aa968 | 2011-07-20 14:05:47 -0700 | [diff] [blame] | 1136 | |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1137 | /* calculate remainging charge at the time of ocv */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1138 | static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1139 | struct pm8921_soc_params *raw, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1140 | int fcc_uah, int batt_temp, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1141 | int chargecycles) |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1142 | { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1143 | int ocv, pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1144 | |
| 1145 | /* calculate remainging charge */ |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1146 | ocv = 0; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1147 | if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) { |
| 1148 | chip->ocv_reading_at_100 = 0; |
| 1149 | chip->cc_reading_at_100 = 0; |
| 1150 | ocv = raw->last_good_ocv_uv; |
| 1151 | } else { |
| 1152 | /* |
| 1153 | * force 100% ocv by selecting the highest voltage the |
| 1154 | * battery could every reach |
| 1155 | */ |
| 1156 | ocv = chip->max_voltage_uv; |
| 1157 | } |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1158 | |
| 1159 | if (ocv == 0) { |
| 1160 | ocv = last_ocv_uv; |
| 1161 | pr_debug("ocv not available using last_ocv_uv=%d\n", ocv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1162 | } |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1163 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1164 | pc = calculate_pc(chip, ocv, batt_temp, chargecycles); |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1165 | pr_debug("ocv = %d pc = %d\n", ocv, pc); |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1166 | return (fcc_uah * pc) / 100; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1169 | static void calculate_soc_params(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1170 | struct pm8921_soc_params *raw, |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1171 | int batt_temp, int chargecycles, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1172 | int *fcc_uah, |
| 1173 | int *unusable_charge_uah, |
| 1174 | int *remaining_charge_uah, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1175 | int *cc_uah, |
| 1176 | int *rbatt) |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1177 | { |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1178 | unsigned long flags; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1179 | int soc_rbatt; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1180 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1181 | *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles); |
| 1182 | pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n", |
| 1183 | *fcc_uah, batt_temp, chargecycles); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1184 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1185 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1186 | spin_lock_irqsave(&chip->bms_100_lock, flags); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1187 | /* calculate remainging charge */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1188 | *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw, |
| 1189 | *fcc_uah, batt_temp, chargecycles); |
| 1190 | pr_debug("RC = %uuAh\n", *remaining_charge_uah); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1191 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1192 | /* calculate cc micro_volt_hour */ |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1193 | calculate_cc_uah(chip, raw->cc, cc_uah); |
| 1194 | pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n", |
| 1195 | *cc_uah, raw->cc, |
| 1196 | (int64_t)raw->cc - chip->cc_reading_at_100, |
| 1197 | chip->cc_reading_at_100); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1198 | spin_unlock_irqrestore(&chip->bms_100_lock, flags); |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1199 | |
| 1200 | soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah; |
| 1201 | if (soc_rbatt < 0) |
| 1202 | soc_rbatt = 0; |
| 1203 | *rbatt = get_rbatt(chip, soc_rbatt, batt_temp); |
| 1204 | |
| 1205 | *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt, |
| 1206 | *fcc_uah, batt_temp, chargecycles); |
| 1207 | pr_debug("UUC = %uuAh\n", *unusable_charge_uah); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1210 | static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1211 | struct pm8921_soc_params *raw, |
| 1212 | int batt_temp, int chargecycles, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1213 | int *ret_fcc_uah) |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1214 | { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1215 | int fcc_uah, unusable_charge_uah; |
| 1216 | int remaining_charge_uah; |
| 1217 | int cc_uah; |
| 1218 | int real_fcc_uah; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1219 | int rbatt; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1220 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1221 | calculate_soc_params(chip, raw, batt_temp, chargecycles, |
| 1222 | &fcc_uah, |
| 1223 | &unusable_charge_uah, |
| 1224 | &remaining_charge_uah, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1225 | &cc_uah, |
| 1226 | &rbatt); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1227 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1228 | real_fcc_uah = remaining_charge_uah - cc_uah; |
| 1229 | *ret_fcc_uah = fcc_uah; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1230 | pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n", |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1231 | real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah); |
| 1232 | return real_fcc_uah; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1233 | } |
| 1234 | /* |
| 1235 | * Remaining Usable Charge = remaining_charge (charge at ocv instance) |
| 1236 | * - coloumb counter charge |
| 1237 | * - unusable charge (due to battery resistance) |
| 1238 | * SOC% = (remaining usable charge/ fcc - usable_charge); |
| 1239 | */ |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1240 | static int calculate_state_of_charge(struct pm8921_bms_chip *chip, |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1241 | struct pm8921_soc_params *raw, |
| 1242 | int batt_temp, int chargecycles) |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1243 | { |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1244 | int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah; |
| 1245 | int remaining_charge_uah, soc; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1246 | int update_userspace = 1; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1247 | int cc_uah; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1248 | int rbatt; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 1249 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1250 | calculate_soc_params(chip, raw, batt_temp, chargecycles, |
| 1251 | &fcc_uah, |
| 1252 | &unusable_charge_uah, |
| 1253 | &remaining_charge_uah, |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1254 | &cc_uah, |
| 1255 | &rbatt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1256 | |
| 1257 | /* calculate remaining usable charge */ |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1258 | remaining_usable_charge_uah = remaining_charge_uah |
| 1259 | - cc_uah |
| 1260 | - unusable_charge_uah; |
| 1261 | |
| 1262 | pr_debug("RUC = %duAh\n", remaining_usable_charge_uah); |
Abhijeet Dharmapurikar | bbae8831 | 2012-02-14 18:56:42 -0800 | [diff] [blame] | 1263 | if (fcc_uah - unusable_charge_uah <= 0) { |
| 1264 | pr_warn("FCC = %duAh, UUC = %duAh forcing soc = 0\n", |
| 1265 | fcc_uah, unusable_charge_uah); |
| 1266 | soc = 0; |
| 1267 | } else { |
| 1268 | soc = (remaining_usable_charge_uah * 100) |
| 1269 | / (fcc_uah - unusable_charge_uah); |
| 1270 | } |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1271 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1272 | if (soc > 100) |
| 1273 | soc = 100; |
| 1274 | pr_debug("SOC = %u%%\n", soc); |
| 1275 | |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 1276 | if (bms_fake_battery != -EINVAL) { |
| 1277 | pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery); |
| 1278 | return bms_fake_battery; |
| 1279 | } |
| 1280 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1281 | if (soc < 0) { |
Abhijeet Dharmapurikar | 21ebadd | 2011-09-28 21:14:16 -0700 | [diff] [blame] | 1282 | pr_err("bad rem_usb_chg = %d rem_chg %d," |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1283 | "cc_uah %d, unusb_chg %d\n", |
| 1284 | remaining_usable_charge_uah, |
| 1285 | remaining_charge_uah, |
| 1286 | cc_uah, unusable_charge_uah); |
| 1287 | |
Abhijeet Dharmapurikar | 21ebadd | 2011-09-28 21:14:16 -0700 | [diff] [blame] | 1288 | pr_err("for bad rem_usb_chg last_ocv_uv = %d" |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1289 | "chargecycles = %d, batt_temp = %d" |
| 1290 | "fcc = %d soc =%d\n", |
| 1291 | last_ocv_uv, chargecycles, batt_temp, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1292 | fcc_uah, soc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1293 | update_userspace = 0; |
Abhijeet Dharmapurikar | 95971a8 | 2011-12-06 18:23:57 -0800 | [diff] [blame] | 1294 | soc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1297 | if (last_soc == -EINVAL || soc <= last_soc) { |
| 1298 | last_soc = update_userspace ? soc : last_soc; |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 1299 | return soc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1300 | } |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1301 | |
| 1302 | /* |
| 1303 | * soc > last_soc |
| 1304 | * the device must be charging for reporting a higher soc, if not ignore |
| 1305 | * this soc and continue reporting the last_soc |
| 1306 | */ |
Abhijeet Dharmapurikar | dba91f4 | 2011-12-21 16:56:13 -0800 | [diff] [blame] | 1307 | if (the_chip->start_percent != -EINVAL) { |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1308 | last_soc = soc; |
| 1309 | } else { |
| 1310 | pr_debug("soc = %d reporting last_soc = %d\n", soc, last_soc); |
| 1311 | soc = last_soc; |
| 1312 | } |
| 1313 | |
Abhijeet Dharmapurikar | 986bd8a | 2012-01-05 15:15:15 -0800 | [diff] [blame] | 1314 | return soc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1315 | } |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1316 | #define MIN_DELTA_625_UV 1000 |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1317 | static void calib_hkadc(struct pm8921_bms_chip *chip) |
| 1318 | { |
| 1319 | int voltage, rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1320 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1321 | int usb_chg; |
| 1322 | int this_delta; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1323 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1324 | rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1325 | if (rc) { |
| 1326 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 1327 | return; |
| 1328 | } |
Abhijeet Dharmapurikar | 619cb0c | 2011-12-15 15:28:14 -0800 | [diff] [blame] | 1329 | voltage = xoadc_reading_to_microvolt(result.adc_code); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1330 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 1331 | pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n", |
| 1332 | result.adc_code, voltage, result.measurement); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1333 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1334 | chip->xoadc_v125 = voltage; |
| 1335 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1336 | rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1337 | if (rc) { |
| 1338 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 1339 | return; |
| 1340 | } |
Abhijeet Dharmapurikar | 619cb0c | 2011-12-15 15:28:14 -0800 | [diff] [blame] | 1341 | voltage = xoadc_reading_to_microvolt(result.adc_code); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1342 | |
| 1343 | usb_chg = usb_chg_plugged_in(); |
| 1344 | pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld " |
| 1345 | "usb_chg = %d\n", |
| 1346 | result.adc_code, voltage, result.measurement, |
| 1347 | usb_chg); |
| 1348 | |
| 1349 | if (usb_chg) |
| 1350 | chip->xoadc_v0625_usb_present = voltage; |
| 1351 | else |
| 1352 | chip->xoadc_v0625_usb_absent = voltage; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1353 | |
| 1354 | chip->xoadc_v0625 = voltage; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1355 | if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) { |
| 1356 | this_delta = chip->xoadc_v0625_usb_present |
| 1357 | - chip->xoadc_v0625_usb_absent; |
| 1358 | pr_debug("this_delta= %duV\n", this_delta); |
| 1359 | if (this_delta > MIN_DELTA_625_UV) |
| 1360 | last_usb_cal_delta_uv = this_delta; |
| 1361 | pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n", |
| 1362 | chip->xoadc_v0625_usb_present, |
| 1363 | chip->xoadc_v0625_usb_absent, |
| 1364 | last_usb_cal_delta_uv); |
| 1365 | } |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | static void calibrate_hkadc_work(struct work_struct *work) |
| 1369 | { |
| 1370 | struct pm8921_bms_chip *chip = container_of(work, |
| 1371 | struct pm8921_bms_chip, calib_hkadc_work); |
| 1372 | |
| 1373 | calib_hkadc(chip); |
| 1374 | } |
| 1375 | |
Abhijeet Dharmapurikar | 1b8e829 | 2012-01-17 11:01:44 -0800 | [diff] [blame] | 1376 | void pm8921_bms_calibrate_hkadc(void) |
| 1377 | { |
| 1378 | schedule_work(&the_chip->calib_hkadc_work); |
| 1379 | } |
| 1380 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 1381 | static void calibrate_ccadc_work(struct work_struct *work) |
| 1382 | { |
| 1383 | struct pm8921_bms_chip *chip = container_of(work, |
| 1384 | struct pm8921_bms_chip, calib_ccadc_work.work); |
| 1385 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 1386 | pm8xxx_calib_ccadc(); |
Abhijeet Dharmapurikar | 1b8e829 | 2012-01-17 11:01:44 -0800 | [diff] [blame] | 1387 | calib_hkadc(chip); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 1388 | schedule_delayed_work(&chip->calib_ccadc_work, |
| 1389 | round_jiffies_relative(msecs_to_jiffies |
| 1390 | (chip->calib_delay_ms))); |
| 1391 | } |
| 1392 | |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1393 | int pm8921_bms_get_vsense_avg(int *result) |
| 1394 | { |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1395 | int rc = -EINVAL; |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1396 | |
| 1397 | if (the_chip) { |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1398 | mutex_lock(&the_chip->bms_output_lock); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1399 | pm_bms_lock_output_data(the_chip); |
| 1400 | rc = read_vsense_avg(the_chip, result); |
| 1401 | pm_bms_unlock_output_data(the_chip); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1402 | mutex_unlock(&the_chip->bms_output_lock); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1403 | } |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1404 | |
| 1405 | pr_err("called before initialization\n"); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1406 | return rc; |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1407 | } |
| 1408 | EXPORT_SYMBOL(pm8921_bms_get_vsense_avg); |
| 1409 | |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1410 | int pm8921_bms_get_battery_current(int *result_ua) |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1411 | { |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 1412 | int vsense; |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1413 | |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1414 | if (!the_chip) { |
| 1415 | pr_err("called before initialization\n"); |
| 1416 | return -EINVAL; |
| 1417 | } |
| 1418 | if (the_chip->r_sense == 0) { |
| 1419 | pr_err("r_sense is zero\n"); |
| 1420 | return -EINVAL; |
| 1421 | } |
| 1422 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1423 | mutex_lock(&the_chip->bms_output_lock); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1424 | pm_bms_lock_output_data(the_chip); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 1425 | read_vsense_avg(the_chip, &vsense); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame] | 1426 | pm_bms_unlock_output_data(the_chip); |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1427 | mutex_unlock(&the_chip->bms_output_lock); |
| 1428 | pr_debug("vsense=%duV\n", vsense); |
Abhijeet Dharmapurikar | a7a1c6b | 2011-08-17 10:04:58 -0700 | [diff] [blame] | 1429 | /* cast for signed division */ |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 1430 | *result_ua = vsense * 1000 / (int)the_chip->r_sense; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1431 | pr_debug("ibat=%duA\n", *result_ua); |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1432 | return 0; |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(pm8921_bms_get_battery_current); |
| 1435 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1436 | int pm8921_bms_get_percent_charge(void) |
| 1437 | { |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1438 | int batt_temp, rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1439 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1440 | struct pm8921_soc_params raw; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1441 | |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1442 | if (!the_chip) { |
| 1443 | pr_err("called before initialization\n"); |
| 1444 | return -EINVAL; |
| 1445 | } |
| 1446 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1447 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1448 | if (rc) { |
| 1449 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1450 | the_chip->batt_temp_channel, rc); |
| 1451 | return rc; |
| 1452 | } |
| 1453 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1454 | result.measurement); |
| 1455 | batt_temp = (int)result.physical; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1456 | |
| 1457 | read_soc_params_raw(the_chip, &raw); |
| 1458 | |
| 1459 | return calculate_state_of_charge(the_chip, &raw, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1460 | batt_temp, last_chargecycles); |
| 1461 | } |
| 1462 | EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge); |
| 1463 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1464 | int pm8921_bms_get_rbatt(void) |
| 1465 | { |
| 1466 | int batt_temp, rc; |
| 1467 | struct pm8xxx_adc_chan_result result; |
| 1468 | struct pm8921_soc_params raw; |
| 1469 | int fcc_uah; |
| 1470 | int unusable_charge_uah; |
| 1471 | int remaining_charge_uah; |
| 1472 | int cc_uah; |
| 1473 | int rbatt; |
| 1474 | |
| 1475 | if (!the_chip) { |
| 1476 | pr_err("called before initialization\n"); |
| 1477 | return -EINVAL; |
| 1478 | } |
| 1479 | |
| 1480 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1481 | if (rc) { |
| 1482 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1483 | the_chip->batt_temp_channel, rc); |
| 1484 | return rc; |
| 1485 | } |
| 1486 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1487 | result.measurement); |
| 1488 | batt_temp = (int)result.physical; |
| 1489 | |
| 1490 | read_soc_params_raw(the_chip, &raw); |
| 1491 | |
| 1492 | calculate_soc_params(the_chip, &raw, batt_temp, last_chargecycles, |
| 1493 | &fcc_uah, |
| 1494 | &unusable_charge_uah, |
| 1495 | &remaining_charge_uah, |
| 1496 | &cc_uah, |
| 1497 | &rbatt); |
| 1498 | return rbatt; |
| 1499 | } |
| 1500 | EXPORT_SYMBOL_GPL(pm8921_bms_get_rbatt); |
| 1501 | |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 1502 | int pm8921_bms_get_fcc(void) |
| 1503 | { |
| 1504 | int batt_temp, rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1505 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 1506 | |
| 1507 | if (!the_chip) { |
| 1508 | pr_err("called before initialization\n"); |
| 1509 | return -EINVAL; |
| 1510 | } |
| 1511 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1512 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 1513 | if (rc) { |
| 1514 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1515 | the_chip->batt_temp_channel, rc); |
| 1516 | return rc; |
| 1517 | } |
| 1518 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1519 | result.measurement); |
| 1520 | batt_temp = (int)result.physical; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1521 | return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles); |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 1522 | } |
| 1523 | EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc); |
| 1524 | |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 1525 | #define IBAT_TOL_MASK 0x0F |
| 1526 | #define OCV_TOL_MASK 0xF0 |
| 1527 | #define IBAT_TOL_DEFAULT 0x03 |
| 1528 | #define IBAT_TOL_NOCHG 0x0F |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1529 | void pm8921_bms_charging_began(void) |
| 1530 | { |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1531 | int batt_temp, rc; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 1532 | struct pm8xxx_adc_chan_result result; |
| 1533 | struct pm8921_soc_params raw; |
| 1534 | |
| 1535 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1536 | if (rc) { |
| 1537 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1538 | the_chip->batt_temp_channel, rc); |
| 1539 | return; |
| 1540 | } |
| 1541 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1542 | result.measurement); |
| 1543 | batt_temp = (int)result.physical; |
| 1544 | |
| 1545 | read_soc_params_raw(the_chip, &raw); |
| 1546 | |
| 1547 | the_chip->start_percent = calculate_state_of_charge(the_chip, &raw, |
| 1548 | batt_temp, last_chargecycles); |
| 1549 | bms_start_percent = the_chip->start_percent; |
| 1550 | bms_start_ocv_uv = raw.last_good_ocv_uv; |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1551 | calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah); |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 1552 | pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 1553 | IBAT_TOL_MASK, IBAT_TOL_DEFAULT); |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1554 | pr_debug("start_percent = %u%%\n", the_chip->start_percent); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1555 | } |
| 1556 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_began); |
| 1557 | |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 1558 | #define DELTA_FCC_PERCENT 3 |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 1559 | #define MIN_START_PERCENT_FOR_LEARNING 30 |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1560 | void pm8921_bms_charging_end(int is_battery_full) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1561 | { |
Abhijeet Dharmapurikar | da8c3d5 | 2012-01-06 14:23:13 -0800 | [diff] [blame] | 1562 | int batt_temp, rc; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1563 | struct pm8xxx_adc_chan_result result; |
| 1564 | struct pm8921_soc_params raw; |
| 1565 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1566 | if (the_chip == NULL) |
| 1567 | return; |
| 1568 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1569 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1570 | if (rc) { |
| 1571 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1572 | the_chip->batt_temp_channel, rc); |
| 1573 | return; |
| 1574 | } |
| 1575 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1576 | result.measurement); |
| 1577 | batt_temp = (int)result.physical; |
| 1578 | |
| 1579 | read_soc_params_raw(the_chip, &raw); |
| 1580 | |
Abhijeet Dharmapurikar | 57aeb92 | 2012-01-25 13:22:26 -0800 | [diff] [blame] | 1581 | calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah); |
| 1582 | |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 1583 | if (is_battery_full |
| 1584 | && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) { |
Abhijeet Dharmapurikar | 57b06b5 | 2011-11-07 19:14:08 -0800 | [diff] [blame] | 1585 | unsigned long flags; |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1586 | int fcc_uah, new_fcc_uah, delta_fcc_uah; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1587 | |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1588 | new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw, |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 1589 | batt_temp, last_chargecycles, |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 1590 | &fcc_uah); |
| 1591 | delta_fcc_uah = new_fcc_uah - fcc_uah; |
| 1592 | if (delta_fcc_uah < 0) |
| 1593 | delta_fcc_uah = -delta_fcc_uah; |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 1594 | |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 1595 | if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) { |
| 1596 | /* new_fcc_uah is outside the scope limit it */ |
| 1597 | if (new_fcc_uah > fcc_uah) |
| 1598 | new_fcc_uah |
| 1599 | = (fcc_uah + DELTA_FCC_PERCENT * fcc_uah); |
| 1600 | else |
| 1601 | new_fcc_uah |
| 1602 | = (fcc_uah - DELTA_FCC_PERCENT * fcc_uah); |
| 1603 | |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 1604 | pr_debug("delta_fcc=%d > %d percent of fcc=%d" |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 1605 | "restring it to %d\n", |
| 1606 | delta_fcc_uah, DELTA_FCC_PERCENT, |
| 1607 | fcc_uah, new_fcc_uah); |
Abhijeet Dharmapurikar | 8309df4 | 2011-11-16 21:04:09 -0800 | [diff] [blame] | 1608 | } |
Abhijeet Dharmapurikar | 57b06b5 | 2011-11-07 19:14:08 -0800 | [diff] [blame] | 1609 | |
Abhijeet Dharmapurikar | fe36d23 | 2012-03-06 20:17:54 -0800 | [diff] [blame] | 1610 | last_real_fcc_mah = new_fcc_uah/1000; |
| 1611 | last_real_fcc_batt_temp = batt_temp; |
| 1612 | readjust_fcc_table(); |
| 1613 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1614 | spin_lock_irqsave(&the_chip->bms_100_lock, flags); |
| 1615 | the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw; |
| 1616 | the_chip->cc_reading_at_100 = raw.cc; |
| 1617 | spin_unlock_irqrestore(&the_chip->bms_100_lock, flags); |
| 1618 | pr_debug("EOC ocv_reading = 0x%x cc = %d\n", |
Abhijeet Dharmapurikar | 57b06b5 | 2011-11-07 19:14:08 -0800 | [diff] [blame] | 1619 | the_chip->ocv_reading_at_100, |
| 1620 | the_chip->cc_reading_at_100); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1623 | the_chip->end_percent = calculate_state_of_charge(the_chip, &raw, |
| 1624 | batt_temp, last_chargecycles); |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 1625 | |
| 1626 | bms_end_percent = the_chip->end_percent; |
| 1627 | bms_end_ocv_uv = raw.last_good_ocv_uv; |
Abhijeet Dharmapurikar | ab83331 | 2011-11-22 18:26:09 -0800 | [diff] [blame] | 1628 | |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1629 | if (the_chip->end_percent > the_chip->start_percent) { |
Abhijeet Dharmapurikar | 8a113f8 | 2012-01-19 10:58:45 -0800 | [diff] [blame] | 1630 | last_charge_increase += |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1631 | the_chip->end_percent - the_chip->start_percent; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1632 | if (last_charge_increase > 100) { |
| 1633 | last_chargecycles++; |
| 1634 | last_charge_increase = last_charge_increase % 100; |
| 1635 | } |
| 1636 | } |
| 1637 | pr_debug("end_percent = %u%% last_charge_increase = %d" |
| 1638 | "last_chargecycles = %d\n", |
Abhijeet Dharmapurikar | 2ce2cc1 | 2011-11-02 15:40:29 -0700 | [diff] [blame] | 1639 | the_chip->end_percent, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1640 | last_charge_increase, |
| 1641 | last_chargecycles); |
Abhijeet Dharmapurikar | dba91f4 | 2011-12-21 16:56:13 -0800 | [diff] [blame] | 1642 | the_chip->start_percent = -EINVAL; |
| 1643 | the_chip->end_percent = -EINVAL; |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 1644 | pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 1645 | IBAT_TOL_MASK, IBAT_TOL_NOCHG); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1646 | } |
| 1647 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_end); |
| 1648 | |
| 1649 | static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data) |
| 1650 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1651 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1652 | return IRQ_HANDLED; |
| 1653 | } |
| 1654 | |
| 1655 | static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data) |
| 1656 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1657 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1658 | return IRQ_HANDLED; |
| 1659 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1660 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1661 | static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data) |
| 1662 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1663 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1664 | return IRQ_HANDLED; |
| 1665 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1666 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1667 | static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data) |
| 1668 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1669 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1670 | return IRQ_HANDLED; |
| 1671 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1672 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1673 | static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data) |
| 1674 | { |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1675 | struct pm8921_bms_chip *chip = data; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1676 | |
| 1677 | pr_debug("irq = %d triggered", irq); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1678 | schedule_work(&chip->calib_hkadc_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1679 | return IRQ_HANDLED; |
| 1680 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1681 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1682 | static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data) |
| 1683 | { |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1684 | struct pm8921_bms_chip *chip = data; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1685 | |
| 1686 | pr_debug("irq = %d triggered", irq); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1687 | schedule_work(&chip->calib_hkadc_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1688 | return IRQ_HANDLED; |
| 1689 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1690 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1691 | static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data) |
| 1692 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1693 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1694 | return IRQ_HANDLED; |
| 1695 | } |
| 1696 | |
| 1697 | struct pm_bms_irq_init_data { |
| 1698 | unsigned int irq_id; |
| 1699 | char *name; |
| 1700 | unsigned long flags; |
| 1701 | irqreturn_t (*handler)(int, void *); |
| 1702 | }; |
| 1703 | |
| 1704 | #define BMS_IRQ(_id, _flags, _handler) \ |
| 1705 | { \ |
| 1706 | .irq_id = _id, \ |
| 1707 | .name = #_id, \ |
| 1708 | .flags = _flags, \ |
| 1709 | .handler = _handler, \ |
| 1710 | } |
| 1711 | |
| 1712 | struct pm_bms_irq_init_data bms_irq_data[] = { |
| 1713 | BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING, |
| 1714 | pm8921_bms_sbi_write_ok_handler), |
| 1715 | BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING, |
| 1716 | pm8921_bms_cc_thr_handler), |
| 1717 | BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING, |
| 1718 | pm8921_bms_vsense_thr_handler), |
| 1719 | BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING, |
| 1720 | pm8921_bms_vsense_for_r_handler), |
| 1721 | BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING, |
| 1722 | pm8921_bms_ocv_for_r_handler), |
| 1723 | BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING, |
| 1724 | pm8921_bms_good_ocv_handler), |
| 1725 | BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING, |
| 1726 | pm8921_bms_vsense_avg_handler), |
| 1727 | }; |
| 1728 | |
| 1729 | static void free_irqs(struct pm8921_bms_chip *chip) |
| 1730 | { |
| 1731 | int i; |
| 1732 | |
| 1733 | for (i = 0; i < PM_BMS_MAX_INTS; i++) |
| 1734 | if (chip->pmic_bms_irq[i]) { |
| 1735 | free_irq(chip->pmic_bms_irq[i], NULL); |
| 1736 | chip->pmic_bms_irq[i] = 0; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | static int __devinit request_irqs(struct pm8921_bms_chip *chip, |
| 1741 | struct platform_device *pdev) |
| 1742 | { |
| 1743 | struct resource *res; |
| 1744 | int ret, i; |
| 1745 | |
| 1746 | ret = 0; |
| 1747 | bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS); |
| 1748 | |
| 1749 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 1750 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1751 | bms_irq_data[i].name); |
| 1752 | if (res == NULL) { |
| 1753 | pr_err("couldn't find %s\n", bms_irq_data[i].name); |
| 1754 | goto err_out; |
| 1755 | } |
| 1756 | ret = request_irq(res->start, bms_irq_data[i].handler, |
| 1757 | bms_irq_data[i].flags, |
| 1758 | bms_irq_data[i].name, chip); |
| 1759 | if (ret < 0) { |
| 1760 | pr_err("couldn't request %d (%s) %d\n", res->start, |
| 1761 | bms_irq_data[i].name, ret); |
| 1762 | goto err_out; |
| 1763 | } |
| 1764 | chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start; |
| 1765 | pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id); |
| 1766 | } |
| 1767 | return 0; |
| 1768 | |
| 1769 | err_out: |
| 1770 | free_irqs(chip); |
| 1771 | return -EINVAL; |
| 1772 | } |
| 1773 | |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1774 | static int pm8921_bms_suspend(struct device *dev) |
| 1775 | { |
| 1776 | int rc; |
| 1777 | struct pm8xxx_adc_chan_result result; |
| 1778 | struct pm8921_bms_chip *chip = dev_get_drvdata(dev); |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1779 | struct pm8921_soc_params raw; |
| 1780 | int fcc_uah; |
| 1781 | int remaining_charge_uah; |
| 1782 | int cc_uah; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1783 | |
| 1784 | chip->batt_temp_suspend = 0; |
| 1785 | rc = pm8xxx_adc_read(chip->batt_temp_channel, &result); |
| 1786 | if (rc) { |
| 1787 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1788 | chip->batt_temp_channel, rc); |
| 1789 | } |
| 1790 | chip->batt_temp_suspend = (int)result.physical; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1791 | read_soc_params_raw(chip, &raw); |
| 1792 | |
| 1793 | fcc_uah = calculate_fcc_uah(chip, |
| 1794 | chip->batt_temp_suspend, last_chargecycles); |
| 1795 | pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n", |
| 1796 | fcc_uah, chip->batt_temp_suspend, last_chargecycles); |
| 1797 | /* calculate remainging charge */ |
| 1798 | remaining_charge_uah = calculate_remaining_charge_uah(chip, &raw, |
| 1799 | fcc_uah, chip->batt_temp_suspend, |
| 1800 | last_chargecycles); |
| 1801 | pr_debug("RC = %uuAh\n", remaining_charge_uah); |
| 1802 | |
| 1803 | /* calculate cc micro_volt_hour */ |
| 1804 | calculate_cc_uah(chip, raw.cc, &cc_uah); |
| 1805 | pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n", |
| 1806 | cc_uah, raw.cc, |
| 1807 | (int64_t)raw.cc - chip->cc_reading_at_100, |
| 1808 | chip->cc_reading_at_100); |
| 1809 | chip->soc_rbatt_suspend = ((remaining_charge_uah - cc_uah) * 100) |
| 1810 | / fcc_uah; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
| 1813 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1814 | #define DELTA_RBATT_PERCENT 10 |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1815 | static int pm8921_bms_resume(struct device *dev) |
| 1816 | { |
| 1817 | struct pm8921_rbatt_params raw; |
| 1818 | struct pm8921_bms_chip *chip = dev_get_drvdata(dev); |
| 1819 | int rbatt; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1820 | int expected_rbatt; |
| 1821 | int scalefactor; |
| 1822 | int delta_rbatt; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1823 | |
| 1824 | read_rbatt_params_raw(chip, &raw); |
| 1825 | rbatt = calculate_rbatt_resume(chip, &raw); |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1826 | |
| 1827 | if (rbatt < 0) |
| 1828 | return 0; |
| 1829 | |
| 1830 | expected_rbatt |
| 1831 | = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt; |
| 1832 | |
| 1833 | if (chip->rbatt_sf_lut) { |
| 1834 | scalefactor = interpolate_scalingfactor(chip, |
| 1835 | chip->rbatt_sf_lut, |
| 1836 | chip->batt_temp_suspend, |
| 1837 | chip->soc_rbatt_suspend); |
| 1838 | rbatt = rbatt * 100 / scalefactor; |
| 1839 | } |
| 1840 | |
| 1841 | delta_rbatt = expected_rbatt - rbatt; |
| 1842 | if (delta_rbatt) |
| 1843 | delta_rbatt = -delta_rbatt; |
| 1844 | /* |
| 1845 | * only update last_rbatt if rbatt is within some |
| 1846 | * percent of expected_rbatt |
| 1847 | */ |
| 1848 | if (delta_rbatt * 100 <= DELTA_RBATT_PERCENT * expected_rbatt) |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1849 | last_rbatt = rbatt; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1850 | |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | static const struct dev_pm_ops pm8921_pm_ops = { |
| 1855 | .suspend = pm8921_bms_suspend, |
| 1856 | .resume = pm8921_bms_resume, |
| 1857 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1858 | #define EN_BMS_BIT BIT(7) |
| 1859 | #define EN_PON_HS_BIT BIT(0) |
| 1860 | static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip) |
| 1861 | { |
| 1862 | int rc; |
| 1863 | |
| 1864 | rc = pm_bms_masked_write(chip, BMS_CONTROL, |
| 1865 | EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT); |
| 1866 | if (rc) { |
| 1867 | pr_err("failed to enable pon and bms addr = %d %d", |
| 1868 | BMS_CONTROL, rc); |
| 1869 | } |
| 1870 | |
Abhijeet Dharmapurikar | cad865a | 2012-02-09 17:14:02 -0800 | [diff] [blame] | 1871 | /* The charger will call start charge later if usb is present */ |
| 1872 | pm_bms_masked_write(chip, BMS_TOLERANCES, |
| 1873 | IBAT_TOL_MASK, IBAT_TOL_NOCHG); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1874 | return 0; |
| 1875 | } |
| 1876 | |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1877 | static void check_initial_ocv(struct pm8921_bms_chip *chip) |
| 1878 | { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1879 | int ocv_uv, rc; |
| 1880 | int16_t ocv_raw; |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1881 | int usb_chg; |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1882 | |
| 1883 | /* |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1884 | * Check if a ocv is available in bms hw, |
| 1885 | * if not compute it here at boot time and save it |
| 1886 | * in the last_ocv_uv. |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1887 | */ |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1888 | ocv_uv = 0; |
| 1889 | pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw); |
Abhijeet Dharmapurikar | acca418 | 2012-02-07 12:52:19 -0800 | [diff] [blame] | 1890 | usb_chg = usb_chg_plugged_in(); |
| 1891 | 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] | 1892 | if (rc || ocv_uv == 0) { |
| 1893 | rc = adc_based_ocv(chip, &ocv_uv); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1894 | if (rc) { |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1895 | pr_err("failed to read adc based ocv_uv rc = %d\n", rc); |
| 1896 | ocv_uv = DEFAULT_OCV_MICROVOLTS; |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 1897 | } |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1898 | last_ocv_uv = ocv_uv; |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1899 | } |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 1900 | pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, last_ocv_uv); |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1903 | static int64_t read_battery_id(struct pm8921_bms_chip *chip) |
| 1904 | { |
| 1905 | int rc; |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1906 | struct pm8xxx_adc_chan_result result; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1907 | |
Siddartha Mohanadoss | af91d90 | 2011-10-20 10:23:34 -0700 | [diff] [blame] | 1908 | rc = pm8xxx_adc_read(chip->batt_id_channel, &result); |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1909 | if (rc) { |
| 1910 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 1911 | chip->vbat_channel, rc); |
| 1912 | return rc; |
| 1913 | } |
| 1914 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 1915 | result.measurement); |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 1916 | return result.adc_code; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 1919 | #define PALLADIUM_ID_MIN 0x7F40 |
| 1920 | #define PALLADIUM_ID_MAX 0x7F5A |
| 1921 | #define DESAY_5200_ID_MIN 0x7F7F |
| 1922 | #define DESAY_5200_ID_MAX 0x802F |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1923 | static int set_battery_data(struct pm8921_bms_chip *chip) |
| 1924 | { |
| 1925 | int64_t battery_id; |
| 1926 | |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 1927 | if (chip->batt_type == BATT_DESAY) |
| 1928 | goto desay; |
| 1929 | else if (chip->batt_type == BATT_PALLADIUM) |
| 1930 | goto palladium; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1931 | |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 1932 | battery_id = read_battery_id(chip); |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1933 | if (battery_id < 0) { |
| 1934 | pr_err("cannot read battery id err = %lld\n", battery_id); |
| 1935 | return battery_id; |
| 1936 | } |
| 1937 | |
| 1938 | if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) { |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 1939 | goto palladium; |
| 1940 | } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX, |
| 1941 | battery_id)) { |
| 1942 | goto desay; |
| 1943 | } else { |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1944 | pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n", |
| 1945 | battery_id); |
| 1946 | goto palladium; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | palladium: |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1950 | chip->fcc = palladium_1500_data.fcc; |
| 1951 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 1952 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 1953 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 1954 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1955 | chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut; |
| 1956 | chip->default_rbatt_mohm |
| 1957 | = palladium_1500_data.default_rbatt_mohm; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1958 | return 0; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 1959 | desay: |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 1960 | chip->fcc = desay_5200_data.fcc; |
| 1961 | chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut; |
David Keitel | 8f2601b | 2012-02-14 22:31:07 -0800 | [diff] [blame] | 1962 | chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut; |
| 1963 | chip->pc_sf_lut = desay_5200_data.pc_sf_lut; |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 1964 | chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut; |
| 1965 | chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1966 | return 0; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1969 | enum bms_request_operation { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1970 | CALC_RBATT, |
| 1971 | CALC_FCC, |
| 1972 | CALC_PC, |
| 1973 | CALC_SOC, |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1974 | CALIB_HKADC, |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 1975 | CALIB_CCADC, |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 1976 | GET_VBAT_VSENSE_SIMULTANEOUS, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1977 | }; |
| 1978 | |
| 1979 | static int test_batt_temp = 5; |
| 1980 | static int test_chargecycle = 150; |
| 1981 | static int test_ocv = 3900000; |
| 1982 | enum { |
| 1983 | TEST_BATT_TEMP, |
| 1984 | TEST_CHARGE_CYCLE, |
| 1985 | TEST_OCV, |
| 1986 | }; |
| 1987 | static int get_test_param(void *data, u64 * val) |
| 1988 | { |
| 1989 | switch ((int)data) { |
| 1990 | case TEST_BATT_TEMP: |
| 1991 | *val = test_batt_temp; |
| 1992 | break; |
| 1993 | case TEST_CHARGE_CYCLE: |
| 1994 | *val = test_chargecycle; |
| 1995 | break; |
| 1996 | case TEST_OCV: |
| 1997 | *val = test_ocv; |
| 1998 | break; |
| 1999 | default: |
| 2000 | return -EINVAL; |
| 2001 | } |
| 2002 | return 0; |
| 2003 | } |
| 2004 | static int set_test_param(void *data, u64 val) |
| 2005 | { |
| 2006 | switch ((int)data) { |
| 2007 | case TEST_BATT_TEMP: |
| 2008 | test_batt_temp = (int)val; |
| 2009 | break; |
| 2010 | case TEST_CHARGE_CYCLE: |
| 2011 | test_chargecycle = (int)val; |
| 2012 | break; |
| 2013 | case TEST_OCV: |
| 2014 | test_ocv = (int)val; |
| 2015 | break; |
| 2016 | default: |
| 2017 | return -EINVAL; |
| 2018 | } |
| 2019 | return 0; |
| 2020 | } |
| 2021 | DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n"); |
| 2022 | |
| 2023 | static int get_calc(void *data, u64 * val) |
| 2024 | { |
| 2025 | int param = (int)data; |
| 2026 | int ret = 0; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2027 | int ibat_ua, vbat_uv; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2028 | struct pm8921_soc_params raw; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2029 | struct pm8921_rbatt_params rraw; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2030 | |
| 2031 | read_soc_params_raw(the_chip, &raw); |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2032 | read_rbatt_params_raw(the_chip, &rraw); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2033 | |
| 2034 | *val = 0; |
| 2035 | |
| 2036 | /* global irq number passed in via data */ |
| 2037 | switch (param) { |
| 2038 | case CALC_RBATT: |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2039 | *val = calculate_rbatt_resume(the_chip, &rraw); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2040 | break; |
| 2041 | case CALC_FCC: |
Abhijeet Dharmapurikar | 9c1e399 | 2011-12-18 15:03:20 -0800 | [diff] [blame] | 2042 | *val = calculate_fcc_uah(the_chip, test_batt_temp, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2043 | test_chargecycle); |
| 2044 | break; |
| 2045 | case CALC_PC: |
| 2046 | *val = calculate_pc(the_chip, test_ocv, test_batt_temp, |
| 2047 | test_chargecycle); |
| 2048 | break; |
| 2049 | case CALC_SOC: |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2050 | *val = calculate_state_of_charge(the_chip, &raw, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2051 | test_batt_temp, test_chargecycle); |
| 2052 | break; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2053 | case CALIB_HKADC: |
| 2054 | /* reading this will trigger calibration */ |
| 2055 | *val = 0; |
| 2056 | calib_hkadc(the_chip); |
| 2057 | break; |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 2058 | case CALIB_CCADC: |
| 2059 | /* reading this will trigger calibration */ |
| 2060 | *val = 0; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 2061 | pm8xxx_calib_ccadc(); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 2062 | break; |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2063 | case GET_VBAT_VSENSE_SIMULTANEOUS: |
| 2064 | /* reading this will call simultaneous vbat and vsense */ |
| 2065 | *val = |
| 2066 | pm8921_bms_get_simultaneous_battery_voltage_and_current( |
| 2067 | &ibat_ua, |
| 2068 | &vbat_uv); |
| 2069 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2070 | default: |
| 2071 | ret = -EINVAL; |
| 2072 | } |
| 2073 | return ret; |
| 2074 | } |
| 2075 | DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n"); |
| 2076 | |
| 2077 | static int get_reading(void *data, u64 * val) |
| 2078 | { |
| 2079 | int param = (int)data; |
| 2080 | int ret = 0; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2081 | struct pm8921_soc_params raw; |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2082 | struct pm8921_rbatt_params rraw; |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2083 | |
| 2084 | read_soc_params_raw(the_chip, &raw); |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2085 | read_rbatt_params_raw(the_chip, &rraw); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2086 | |
| 2087 | *val = 0; |
| 2088 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2089 | switch (param) { |
| 2090 | case CC_MSB: |
| 2091 | case CC_LSB: |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2092 | *val = raw.cc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2093 | break; |
| 2094 | case LAST_GOOD_OCV_VALUE: |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2095 | *val = raw.last_good_ocv_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2096 | break; |
| 2097 | case VBATT_FOR_RBATT: |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2098 | *val = rraw.vbatt_for_rbatt_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2099 | break; |
| 2100 | case VSENSE_FOR_RBATT: |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2101 | *val = rraw.vsense_for_rbatt_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2102 | break; |
| 2103 | case OCV_FOR_RBATT: |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2104 | *val = rraw.ocv_for_rbatt_uv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2105 | break; |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 2106 | case VSENSE_AVG: |
| 2107 | read_vsense_avg(the_chip, (uint *)val); |
| 2108 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2109 | default: |
| 2110 | ret = -EINVAL; |
| 2111 | } |
| 2112 | return ret; |
| 2113 | } |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 2114 | DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2115 | |
| 2116 | static int get_rt_status(void *data, u64 * val) |
| 2117 | { |
| 2118 | int i = (int)data; |
| 2119 | int ret; |
| 2120 | |
| 2121 | /* global irq number passed in via data */ |
| 2122 | ret = pm_bms_get_rt_status(the_chip, i); |
| 2123 | *val = ret; |
| 2124 | return 0; |
| 2125 | } |
| 2126 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 2127 | |
| 2128 | static int get_reg(void *data, u64 * val) |
| 2129 | { |
| 2130 | int addr = (int)data; |
| 2131 | int ret; |
| 2132 | u8 temp; |
| 2133 | |
| 2134 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 2135 | if (ret) { |
| 2136 | pr_err("pm8xxx_readb to %x value = %d errored = %d\n", |
| 2137 | addr, temp, ret); |
| 2138 | return -EAGAIN; |
| 2139 | } |
| 2140 | *val = temp; |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | static int set_reg(void *data, u64 val) |
| 2145 | { |
| 2146 | int addr = (int)data; |
| 2147 | int ret; |
| 2148 | u8 temp; |
| 2149 | |
| 2150 | temp = (u8) val; |
| 2151 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 2152 | if (ret) { |
| 2153 | pr_err("pm8xxx_writeb to %x value = %d errored = %d\n", |
| 2154 | addr, temp, ret); |
| 2155 | return -EAGAIN; |
| 2156 | } |
| 2157 | return 0; |
| 2158 | } |
| 2159 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 2160 | |
| 2161 | static void create_debugfs_entries(struct pm8921_bms_chip *chip) |
| 2162 | { |
| 2163 | int i; |
| 2164 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 2165 | chip->dent = debugfs_create_dir("pm8921-bms", NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2166 | |
| 2167 | if (IS_ERR(chip->dent)) { |
| 2168 | pr_err("pmic bms couldnt create debugfs dir\n"); |
| 2169 | return; |
| 2170 | } |
| 2171 | |
| 2172 | debugfs_create_file("BMS_CONTROL", 0644, chip->dent, |
| 2173 | (void *)BMS_CONTROL, ®_fops); |
| 2174 | debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent, |
| 2175 | (void *)BMS_OUTPUT0, ®_fops); |
| 2176 | debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent, |
| 2177 | (void *)BMS_OUTPUT1, ®_fops); |
| 2178 | debugfs_create_file("BMS_TEST1", 0644, chip->dent, |
| 2179 | (void *)BMS_TEST1, ®_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2180 | |
| 2181 | debugfs_create_file("test_batt_temp", 0644, chip->dent, |
| 2182 | (void *)TEST_BATT_TEMP, &temp_fops); |
| 2183 | debugfs_create_file("test_chargecycle", 0644, chip->dent, |
| 2184 | (void *)TEST_CHARGE_CYCLE, &temp_fops); |
| 2185 | debugfs_create_file("test_ocv", 0644, chip->dent, |
| 2186 | (void *)TEST_OCV, &temp_fops); |
| 2187 | |
| 2188 | debugfs_create_file("read_cc", 0644, chip->dent, |
| 2189 | (void *)CC_MSB, &reading_fops); |
| 2190 | debugfs_create_file("read_last_good_ocv", 0644, chip->dent, |
| 2191 | (void *)LAST_GOOD_OCV_VALUE, &reading_fops); |
| 2192 | debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent, |
| 2193 | (void *)VBATT_FOR_RBATT, &reading_fops); |
| 2194 | debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent, |
| 2195 | (void *)VSENSE_FOR_RBATT, &reading_fops); |
| 2196 | debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent, |
| 2197 | (void *)OCV_FOR_RBATT, &reading_fops); |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 2198 | debugfs_create_file("read_vsense_avg", 0644, chip->dent, |
| 2199 | (void *)VSENSE_AVG, &reading_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2200 | |
| 2201 | debugfs_create_file("show_rbatt", 0644, chip->dent, |
| 2202 | (void *)CALC_RBATT, &calc_fops); |
| 2203 | debugfs_create_file("show_fcc", 0644, chip->dent, |
| 2204 | (void *)CALC_FCC, &calc_fops); |
| 2205 | debugfs_create_file("show_pc", 0644, chip->dent, |
| 2206 | (void *)CALC_PC, &calc_fops); |
| 2207 | debugfs_create_file("show_soc", 0644, chip->dent, |
| 2208 | (void *)CALC_SOC, &calc_fops); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2209 | debugfs_create_file("calib_hkadc", 0644, chip->dent, |
| 2210 | (void *)CALIB_HKADC, &calc_fops); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 2211 | debugfs_create_file("calib_ccadc", 0644, chip->dent, |
| 2212 | (void *)CALIB_CCADC, &calc_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2213 | |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2214 | debugfs_create_file("simultaneous", 0644, chip->dent, |
| 2215 | (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops); |
| 2216 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2217 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 2218 | if (chip->pmic_bms_irq[bms_irq_data[i].irq_id]) |
| 2219 | debugfs_create_file(bms_irq_data[i].name, 0444, |
| 2220 | chip->dent, |
| 2221 | (void *)bms_irq_data[i].irq_id, |
| 2222 | &rt_fops); |
| 2223 | } |
| 2224 | } |
| 2225 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 2226 | #define REG_SBI_CONFIG 0x04F |
| 2227 | #define PAGE3_ENABLE_MASK 0x6 |
| 2228 | #define PROGRAM_REV_MASK 0x0F |
| 2229 | #define PROGRAM_REV 0x9 |
| 2230 | static int read_ocv_trim(struct pm8921_bms_chip *chip) |
| 2231 | { |
| 2232 | int rc; |
| 2233 | u8 reg, sbi_config; |
| 2234 | |
| 2235 | rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config); |
| 2236 | if (rc) { |
| 2237 | pr_err("error = %d reading sbi config reg\n", rc); |
| 2238 | return rc; |
| 2239 | } |
| 2240 | |
| 2241 | reg = sbi_config | PAGE3_ENABLE_MASK; |
| 2242 | rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg); |
| 2243 | if (rc) { |
| 2244 | pr_err("error = %d writing sbi config reg\n", rc); |
| 2245 | return rc; |
| 2246 | } |
| 2247 | |
| 2248 | rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, ®); |
| 2249 | if (rc) |
| 2250 | pr_err("Error %d reading %d addr %d\n", |
| 2251 | rc, reg, TEST_PROGRAM_REV); |
| 2252 | pr_err("program rev reg is 0x%x\n", reg); |
| 2253 | reg &= PROGRAM_REV_MASK; |
| 2254 | |
| 2255 | /* If the revision is equal or higher do not adjust trim delta */ |
| 2256 | if (reg >= PROGRAM_REV) { |
| 2257 | chip->amux_2_trim_delta = 0; |
| 2258 | goto restore_sbi_config; |
| 2259 | } |
| 2260 | |
| 2261 | rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, ®); |
| 2262 | if (rc) { |
| 2263 | pr_err("error = %d reading trim reg\n", rc); |
| 2264 | return rc; |
| 2265 | } |
| 2266 | |
| 2267 | pr_err("trim reg is 0x%x\n", reg); |
| 2268 | chip->amux_2_trim_delta = abs(0x49 - reg); |
| 2269 | pr_err("trim delta is %d\n", chip->amux_2_trim_delta); |
| 2270 | |
| 2271 | restore_sbi_config: |
| 2272 | rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config); |
| 2273 | if (rc) { |
| 2274 | pr_err("error = %d writing sbi config reg\n", rc); |
| 2275 | return rc; |
| 2276 | } |
| 2277 | |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2281 | static int __devinit pm8921_bms_probe(struct platform_device *pdev) |
| 2282 | { |
| 2283 | int rc = 0; |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 2284 | int vbatt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2285 | struct pm8921_bms_chip *chip; |
| 2286 | const struct pm8921_bms_platform_data *pdata |
| 2287 | = pdev->dev.platform_data; |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 2288 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2289 | if (!pdata) { |
| 2290 | pr_err("missing platform data\n"); |
| 2291 | return -EINVAL; |
| 2292 | } |
| 2293 | |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 2294 | chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2295 | if (!chip) { |
| 2296 | pr_err("Cannot allocate pm_bms_chip\n"); |
| 2297 | return -ENOMEM; |
| 2298 | } |
Abhijeet Dharmapurikar | c9857ca | 2012-02-07 15:56:06 -0800 | [diff] [blame] | 2299 | mutex_init(&chip->bms_output_lock); |
Abhijeet Dharmapurikar | 228acb9 | 2011-11-21 19:21:48 -0800 | [diff] [blame] | 2300 | spin_lock_init(&chip->bms_100_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2301 | chip->dev = &pdev->dev; |
| 2302 | chip->r_sense = pdata->r_sense; |
| 2303 | chip->i_test = pdata->i_test; |
| 2304 | chip->v_failure = pdata->v_failure; |
| 2305 | chip->calib_delay_ms = pdata->calib_delay_ms; |
Abhijeet Dharmapurikar | a93ede8 | 2011-11-17 12:20:03 -0800 | [diff] [blame] | 2306 | chip->max_voltage_uv = pdata->max_voltage_uv; |
David Keitel | 35e1187 | 2012-02-17 17:40:42 -0800 | [diff] [blame] | 2307 | chip->batt_type = pdata->battery_type; |
Abhijeet Dharmapurikar | dba91f4 | 2011-12-21 16:56:13 -0800 | [diff] [blame] | 2308 | chip->start_percent = -EINVAL; |
| 2309 | chip->end_percent = -EINVAL; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 2310 | rc = set_battery_data(chip); |
| 2311 | if (rc) { |
| 2312 | pr_err("%s bad battery data %d\n", __func__, rc); |
Abhijeet Dharmapurikar | 4a4456e | 2011-09-27 15:10:38 -0700 | [diff] [blame] | 2313 | goto free_chip; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 2314 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2315 | |
Abhijeet Dharmapurikar | f6fdcaa0 | 2012-02-27 00:10:10 -0800 | [diff] [blame] | 2316 | if (chip->pc_temp_ocv_lut == NULL) { |
| 2317 | pr_err("temp ocv lut table is NULL\n"); |
| 2318 | rc = -EINVAL; |
| 2319 | goto free_chip; |
| 2320 | } |
| 2321 | |
| 2322 | /* set defaults in the battery data */ |
| 2323 | if (chip->default_rbatt_mohm <= 0) |
| 2324 | chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS; |
| 2325 | |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 2326 | chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel; |
| 2327 | chip->vbat_channel = pdata->bms_cdata.vbat_channel; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2328 | chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel; |
| 2329 | chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 2330 | chip->batt_id_channel = pdata->bms_cdata.batt_id_channel; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 2331 | chip->revision = pm8xxx_get_revision(chip->dev->parent); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2332 | INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work); |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 2333 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2334 | rc = request_irqs(chip, pdev); |
| 2335 | if (rc) { |
| 2336 | pr_err("couldn't register interrupts rc = %d\n", rc); |
| 2337 | goto free_chip; |
| 2338 | } |
| 2339 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 2340 | rc = pm8921_bms_hw_init(chip); |
| 2341 | if (rc) { |
| 2342 | pr_err("couldn't init hardware rc = %d\n", rc); |
| 2343 | goto free_irqs; |
| 2344 | } |
| 2345 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2346 | platform_set_drvdata(pdev, chip); |
| 2347 | the_chip = chip; |
| 2348 | create_debugfs_entries(chip); |
| 2349 | |
Abhijeet Dharmapurikar | fd8655c | 2012-03-18 16:41:58 -0700 | [diff] [blame^] | 2350 | rc = read_ocv_trim(chip); |
| 2351 | if (rc) { |
| 2352 | pr_err("couldn't adjust ocv_trim rc= %d\n", rc); |
| 2353 | goto free_irqs; |
| 2354 | } |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2355 | check_initial_ocv(chip); |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 2356 | |
| 2357 | INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work); |
| 2358 | /* begin calibration only on chips > 2.0 */ |
| 2359 | if (chip->revision >= PM8XXX_REVISION_8921_2p0) |
Abhijeet Dharmapurikar | ccfc4f3 | 2012-01-16 17:35:18 -0800 | [diff] [blame] | 2360 | schedule_delayed_work(&chip->calib_ccadc_work, 0); |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 2361 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 2362 | /* initial hkadc calibration */ |
| 2363 | schedule_work(&chip->calib_hkadc_work); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 2364 | /* enable the vbatt reading interrupts for scheduling hkadc calib */ |
| 2365 | pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV); |
| 2366 | pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R); |
Abhijeet Dharmapurikar | 21ebadd | 2011-09-28 21:14:16 -0700 | [diff] [blame] | 2367 | |
Abhijeet Dharmapurikar | c23264c | 2011-10-28 19:34:56 -0700 | [diff] [blame] | 2368 | get_battery_uvolts(chip, &vbatt); |
| 2369 | pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n", |
| 2370 | pm8921_bms_get_percent_charge(), |
| 2371 | vbatt, last_ocv_uv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2372 | return 0; |
| 2373 | |
Abhijeet Dharmapurikar | 1834c6d | 2011-10-28 20:00:02 -0700 | [diff] [blame] | 2374 | free_irqs: |
| 2375 | free_irqs(chip); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2376 | free_chip: |
| 2377 | kfree(chip); |
| 2378 | return rc; |
| 2379 | } |
| 2380 | |
| 2381 | static int __devexit pm8921_bms_remove(struct platform_device *pdev) |
| 2382 | { |
| 2383 | struct pm8921_bms_chip *chip = platform_get_drvdata(pdev); |
| 2384 | |
| 2385 | free_irqs(chip); |
Abhijeet Dharmapurikar | 05d4274 | 2011-10-18 19:26:03 -0700 | [diff] [blame] | 2386 | kfree(chip->adjusted_fcc_temp_lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2387 | platform_set_drvdata(pdev, NULL); |
| 2388 | the_chip = NULL; |
| 2389 | kfree(chip); |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | static struct platform_driver pm8921_bms_driver = { |
| 2394 | .probe = pm8921_bms_probe, |
| 2395 | .remove = __devexit_p(pm8921_bms_remove), |
| 2396 | .driver = { |
| 2397 | .name = PM8921_BMS_DEV_NAME, |
| 2398 | .owner = THIS_MODULE, |
Abhijeet Dharmapurikar | 1a7561a | 2012-02-26 22:06:36 -0800 | [diff] [blame] | 2399 | .pm = &pm8921_pm_ops |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2400 | }, |
| 2401 | }; |
| 2402 | |
| 2403 | static int __init pm8921_bms_init(void) |
| 2404 | { |
| 2405 | return platform_driver_register(&pm8921_bms_driver); |
| 2406 | } |
| 2407 | |
| 2408 | static void __exit pm8921_bms_exit(void) |
| 2409 | { |
| 2410 | platform_driver_unregister(&pm8921_bms_driver); |
| 2411 | } |
| 2412 | |
| 2413 | late_initcall(pm8921_bms_init); |
| 2414 | module_exit(pm8921_bms_exit); |
| 2415 | |
| 2416 | MODULE_LICENSE("GPL v2"); |
| 2417 | MODULE_DESCRIPTION("PMIC8921 bms driver"); |
| 2418 | MODULE_VERSION("1.0"); |
| 2419 | MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME); |