Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 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 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/mfd/pmic8058.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/bitops.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/msm-charger.h> |
| 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/msm_adc.h> |
| 27 | #include <linux/notifier.h> |
| 28 | #include <linux/pmic8058-batt-alarm.h> |
| 29 | |
| 30 | #include <mach/msm_xo.h> |
| 31 | #include <mach/msm_hsusb.h> |
| 32 | |
| 33 | /* Config Regs and their bits*/ |
| 34 | #define PM8058_CHG_TEST 0x75 |
| 35 | #define IGNORE_LL 2 |
| 36 | #define PM8058_CHG_TEST_2 0xEA |
| 37 | #define PM8058_CHG_TEST_3 0xEB |
| 38 | #define PM8058_OVP_TEST_REG 0xF6 |
| 39 | #define FORCE_OVP_OFF 3 |
| 40 | |
| 41 | #define PM8058_CHG_CNTRL 0x1E |
| 42 | #define CHG_TRICKLE_EN 7 |
| 43 | #define CHG_USB_SUSPEND 6 |
| 44 | #define CHG_IMON_CAL 5 |
| 45 | #define CHG_IMON_GAIN 4 |
| 46 | #define CHG_CHARGE_BAT 3 |
| 47 | #define CHG_VBUS_FROM_BOOST_OVRD 2 |
| 48 | #define CHG_CHARGE_DIS 1 |
| 49 | #define CHG_VCP_EN 0 |
| 50 | |
| 51 | #define PM8058_CHG_CNTRL_2 0xD8 |
| 52 | #define ATC_DIS 7 /* coincell backed */ |
| 53 | #define CHARGE_AUTO_DIS 6 |
| 54 | #define DUMB_CHG_OVRD 5 /* coincell backed */ |
| 55 | #define ENUM_DONE 4 |
| 56 | #define CHG_TEMP_MODE 3 |
| 57 | #define CHG_BATT_TEMP_DIS 1 /* coincell backed */ |
| 58 | #define CHG_FAILED_CLEAR 0 |
| 59 | |
| 60 | #define PM8058_CHG_VMAX_SEL 0x21 |
| 61 | #define PM8058_CHG_VBAT_DET 0xD9 |
| 62 | #define PM8058_CHG_IMAX 0x1F |
| 63 | #define PM8058_CHG_TRICKLE 0xDB |
| 64 | #define PM8058_CHG_ITERM 0xDC |
| 65 | #define PM8058_CHG_TTRKL_MAX 0xE1 |
| 66 | #define PM8058_CHG_TCHG_MAX 0xE4 |
| 67 | #define PM8058_CHG_TEMP_THRESH 0xE2 |
| 68 | #define PM8058_CHG_TEMP_REG 0xE3 |
| 69 | #define PM8058_CHG_PULSE 0x22 |
| 70 | |
| 71 | /* IRQ STATUS and CLEAR */ |
| 72 | #define PM8058_CHG_STATUS_CLEAR_IRQ_1 0x31 |
| 73 | #define PM8058_CHG_STATUS_CLEAR_IRQ_3 0x33 |
| 74 | #define PM8058_CHG_STATUS_CLEAR_IRQ_10 0xB3 |
| 75 | #define PM8058_CHG_STATUS_CLEAR_IRQ_11 0xB4 |
| 76 | |
| 77 | /* IRQ MASKS */ |
| 78 | #define PM8058_CHG_MASK_IRQ_1 0x38 |
| 79 | |
| 80 | #define PM8058_CHG_MASK_IRQ_3 0x3A |
| 81 | #define PM8058_CHG_MASK_IRQ_10 0xBA |
| 82 | #define PM8058_CHG_MASK_IRQ_11 0xBB |
| 83 | |
| 84 | /* IRQ Real time status regs */ |
| 85 | #define PM8058_CHG_STATUS_RT_1 0x3F |
| 86 | #define STATUS_RTCHGVAL 7 |
| 87 | #define STATUS_RTCHGINVAL 6 |
| 88 | #define STATUS_RTBATT_REPLACE 5 |
| 89 | #define STATUS_RTVBATDET_LOW 4 |
| 90 | #define STATUS_RTCHGILIM 3 |
| 91 | #define STATUS_RTPCTDONE 1 |
| 92 | #define STATUS_RTVCP 0 |
| 93 | #define PM8058_CHG_STATUS_RT_3 0x41 |
| 94 | #define PM8058_CHG_STATUS_RT_10 0xC1 |
| 95 | #define PM8058_CHG_STATUS_RT_11 0xC2 |
| 96 | |
| 97 | /* VTRIM */ |
| 98 | #define PM8058_CHG_VTRIM 0x1D |
| 99 | #define PM8058_CHG_VBATDET_TRIM 0x1E |
| 100 | #define PM8058_CHG_ITRIM 0x1F |
| 101 | #define PM8058_CHG_TTRIM 0x20 |
| 102 | |
| 103 | #define AUTO_CHARGING_VMAXSEL 4200 |
| 104 | #define AUTO_CHARGING_FAST_TIME_MAX_MINUTES 512 |
| 105 | #define AUTO_CHARGING_TRICKLE_TIME_MINUTES 30 |
| 106 | #define AUTO_CHARGING_VEOC_ITERM 100 |
| 107 | #define AUTO_CHARGING_IEOC_ITERM 160 |
| 108 | #define AUTO_CHARGING_RESUME_MV 4100 |
| 109 | |
| 110 | #define AUTO_CHARGING_VBATDET 4150 |
| 111 | #define AUTO_CHARGING_VBATDET_DEBOUNCE_TIME_MS 3000 |
| 112 | #define AUTO_CHARGING_VEOC_VBATDET 4100 |
| 113 | #define AUTO_CHARGING_VEOC_TCHG 16 |
| 114 | #define AUTO_CHARGING_VEOC_TCHG_FINAL_CYCLE 32 |
| 115 | #define AUTO_CHARGING_VEOC_BEGIN_TIME_MS 5400000 |
| 116 | |
| 117 | #define AUTO_CHARGING_VEOC_VBAT_LOW_CHECK_TIME_MS 60000 |
| 118 | #define AUTO_CHARGING_RESUME_CHARGE_DETECTION_COUNTER 5 |
| 119 | |
| 120 | #define AUTO_CHARGING_DONE_CHECK_TIME_MS 1000 |
| 121 | |
| 122 | #define PM8058_CHG_I_STEP_MA 50 |
| 123 | #define PM8058_CHG_I_MIN_MA 50 |
| 124 | #define PM8058_CHG_T_TCHG_SHIFT 2 |
| 125 | #define PM8058_CHG_I_TERM_STEP_MA 10 |
| 126 | #define PM8058_CHG_V_STEP_MV 25 |
| 127 | #define PM8058_CHG_V_MIN_MV 2400 |
| 128 | /* |
| 129 | * enum pmic_chg_interrupts: pmic interrupts |
| 130 | * @CHGVAL_IRQ: charger V between 3.3 and 7.9 |
| 131 | * @CHGINVAL_IRQ: charger V outside 3.3 and 7.9 |
| 132 | * @VBATDET_LOW_IRQ: VBAT < VBATDET |
| 133 | * @VCP_IRQ: VDD went below VBAT: BAT_FET is turned on |
| 134 | * @CHGILIM_IRQ: mA consumed>IMAXSEL: chgloop draws less mA |
| 135 | * @ATC_DONE_IRQ: Auto Trickle done |
| 136 | * @ATCFAIL_IRQ: Auto Trickle fail |
| 137 | * @AUTO_CHGDONE_IRQ: Auto chg done |
| 138 | * @AUTO_CHGFAIL_IRQ: time exceeded w/o reaching term current |
| 139 | * @CHGSTATE_IRQ: something happend causing a state change |
| 140 | * @FASTCHG_IRQ: trkl charging completed: moving to fastchg |
| 141 | * @CHG_END_IRQ: mA has dropped to termination current |
| 142 | * @BATTTEMP_IRQ: batt temp is out of range |
| 143 | * @CHGHOT_IRQ: the pass device is too hot |
| 144 | * @CHGTLIMIT_IRQ: unused |
| 145 | * @CHG_GONE_IRQ: charger was removed |
| 146 | * @VCPMAJOR_IRQ: vcp major |
| 147 | * @VBATDET_IRQ: VBAT >= VBATDET |
| 148 | * @BATFET_IRQ: BATFET closed |
| 149 | * @BATT_REPLACE_IRQ: |
| 150 | * @BATTCONNECT_IRQ: |
| 151 | */ |
| 152 | enum pmic_chg_interrupts { |
| 153 | CHGVAL_IRQ, |
| 154 | CHGINVAL_IRQ, |
| 155 | VBATDET_LOW_IRQ, |
| 156 | VCP_IRQ, |
| 157 | CHGILIM_IRQ, |
| 158 | ATC_DONE_IRQ, |
| 159 | ATCFAIL_IRQ, |
| 160 | AUTO_CHGDONE_IRQ, |
| 161 | AUTO_CHGFAIL_IRQ, |
| 162 | CHGSTATE_IRQ, |
| 163 | FASTCHG_IRQ, |
| 164 | CHG_END_IRQ, |
| 165 | BATTTEMP_IRQ, |
| 166 | CHGHOT_IRQ, |
| 167 | CHGTLIMIT_IRQ, |
| 168 | CHG_GONE_IRQ, |
| 169 | VCPMAJOR_IRQ, |
| 170 | VBATDET_IRQ, |
| 171 | BATFET_IRQ, |
| 172 | BATT_REPLACE_IRQ, |
| 173 | BATTCONNECT_IRQ, |
| 174 | PMIC_CHG_MAX_INTS |
| 175 | }; |
| 176 | |
| 177 | struct pm8058_charger { |
| 178 | struct pmic_charger_pdata *pdata; |
| 179 | struct pm8058_chip *pm_chip; |
| 180 | struct device *dev; |
| 181 | |
| 182 | int pmic_chg_irq[PMIC_CHG_MAX_INTS]; |
| 183 | DECLARE_BITMAP(enabled_irqs, PMIC_CHG_MAX_INTS); |
| 184 | |
| 185 | struct delayed_work chg_done_check_work; |
| 186 | struct delayed_work check_vbat_low_work; |
| 187 | struct delayed_work veoc_begin_work; |
| 188 | struct delayed_work charging_check_work; |
| 189 | int waiting_for_topoff; |
| 190 | int waiting_for_veoc; |
| 191 | int vbatdet; |
| 192 | struct msm_hardware_charger hw_chg; |
| 193 | int current_charger_current; |
| 194 | int disabled; |
| 195 | |
| 196 | struct msm_xo_voter *voter; |
| 197 | struct dentry *dent; |
| 198 | |
| 199 | int inited; |
| 200 | int present; |
| 201 | }; |
| 202 | |
| 203 | static struct pm8058_charger pm8058_chg; |
| 204 | static struct msm_hardware_charger usb_hw_chg; |
| 205 | |
| 206 | |
| 207 | static int msm_battery_gauge_alarm_notify(struct notifier_block *nb, |
| 208 | unsigned long status, void *unused); |
| 209 | |
| 210 | static struct notifier_block alarm_notifier = { |
| 211 | .notifier_call = msm_battery_gauge_alarm_notify, |
| 212 | }; |
| 213 | |
| 214 | static int resume_mv = AUTO_CHARGING_RESUME_MV; |
| 215 | static DEFINE_MUTEX(batt_alarm_lock); |
| 216 | static int resume_mv_set(const char *val, struct kernel_param *kp); |
| 217 | module_param_call(resume_mv, resume_mv_set, param_get_int, |
| 218 | &resume_mv, S_IRUGO | S_IWUSR); |
| 219 | |
| 220 | static int resume_mv_set(const char *val, struct kernel_param *kp) |
| 221 | { |
| 222 | int rc; |
| 223 | |
| 224 | mutex_lock(&batt_alarm_lock); |
| 225 | |
| 226 | rc = param_set_int(val, kp); |
| 227 | if (rc) |
| 228 | goto out; |
| 229 | |
| 230 | rc = pm8058_batt_alarm_threshold_set(resume_mv, 4300); |
| 231 | |
| 232 | out: |
| 233 | mutex_unlock(&batt_alarm_lock); |
| 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | static void pm8058_chg_enable_irq(int interrupt) |
| 238 | { |
| 239 | if (!__test_and_set_bit(interrupt, pm8058_chg.enabled_irqs)) { |
| 240 | dev_dbg(pm8058_chg.dev, "%s %d\n", __func__, |
| 241 | pm8058_chg.pmic_chg_irq[interrupt]); |
| 242 | enable_irq(pm8058_chg.pmic_chg_irq[interrupt]); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static void pm8058_chg_disable_irq(int interrupt) |
| 247 | { |
| 248 | if (__test_and_clear_bit(interrupt, pm8058_chg.enabled_irqs)) { |
| 249 | dev_dbg(pm8058_chg.dev, "%s %d\n", __func__, |
| 250 | pm8058_chg.pmic_chg_irq[interrupt]); |
| 251 | disable_irq_nosync(pm8058_chg.pmic_chg_irq[interrupt]); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static int pm_chg_get_rt_status(int irq) |
| 256 | { |
| 257 | int count = 3; |
| 258 | int ret; |
| 259 | |
| 260 | while ((ret = |
| 261 | pm8058_irq_get_rt_status(pm8058_chg.pm_chip, irq)) == -EAGAIN |
| 262 | && count--) { |
| 263 | dev_info(pm8058_chg.dev, "%s trycount=%d\n", __func__, count); |
| 264 | cpu_relax(); |
| 265 | } |
| 266 | if (ret == -EAGAIN) |
| 267 | return 0; |
| 268 | else |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static int is_chg_plugged_in(void) |
| 273 | { |
| 274 | return pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]); |
| 275 | } |
| 276 | |
| 277 | #ifdef DEBUG |
| 278 | static void __dump_chg_regs(void) |
| 279 | { |
| 280 | u8 temp; |
| 281 | int temp2; |
| 282 | |
| 283 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1); |
| 284 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_CNTRL = 0x%x\n", temp); |
| 285 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 286 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_CNTRL_2 = 0x%x\n", temp); |
| 287 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_VMAX_SEL, &temp, 1); |
| 288 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_VMAX_SEL = 0x%x\n", temp); |
| 289 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_VBAT_DET, &temp, 1); |
| 290 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_VBAT_DET = 0x%x\n", temp); |
| 291 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_IMAX, &temp, 1); |
| 292 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_IMAX = 0x%x\n", temp); |
| 293 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TRICKLE, &temp, 1); |
| 294 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_TRICKLE = 0x%x\n", temp); |
| 295 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_ITERM, &temp, 1); |
| 296 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_ITERM = 0x%x\n", temp); |
| 297 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TTRKL_MAX, &temp, 1); |
| 298 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_TTRKL_MAX = 0x%x\n", temp); |
| 299 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TCHG_MAX, &temp, 1); |
| 300 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_TCHG_MAX = 0x%x\n", temp); |
| 301 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEMP_THRESH, &temp, 1); |
| 302 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_TEMP_THRESH = 0x%x\n", temp); |
| 303 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEMP_REG, &temp, 1); |
| 304 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_TEMP_REG = 0x%x\n", temp); |
| 305 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_PULSE, &temp, 1); |
| 306 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_PULSE = 0x%x\n", temp); |
| 307 | |
| 308 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_1, |
| 309 | &temp, 1); |
| 310 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_1 = 0x%x\n", temp); |
| 311 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_3, |
| 312 | &temp, 1); |
| 313 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_3 = 0x%x\n", temp); |
| 314 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_10, |
| 315 | &temp, 1); |
| 316 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_10 = 0x%x\n", |
| 317 | temp); |
| 318 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_11, |
| 319 | &temp, 1); |
| 320 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_11 = 0x%x\n", |
| 321 | temp); |
| 322 | |
| 323 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_1, &temp, 1); |
| 324 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_1 = 0x%x\n", temp); |
| 325 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_3, &temp, 1); |
| 326 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_3 = 0x%x\n", temp); |
| 327 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_10, &temp, 1); |
| 328 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_10 = 0x%x\n", temp); |
| 329 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_11, &temp, 1); |
| 330 | dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_11 = 0x%x\n", temp); |
| 331 | |
| 332 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]); |
| 333 | dev_dbg(pm8058_chg.dev, "CHGVAL_IRQ = %d\n", temp2); |
| 334 | |
| 335 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ]); |
| 336 | dev_dbg(pm8058_chg.dev, "CHGINVAL_IRQ = %d\n", temp2); |
| 337 | |
| 338 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_LOW_IRQ]); |
| 339 | dev_dbg(pm8058_chg.dev, "VBATDET_LOW_IRQ= %d\n", temp2); |
| 340 | |
| 341 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VCP_IRQ]); |
| 342 | dev_dbg(pm8058_chg.dev, "VCP_IRQ= %d\n", temp2); |
| 343 | |
| 344 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGILIM_IRQ]); |
| 345 | dev_dbg(pm8058_chg.dev, "CHGILIM_IRQ= %d\n", temp2); |
| 346 | |
| 347 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[ATC_DONE_IRQ]); |
| 348 | dev_dbg(pm8058_chg.dev, "ATC_DONE_IRQ= %d\n", temp2); |
| 349 | |
| 350 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[ATCFAIL_IRQ]); |
| 351 | dev_dbg(pm8058_chg.dev, "ATCFAIL_IRQ= %d\n", temp2); |
| 352 | |
| 353 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ]); |
| 354 | dev_dbg(pm8058_chg.dev, "AUTO_CHGDONE_IRQ= %d\n", temp2); |
| 355 | |
| 356 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ]); |
| 357 | dev_dbg(pm8058_chg.dev, "AUTO_CHGFAIL_IRQ= %d\n", temp2); |
| 358 | |
| 359 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ]); |
| 360 | dev_dbg(pm8058_chg.dev, "CHGSTATE_IRQ= %d\n", temp2); |
| 361 | |
| 362 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[FASTCHG_IRQ]); |
| 363 | dev_dbg(pm8058_chg.dev, "FASTCHG_IRQ= %d\n", temp2); |
| 364 | |
| 365 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHG_END_IRQ]); |
| 366 | dev_dbg(pm8058_chg.dev, "CHG_END_IRQ= %d\n", temp2); |
| 367 | |
| 368 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ]); |
| 369 | dev_dbg(pm8058_chg.dev, "BATTTEMP_IRQ= %d\n", temp2); |
| 370 | |
| 371 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGHOT_IRQ]); |
| 372 | dev_dbg(pm8058_chg.dev, "CHGHOT_IRQ= %d\n", temp2); |
| 373 | |
| 374 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGTLIMIT_IRQ]); |
| 375 | dev_dbg(pm8058_chg.dev, "CHGTLIMIT_IRQ= %d\n", temp2); |
| 376 | |
| 377 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHG_GONE_IRQ]); |
| 378 | dev_dbg(pm8058_chg.dev, "CHG_GONE_IRQ= %d\n", temp2); |
| 379 | |
| 380 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VCPMAJOR_IRQ]); |
| 381 | dev_dbg(pm8058_chg.dev, "VCPMAJOR_IRQ= %d\n", temp2); |
| 382 | |
| 383 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_IRQ]); |
| 384 | dev_dbg(pm8058_chg.dev, "VBATDET_IRQ= %d\n", temp2); |
| 385 | |
| 386 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATFET_IRQ]); |
| 387 | dev_dbg(pm8058_chg.dev, "BATFET_IRQ= %d\n", temp2); |
| 388 | |
| 389 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ]); |
| 390 | dev_dbg(pm8058_chg.dev, "BATT_REPLACE_IRQ= %d\n", temp2); |
| 391 | |
| 392 | temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]); |
| 393 | dev_dbg(pm8058_chg.dev, "BATTCONNECT_IRQ= %d\n", temp2); |
| 394 | } |
| 395 | #else |
| 396 | static inline void __dump_chg_regs(void) |
| 397 | { |
| 398 | } |
| 399 | #endif |
| 400 | |
| 401 | /* SSBI register access helper functions */ |
| 402 | static int pm_chg_suspend(int value) |
| 403 | { |
| 404 | u8 temp; |
| 405 | int ret; |
| 406 | |
| 407 | ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1); |
| 408 | if (ret) |
| 409 | return ret; |
| 410 | if (value) |
| 411 | temp |= BIT(CHG_USB_SUSPEND); |
| 412 | else |
| 413 | temp &= ~BIT(CHG_USB_SUSPEND); |
| 414 | |
| 415 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1); |
| 416 | } |
| 417 | |
| 418 | static int pm_chg_auto_disable(int value) |
| 419 | { |
| 420 | u8 temp; |
| 421 | int ret; |
| 422 | |
| 423 | ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 424 | if (ret) |
| 425 | return ret; |
| 426 | if (value) |
| 427 | temp |= BIT(CHARGE_AUTO_DIS); |
| 428 | else |
| 429 | temp &= ~BIT(CHARGE_AUTO_DIS); |
| 430 | |
| 431 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 432 | } |
| 433 | |
| 434 | static int pm_chg_batt_temp_disable(int value) |
| 435 | { |
| 436 | u8 temp; |
| 437 | int ret; |
| 438 | |
| 439 | ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 440 | if (ret) |
| 441 | return ret; |
| 442 | if (value) |
| 443 | temp |= BIT(CHG_BATT_TEMP_DIS); |
| 444 | else |
| 445 | temp &= ~BIT(CHG_BATT_TEMP_DIS); |
| 446 | |
| 447 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 448 | } |
| 449 | |
| 450 | static int pm_chg_vbatdet_set(int voltage) |
| 451 | { |
| 452 | u8 temp; |
| 453 | int diff; |
| 454 | |
| 455 | diff = (voltage - PM8058_CHG_V_MIN_MV); |
| 456 | if (diff < 0) { |
| 457 | dev_warn(pm8058_chg.dev, "%s bad mV=%d asked to set\n", |
| 458 | __func__, voltage); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
| 462 | temp = diff / PM8058_CHG_V_STEP_MV; |
| 463 | dev_dbg(pm8058_chg.dev, "%s voltage=%d setting %02x\n", __func__, |
| 464 | voltage, temp); |
| 465 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_VBAT_DET, &temp, 1); |
| 466 | } |
| 467 | |
| 468 | static int pm_chg_imaxsel_set(int chg_current) |
| 469 | { |
| 470 | u8 temp; |
| 471 | int diff; |
| 472 | |
| 473 | diff = chg_current - PM8058_CHG_I_MIN_MA; |
| 474 | if (diff < 0) { |
| 475 | dev_warn(pm8058_chg.dev, "%s bad mA=%d asked to set\n", |
| 476 | __func__, chg_current); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | temp = diff / PM8058_CHG_I_STEP_MA; |
| 480 | /* make sure we arent writing more than 5 bits of data */ |
| 481 | if (temp > 31) { |
| 482 | dev_warn(pm8058_chg.dev, "%s max mA=1500 requested mA=%d\n", |
| 483 | __func__, chg_current); |
| 484 | temp = 31; |
| 485 | } |
| 486 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_IMAX, &temp, 1); |
| 487 | } |
| 488 | |
| 489 | #define PM8058_CHG_VMAX_MIN 3300 |
| 490 | #define PM8058_CHG_VMAX_MAX 5500 |
| 491 | static int pm_chg_vmaxsel_set(int voltage) |
| 492 | { |
| 493 | u8 temp; |
| 494 | |
| 495 | if (voltage < PM8058_CHG_VMAX_MIN || voltage > PM8058_CHG_VMAX_MAX) { |
| 496 | dev_warn(pm8058_chg.dev, "%s bad mV=%d asked to set\n", |
| 497 | __func__, voltage); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | temp = (voltage - PM8058_CHG_V_MIN_MV) / PM8058_CHG_V_STEP_MV; |
| 501 | dev_dbg(pm8058_chg.dev, "%s mV=%d setting %02x\n", __func__, voltage, |
| 502 | temp); |
| 503 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_VMAX_SEL, &temp, 1); |
| 504 | } |
| 505 | |
| 506 | static int pm_chg_failed_clear(int value) |
| 507 | { |
| 508 | u8 temp; |
| 509 | int ret; |
| 510 | |
| 511 | ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 512 | if (ret) |
| 513 | return ret; |
| 514 | if (value) |
| 515 | temp |= BIT(CHG_FAILED_CLEAR); |
| 516 | else |
| 517 | temp &= ~BIT(CHG_FAILED_CLEAR); |
| 518 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 519 | } |
| 520 | |
| 521 | static int pm_chg_iterm_set(int chg_current) |
| 522 | { |
| 523 | u8 temp; |
| 524 | |
| 525 | temp = (chg_current / PM8058_CHG_I_TERM_STEP_MA) - 1; |
| 526 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_ITERM, &temp, 1); |
| 527 | } |
| 528 | |
| 529 | static int pm_chg_tchg_set(int minutes) |
| 530 | { |
| 531 | u8 temp; |
| 532 | |
| 533 | temp = (minutes >> PM8058_CHG_T_TCHG_SHIFT) - 1; |
| 534 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TCHG_MAX, &temp, 1); |
| 535 | } |
| 536 | |
| 537 | static int pm_chg_ttrkl_set(int minutes) |
| 538 | { |
| 539 | u8 temp; |
| 540 | |
| 541 | temp = minutes - 1; |
| 542 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TTRKL_MAX, &temp, 1); |
| 543 | } |
| 544 | |
| 545 | static int pm_chg_enum_done_enable(int value) |
| 546 | { |
| 547 | u8 temp; |
| 548 | int ret; |
| 549 | |
| 550 | ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 551 | if (ret) |
| 552 | return ret; |
| 553 | if (value) |
| 554 | temp |= BIT(ENUM_DONE); |
| 555 | else |
| 556 | temp &= ~BIT(ENUM_DONE); |
| 557 | |
| 558 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1); |
| 559 | } |
| 560 | |
| 561 | static uint32_t get_fsm_state(void) |
| 562 | { |
| 563 | u8 temp; |
| 564 | |
| 565 | temp = 0x00; |
| 566 | pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1); |
| 567 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1); |
| 568 | return (uint32_t)temp; |
| 569 | } |
| 570 | |
| 571 | static int get_fsm_status(void *data, u64 * val) |
| 572 | { |
| 573 | *val = get_fsm_state(); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int __pm8058_start_charging(int chg_current, int termination_current, |
| 578 | int time) |
| 579 | { |
| 580 | int ret = 0; |
| 581 | |
| 582 | if (pm8058_chg.disabled) |
| 583 | goto out; |
| 584 | |
| 585 | dev_info(pm8058_chg.dev, "%s %dmA %dmin\n", |
| 586 | __func__, chg_current, time); |
| 587 | |
| 588 | ret = pm_chg_auto_disable(1); |
| 589 | if (ret) |
| 590 | goto out; |
| 591 | |
| 592 | ret = pm_chg_suspend(0); |
| 593 | if (ret) |
| 594 | goto out; |
| 595 | |
| 596 | ret = pm_chg_imaxsel_set(chg_current); |
| 597 | if (ret) |
| 598 | goto out; |
| 599 | |
| 600 | ret = pm_chg_failed_clear(1); |
| 601 | if (ret) |
| 602 | goto out; |
| 603 | |
| 604 | ret = pm_chg_iterm_set(termination_current); |
| 605 | if (ret) |
| 606 | goto out; |
| 607 | |
| 608 | ret = pm_chg_tchg_set(time); |
| 609 | if (ret) |
| 610 | goto out; |
| 611 | |
| 612 | ret = pm_chg_ttrkl_set(AUTO_CHARGING_TRICKLE_TIME_MINUTES); |
| 613 | if (ret) |
| 614 | goto out; |
| 615 | |
| 616 | ret = pm_chg_batt_temp_disable(0); |
| 617 | if (ret) |
| 618 | goto out; |
| 619 | |
| 620 | if (pm8058_chg.voter == NULL) |
| 621 | pm8058_chg.voter = msm_xo_get(MSM_XO_TCXO_D1, "pm8058_charger"); |
| 622 | msm_xo_mode_vote(pm8058_chg.voter, MSM_XO_MODE_ON); |
| 623 | |
| 624 | ret = pm_chg_enum_done_enable(1); |
| 625 | if (ret) |
| 626 | goto out; |
| 627 | |
| 628 | wmb(); |
| 629 | |
| 630 | ret = pm_chg_auto_disable(0); |
| 631 | if (ret) |
| 632 | goto out; |
| 633 | |
| 634 | /* wait for the enable to update interrupt status*/ |
| 635 | msleep(20); |
| 636 | |
| 637 | pm8058_chg_enable_irq(AUTO_CHGFAIL_IRQ); |
| 638 | pm8058_chg_enable_irq(CHGHOT_IRQ); |
| 639 | pm8058_chg_enable_irq(AUTO_CHGDONE_IRQ); |
| 640 | pm8058_chg_enable_irq(CHG_END_IRQ); |
| 641 | pm8058_chg_enable_irq(CHGSTATE_IRQ); |
| 642 | |
| 643 | out: |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | static void chg_done_cleanup(void) |
| 648 | { |
| 649 | dev_info(pm8058_chg.dev, "%s notify pm8058 charging completion" |
| 650 | "\n", __func__); |
| 651 | |
| 652 | pm8058_chg_disable_irq(AUTO_CHGDONE_IRQ); |
| 653 | cancel_delayed_work_sync(&pm8058_chg.veoc_begin_work); |
| 654 | cancel_delayed_work_sync(&pm8058_chg.check_vbat_low_work); |
| 655 | |
| 656 | pm8058_chg_disable_irq(CHG_END_IRQ); |
| 657 | |
| 658 | pm8058_chg_disable_irq(VBATDET_LOW_IRQ); |
| 659 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 660 | pm8058_chg.waiting_for_veoc = 0; |
| 661 | pm8058_chg.waiting_for_topoff = 0; |
| 662 | |
| 663 | pm_chg_auto_disable(1); |
| 664 | |
| 665 | msm_charger_notify_event(&usb_hw_chg, CHG_DONE_EVENT); |
| 666 | } |
| 667 | |
| 668 | static void chg_done_check_work(struct work_struct *work) |
| 669 | { |
| 670 | chg_done_cleanup(); |
| 671 | } |
| 672 | |
| 673 | static void charging_check_work(struct work_struct *work) |
| 674 | { |
| 675 | uint32_t fsm_state = get_fsm_state(); |
| 676 | int rc; |
| 677 | |
| 678 | switch (fsm_state) { |
| 679 | /* We're charging, so disarm alarm */ |
| 680 | case 2: |
| 681 | case 7: |
| 682 | case 8: |
| 683 | rc = pm8058_batt_alarm_state_set(0, 0); |
| 684 | if (rc) |
| 685 | dev_err(pm8058_chg.dev, |
| 686 | "%s: unable to set alarm state\n", __func__); |
| 687 | break; |
| 688 | default: |
| 689 | /* Still not charging, so update driver state */ |
| 690 | chg_done_cleanup(); |
| 691 | break; |
| 692 | }; |
| 693 | } |
| 694 | |
| 695 | static int pm8058_start_charging(struct msm_hardware_charger *hw_chg, |
| 696 | int chg_voltage, int chg_current) |
| 697 | { |
| 698 | int vbat_higher_than_vbatdet; |
| 699 | int ret = 0; |
| 700 | |
| 701 | cancel_delayed_work_sync(&pm8058_chg.charging_check_work); |
| 702 | |
| 703 | /* |
| 704 | * adjust the max current for PC USB connection - set the higher limit |
| 705 | * to 450 and make sure we never cross it |
| 706 | */ |
| 707 | if (chg_current == 500) |
| 708 | chg_current = 450; |
| 709 | pm8058_chg.current_charger_current = chg_current; |
| 710 | pm8058_chg_enable_irq(FASTCHG_IRQ); |
| 711 | |
| 712 | ret = pm_chg_vmaxsel_set(chg_voltage); |
| 713 | if (ret) |
| 714 | goto out; |
| 715 | |
| 716 | /* set vbat to CC to CV threshold */ |
| 717 | ret = pm_chg_vbatdet_set(AUTO_CHARGING_VBATDET); |
| 718 | if (ret) |
| 719 | goto out; |
| 720 | |
| 721 | pm8058_chg.vbatdet = AUTO_CHARGING_VBATDET; |
| 722 | /* |
| 723 | * get the state of vbat and if it is higher than |
| 724 | * AUTO_CHARGING_VBATDET we start the veoc start timer |
| 725 | * else wait for the voltage to go to AUTO_CHARGING_VBATDET |
| 726 | * and then start the 90 min timer |
| 727 | */ |
| 728 | vbat_higher_than_vbatdet = |
| 729 | pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_IRQ]); |
| 730 | if (vbat_higher_than_vbatdet) { |
| 731 | /* |
| 732 | * we are in constant voltage phase of charging |
| 733 | * IEOC should happen withing 90 mins of this instant |
| 734 | * else we enable VEOC |
| 735 | */ |
| 736 | dev_info(pm8058_chg.dev, "%s begin veoc timer\n", __func__); |
| 737 | schedule_delayed_work(&pm8058_chg.veoc_begin_work, |
| 738 | round_jiffies_relative(msecs_to_jiffies |
| 739 | (AUTO_CHARGING_VEOC_BEGIN_TIME_MS))); |
| 740 | } else |
| 741 | pm8058_chg_enable_irq(VBATDET_IRQ); |
| 742 | |
| 743 | ret = __pm8058_start_charging(chg_current, AUTO_CHARGING_IEOC_ITERM, |
| 744 | AUTO_CHARGING_FAST_TIME_MAX_MINUTES); |
| 745 | pm8058_chg.current_charger_current = chg_current; |
| 746 | |
| 747 | /* |
| 748 | * We want to check the FSM state to verify we're charging. We must |
| 749 | * wait before doing this to allow the VBATDET to settle. The worst |
| 750 | * case for this is two seconds. The batt alarm does not have this |
| 751 | * delay. |
| 752 | */ |
| 753 | schedule_delayed_work(&pm8058_chg.charging_check_work, |
| 754 | round_jiffies_relative(msecs_to_jiffies |
| 755 | (AUTO_CHARGING_VBATDET_DEBOUNCE_TIME_MS))); |
| 756 | |
| 757 | out: |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | static void veoc_begin_work(struct work_struct *work) |
| 762 | { |
| 763 | /* we have been doing CV for 90mins with no signs of IEOC |
| 764 | * start checking for VEOC in addition with 16min charges*/ |
| 765 | dev_info(pm8058_chg.dev, "%s begin veoc detection\n", __func__); |
| 766 | pm8058_chg.waiting_for_veoc = 1; |
| 767 | /* |
| 768 | * disable VBATDET irq we dont need it unless we are at the end of |
| 769 | * charge cycle |
| 770 | */ |
| 771 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 772 | __pm8058_start_charging(pm8058_chg.current_charger_current, |
| 773 | AUTO_CHARGING_VEOC_ITERM, |
| 774 | AUTO_CHARGING_VEOC_TCHG); |
| 775 | } |
| 776 | |
| 777 | static void vbat_low_work(struct work_struct *work) |
| 778 | { |
| 779 | /* |
| 780 | * It has been one minute and the battery still holds voltage |
| 781 | * start the final topoff - charging is almost done |
| 782 | */ |
| 783 | dev_info(pm8058_chg.dev, "%s vbatt maintains for a minute" |
| 784 | "starting topoff\n", __func__); |
| 785 | pm8058_chg.waiting_for_veoc = 0; |
| 786 | pm8058_chg.waiting_for_topoff = 1; |
| 787 | pm8058_chg_disable_irq(VBATDET_LOW_IRQ); |
| 788 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 789 | __pm8058_start_charging(pm8058_chg.current_charger_current, |
| 790 | AUTO_CHARGING_VEOC_ITERM, |
| 791 | AUTO_CHARGING_VEOC_TCHG_FINAL_CYCLE); |
| 792 | } |
| 793 | |
| 794 | |
| 795 | static irqreturn_t pm8058_chg_chgval_handler(int irq, void *dev_id) |
| 796 | { |
| 797 | u8 old, temp; |
| 798 | int ret; |
| 799 | |
| 800 | if (is_chg_plugged_in()) { /* this debounces it */ |
| 801 | if (!pm8058_chg.present) { |
| 802 | msm_charger_notify_event(&usb_hw_chg, |
| 803 | CHG_INSERTED_EVENT); |
| 804 | pm8058_chg.present = 1; |
| 805 | } |
| 806 | } else { |
| 807 | if (pm8058_chg.present) { |
| 808 | ret = pm8058_read(pm8058_chg.pm_chip, |
| 809 | PM8058_OVP_TEST_REG, |
| 810 | &old, 1); |
| 811 | temp = old | BIT(FORCE_OVP_OFF); |
| 812 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 813 | PM8058_OVP_TEST_REG, |
| 814 | &temp, 1); |
| 815 | temp = 0xFC; |
| 816 | ret = pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST, |
| 817 | &temp, 1); |
| 818 | /* 10 ms sleep is for the VCHG to discharge */ |
| 819 | msleep(10); |
| 820 | temp = 0xF0; |
| 821 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 822 | PM8058_CHG_TEST, |
| 823 | &temp, 1); |
| 824 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 825 | PM8058_OVP_TEST_REG, |
| 826 | &old, 1); |
| 827 | |
| 828 | pm_chg_enum_done_enable(0); |
| 829 | pm_chg_auto_disable(1); |
| 830 | msm_charger_notify_event(&usb_hw_chg, |
| 831 | CHG_REMOVED_EVENT); |
| 832 | pm8058_chg.present = 0; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | return IRQ_HANDLED; |
| 837 | } |
| 838 | |
| 839 | static irqreturn_t pm8058_chg_chginval_handler(int irq, void *dev_id) |
| 840 | { |
| 841 | u8 old, temp; |
| 842 | int ret; |
| 843 | |
| 844 | if (pm8058_chg.present) { |
| 845 | pm8058_chg_disable_irq(CHGINVAL_IRQ); |
| 846 | |
| 847 | pm_chg_enum_done_enable(0); |
| 848 | pm_chg_auto_disable(1); |
| 849 | ret = pm8058_read(pm8058_chg.pm_chip, |
| 850 | PM8058_OVP_TEST_REG, &old, 1); |
| 851 | temp = old | BIT(FORCE_OVP_OFF); |
| 852 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 853 | PM8058_OVP_TEST_REG, &temp, 1); |
| 854 | temp = 0xFC; |
| 855 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 856 | PM8058_CHG_TEST, &temp, 1); |
| 857 | /* 10 ms sleep is for the VCHG to discharge */ |
| 858 | msleep(10); |
| 859 | temp = 0xF0; |
| 860 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 861 | PM8058_CHG_TEST, &temp, 1); |
| 862 | ret = pm8058_write(pm8058_chg.pm_chip, |
| 863 | PM8058_OVP_TEST_REG, &old, 1); |
| 864 | |
| 865 | if (!is_chg_plugged_in()) { |
| 866 | msm_charger_notify_event(&usb_hw_chg, |
| 867 | CHG_REMOVED_EVENT); |
| 868 | pm8058_chg.present = 0; |
| 869 | } else { |
| 870 | /* was a fake */ |
| 871 | pm8058_chg_enable_irq(CHGINVAL_IRQ); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return IRQ_HANDLED; |
| 876 | } |
| 877 | |
| 878 | static irqreturn_t pm8058_chg_auto_chgdone_handler(int irq, void *dev_id) |
| 879 | { |
| 880 | dev_info(pm8058_chg.dev, "%s waiting a sec to confirm\n", |
| 881 | __func__); |
| 882 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 883 | if (!delayed_work_pending(&pm8058_chg.chg_done_check_work)) { |
| 884 | schedule_delayed_work(&pm8058_chg.chg_done_check_work, |
| 885 | round_jiffies_relative(msecs_to_jiffies |
| 886 | (AUTO_CHARGING_DONE_CHECK_TIME_MS))); |
| 887 | } |
| 888 | return IRQ_HANDLED; |
| 889 | } |
| 890 | |
| 891 | /* can only happen with the pmic charger when it has been charing |
| 892 | * for either 16 mins wating for VEOC or 32 mins for topoff |
| 893 | * without a IEOC indication */ |
| 894 | static irqreturn_t pm8058_chg_auto_chgfail_handler(int irq, void *dev_id) |
| 895 | { |
| 896 | pm8058_chg_disable_irq(AUTO_CHGFAIL_IRQ); |
| 897 | |
| 898 | if (pm8058_chg.waiting_for_topoff == 1) { |
| 899 | dev_info(pm8058_chg.dev, "%s topoff done, charging done\n", |
| 900 | __func__); |
| 901 | pm8058_chg.waiting_for_topoff = 0; |
| 902 | /* notify we are done charging */ |
| 903 | msm_charger_notify_event(&usb_hw_chg, CHG_DONE_EVENT); |
| 904 | } else { |
| 905 | /* start one minute timer and monitor VBATDET_LOW */ |
| 906 | dev_info(pm8058_chg.dev, "%s monitoring vbat_low for a" |
| 907 | "minute\n", __func__); |
| 908 | schedule_delayed_work(&pm8058_chg.check_vbat_low_work, |
| 909 | round_jiffies_relative(msecs_to_jiffies |
| 910 | (AUTO_CHARGING_VEOC_VBAT_LOW_CHECK_TIME_MS))); |
| 911 | |
| 912 | /* note we are waiting on veoc */ |
| 913 | pm8058_chg.waiting_for_veoc = 1; |
| 914 | |
| 915 | pm_chg_vbatdet_set(AUTO_CHARGING_VEOC_VBATDET); |
| 916 | pm8058_chg.vbatdet = AUTO_CHARGING_VEOC_VBATDET; |
| 917 | pm8058_chg_enable_irq(VBATDET_LOW_IRQ); |
| 918 | } |
| 919 | return IRQ_HANDLED; |
| 920 | } |
| 921 | |
| 922 | static irqreturn_t pm8058_chg_chgstate_handler(int irq, void *dev_id) |
| 923 | { |
| 924 | u8 temp; |
| 925 | |
| 926 | temp = 0x00; |
| 927 | if (!pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1)) { |
| 928 | pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1); |
| 929 | dev_dbg(pm8058_chg.dev, "%s state=%d\n", __func__, temp); |
| 930 | } |
| 931 | return IRQ_HANDLED; |
| 932 | } |
| 933 | |
| 934 | static irqreturn_t pm8058_chg_fastchg_handler(int irq, void *dev_id) |
| 935 | { |
| 936 | pm8058_chg_disable_irq(FASTCHG_IRQ); |
| 937 | |
| 938 | /* we have begun the fast charging state */ |
| 939 | dev_info(pm8058_chg.dev, "%s begin fast charging" |
| 940 | , __func__); |
| 941 | msm_charger_notify_event(&usb_hw_chg, CHG_BATT_BEGIN_FAST_CHARGING); |
| 942 | return IRQ_HANDLED; |
| 943 | } |
| 944 | |
| 945 | static irqreturn_t pm8058_chg_batttemp_handler(int irq, void *dev_id) |
| 946 | { |
| 947 | int ret; |
| 948 | |
| 949 | /* we could get temperature |
| 950 | * interrupt when the battery is plugged out |
| 951 | */ |
| 952 | ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]); |
| 953 | if (ret) { |
| 954 | msm_charger_notify_event(&usb_hw_chg, CHG_BATT_REMOVED); |
| 955 | } else { |
| 956 | /* read status to determine we are inrange or outofrange */ |
| 957 | ret = |
| 958 | pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ]); |
| 959 | if (ret) |
| 960 | msm_charger_notify_event(&usb_hw_chg, |
| 961 | CHG_BATT_TEMP_OUTOFRANGE); |
| 962 | else |
| 963 | msm_charger_notify_event(&usb_hw_chg, |
| 964 | CHG_BATT_TEMP_INRANGE); |
| 965 | } |
| 966 | |
| 967 | return IRQ_HANDLED; |
| 968 | } |
| 969 | |
| 970 | static irqreturn_t pm8058_chg_vbatdet_handler(int irq, void *dev_id) |
| 971 | { |
| 972 | int ret; |
| 973 | |
| 974 | /* settling time */ |
| 975 | msleep(20); |
| 976 | ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_IRQ]); |
| 977 | |
| 978 | if (ret) { |
| 979 | if (pm8058_chg.vbatdet == AUTO_CHARGING_VBATDET |
| 980 | && !delayed_work_pending(&pm8058_chg.veoc_begin_work)) { |
| 981 | /* |
| 982 | * we are in constant voltage phase of charging |
| 983 | * IEOC should happen withing 90 mins of this instant |
| 984 | * else we enable VEOC |
| 985 | */ |
| 986 | dev_info(pm8058_chg.dev, "%s entered constant voltage" |
| 987 | "begin veoc timer\n", __func__); |
| 988 | schedule_delayed_work(&pm8058_chg.veoc_begin_work, |
| 989 | round_jiffies_relative |
| 990 | (msecs_to_jiffies |
| 991 | (AUTO_CHARGING_VEOC_BEGIN_TIME_MS))); |
| 992 | } |
| 993 | } else { |
| 994 | if (pm8058_chg.vbatdet == AUTO_CHARGING_VEOC_VBATDET) { |
| 995 | cancel_delayed_work_sync( |
| 996 | &pm8058_chg.check_vbat_low_work); |
| 997 | |
| 998 | if (pm8058_chg.waiting_for_topoff |
| 999 | || pm8058_chg.waiting_for_veoc) { |
| 1000 | /* |
| 1001 | * the battery dropped its voltage below 4100 |
| 1002 | * around a minute charge the battery for 16 |
| 1003 | * mins and check vbat again for a minute |
| 1004 | */ |
| 1005 | dev_info(pm8058_chg.dev, "%s batt dropped vlt" |
| 1006 | "within a minute\n", __func__); |
| 1007 | pm8058_chg.waiting_for_topoff = 0; |
| 1008 | pm8058_chg.waiting_for_veoc = 1; |
| 1009 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 1010 | __pm8058_start_charging(pm8058_chg. |
| 1011 | current_charger_current, |
| 1012 | AUTO_CHARGING_VEOC_ITERM, |
| 1013 | AUTO_CHARGING_VEOC_TCHG); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | return IRQ_HANDLED; |
| 1018 | } |
| 1019 | |
| 1020 | static irqreturn_t pm8058_chg_batt_replace_handler(int irq, void *dev_id) |
| 1021 | { |
| 1022 | int ret; |
| 1023 | |
| 1024 | pm8058_chg_disable_irq(BATT_REPLACE_IRQ); |
| 1025 | ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ]); |
| 1026 | if (ret) { |
| 1027 | msm_charger_notify_event(&usb_hw_chg, CHG_BATT_INSERTED); |
| 1028 | /* |
| 1029 | * battery is present enable batt removal |
| 1030 | * and batt temperatture interrupt |
| 1031 | */ |
| 1032 | pm8058_chg_enable_irq(BATTCONNECT_IRQ); |
| 1033 | } |
| 1034 | return IRQ_HANDLED; |
| 1035 | } |
| 1036 | |
| 1037 | static irqreturn_t pm8058_chg_battconnect_handler(int irq, void *dev_id) |
| 1038 | { |
| 1039 | int ret; |
| 1040 | |
| 1041 | ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]); |
| 1042 | if (ret) { |
| 1043 | msm_charger_notify_event(&usb_hw_chg, CHG_BATT_REMOVED); |
| 1044 | } else { |
| 1045 | msm_charger_notify_event(&usb_hw_chg, CHG_BATT_INSERTED); |
| 1046 | pm8058_chg_enable_irq(BATTTEMP_IRQ); |
| 1047 | } |
| 1048 | |
| 1049 | return IRQ_HANDLED; |
| 1050 | } |
| 1051 | |
| 1052 | static int get_rt_status(void *data, u64 * val) |
| 1053 | { |
| 1054 | int i = (int)data; |
| 1055 | int ret; |
| 1056 | |
| 1057 | ret = pm_chg_get_rt_status(i); |
| 1058 | *val = ret; |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 1063 | DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n"); |
| 1064 | |
| 1065 | static void free_irqs(void) |
| 1066 | { |
| 1067 | int i; |
| 1068 | |
| 1069 | for (i = 0; i < PMIC_CHG_MAX_INTS; i++) |
| 1070 | if (pm8058_chg.pmic_chg_irq[i]) { |
| 1071 | free_irq(pm8058_chg.pmic_chg_irq[i], NULL); |
| 1072 | pm8058_chg.pmic_chg_irq[i] = 0; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | static int __devinit request_irqs(struct platform_device *pdev) |
| 1077 | { |
| 1078 | struct resource *res; |
| 1079 | int ret; |
| 1080 | |
| 1081 | ret = 0; |
| 1082 | bitmap_fill(pm8058_chg.enabled_irqs, PMIC_CHG_MAX_INTS); |
| 1083 | |
| 1084 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "CHGVAL"); |
| 1085 | if (res == NULL) { |
| 1086 | dev_err(pm8058_chg.dev, |
| 1087 | "%s:couldnt find resource CHGVAL\n", __func__); |
| 1088 | goto err_out; |
| 1089 | } else { |
| 1090 | ret = request_threaded_irq(res->start, NULL, |
| 1091 | pm8058_chg_chgval_handler, |
| 1092 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1093 | res->name, NULL); |
| 1094 | if (ret < 0) { |
| 1095 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1096 | __func__, res->start, ret); |
| 1097 | goto err_out; |
| 1098 | } else { |
| 1099 | pm8058_chg.pmic_chg_irq[CHGVAL_IRQ] = res->start; |
| 1100 | pm8058_chg_disable_irq(CHGVAL_IRQ); |
| 1101 | enable_irq_wake(pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "CHGINVAL"); |
| 1106 | if (res == NULL) { |
| 1107 | dev_err(pm8058_chg.dev, |
| 1108 | "%s:couldnt find resource CHGINVAL\n", __func__); |
| 1109 | goto err_out; |
| 1110 | } else { |
| 1111 | ret = request_threaded_irq(res->start, NULL, |
| 1112 | pm8058_chg_chginval_handler, |
| 1113 | IRQF_TRIGGER_RISING, res->name, NULL); |
| 1114 | if (ret < 0) { |
| 1115 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1116 | __func__, res->start, ret); |
| 1117 | goto err_out; |
| 1118 | } else { |
| 1119 | pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ] = res->start; |
| 1120 | pm8058_chg_disable_irq(CHGINVAL_IRQ); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1125 | "AUTO_CHGDONE"); |
| 1126 | if (res == NULL) { |
| 1127 | dev_err(pm8058_chg.dev, |
| 1128 | "%s:couldnt find resource AUTO_CHGDONE\n", __func__); |
| 1129 | goto err_out; |
| 1130 | } else { |
| 1131 | ret = request_threaded_irq(res->start, NULL, |
| 1132 | pm8058_chg_auto_chgdone_handler, |
| 1133 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1134 | res->name, NULL); |
| 1135 | if (ret < 0) { |
| 1136 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1137 | __func__, res->start, ret); |
| 1138 | goto err_out; |
| 1139 | } else { |
| 1140 | pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ] = res->start; |
| 1141 | pm8058_chg_disable_irq(AUTO_CHGDONE_IRQ); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1146 | "AUTO_CHGFAIL"); |
| 1147 | if (res == NULL) { |
| 1148 | dev_err(pm8058_chg.dev, |
| 1149 | "%s:couldnt find resource AUTO_CHGFAIL\n", __func__); |
| 1150 | goto err_out; |
| 1151 | } else { |
| 1152 | ret = request_threaded_irq(res->start, NULL, |
| 1153 | pm8058_chg_auto_chgfail_handler, |
| 1154 | IRQF_TRIGGER_RISING, res->name, NULL); |
| 1155 | if (ret < 0) { |
| 1156 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1157 | __func__, res->start, ret); |
| 1158 | goto err_out; |
| 1159 | } else { |
| 1160 | pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ] = res->start; |
| 1161 | pm8058_chg_disable_irq(AUTO_CHGFAIL_IRQ); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "CHGSTATE"); |
| 1166 | if (res == NULL) { |
| 1167 | dev_err(pm8058_chg.dev, |
| 1168 | "%s:couldnt find resource CHGSTATE\n", __func__); |
| 1169 | goto err_out; |
| 1170 | } else { |
| 1171 | ret = request_threaded_irq(res->start, NULL, |
| 1172 | pm8058_chg_chgstate_handler, |
| 1173 | IRQF_TRIGGER_RISING, res->name, NULL); |
| 1174 | if (ret < 0) { |
| 1175 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1176 | __func__, res->start, ret); |
| 1177 | goto err_out; |
| 1178 | } else { |
| 1179 | pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ] = res->start; |
| 1180 | pm8058_chg_disable_irq(CHGSTATE_IRQ); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "FASTCHG"); |
| 1185 | if (res == NULL) { |
| 1186 | dev_err(pm8058_chg.dev, |
| 1187 | "%s:couldnt find resource FASTCHG\n", __func__); |
| 1188 | goto err_out; |
| 1189 | } else { |
| 1190 | ret = request_threaded_irq(res->start, NULL, |
| 1191 | pm8058_chg_fastchg_handler, |
| 1192 | IRQF_TRIGGER_RISING, res->name, NULL); |
| 1193 | if (ret < 0) { |
| 1194 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1195 | __func__, res->start, ret); |
| 1196 | goto err_out; |
| 1197 | } else { |
| 1198 | pm8058_chg.pmic_chg_irq[FASTCHG_IRQ] = res->start; |
| 1199 | pm8058_chg_disable_irq(FASTCHG_IRQ); |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "BATTTEMP"); |
| 1204 | if (res == NULL) { |
| 1205 | dev_err(pm8058_chg.dev, |
| 1206 | "%s:couldnt find resource CHG_END\n", __func__); |
| 1207 | goto err_out; |
| 1208 | } else { |
| 1209 | ret = request_threaded_irq(res->start, NULL, |
| 1210 | pm8058_chg_batttemp_handler, |
| 1211 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1212 | res->name, NULL); |
| 1213 | if (ret < 0) { |
| 1214 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1215 | __func__, res->start, ret); |
| 1216 | goto err_out; |
| 1217 | } else { |
| 1218 | pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ] = res->start; |
| 1219 | pm8058_chg_disable_irq(BATTTEMP_IRQ); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1224 | "BATT_REPLACE"); |
| 1225 | if (res == NULL) { |
| 1226 | dev_err(pm8058_chg.dev, |
| 1227 | "%s:couldnt find resource BATT_REPLACE\n", __func__); |
| 1228 | goto err_out; |
| 1229 | } else { |
| 1230 | ret = request_threaded_irq(res->start, NULL, |
| 1231 | pm8058_chg_batt_replace_handler, |
| 1232 | IRQF_TRIGGER_RISING, res->name, NULL); |
| 1233 | if (ret < 0) { |
| 1234 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1235 | __func__, res->start, ret); |
| 1236 | goto err_out; |
| 1237 | } else { |
| 1238 | pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ] = res->start; |
| 1239 | pm8058_chg_disable_irq(BATT_REPLACE_IRQ); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "BATTCONNECT"); |
| 1244 | if (res == NULL) { |
| 1245 | dev_err(pm8058_chg.dev, |
| 1246 | "%s:couldnt find resource BATTCONNECT\n", __func__); |
| 1247 | goto err_out; |
| 1248 | } else { |
| 1249 | ret = request_threaded_irq(res->start, NULL, |
| 1250 | pm8058_chg_battconnect_handler, |
| 1251 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1252 | res->name, NULL); |
| 1253 | if (ret < 0) { |
| 1254 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1255 | __func__, res->start, ret); |
| 1256 | goto err_out; |
| 1257 | } else { |
| 1258 | pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ] = res->start; |
| 1259 | pm8058_chg_disable_irq(BATTCONNECT_IRQ); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "VBATDET"); |
| 1264 | if (res == NULL) { |
| 1265 | dev_err(pm8058_chg.dev, |
| 1266 | "%s:couldnt find resource VBATDET\n", __func__); |
| 1267 | goto err_out; |
| 1268 | } else { |
| 1269 | ret = request_threaded_irq(res->start, NULL, |
| 1270 | pm8058_chg_vbatdet_handler, |
| 1271 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1272 | res->name, NULL); |
| 1273 | if (ret < 0) { |
| 1274 | dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n", |
| 1275 | __func__, res->start, ret); |
| 1276 | goto err_out; |
| 1277 | } else { |
| 1278 | pm8058_chg.pmic_chg_irq[VBATDET_IRQ] = res->start; |
| 1279 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | return 0; |
| 1284 | |
| 1285 | err_out: |
| 1286 | free_irqs(); |
| 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | |
| 1290 | static int pm8058_get_charge_batt(void) |
| 1291 | { |
| 1292 | u8 temp; |
| 1293 | int rc; |
| 1294 | |
| 1295 | rc = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1); |
| 1296 | if (rc) |
| 1297 | return rc; |
| 1298 | |
| 1299 | temp &= BIT(CHG_CHARGE_BAT); |
| 1300 | if (temp) |
| 1301 | temp = 1; |
| 1302 | return temp; |
| 1303 | } |
| 1304 | EXPORT_SYMBOL(pm8058_get_charge_batt); |
| 1305 | |
| 1306 | static int pm8058_set_charge_batt(int on) |
| 1307 | { |
| 1308 | u8 temp; |
| 1309 | int rc; |
| 1310 | |
| 1311 | rc = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1); |
| 1312 | if (rc) |
| 1313 | return rc; |
| 1314 | if (on) |
| 1315 | temp |= BIT(CHG_CHARGE_BAT); |
| 1316 | else |
| 1317 | temp &= ~BIT(CHG_CHARGE_BAT); |
| 1318 | |
| 1319 | return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1); |
| 1320 | |
| 1321 | } |
| 1322 | EXPORT_SYMBOL(pm8058_set_charge_batt); |
| 1323 | |
| 1324 | static int get_charge_batt(void *data, u64 * val) |
| 1325 | { |
| 1326 | int ret; |
| 1327 | |
| 1328 | ret = pm8058_get_charge_batt(); |
| 1329 | if (ret < 0) |
| 1330 | return ret; |
| 1331 | |
| 1332 | *val = ret; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | static int set_charge_batt(void *data, u64 val) |
| 1337 | { |
| 1338 | return pm8058_set_charge_batt(val); |
| 1339 | } |
| 1340 | DEFINE_SIMPLE_ATTRIBUTE(fet_fops, get_charge_batt, set_charge_batt, "%llu\n"); |
| 1341 | |
| 1342 | static void pm8058_chg_determine_initial_state(void) |
| 1343 | { |
| 1344 | if (is_chg_plugged_in()) { |
| 1345 | pm8058_chg.present = 1; |
| 1346 | msm_charger_notify_event(&usb_hw_chg, CHG_INSERTED_EVENT); |
| 1347 | dev_info(pm8058_chg.dev, "%s charger present\n", __func__); |
| 1348 | } else { |
| 1349 | pm8058_chg.present = 0; |
| 1350 | dev_info(pm8058_chg.dev, "%s charger absent\n", __func__); |
| 1351 | } |
| 1352 | pm8058_chg_enable_irq(CHGVAL_IRQ); |
| 1353 | } |
| 1354 | |
| 1355 | static int pm8058_stop_charging(struct msm_hardware_charger *hw_chg) |
| 1356 | { |
| 1357 | int ret; |
| 1358 | |
| 1359 | dev_info(pm8058_chg.dev, "%s stopping charging\n", __func__); |
| 1360 | cancel_delayed_work_sync(&pm8058_chg.veoc_begin_work); |
| 1361 | cancel_delayed_work_sync(&pm8058_chg.check_vbat_low_work); |
| 1362 | cancel_delayed_work_sync(&pm8058_chg.chg_done_check_work); |
| 1363 | cancel_delayed_work_sync(&pm8058_chg.charging_check_work); |
| 1364 | |
| 1365 | ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[FASTCHG_IRQ]); |
| 1366 | if (ret == 1) |
| 1367 | pm_chg_suspend(1); |
| 1368 | else |
| 1369 | dev_err(pm8058_chg.dev, |
| 1370 | "%s called when not fast-charging\n", __func__); |
| 1371 | |
| 1372 | pm_chg_failed_clear(1); |
| 1373 | |
| 1374 | pm8058_chg.waiting_for_veoc = 0; |
| 1375 | pm8058_chg.waiting_for_topoff = 0; |
| 1376 | |
| 1377 | /* disable the irqs enabled while charging */ |
| 1378 | pm8058_chg_disable_irq(AUTO_CHGFAIL_IRQ); |
| 1379 | pm8058_chg_disable_irq(CHGHOT_IRQ); |
| 1380 | pm8058_chg_disable_irq(AUTO_CHGDONE_IRQ); |
| 1381 | pm8058_chg_disable_irq(FASTCHG_IRQ); |
| 1382 | pm8058_chg_disable_irq(CHG_END_IRQ); |
| 1383 | pm8058_chg_disable_irq(VBATDET_IRQ); |
| 1384 | pm8058_chg_disable_irq(VBATDET_LOW_IRQ); |
| 1385 | if (pm8058_chg.voter) |
| 1386 | msm_xo_mode_vote(pm8058_chg.voter, MSM_XO_MODE_OFF); |
| 1387 | |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | static int get_status(void *data, u64 * val) |
| 1392 | { |
| 1393 | *val = pm8058_chg.current_charger_current; |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static int set_status(void *data, u64 val) |
| 1398 | { |
| 1399 | |
| 1400 | pm8058_chg.current_charger_current = val; |
| 1401 | if (pm8058_chg.current_charger_current) |
| 1402 | pm8058_start_charging(NULL, |
| 1403 | AUTO_CHARGING_VMAXSEL, |
| 1404 | pm8058_chg.current_charger_current); |
| 1405 | else |
| 1406 | pm8058_stop_charging(NULL); |
| 1407 | return 0; |
| 1408 | } |
| 1409 | DEFINE_SIMPLE_ATTRIBUTE(chg_fops, get_status, set_status, "%llu\n"); |
| 1410 | |
| 1411 | static int set_disable_status_param(const char *val, struct kernel_param *kp) |
| 1412 | { |
| 1413 | int ret; |
| 1414 | |
| 1415 | ret = param_set_int(val, kp); |
| 1416 | if (ret) |
| 1417 | return ret; |
| 1418 | |
| 1419 | if (pm8058_chg.inited && pm8058_chg.disabled) { |
| 1420 | /* |
| 1421 | * stop_charging is called during usb suspend |
| 1422 | * act as the usb is removed by disabling auto and enum |
| 1423 | */ |
| 1424 | pm_chg_enum_done_enable(0); |
| 1425 | pm_chg_auto_disable(1); |
| 1426 | pm8058_stop_charging(NULL); |
| 1427 | } |
| 1428 | return 0; |
| 1429 | } |
| 1430 | module_param_call(disabled, set_disable_status_param, param_get_uint, |
| 1431 | &(pm8058_chg.disabled), 0644); |
| 1432 | |
| 1433 | static int pm8058_charging_switched(struct msm_hardware_charger *hw_chg) |
| 1434 | { |
| 1435 | u8 temp; |
| 1436 | |
| 1437 | temp = 0xA3; |
| 1438 | pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_2, &temp, 1); |
| 1439 | temp = 0x84; |
| 1440 | pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_2, &temp, 1); |
| 1441 | msleep(2); |
| 1442 | temp = 0x80; |
| 1443 | pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_2, &temp, 1); |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | static int get_reg(void *data, u64 * val) |
| 1448 | { |
| 1449 | int i = (int)data; |
| 1450 | int ret; |
| 1451 | u8 temp; |
| 1452 | |
| 1453 | ret = pm8058_read(pm8058_chg.pm_chip, i, &temp, 1); |
| 1454 | if (ret) |
| 1455 | return -EAGAIN; |
| 1456 | *val = temp; |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | static int set_reg(void *data, u64 val) |
| 1461 | { |
| 1462 | int i = (int)data; |
| 1463 | int ret; |
| 1464 | u8 temp; |
| 1465 | |
| 1466 | temp = (u8) val; |
| 1467 | ret = pm8058_write(pm8058_chg.pm_chip, i, &temp, 1); |
| 1468 | mb(); |
| 1469 | if (ret) |
| 1470 | return -EAGAIN; |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "%llu\n"); |
| 1475 | |
| 1476 | #ifdef CONFIG_DEBUG_FS |
| 1477 | static void create_debugfs_entries(void) |
| 1478 | { |
| 1479 | pm8058_chg.dent = debugfs_create_dir("pm8058_usb_chg", NULL); |
| 1480 | |
| 1481 | if (IS_ERR(pm8058_chg.dent)) { |
| 1482 | pr_err("pmic charger couldnt create debugfs dir\n"); |
| 1483 | return; |
| 1484 | } |
| 1485 | |
| 1486 | debugfs_create_file("CHG_CNTRL", 0644, pm8058_chg.dent, |
| 1487 | (void *)PM8058_CHG_CNTRL, ®_fops); |
| 1488 | debugfs_create_file("CHG_CNTRL_2", 0644, pm8058_chg.dent, |
| 1489 | (void *)PM8058_CHG_CNTRL_2, ®_fops); |
| 1490 | debugfs_create_file("CHG_VMAX_SEL", 0644, pm8058_chg.dent, |
| 1491 | (void *)PM8058_CHG_VMAX_SEL, ®_fops); |
| 1492 | debugfs_create_file("CHG_VBAT_DET", 0644, pm8058_chg.dent, |
| 1493 | (void *)PM8058_CHG_VBAT_DET, ®_fops); |
| 1494 | debugfs_create_file("CHG_IMAX", 0644, pm8058_chg.dent, |
| 1495 | (void *)PM8058_CHG_IMAX, ®_fops); |
| 1496 | debugfs_create_file("CHG_TRICKLE", 0644, pm8058_chg.dent, |
| 1497 | (void *)PM8058_CHG_TRICKLE, ®_fops); |
| 1498 | debugfs_create_file("CHG_ITERM", 0644, pm8058_chg.dent, |
| 1499 | (void *)PM8058_CHG_ITERM, ®_fops); |
| 1500 | debugfs_create_file("CHG_TTRKL_MAX", 0644, pm8058_chg.dent, |
| 1501 | (void *)PM8058_CHG_TTRKL_MAX, ®_fops); |
| 1502 | debugfs_create_file("CHG_TCHG_MAX", 0644, pm8058_chg.dent, |
| 1503 | (void *)PM8058_CHG_TCHG_MAX, ®_fops); |
| 1504 | debugfs_create_file("CHG_TEMP_THRESH", 0644, pm8058_chg.dent, |
| 1505 | (void *)PM8058_CHG_TEMP_THRESH, ®_fops); |
| 1506 | debugfs_create_file("CHG_TEMP_REG", 0644, pm8058_chg.dent, |
| 1507 | (void *)PM8058_CHG_TEMP_REG, ®_fops); |
| 1508 | |
| 1509 | debugfs_create_file("FSM_STATE", 0644, pm8058_chg.dent, NULL, |
| 1510 | &fsm_fops); |
| 1511 | |
| 1512 | debugfs_create_file("stop", 0644, pm8058_chg.dent, NULL, |
| 1513 | &chg_fops); |
| 1514 | |
| 1515 | if (pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]) |
| 1516 | debugfs_create_file("CHGVAL", 0444, pm8058_chg.dent, |
| 1517 | (void *)pm8058_chg.pmic_chg_irq[CHGVAL_IRQ], |
| 1518 | &rt_fops); |
| 1519 | |
| 1520 | if (pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ]) |
| 1521 | debugfs_create_file("CHGINVAL", 0444, pm8058_chg.dent, (void *) |
| 1522 | pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ], |
| 1523 | &rt_fops); |
| 1524 | if (pm8058_chg.pmic_chg_irq[CHGILIM_IRQ]) |
| 1525 | debugfs_create_file("CHGILIM", 0444, pm8058_chg.dent, (void *) |
| 1526 | pm8058_chg.pmic_chg_irq[CHGILIM_IRQ], |
| 1527 | &rt_fops); |
| 1528 | if (pm8058_chg.pmic_chg_irq[VCP_IRQ]) |
| 1529 | debugfs_create_file("VCP", 0444, pm8058_chg.dent, |
| 1530 | (void *)pm8058_chg.pmic_chg_irq[VCP_IRQ], |
| 1531 | &rt_fops); |
| 1532 | if (pm8058_chg.pmic_chg_irq[ATC_DONE_IRQ]) |
| 1533 | debugfs_create_file("ATC_DONE", 0444, pm8058_chg.dent, (void *) |
| 1534 | pm8058_chg.pmic_chg_irq[ATC_DONE_IRQ], |
| 1535 | &rt_fops); |
| 1536 | if (pm8058_chg.pmic_chg_irq[ATCFAIL_IRQ]) |
| 1537 | debugfs_create_file("ATCFAIL", 0444, pm8058_chg.dent, (void *) |
| 1538 | pm8058_chg.pmic_chg_irq[ATCFAIL_IRQ], |
| 1539 | &rt_fops); |
| 1540 | if (pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ]) |
| 1541 | debugfs_create_file("AUTO_CHGDONE", 0444, pm8058_chg.dent, |
| 1542 | (void *) |
| 1543 | pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ], |
| 1544 | &rt_fops); |
| 1545 | if (pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ]) |
| 1546 | debugfs_create_file("AUTO_CHGFAIL", 0444, pm8058_chg.dent, |
| 1547 | (void *) |
| 1548 | pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ], |
| 1549 | &rt_fops); |
| 1550 | if (pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ]) |
| 1551 | debugfs_create_file("CHGSTATE", 0444, pm8058_chg.dent, (void *) |
| 1552 | pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ], |
| 1553 | &rt_fops); |
| 1554 | if (pm8058_chg.pmic_chg_irq[FASTCHG_IRQ]) |
| 1555 | debugfs_create_file("FASTCHG", 0444, pm8058_chg.dent, (void *) |
| 1556 | pm8058_chg.pmic_chg_irq[FASTCHG_IRQ], |
| 1557 | &rt_fops); |
| 1558 | if (pm8058_chg.pmic_chg_irq[CHG_END_IRQ]) |
| 1559 | debugfs_create_file("CHG_END", 0444, pm8058_chg.dent, (void *) |
| 1560 | pm8058_chg.pmic_chg_irq[CHG_END_IRQ], |
| 1561 | &rt_fops); |
| 1562 | if (pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ]) |
| 1563 | debugfs_create_file("BATTTEMP", 0444, pm8058_chg.dent, (void *) |
| 1564 | pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ], |
| 1565 | &rt_fops); |
| 1566 | if (pm8058_chg.pmic_chg_irq[CHGHOT_IRQ]) |
| 1567 | debugfs_create_file("CHGHOT", 0444, pm8058_chg.dent, |
| 1568 | (void *)pm8058_chg.pmic_chg_irq[CHGHOT_IRQ], |
| 1569 | &rt_fops); |
| 1570 | if (pm8058_chg.pmic_chg_irq[CHGTLIMIT_IRQ]) |
| 1571 | debugfs_create_file("CHGTLIMIT", 0444, pm8058_chg.dent, (void *) |
| 1572 | pm8058_chg.pmic_chg_irq[CHGTLIMIT_IRQ], |
| 1573 | &rt_fops); |
| 1574 | if (pm8058_chg.pmic_chg_irq[CHG_GONE_IRQ]) |
| 1575 | debugfs_create_file("CHG_GONE", 0444, pm8058_chg.dent, (void *) |
| 1576 | pm8058_chg.pmic_chg_irq[CHG_GONE_IRQ], |
| 1577 | &rt_fops); |
| 1578 | if (pm8058_chg.pmic_chg_irq[VCPMAJOR_IRQ]) |
| 1579 | debugfs_create_file("VCPMAJOR", 0444, pm8058_chg.dent, (void *) |
| 1580 | pm8058_chg.pmic_chg_irq[VCPMAJOR_IRQ], |
| 1581 | &rt_fops); |
| 1582 | if (pm8058_chg.pmic_chg_irq[BATFET_IRQ]) |
| 1583 | debugfs_create_file("BATFET", 0444, pm8058_chg.dent, |
| 1584 | (void *)pm8058_chg.pmic_chg_irq[BATFET_IRQ], |
| 1585 | &rt_fops); |
| 1586 | if (pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ]) |
| 1587 | debugfs_create_file("BATT_REPLACE", 0444, pm8058_chg.dent, |
| 1588 | (void *) |
| 1589 | pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ], |
| 1590 | &rt_fops); |
| 1591 | if (pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]) |
| 1592 | debugfs_create_file("BATTCONNECT", 0444, pm8058_chg.dent, |
| 1593 | (void *) |
| 1594 | pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ], |
| 1595 | &rt_fops); |
| 1596 | if (pm8058_chg.pmic_chg_irq[VBATDET_IRQ]) |
| 1597 | debugfs_create_file("VBATDET", 0444, pm8058_chg.dent, (void *) |
| 1598 | pm8058_chg.pmic_chg_irq[VBATDET_IRQ], |
| 1599 | &rt_fops); |
| 1600 | if (pm8058_chg.pmic_chg_irq[VBATDET_LOW_IRQ]) |
| 1601 | debugfs_create_file("VBATDET_LOW", 0444, pm8058_chg.dent, |
| 1602 | (void *) |
| 1603 | pm8058_chg.pmic_chg_irq[VBATDET_LOW_IRQ], |
| 1604 | &rt_fops); |
| 1605 | debugfs_create_file("CHARGE_BATT", 0444, pm8058_chg.dent, |
| 1606 | NULL, |
| 1607 | &fet_fops); |
| 1608 | } |
| 1609 | #else |
| 1610 | static inline void create_debugfs_entries(void) |
| 1611 | { |
| 1612 | } |
| 1613 | #endif |
| 1614 | |
| 1615 | static void remove_debugfs_entries(void) |
| 1616 | { |
| 1617 | debugfs_remove_recursive(pm8058_chg.dent); |
| 1618 | } |
| 1619 | |
| 1620 | static struct msm_hardware_charger usb_hw_chg = { |
| 1621 | .type = CHG_TYPE_USB, |
| 1622 | .rating = 1, |
| 1623 | .name = "pm8058-usb", |
| 1624 | .start_charging = pm8058_start_charging, |
| 1625 | .stop_charging = pm8058_stop_charging, |
| 1626 | .charging_switched = pm8058_charging_switched, |
| 1627 | }; |
| 1628 | |
| 1629 | static int batt_read_adc(int channel, int *mv_reading) |
| 1630 | { |
| 1631 | int ret; |
| 1632 | void *h; |
| 1633 | struct adc_chan_result adc_chan_result; |
| 1634 | struct completion conv_complete_evt; |
| 1635 | |
| 1636 | pr_debug("%s: called for %d\n", __func__, channel); |
| 1637 | ret = adc_channel_open(channel, &h); |
| 1638 | if (ret) { |
| 1639 | pr_err("%s: couldnt open channel %d ret=%d\n", |
| 1640 | __func__, channel, ret); |
| 1641 | goto out; |
| 1642 | } |
| 1643 | init_completion(&conv_complete_evt); |
| 1644 | ret = adc_channel_request_conv(h, &conv_complete_evt); |
| 1645 | if (ret) { |
| 1646 | pr_err("%s: couldnt request conv channel %d ret=%d\n", |
| 1647 | __func__, channel, ret); |
| 1648 | goto out; |
| 1649 | } |
| 1650 | wait_for_completion(&conv_complete_evt); |
| 1651 | ret = adc_channel_read_result(h, &adc_chan_result); |
| 1652 | if (ret) { |
| 1653 | pr_err("%s: couldnt read result channel %d ret=%d\n", |
| 1654 | __func__, channel, ret); |
| 1655 | goto out; |
| 1656 | } |
| 1657 | ret = adc_channel_close(h); |
| 1658 | if (ret) { |
| 1659 | pr_err("%s: couldnt close channel %d ret=%d\n", |
| 1660 | __func__, channel, ret); |
| 1661 | } |
| 1662 | if (mv_reading) |
| 1663 | *mv_reading = adc_chan_result.measurement; |
| 1664 | |
| 1665 | pr_debug("%s: done for %d\n", __func__, channel); |
| 1666 | return adc_chan_result.physical; |
| 1667 | out: |
| 1668 | pr_debug("%s: done for %d\n", __func__, channel); |
| 1669 | return -EINVAL; |
| 1670 | |
| 1671 | } |
| 1672 | |
| 1673 | #define BATT_THERM_OPEN_MV 2000 |
| 1674 | static int pm8058_is_battery_present(void) |
| 1675 | { |
| 1676 | int mv_reading; |
| 1677 | |
| 1678 | mv_reading = 0; |
| 1679 | batt_read_adc(CHANNEL_ADC_BATT_THERM, &mv_reading); |
| 1680 | pr_debug("%s: therm_raw is %d\n", __func__, mv_reading); |
| 1681 | if (mv_reading > 0 && mv_reading < BATT_THERM_OPEN_MV) |
| 1682 | return 1; |
| 1683 | |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
| 1687 | static int pm8058_get_battery_temperature(void) |
| 1688 | { |
| 1689 | return batt_read_adc(CHANNEL_ADC_BATT_THERM, NULL); |
| 1690 | } |
| 1691 | |
| 1692 | #define BATT_THERM_OPERATIONAL_MAX_CELCIUS 40 |
| 1693 | #define BATT_THERM_OPERATIONAL_MIN_CELCIUS 0 |
| 1694 | static int pm8058_is_battery_temp_within_range(void) |
| 1695 | { |
| 1696 | int therm_celcius; |
| 1697 | |
| 1698 | therm_celcius = pm8058_get_battery_temperature(); |
| 1699 | pr_debug("%s: therm_celcius is %d\n", __func__, therm_celcius); |
| 1700 | if (therm_celcius > 0 |
| 1701 | && therm_celcius > BATT_THERM_OPERATIONAL_MIN_CELCIUS |
| 1702 | && therm_celcius < BATT_THERM_OPERATIONAL_MAX_CELCIUS) |
| 1703 | return 1; |
| 1704 | |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | #define BATT_ID_MAX_MV 800 |
| 1709 | #define BATT_ID_MIN_MV 600 |
| 1710 | static int pm8058_is_battery_id_valid(void) |
| 1711 | { |
| 1712 | int batt_id_mv; |
| 1713 | |
| 1714 | batt_id_mv = batt_read_adc(CHANNEL_ADC_BATT_ID, NULL); |
| 1715 | pr_debug("%s: batt_id_mv is %d\n", __func__, batt_id_mv); |
| 1716 | |
| 1717 | /* |
| 1718 | * The readings are not in range |
| 1719 | * assume battery is present for now |
| 1720 | */ |
| 1721 | return 1; |
| 1722 | |
| 1723 | if (batt_id_mv > 0 |
| 1724 | && batt_id_mv > BATT_ID_MIN_MV |
| 1725 | && batt_id_mv < BATT_ID_MAX_MV) |
| 1726 | return 1; |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | /* returns voltage in mV */ |
| 1732 | static int pm8058_get_battery_mvolts(void) |
| 1733 | { |
| 1734 | int vbatt_mv; |
| 1735 | |
| 1736 | vbatt_mv = batt_read_adc(CHANNEL_ADC_VBATT, NULL); |
| 1737 | pr_debug("%s: vbatt_mv is %d\n", __func__, vbatt_mv); |
| 1738 | if (vbatt_mv > 0) |
| 1739 | return vbatt_mv; |
| 1740 | /* |
| 1741 | * return 0 to tell the upper layers |
| 1742 | * we couldnt read the battery voltage |
| 1743 | */ |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | static int msm_battery_gauge_alarm_notify(struct notifier_block *nb, |
| 1748 | unsigned long status, void *unused) |
| 1749 | { |
| 1750 | int rc; |
| 1751 | |
| 1752 | pr_info("%s: status: %lu\n", __func__, status); |
| 1753 | |
| 1754 | switch (status) { |
| 1755 | case 0: |
| 1756 | dev_err(pm8058_chg.dev, |
| 1757 | "%s: spurious interrupt\n", __func__); |
| 1758 | break; |
| 1759 | /* expected case - trip of low threshold */ |
| 1760 | case 1: |
| 1761 | rc = pm8058_batt_alarm_state_set(0, 0); |
| 1762 | if (rc) |
| 1763 | dev_err(pm8058_chg.dev, |
| 1764 | "%s: unable to set alarm state\n", __func__); |
| 1765 | msm_charger_notify_event(NULL, CHG_BATT_NEEDS_RECHARGING); |
| 1766 | break; |
| 1767 | case 2: |
| 1768 | dev_err(pm8058_chg.dev, |
| 1769 | "%s: trip of high threshold\n", __func__); |
| 1770 | break; |
| 1771 | default: |
| 1772 | dev_err(pm8058_chg.dev, |
| 1773 | "%s: error received\n", __func__); |
| 1774 | }; |
| 1775 | |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | static int pm8058_monitor_for_recharging(void) |
| 1780 | { |
| 1781 | /* enable low comparator */ |
| 1782 | return pm8058_batt_alarm_state_set(1, 0); |
| 1783 | } |
| 1784 | |
| 1785 | static struct msm_battery_gauge pm8058_batt_gauge = { |
| 1786 | .get_battery_mvolts = pm8058_get_battery_mvolts, |
| 1787 | .get_battery_temperature = pm8058_get_battery_temperature, |
| 1788 | .is_battery_present = pm8058_is_battery_present, |
| 1789 | .is_battery_temp_within_range = pm8058_is_battery_temp_within_range, |
| 1790 | .is_battery_id_valid = pm8058_is_battery_id_valid, |
| 1791 | .monitor_for_recharging = pm8058_monitor_for_recharging, |
| 1792 | }; |
| 1793 | |
| 1794 | static int pm8058_usb_voltage_lower_limit(void) |
| 1795 | { |
| 1796 | u8 temp, old; |
| 1797 | int ret = 0; |
| 1798 | |
| 1799 | temp = 0x10; |
| 1800 | ret |= pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST, &temp, 1); |
| 1801 | ret |= pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEST, &old, 1); |
| 1802 | old = old & ~BIT(IGNORE_LL); |
| 1803 | temp = 0x90 | (0xF & old); |
| 1804 | ret |= pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST, &temp, 1); |
| 1805 | |
| 1806 | return ret; |
| 1807 | } |
| 1808 | |
| 1809 | static int __devinit pm8058_charger_probe(struct platform_device *pdev) |
| 1810 | { |
| 1811 | struct pm8058_chip *pm_chip; |
| 1812 | int rc = 0; |
| 1813 | |
| 1814 | pm_chip = dev_get_drvdata(pdev->dev.parent); |
| 1815 | if (pm_chip == NULL) { |
| 1816 | pr_err("%s:no parent data passed in.\n", __func__); |
| 1817 | return -EFAULT; |
| 1818 | } |
| 1819 | |
| 1820 | pm8058_chg.pm_chip = pm_chip; |
| 1821 | pm8058_chg.pdata = pdev->dev.platform_data; |
| 1822 | pm8058_chg.dev = &pdev->dev; |
| 1823 | |
| 1824 | rc = request_irqs(pdev); |
| 1825 | if (rc) { |
| 1826 | pr_err("%s: couldnt register interrupts\n", __func__); |
| 1827 | goto out; |
| 1828 | } |
| 1829 | |
| 1830 | rc = pm8058_usb_voltage_lower_limit(); |
| 1831 | if (rc) { |
| 1832 | pr_err("%s: couldnt set ignore lower limit bit to 0\n", |
| 1833 | __func__); |
| 1834 | goto free_irq; |
| 1835 | } |
| 1836 | |
| 1837 | rc = msm_charger_register(&usb_hw_chg); |
| 1838 | if (rc) { |
| 1839 | pr_err("%s: msm_charger_register failed ret=%d\n", |
| 1840 | __func__, rc); |
| 1841 | goto free_irq; |
| 1842 | } |
| 1843 | |
| 1844 | pm_chg_batt_temp_disable(0); |
| 1845 | msm_battery_gauge_register(&pm8058_batt_gauge); |
| 1846 | __dump_chg_regs(); |
| 1847 | |
| 1848 | create_debugfs_entries(); |
| 1849 | INIT_DELAYED_WORK(&pm8058_chg.veoc_begin_work, veoc_begin_work); |
| 1850 | INIT_DELAYED_WORK(&pm8058_chg.check_vbat_low_work, vbat_low_work); |
| 1851 | INIT_DELAYED_WORK(&pm8058_chg.chg_done_check_work, chg_done_check_work); |
| 1852 | INIT_DELAYED_WORK(&pm8058_chg.charging_check_work, charging_check_work); |
| 1853 | |
| 1854 | /* determine what state the charger is in */ |
| 1855 | pm8058_chg_determine_initial_state(); |
| 1856 | |
| 1857 | pm8058_chg_enable_irq(BATTTEMP_IRQ); |
| 1858 | pm8058_chg_enable_irq(BATTCONNECT_IRQ); |
| 1859 | |
| 1860 | rc = pm8058_batt_alarm_state_set(0, 0); |
| 1861 | if (rc) { |
| 1862 | pr_err("%s: unable to set batt alarm state\n", __func__); |
| 1863 | goto free_irq; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * The batt-alarm driver requires sane values for both min / max, |
| 1868 | * regardless of whether they're both activated. |
| 1869 | */ |
| 1870 | rc = pm8058_batt_alarm_threshold_set(resume_mv, 4300); |
| 1871 | if (rc) { |
| 1872 | pr_err("%s: unable to set batt alarm threshold\n", __func__); |
| 1873 | goto free_irq; |
| 1874 | } |
| 1875 | |
| 1876 | rc = pm8058_batt_alarm_hold_time_set(PM8058_BATT_ALARM_HOLD_TIME_16_MS); |
| 1877 | if (rc) { |
| 1878 | pr_err("%s: unable to set batt alarm hold time\n", __func__); |
| 1879 | goto free_irq; |
| 1880 | } |
| 1881 | |
| 1882 | /* PWM enabled at 2Hz */ |
| 1883 | rc = pm8058_batt_alarm_pwm_rate_set(1, 7, 4); |
| 1884 | if (rc) { |
| 1885 | pr_err("%s: unable to set batt alarm pwm rate\n", __func__); |
| 1886 | goto free_irq; |
| 1887 | } |
| 1888 | |
| 1889 | rc = pm8058_batt_alarm_register_notifier(&alarm_notifier); |
| 1890 | if (rc) { |
| 1891 | pr_err("%s: unable to register alarm notifier\n", __func__); |
| 1892 | goto free_irq; |
| 1893 | } |
| 1894 | |
| 1895 | pm8058_chg.inited = 1; |
| 1896 | |
| 1897 | return 0; |
| 1898 | |
| 1899 | free_irq: |
| 1900 | free_irqs(); |
| 1901 | out: |
| 1902 | return rc; |
| 1903 | } |
| 1904 | |
| 1905 | static int __devexit pm8058_charger_remove(struct platform_device *pdev) |
| 1906 | { |
| 1907 | struct pm8058_charger_chip *chip = platform_get_drvdata(pdev); |
| 1908 | int rc; |
| 1909 | |
| 1910 | msm_charger_notify_event(&usb_hw_chg, CHG_REMOVED_EVENT); |
| 1911 | msm_charger_unregister(&usb_hw_chg); |
| 1912 | cancel_delayed_work_sync(&pm8058_chg.veoc_begin_work); |
| 1913 | cancel_delayed_work_sync(&pm8058_chg.check_vbat_low_work); |
| 1914 | cancel_delayed_work_sync(&pm8058_chg.charging_check_work); |
| 1915 | free_irqs(); |
| 1916 | remove_debugfs_entries(); |
| 1917 | kfree(chip); |
| 1918 | |
| 1919 | rc = pm8058_batt_alarm_state_set(0, 0); |
| 1920 | if (rc) |
| 1921 | pr_err("%s: unable to set batt alarm state\n", __func__); |
| 1922 | |
| 1923 | rc |= pm8058_batt_alarm_unregister_notifier(&alarm_notifier); |
| 1924 | if (rc) |
| 1925 | pr_err("%s: unable to register alarm notifier\n", __func__); |
| 1926 | return rc; |
| 1927 | } |
| 1928 | |
| 1929 | static struct platform_driver pm8058_charger_driver = { |
| 1930 | .probe = pm8058_charger_probe, |
| 1931 | .remove = __devexit_p(pm8058_charger_remove), |
| 1932 | .driver = { |
| 1933 | .name = "pm8058-charger", |
| 1934 | .owner = THIS_MODULE, |
| 1935 | }, |
| 1936 | }; |
| 1937 | |
| 1938 | static int __init pm8058_charger_init(void) |
| 1939 | { |
| 1940 | return platform_driver_register(&pm8058_charger_driver); |
| 1941 | } |
| 1942 | |
| 1943 | static void __exit pm8058_charger_exit(void) |
| 1944 | { |
| 1945 | platform_driver_unregister(&pm8058_charger_driver); |
| 1946 | } |
| 1947 | |
| 1948 | late_initcall(pm8058_charger_init); |
| 1949 | module_exit(pm8058_charger_exit); |
| 1950 | |
| 1951 | MODULE_LICENSE("GPL v2"); |
| 1952 | MODULE_DESCRIPTION("PMIC8058 BATTERY driver"); |
| 1953 | MODULE_VERSION("1.0"); |
| 1954 | MODULE_ALIAS("platform:pm8058_charger"); |