Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 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 | #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> |
| 19 | #include <linux/mfd/pm8xxx/pm8921-charger.h> |
| 20 | #include <linux/mfd/pm8xxx/pm8921-bms.h> |
| 21 | #include <linux/mfd/pm8921-adc.h> |
| 22 | #include <linux/mfd/pm8xxx/core.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/power_supply.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/slab.h> |
| 30 | |
| 31 | #include <mach/msm_xo.h> |
| 32 | #include <mach/msm_hsusb.h> |
| 33 | |
| 34 | #define CHG_BUCK_CLOCK_CTRL 0x14 |
| 35 | |
| 36 | #define PBL_ACCESS1 0x04 |
| 37 | #define PBL_ACCESS2 0x05 |
| 38 | #define SYS_CONFIG_1 0x06 |
| 39 | #define SYS_CONFIG_2 0x07 |
| 40 | #define CHG_CNTRL 0x204 |
| 41 | #define CHG_IBAT_MAX 0x205 |
| 42 | #define CHG_TEST 0x206 |
| 43 | #define CHG_BUCK_CTRL_TEST1 0x207 |
| 44 | #define CHG_BUCK_CTRL_TEST2 0x208 |
| 45 | #define CHG_BUCK_CTRL_TEST3 0x209 |
| 46 | #define COMPARATOR_OVERRIDE 0x20A |
| 47 | #define PSI_TXRX_SAMPLE_DATA_0 0x20B |
| 48 | #define PSI_TXRX_SAMPLE_DATA_1 0x20C |
| 49 | #define PSI_TXRX_SAMPLE_DATA_2 0x20D |
| 50 | #define PSI_TXRX_SAMPLE_DATA_3 0x20E |
| 51 | #define PSI_CONFIG_STATUS 0x20F |
| 52 | #define CHG_IBAT_SAFE 0x210 |
| 53 | #define CHG_ITRICKLE 0x211 |
| 54 | #define CHG_CNTRL_2 0x212 |
| 55 | #define CHG_VBAT_DET 0x213 |
| 56 | #define CHG_VTRICKLE 0x214 |
| 57 | #define CHG_ITERM 0x215 |
| 58 | #define CHG_CNTRL_3 0x216 |
| 59 | #define CHG_VIN_MIN 0x217 |
| 60 | #define CHG_TWDOG 0x218 |
| 61 | #define CHG_TTRKL_MAX 0x219 |
| 62 | #define CHG_TEMP_THRESH 0x21A |
| 63 | #define CHG_TCHG_MAX 0x21B |
| 64 | #define USB_OVP_CONTROL 0x21C |
| 65 | #define DC_OVP_CONTROL 0x21D |
| 66 | #define USB_OVP_TEST 0x21E |
| 67 | #define DC_OVP_TEST 0x21F |
| 68 | #define CHG_VDD_MAX 0x220 |
| 69 | #define CHG_VDD_SAFE 0x221 |
| 70 | #define CHG_VBAT_BOOT_THRESH 0x222 |
| 71 | #define USB_OVP_TRIM 0x355 |
| 72 | #define BUCK_CONTROL_TRIM1 0x356 |
| 73 | #define BUCK_CONTROL_TRIM2 0x357 |
| 74 | #define BUCK_CONTROL_TRIM3 0x358 |
| 75 | #define BUCK_CONTROL_TRIM4 0x359 |
| 76 | #define CHG_DEFAULTS_TRIM 0x35A |
| 77 | #define CHG_ITRIM 0x35B |
| 78 | #define CHG_TTRIM 0x35C |
| 79 | #define CHG_COMP_OVR 0x20A |
| 80 | |
| 81 | enum chg_fsm_state { |
| 82 | FSM_STATE_OFF_0 = 0, |
| 83 | FSM_STATE_BATFETDET_START_12 = 12, |
| 84 | FSM_STATE_BATFETDET_END_16 = 16, |
| 85 | FSM_STATE_ON_CHG_HIGHI_1 = 1, |
| 86 | FSM_STATE_ATC_2A = 2, |
| 87 | FSM_STATE_ATC_2B = 18, |
| 88 | FSM_STATE_ON_BAT_3 = 3, |
| 89 | FSM_STATE_ATC_FAIL_4 = 4 , |
| 90 | FSM_STATE_DELAY_5 = 5, |
| 91 | FSM_STATE_ON_CHG_AND_BAT_6 = 6, |
| 92 | FSM_STATE_FAST_CHG_7 = 7, |
| 93 | FSM_STATE_TRKL_CHG_8 = 8, |
| 94 | FSM_STATE_CHG_FAIL_9 = 9, |
| 95 | FSM_STATE_EOC_10 = 10, |
| 96 | FSM_STATE_ON_CHG_VREGOK_11 = 11, |
| 97 | FSM_STATE_ATC_PAUSE_13 = 13, |
| 98 | FSM_STATE_FAST_CHG_PAUSE_14 = 14, |
| 99 | FSM_STATE_TRKL_CHG_PAUSE_15 = 15, |
| 100 | FSM_STATE_START_BOOT = 20, |
| 101 | FSM_STATE_FLCB_VREGOK = 21, |
| 102 | FSM_STATE_FLCB = 22, |
| 103 | }; |
| 104 | |
| 105 | enum pmic_chg_interrupts { |
| 106 | USBIN_VALID_IRQ = 0, |
| 107 | USBIN_OV_IRQ, |
| 108 | BATT_INSERTED_IRQ, |
| 109 | VBATDET_LOW_IRQ, |
| 110 | USBIN_UV_IRQ, |
| 111 | VBAT_OV_IRQ, |
| 112 | CHGWDOG_IRQ, |
| 113 | VCP_IRQ, |
| 114 | ATCDONE_IRQ, |
| 115 | ATCFAIL_IRQ, |
| 116 | CHGDONE_IRQ, |
| 117 | CHGFAIL_IRQ, |
| 118 | CHGSTATE_IRQ, |
| 119 | LOOP_CHANGE_IRQ, |
| 120 | FASTCHG_IRQ, |
| 121 | TRKLCHG_IRQ, |
| 122 | BATT_REMOVED_IRQ, |
| 123 | BATTTEMP_HOT_IRQ, |
| 124 | CHGHOT_IRQ, |
| 125 | BATTTEMP_COLD_IRQ, |
| 126 | CHG_GONE_IRQ, |
| 127 | BAT_TEMP_OK_IRQ, |
| 128 | COARSE_DET_LOW_IRQ, |
| 129 | VDD_LOOP_IRQ, |
| 130 | VREG_OV_IRQ, |
| 131 | VBATDET_IRQ, |
| 132 | BATFET_IRQ, |
| 133 | PSI_IRQ, |
| 134 | DCIN_VALID_IRQ, |
| 135 | DCIN_OV_IRQ, |
| 136 | DCIN_UV_IRQ, |
| 137 | PM_CHG_MAX_INTS, |
| 138 | }; |
| 139 | |
| 140 | struct bms_notify { |
| 141 | int is_charging; |
| 142 | struct work_struct work; |
| 143 | }; |
| 144 | |
| 145 | /** |
| 146 | * struct pm8921_chg_chip -device information |
| 147 | * @dev: device pointer to access the parent |
| 148 | * @is_usb_path_used: indicates whether USB charging is used at all |
| 149 | * @is_usb_path_used: indicates whether DC charging is used at all |
| 150 | * @usb_present: present status of usb |
| 151 | * @dc_present: present status of dc |
| 152 | * @usb_charger_current: usb current to charge the battery with used when |
| 153 | * the usb path is enabled or charging is resumed |
| 154 | * @safety_time: max time for which charging will happen |
| 155 | * @update_time: how frequently the userland needs to be updated |
| 156 | * @max_voltage: the max volts the batt should be charged up to |
| 157 | * @min_voltage: the min battery voltage before turning the FETon |
| 158 | * @resume_voltage: the voltage at which the battery should resume |
| 159 | * charging |
| 160 | * @term_current: The charging based term current |
| 161 | * |
| 162 | */ |
| 163 | struct pm8921_chg_chip { |
| 164 | struct device *dev; |
| 165 | unsigned int usb_present; |
| 166 | unsigned int dc_present; |
| 167 | unsigned int usb_charger_current; |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 168 | unsigned int max_bat_chg_current; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 169 | unsigned int pmic_chg_irq[PM_CHG_MAX_INTS]; |
| 170 | unsigned int safety_time; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 171 | unsigned int ttrkl_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 172 | unsigned int update_time; |
| 173 | unsigned int max_voltage; |
| 174 | unsigned int min_voltage; |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 175 | unsigned int cool_temp; |
| 176 | unsigned int warm_temp; |
| 177 | unsigned int temp_check_period; |
| 178 | unsigned int cool_bat_chg_current; |
| 179 | unsigned int warm_bat_chg_current; |
| 180 | unsigned int cool_bat_voltage; |
| 181 | unsigned int warm_bat_voltage; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | unsigned int resume_voltage; |
| 183 | unsigned int term_current; |
| 184 | unsigned int vbat_channel; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 185 | unsigned int batt_temp_channel; |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 186 | unsigned int batt_id_channel; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 187 | struct power_supply usb_psy; |
| 188 | struct power_supply dc_psy; |
| 189 | struct power_supply batt_psy; |
| 190 | struct dentry *dent; |
| 191 | struct bms_notify bms_notify; |
| 192 | DECLARE_BITMAP(enabled_irqs, PM_CHG_MAX_INTS); |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 193 | struct work_struct battery_id_valid_work; |
| 194 | int64_t batt_id_min; |
| 195 | int64_t batt_id_max; |
Abhijeet Dharmapurikar | 26cef9c | 2011-08-25 19:16:42 -0700 | [diff] [blame] | 196 | int trkl_voltage; |
| 197 | int weak_voltage; |
| 198 | int trkl_current; |
| 199 | int weak_current; |
Abhijeet Dharmapurikar | 6fe50a8 | 2011-08-25 21:33:14 -0700 | [diff] [blame] | 200 | int vin_min; |
Abhijeet Dharmapurikar | ad74236 | 2011-08-29 19:50:02 -0700 | [diff] [blame^] | 201 | int *thermal_mitigation; |
| 202 | int thermal_levels; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static int charging_disabled; |
Abhijeet Dharmapurikar | ad74236 | 2011-08-29 19:50:02 -0700 | [diff] [blame^] | 206 | static int thermal_mitigation; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 207 | |
| 208 | static struct pm8921_chg_chip *the_chip; |
| 209 | |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 210 | static struct pm8921_adc_arb_btm_param btm_config; |
| 211 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 212 | static int pm_chg_masked_write(struct pm8921_chg_chip *chip, u16 addr, |
| 213 | u8 mask, u8 val) |
| 214 | { |
| 215 | int rc; |
| 216 | u8 reg; |
| 217 | |
| 218 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 219 | if (rc) { |
| 220 | pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n", addr, rc); |
| 221 | return rc; |
| 222 | } |
| 223 | reg &= ~mask; |
| 224 | reg |= val & mask; |
| 225 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 226 | if (rc) { |
| 227 | pr_err("pm8xxx_writeb failed: addr=%03X, rc=%d\n", addr, rc); |
| 228 | return rc; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | #define CAPTURE_FSM_STATE_CMD 0xC2 |
| 234 | #define READ_BANK_7 0x70 |
| 235 | #define READ_BANK_4 0x40 |
| 236 | static int pm_chg_get_fsm_state(struct pm8921_chg_chip *chip) |
| 237 | { |
| 238 | u8 temp; |
| 239 | int err, ret = 0; |
| 240 | |
| 241 | temp = CAPTURE_FSM_STATE_CMD; |
| 242 | err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp); |
| 243 | if (err) { |
| 244 | pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST); |
| 245 | return err; |
| 246 | } |
| 247 | |
| 248 | temp = READ_BANK_7; |
| 249 | err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp); |
| 250 | if (err) { |
| 251 | pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST); |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp); |
| 256 | if (err) { |
| 257 | pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err); |
| 258 | return err; |
| 259 | } |
| 260 | /* get the lower 4 bits */ |
| 261 | ret = temp & 0xF; |
| 262 | |
| 263 | temp = READ_BANK_4; |
| 264 | err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp); |
| 265 | if (err) { |
| 266 | pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST); |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp); |
| 271 | if (err) { |
| 272 | pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err); |
| 273 | return err; |
| 274 | } |
| 275 | /* get the upper 1 bit */ |
| 276 | ret |= (temp & 0x1) << 4; |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | #define CHG_USB_SUSPEND_BIT BIT(2) |
| 281 | static int pm_chg_usb_suspend_enable(struct pm8921_chg_chip *chip, int enable) |
| 282 | { |
| 283 | return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_USB_SUSPEND_BIT, |
| 284 | enable ? CHG_USB_SUSPEND_BIT : 0); |
| 285 | } |
| 286 | |
| 287 | #define CHG_EN_BIT BIT(7) |
| 288 | static int pm_chg_auto_enable(struct pm8921_chg_chip *chip, int enable) |
| 289 | { |
| 290 | return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_EN_BIT, |
| 291 | enable ? CHG_EN_BIT : 0); |
| 292 | } |
| 293 | |
| 294 | #define CHG_CHARGE_DIS_BIT BIT(1) |
| 295 | static int pm_chg_charge_dis(struct pm8921_chg_chip *chip, int disable) |
| 296 | { |
| 297 | return pm_chg_masked_write(chip, CHG_CNTRL, CHG_CHARGE_DIS_BIT, |
| 298 | disable ? CHG_CHARGE_DIS_BIT : 0); |
| 299 | } |
| 300 | |
| 301 | #define PM8921_CHG_V_MIN_MV 3240 |
| 302 | #define PM8921_CHG_V_STEP_MV 20 |
| 303 | #define PM8921_CHG_VDDMAX_MAX 4500 |
| 304 | #define PM8921_CHG_VDDMAX_MIN 3400 |
| 305 | #define PM8921_CHG_V_MASK 0x7F |
| 306 | static int pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage) |
| 307 | { |
| 308 | u8 temp; |
| 309 | |
| 310 | if (voltage < PM8921_CHG_VDDMAX_MIN |
| 311 | || voltage > PM8921_CHG_VDDMAX_MAX) { |
| 312 | pr_err("bad mV=%d asked to set\n", voltage); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV; |
| 316 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 317 | return pm_chg_masked_write(chip, CHG_VDD_MAX, PM8921_CHG_V_MASK, temp); |
| 318 | } |
| 319 | |
| 320 | #define PM8921_CHG_VDDSAFE_MIN 3400 |
| 321 | #define PM8921_CHG_VDDSAFE_MAX 4500 |
| 322 | static int pm_chg_vddsafe_set(struct pm8921_chg_chip *chip, int voltage) |
| 323 | { |
| 324 | u8 temp; |
| 325 | |
| 326 | if (voltage < PM8921_CHG_VDDSAFE_MIN |
| 327 | || voltage > PM8921_CHG_VDDSAFE_MAX) { |
| 328 | pr_err("bad mV=%d asked to set\n", voltage); |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV; |
| 332 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 333 | return pm_chg_masked_write(chip, CHG_VDD_SAFE, PM8921_CHG_V_MASK, temp); |
| 334 | } |
| 335 | |
| 336 | #define PM8921_CHG_VBATDET_MIN 3240 |
| 337 | #define PM8921_CHG_VBATDET_MAX 5780 |
| 338 | static int pm_chg_vbatdet_set(struct pm8921_chg_chip *chip, int voltage) |
| 339 | { |
| 340 | u8 temp; |
| 341 | |
| 342 | if (voltage < PM8921_CHG_VBATDET_MIN |
| 343 | || voltage > PM8921_CHG_VBATDET_MAX) { |
| 344 | pr_err("bad mV=%d asked to set\n", voltage); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV; |
| 348 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 349 | return pm_chg_masked_write(chip, CHG_VBAT_DET, PM8921_CHG_V_MASK, temp); |
| 350 | } |
| 351 | |
Abhijeet Dharmapurikar | 6fe50a8 | 2011-08-25 21:33:14 -0700 | [diff] [blame] | 352 | #define PM8921_CHG_VINMIN_MIN_MV 3800 |
| 353 | #define PM8921_CHG_VINMIN_STEP_MV 100 |
| 354 | #define PM8921_CHG_VINMIN_USABLE_MAX 6500 |
| 355 | #define PM8921_CHG_VINMIN_USABLE_MIN 4300 |
| 356 | #define PM8921_CHG_VINMIN_MASK 0x1F |
| 357 | static int pm_chg_vinmin_set(struct pm8921_chg_chip *chip, int voltage) |
| 358 | { |
| 359 | u8 temp; |
| 360 | |
| 361 | if (voltage < PM8921_CHG_VINMIN_USABLE_MIN |
| 362 | || voltage > PM8921_CHG_VINMIN_USABLE_MAX) { |
| 363 | pr_err("bad mV=%d asked to set\n", voltage); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | temp = (voltage - PM8921_CHG_VINMIN_MIN_MV) / PM8921_CHG_VINMIN_STEP_MV; |
| 367 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 368 | return pm_chg_masked_write(chip, CHG_VIN_MIN, PM8921_CHG_VINMIN_MASK, |
| 369 | temp); |
| 370 | } |
| 371 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | #define PM8921_CHG_IBATMAX_MIN 325 |
| 373 | #define PM8921_CHG_IBATMAX_MAX 2000 |
| 374 | #define PM8921_CHG_I_MIN_MA 225 |
| 375 | #define PM8921_CHG_I_STEP_MA 50 |
| 376 | #define PM8921_CHG_I_MASK 0x3F |
| 377 | static int pm_chg_ibatmax_set(struct pm8921_chg_chip *chip, int chg_current) |
| 378 | { |
| 379 | u8 temp; |
| 380 | |
| 381 | if (chg_current < PM8921_CHG_IBATMAX_MIN |
| 382 | || chg_current > PM8921_CHG_IBATMAX_MAX) { |
| 383 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA; |
| 387 | return pm_chg_masked_write(chip, CHG_IBAT_MAX, PM8921_CHG_I_MASK, temp); |
| 388 | } |
| 389 | |
| 390 | #define PM8921_CHG_IBATSAFE_MIN 225 |
| 391 | #define PM8921_CHG_IBATSAFE_MAX 3375 |
| 392 | static int pm_chg_ibatsafe_set(struct pm8921_chg_chip *chip, int chg_current) |
| 393 | { |
| 394 | u8 temp; |
| 395 | |
| 396 | if (chg_current < PM8921_CHG_IBATSAFE_MIN |
| 397 | || chg_current > PM8921_CHG_IBATSAFE_MAX) { |
| 398 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA; |
| 402 | return pm_chg_masked_write(chip, CHG_IBAT_SAFE, |
| 403 | PM8921_CHG_I_MASK, temp); |
| 404 | } |
| 405 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 406 | #define PM8921_CHG_ITERM_MIN_MA 50 |
Abhijeet Dharmapurikar | 71816fd | 2011-08-12 17:14:10 -0700 | [diff] [blame] | 407 | #define PM8921_CHG_ITERM_MAX_MA 200 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | #define PM8921_CHG_ITERM_STEP_MA 10 |
| 409 | #define PM8921_CHG_ITERM_MASK 0xF |
| 410 | static int pm_chg_iterm_set(struct pm8921_chg_chip *chip, int chg_current) |
| 411 | { |
| 412 | u8 temp; |
| 413 | |
Abhijeet Dharmapurikar | 71816fd | 2011-08-12 17:14:10 -0700 | [diff] [blame] | 414 | if (chg_current < PM8921_CHG_ITERM_MIN_MA |
| 415 | || chg_current > PM8921_CHG_ITERM_MAX_MA) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 416 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | temp = (chg_current - PM8921_CHG_ITERM_MIN_MA) |
| 421 | / PM8921_CHG_ITERM_STEP_MA; |
Abhijeet Dharmapurikar | 71816fd | 2011-08-12 17:14:10 -0700 | [diff] [blame] | 422 | return pm_chg_masked_write(chip, CHG_ITERM, PM8921_CHG_ITERM_MASK, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | temp); |
| 424 | } |
| 425 | |
| 426 | #define PM8921_CHG_IUSB_MASK 0x1C |
| 427 | #define PM8921_CHG_IUSB_MAX 7 |
| 428 | #define PM8921_CHG_IUSB_MIN 0 |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 429 | static int pm_chg_iusbmax_set(struct pm8921_chg_chip *chip, int reg_val) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 430 | { |
| 431 | u8 temp; |
| 432 | |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 433 | if (reg_val < PM8921_CHG_IUSB_MIN || reg_val > PM8921_CHG_IUSB_MAX) { |
| 434 | pr_err("bad mA=%d asked to set\n", reg_val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 435 | return -EINVAL; |
| 436 | } |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 437 | temp = reg_val << 2; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 438 | return pm_chg_masked_write(chip, PBL_ACCESS2, PM8921_CHG_IUSB_MASK, |
| 439 | temp); |
| 440 | } |
| 441 | |
| 442 | #define PM8921_CHG_WD_MASK 0x1F |
| 443 | static int pm_chg_disable_wd(struct pm8921_chg_chip *chip) |
| 444 | { |
| 445 | /* writing 0 to the wd timer disables it */ |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 446 | return pm_chg_masked_write(chip, CHG_TWDOG, PM8921_CHG_WD_MASK, 0); |
| 447 | } |
| 448 | |
| 449 | #define PM8921_CHG_TCHG_MASK 0x3F |
| 450 | #define PM8921_CHG_TCHG_MIN 4 |
| 451 | #define PM8921_CHG_TCHG_MAX 512 |
| 452 | #define PM8921_CHG_TCHG_STEP 4 |
| 453 | static int pm_chg_tchg_max_set(struct pm8921_chg_chip *chip, int minutes) |
| 454 | { |
| 455 | u8 temp; |
| 456 | |
| 457 | if (minutes < PM8921_CHG_TCHG_MIN || minutes > PM8921_CHG_TCHG_MAX) { |
| 458 | pr_err("bad max minutes =%d asked to set\n", minutes); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
| 462 | temp = (minutes - 1)/PM8921_CHG_TCHG_STEP; |
| 463 | return pm_chg_masked_write(chip, CHG_TCHG_MAX, PM8921_CHG_TCHG_MASK, |
| 464 | temp); |
| 465 | } |
| 466 | |
| 467 | #define PM8921_CHG_TTRKL_MASK 0x1F |
| 468 | #define PM8921_CHG_TTRKL_MIN 1 |
| 469 | #define PM8921_CHG_TTRKL_MAX 64 |
| 470 | static int pm_chg_ttrkl_max_set(struct pm8921_chg_chip *chip, int minutes) |
| 471 | { |
| 472 | u8 temp; |
| 473 | |
| 474 | if (minutes < PM8921_CHG_TTRKL_MIN || minutes > PM8921_CHG_TTRKL_MAX) { |
| 475 | pr_err("bad max minutes =%d asked to set\n", minutes); |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | |
| 479 | temp = minutes - 1; |
| 480 | return pm_chg_masked_write(chip, CHG_TTRKL_MAX, PM8921_CHG_TTRKL_MASK, |
| 481 | temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Abhijeet Dharmapurikar | 26cef9c | 2011-08-25 19:16:42 -0700 | [diff] [blame] | 484 | #define PM8921_CHG_VTRKL_MIN_MV 2050 |
| 485 | #define PM8921_CHG_VTRKL_MAX_MV 2800 |
| 486 | #define PM8921_CHG_VTRKL_STEP_MV 50 |
| 487 | #define PM8921_CHG_VTRKL_SHIFT 4 |
| 488 | #define PM8921_CHG_VTRKL_MASK 0xF0 |
| 489 | static int pm_chg_vtrkl_low_set(struct pm8921_chg_chip *chip, int millivolts) |
| 490 | { |
| 491 | u8 temp; |
| 492 | |
| 493 | if (millivolts < PM8921_CHG_VTRKL_MIN_MV |
| 494 | || millivolts > PM8921_CHG_VTRKL_MAX_MV) { |
| 495 | pr_err("bad voltage = %dmV asked to set\n", millivolts); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | temp = (millivolts - PM8921_CHG_VTRKL_MIN_MV)/PM8921_CHG_VTRKL_STEP_MV; |
| 500 | temp = temp << PM8921_CHG_VTRKL_SHIFT; |
| 501 | return pm_chg_masked_write(chip, CHG_VTRICKLE, PM8921_CHG_VTRKL_MASK, |
| 502 | temp); |
| 503 | } |
| 504 | |
| 505 | #define PM8921_CHG_VWEAK_MIN_MV 2100 |
| 506 | #define PM8921_CHG_VWEAK_MAX_MV 3600 |
| 507 | #define PM8921_CHG_VWEAK_STEP_MV 100 |
| 508 | #define PM8921_CHG_VWEAK_MASK 0x0F |
| 509 | static int pm_chg_vweak_set(struct pm8921_chg_chip *chip, int millivolts) |
| 510 | { |
| 511 | u8 temp; |
| 512 | |
| 513 | if (millivolts < PM8921_CHG_VWEAK_MIN_MV |
| 514 | || millivolts > PM8921_CHG_VWEAK_MAX_MV) { |
| 515 | pr_err("bad voltage = %dmV asked to set\n", millivolts); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | temp = (millivolts - PM8921_CHG_VWEAK_MIN_MV)/PM8921_CHG_VWEAK_STEP_MV; |
| 520 | return pm_chg_masked_write(chip, CHG_VTRICKLE, PM8921_CHG_VWEAK_MASK, |
| 521 | temp); |
| 522 | } |
| 523 | |
| 524 | #define PM8921_CHG_ITRKL_MIN_MA 50 |
| 525 | #define PM8921_CHG_ITRKL_MAX_MA 200 |
| 526 | #define PM8921_CHG_ITRKL_MASK 0x0F |
| 527 | #define PM8921_CHG_ITRKL_STEP_MA 10 |
| 528 | static int pm_chg_itrkl_set(struct pm8921_chg_chip *chip, int milliamps) |
| 529 | { |
| 530 | u8 temp; |
| 531 | |
| 532 | if (milliamps < PM8921_CHG_ITRKL_MIN_MA |
| 533 | || milliamps > PM8921_CHG_ITRKL_MAX_MA) { |
| 534 | pr_err("bad current = %dmA asked to set\n", milliamps); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
| 538 | temp = (milliamps - PM8921_CHG_ITRKL_MIN_MA)/PM8921_CHG_ITRKL_STEP_MA; |
| 539 | |
| 540 | return pm_chg_masked_write(chip, CHG_ITRICKLE, PM8921_CHG_ITRKL_MASK, |
| 541 | temp); |
| 542 | } |
| 543 | |
| 544 | #define PM8921_CHG_IWEAK_MIN_MA 325 |
| 545 | #define PM8921_CHG_IWEAK_MAX_MA 525 |
| 546 | #define PM8921_CHG_IWEAK_SHIFT 7 |
| 547 | #define PM8921_CHG_IWEAK_MASK 0x80 |
| 548 | static int pm_chg_iweak_set(struct pm8921_chg_chip *chip, int milliamps) |
| 549 | { |
| 550 | u8 temp; |
| 551 | |
| 552 | if (milliamps < PM8921_CHG_IWEAK_MIN_MA |
| 553 | || milliamps > PM8921_CHG_IWEAK_MAX_MA) { |
| 554 | pr_err("bad current = %dmA asked to set\n", milliamps); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
| 558 | if (milliamps < PM8921_CHG_IWEAK_MAX_MA) |
| 559 | temp = 0; |
| 560 | else |
| 561 | temp = 1; |
| 562 | |
| 563 | temp = temp << PM8921_CHG_IWEAK_SHIFT; |
| 564 | return pm_chg_masked_write(chip, CHG_ITRICKLE, PM8921_CHG_IWEAK_MASK, |
| 565 | temp); |
| 566 | } |
| 567 | |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 568 | static int64_t read_battery_id(struct pm8921_chg_chip *chip) |
| 569 | { |
| 570 | int rc; |
| 571 | struct pm8921_adc_chan_result result; |
| 572 | |
| 573 | rc = pm8921_adc_read(chip->batt_id_channel, &result); |
| 574 | if (rc) { |
| 575 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 576 | chip->vbat_channel, rc); |
| 577 | return rc; |
| 578 | } |
| 579 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 580 | result.measurement); |
| 581 | return result.physical; |
| 582 | } |
| 583 | |
| 584 | static int is_battery_valid(struct pm8921_chg_chip *chip) |
| 585 | { |
| 586 | int64_t rc; |
| 587 | |
| 588 | if (chip->batt_id_min == 0 && chip->batt_id_max == 0) |
| 589 | return 1; |
| 590 | |
| 591 | rc = read_battery_id(chip); |
| 592 | if (rc < 0) { |
| 593 | pr_err("error reading batt id channel = %d, rc = %lld\n", |
| 594 | chip->vbat_channel, rc); |
| 595 | /* assume battery id is valid when adc error happens */ |
| 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | if (rc < chip->batt_id_min || rc > chip->batt_id_max) { |
| 600 | pr_err("batt_id phy =%lld is not valid\n", rc); |
| 601 | return 0; |
| 602 | } |
| 603 | return 1; |
| 604 | } |
| 605 | |
| 606 | static void check_battery_valid(struct pm8921_chg_chip *chip) |
| 607 | { |
| 608 | if (is_battery_valid(chip) == 0) { |
| 609 | pr_err("batt_id not valid, disbling charging\n"); |
| 610 | pm_chg_auto_enable(chip, 0); |
| 611 | } else { |
| 612 | pm_chg_auto_enable(chip, !charging_disabled); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | static void battery_id_valid(struct work_struct *work) |
| 617 | { |
| 618 | struct pm8921_chg_chip *chip = container_of(work, |
| 619 | struct pm8921_chg_chip, battery_id_valid_work); |
| 620 | |
| 621 | check_battery_valid(chip); |
| 622 | } |
| 623 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | static void pm8921_chg_enable_irq(struct pm8921_chg_chip *chip, int interrupt) |
| 625 | { |
| 626 | if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) { |
| 627 | dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]); |
| 628 | enable_irq(chip->pmic_chg_irq[interrupt]); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static void pm8921_chg_disable_irq(struct pm8921_chg_chip *chip, int interrupt) |
| 633 | { |
| 634 | if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) { |
| 635 | dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]); |
| 636 | disable_irq_nosync(chip->pmic_chg_irq[interrupt]); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static int pm_chg_get_rt_status(struct pm8921_chg_chip *chip, int irq_id) |
| 641 | { |
| 642 | return pm8xxx_read_irq_stat(chip->dev->parent, |
| 643 | chip->pmic_chg_irq[irq_id]); |
| 644 | } |
| 645 | |
| 646 | /* Treat OverVoltage/UnderVoltage as source missing */ |
| 647 | static int is_usb_chg_plugged_in(struct pm8921_chg_chip *chip) |
| 648 | { |
| 649 | int pres, ov, uv; |
| 650 | |
| 651 | pres = pm_chg_get_rt_status(chip, USBIN_VALID_IRQ); |
| 652 | ov = pm_chg_get_rt_status(chip, USBIN_OV_IRQ); |
| 653 | uv = pm_chg_get_rt_status(chip, USBIN_UV_IRQ); |
| 654 | |
| 655 | return pres && !ov && !uv; |
| 656 | } |
| 657 | |
| 658 | /* Treat OverVoltage/UnderVoltage as source missing */ |
| 659 | static int is_dc_chg_plugged_in(struct pm8921_chg_chip *chip) |
| 660 | { |
| 661 | int pres, ov, uv; |
| 662 | |
| 663 | pres = pm_chg_get_rt_status(chip, DCIN_VALID_IRQ); |
| 664 | ov = pm_chg_get_rt_status(chip, DCIN_OV_IRQ); |
| 665 | uv = pm_chg_get_rt_status(chip, DCIN_UV_IRQ); |
| 666 | |
| 667 | return pres && !ov && !uv; |
| 668 | } |
| 669 | |
| 670 | static int is_battery_charging(int fsm_state) |
| 671 | { |
| 672 | switch (fsm_state) { |
| 673 | case FSM_STATE_ATC_2A: |
| 674 | case FSM_STATE_ATC_2B: |
| 675 | case FSM_STATE_ON_CHG_AND_BAT_6: |
| 676 | case FSM_STATE_FAST_CHG_7: |
| 677 | case FSM_STATE_TRKL_CHG_8: |
| 678 | return 1; |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static void bms_notify(struct work_struct *work) |
| 684 | { |
| 685 | struct bms_notify *n = container_of(work, struct bms_notify, work); |
| 686 | |
| 687 | if (n->is_charging) |
| 688 | pm8921_bms_charging_began(); |
| 689 | else |
| 690 | pm8921_bms_charging_end(); |
| 691 | } |
| 692 | |
| 693 | static enum power_supply_property pm_power_props[] = { |
| 694 | POWER_SUPPLY_PROP_PRESENT, |
Willie Ruan | 9ad6d83 | 2011-08-19 11:58:42 -0700 | [diff] [blame] | 695 | POWER_SUPPLY_PROP_ONLINE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | static char *pm_power_supplied_to[] = { |
| 699 | "battery", |
| 700 | }; |
| 701 | |
| 702 | static int pm_power_get_property(struct power_supply *psy, |
| 703 | enum power_supply_property psp, |
| 704 | union power_supply_propval *val) |
| 705 | { |
| 706 | struct pm8921_chg_chip *chip; |
| 707 | |
| 708 | switch (psp) { |
| 709 | case POWER_SUPPLY_PROP_PRESENT: |
Willie Ruan | 9ad6d83 | 2011-08-19 11:58:42 -0700 | [diff] [blame] | 710 | case POWER_SUPPLY_PROP_ONLINE: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 711 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) { |
| 712 | chip = container_of(psy, struct pm8921_chg_chip, |
| 713 | dc_psy); |
| 714 | val->intval = is_dc_chg_plugged_in(chip); |
| 715 | } |
| 716 | if (psy->type == POWER_SUPPLY_TYPE_USB) { |
| 717 | chip = container_of(psy, struct pm8921_chg_chip, |
| 718 | usb_psy); |
| 719 | val->intval = is_usb_chg_plugged_in(chip); |
| 720 | } |
| 721 | break; |
| 722 | default: |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static enum power_supply_property msm_batt_power_props[] = { |
| 729 | POWER_SUPPLY_PROP_STATUS, |
| 730 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 731 | POWER_SUPPLY_PROP_HEALTH, |
| 732 | POWER_SUPPLY_PROP_PRESENT, |
| 733 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 734 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 735 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 736 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 737 | POWER_SUPPLY_PROP_CAPACITY, |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 738 | POWER_SUPPLY_PROP_CURRENT_NOW, |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 739 | POWER_SUPPLY_PROP_TEMP, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 740 | }; |
| 741 | |
| 742 | static int get_prop_battery_mvolts(struct pm8921_chg_chip *chip) |
| 743 | { |
| 744 | int rc; |
| 745 | struct pm8921_adc_chan_result result; |
| 746 | |
| 747 | rc = pm8921_adc_read(chip->vbat_channel, &result); |
| 748 | if (rc) { |
| 749 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 750 | chip->vbat_channel, rc); |
| 751 | return rc; |
| 752 | } |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 753 | pr_debug("mvolts phy = %lld meas = 0x%llx\n", result.physical, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 754 | result.measurement); |
| 755 | return (int)result.physical; |
| 756 | } |
| 757 | |
| 758 | static int get_prop_batt_capacity(struct pm8921_chg_chip *chip) |
| 759 | { |
Abhijeet Dharmapurikar | 5a7df4e | 2011-07-27 13:16:24 -0700 | [diff] [blame] | 760 | int percent_soc = pm8921_bms_get_percent_charge(); |
| 761 | |
| 762 | if (percent_soc <= 10) |
| 763 | pr_warn("low battery charge = %d%%\n", percent_soc); |
| 764 | |
| 765 | return percent_soc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 768 | static int get_prop_batt_current(struct pm8921_chg_chip *chip) |
| 769 | { |
| 770 | int result_ma, rc; |
| 771 | |
| 772 | rc = pm8921_bms_get_battery_current(&result_ma); |
| 773 | if (rc) { |
| 774 | pr_err("unable to get batt current rc = %d\n", rc); |
| 775 | return rc; |
| 776 | } else { |
| 777 | return result_ma; |
| 778 | } |
| 779 | } |
| 780 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 781 | static int get_prop_batt_health(struct pm8921_chg_chip *chip) |
| 782 | { |
| 783 | int temp; |
| 784 | |
| 785 | temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ); |
| 786 | if (temp) |
| 787 | return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 788 | |
| 789 | temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ); |
| 790 | if (temp) |
| 791 | return POWER_SUPPLY_HEALTH_COLD; |
| 792 | |
| 793 | return POWER_SUPPLY_HEALTH_GOOD; |
| 794 | } |
| 795 | |
| 796 | static int get_prop_batt_present(struct pm8921_chg_chip *chip) |
| 797 | { |
| 798 | return pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ); |
| 799 | } |
| 800 | |
| 801 | static int get_prop_charge_type(struct pm8921_chg_chip *chip) |
| 802 | { |
| 803 | int temp; |
| 804 | |
| 805 | temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ); |
| 806 | if (temp) |
| 807 | return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 808 | |
| 809 | temp = pm_chg_get_rt_status(chip, FASTCHG_IRQ); |
| 810 | if (temp) |
| 811 | return POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 812 | |
| 813 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 814 | } |
| 815 | |
| 816 | static int get_prop_batt_status(struct pm8921_chg_chip *chip) |
| 817 | { |
| 818 | int temp = 0; |
| 819 | |
| 820 | /* TODO reading the FSM state is more reliable */ |
| 821 | temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ); |
| 822 | |
| 823 | temp |= pm_chg_get_rt_status(chip, FASTCHG_IRQ); |
| 824 | if (temp) |
| 825 | return POWER_SUPPLY_STATUS_CHARGING; |
| 826 | /* |
| 827 | * The battery is not charging |
| 828 | * check the FET - if on battery is discharging |
| 829 | * - if off battery is isolated(full) and the system |
| 830 | * is being driven from a charger |
| 831 | */ |
| 832 | temp = pm_chg_get_rt_status(chip, BATFET_IRQ); |
| 833 | if (temp) |
| 834 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 835 | |
| 836 | return POWER_SUPPLY_STATUS_FULL; |
| 837 | } |
| 838 | |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 839 | static int get_prop_batt_temp(struct pm8921_chg_chip *chip) |
| 840 | { |
| 841 | int rc; |
| 842 | struct pm8921_adc_chan_result result; |
| 843 | |
| 844 | rc = pm8921_adc_read(chip->batt_temp_channel, &result); |
| 845 | if (rc) { |
| 846 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 847 | chip->vbat_channel, rc); |
| 848 | return rc; |
| 849 | } |
| 850 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 851 | result.measurement); |
| 852 | return (int)result.physical; |
| 853 | } |
| 854 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 855 | static int pm_batt_power_get_property(struct power_supply *psy, |
| 856 | enum power_supply_property psp, |
| 857 | union power_supply_propval *val) |
| 858 | { |
| 859 | struct pm8921_chg_chip *chip = container_of(psy, struct pm8921_chg_chip, |
| 860 | batt_psy); |
| 861 | |
| 862 | switch (psp) { |
| 863 | case POWER_SUPPLY_PROP_STATUS: |
| 864 | val->intval = get_prop_batt_status(chip); |
| 865 | break; |
| 866 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 867 | val->intval = get_prop_charge_type(chip); |
| 868 | break; |
| 869 | case POWER_SUPPLY_PROP_HEALTH: |
| 870 | val->intval = get_prop_batt_health(chip); |
| 871 | break; |
| 872 | case POWER_SUPPLY_PROP_PRESENT: |
| 873 | val->intval = get_prop_batt_present(chip); |
| 874 | break; |
| 875 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 876 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 877 | break; |
| 878 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 879 | val->intval = chip->max_voltage; |
| 880 | break; |
| 881 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 882 | val->intval = chip->min_voltage; |
| 883 | break; |
| 884 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 885 | val->intval = get_prop_battery_mvolts(chip); |
| 886 | break; |
| 887 | case POWER_SUPPLY_PROP_CAPACITY: |
| 888 | val->intval = get_prop_batt_capacity(chip); |
| 889 | break; |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 890 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 891 | val->intval = get_prop_batt_current(chip); |
| 892 | break; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 893 | case POWER_SUPPLY_PROP_TEMP: |
| 894 | val->intval = get_prop_batt_temp(chip); |
| 895 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 896 | default: |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | static void (*notify_vbus_state_func_ptr)(int); |
| 904 | static int usb_chg_current; |
| 905 | static DEFINE_SPINLOCK(vbus_lock); |
| 906 | |
| 907 | int pm8921_charger_register_vbus_sn(void (*callback)(int)) |
| 908 | { |
| 909 | pr_debug("%p\n", callback); |
| 910 | notify_vbus_state_func_ptr = callback; |
| 911 | return 0; |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(pm8921_charger_register_vbus_sn); |
| 914 | |
| 915 | /* this is passed to the hsusb via platform_data msm_otg_pdata */ |
| 916 | void pm8921_charger_unregister_vbus_sn(void (*callback)(int)) |
| 917 | { |
| 918 | pr_debug("%p\n", callback); |
| 919 | notify_vbus_state_func_ptr = NULL; |
| 920 | } |
| 921 | EXPORT_SYMBOL_GPL(pm8921_charger_unregister_vbus_sn); |
| 922 | |
| 923 | static void notify_usb_of_the_plugin_event(int plugin) |
| 924 | { |
| 925 | plugin = !!plugin; |
| 926 | if (notify_vbus_state_func_ptr) { |
| 927 | pr_debug("notifying plugin\n"); |
| 928 | (*notify_vbus_state_func_ptr) (plugin); |
| 929 | } else { |
| 930 | pr_debug("unable to notify plugin\n"); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | struct usb_ma_limit_entry { |
| 935 | int usb_ma; |
| 936 | u8 chg_iusb_value; |
| 937 | }; |
| 938 | |
| 939 | static struct usb_ma_limit_entry usb_ma_table[] = { |
| 940 | {100, 0}, |
| 941 | {500, 1}, |
| 942 | {700, 2}, |
| 943 | {850, 3}, |
| 944 | {900, 4}, |
| 945 | {1100, 5}, |
| 946 | {1300, 6}, |
| 947 | {1500, 7}, |
| 948 | }; |
| 949 | |
| 950 | /* assumes vbus_lock is held */ |
| 951 | static void __pm8921_charger_vbus_draw(unsigned int mA) |
| 952 | { |
| 953 | int i, rc; |
| 954 | |
| 955 | if (mA > 0 && mA <= 2) { |
| 956 | usb_chg_current = 0; |
| 957 | rc = pm_chg_iusbmax_set(the_chip, |
| 958 | usb_ma_table[0].chg_iusb_value); |
| 959 | if (rc) { |
| 960 | pr_err("unable to set iusb to %d rc = %d\n", |
| 961 | usb_ma_table[0].chg_iusb_value, rc); |
| 962 | } |
| 963 | rc = pm_chg_usb_suspend_enable(the_chip, 1); |
| 964 | if (rc) |
| 965 | pr_err("fail to set suspend bit rc=%d\n", rc); |
| 966 | } else { |
| 967 | rc = pm_chg_usb_suspend_enable(the_chip, 0); |
| 968 | if (rc) |
| 969 | pr_err("fail to reset suspend bit rc=%d\n", rc); |
| 970 | for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) { |
| 971 | if (usb_ma_table[i].usb_ma <= mA) |
| 972 | break; |
| 973 | } |
| 974 | if (i < 0) |
| 975 | i = 0; |
| 976 | rc = pm_chg_iusbmax_set(the_chip, |
| 977 | usb_ma_table[i].chg_iusb_value); |
| 978 | if (rc) { |
| 979 | pr_err("unable to set iusb to %d rc = %d\n", |
| 980 | usb_ma_table[i].chg_iusb_value, rc); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /* USB calls these to tell us how much max usb current the system can draw */ |
| 986 | void pm8921_charger_vbus_draw(unsigned int mA) |
| 987 | { |
| 988 | unsigned long flags; |
| 989 | |
| 990 | pr_debug("Enter charge=%d\n", mA); |
| 991 | spin_lock_irqsave(&vbus_lock, flags); |
| 992 | if (the_chip) { |
| 993 | __pm8921_charger_vbus_draw(mA); |
| 994 | } else { |
| 995 | /* |
| 996 | * called before pmic initialized, |
| 997 | * save this value and use it at probe |
| 998 | */ |
| 999 | usb_chg_current = mA; |
| 1000 | } |
| 1001 | spin_unlock_irqrestore(&vbus_lock, flags); |
| 1002 | } |
| 1003 | EXPORT_SYMBOL_GPL(pm8921_charger_vbus_draw); |
| 1004 | |
Abhijeet Dharmapurikar | 1f52c41 | 2011-08-12 17:11:45 -0700 | [diff] [blame] | 1005 | int pm8921_charger_enable(bool enable) |
| 1006 | { |
| 1007 | int rc; |
| 1008 | |
| 1009 | if (!the_chip) { |
| 1010 | pr_err("called before init\n"); |
| 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | enable = !!enable; |
| 1014 | rc = pm_chg_auto_enable(the_chip, enable); |
| 1015 | if (rc) |
| 1016 | pr_err("Failed rc=%d\n", rc); |
| 1017 | return rc; |
| 1018 | } |
| 1019 | EXPORT_SYMBOL(pm8921_charger_enable); |
| 1020 | |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1021 | int pm8921_is_usb_chg_plugged_in(void) |
| 1022 | { |
| 1023 | if (!the_chip) { |
| 1024 | pr_err("called before init\n"); |
| 1025 | return -EINVAL; |
| 1026 | } |
| 1027 | return is_usb_chg_plugged_in(the_chip); |
| 1028 | } |
| 1029 | EXPORT_SYMBOL(pm8921_is_usb_chg_plugged_in); |
| 1030 | |
| 1031 | int pm8921_is_dc_chg_plugged_in(void) |
| 1032 | { |
| 1033 | if (!the_chip) { |
| 1034 | pr_err("called before init\n"); |
| 1035 | return -EINVAL; |
| 1036 | } |
| 1037 | return is_dc_chg_plugged_in(the_chip); |
| 1038 | } |
| 1039 | EXPORT_SYMBOL(pm8921_is_dc_chg_plugged_in); |
| 1040 | |
| 1041 | int pm8921_is_battery_present(void) |
| 1042 | { |
| 1043 | if (!the_chip) { |
| 1044 | pr_err("called before init\n"); |
| 1045 | return -EINVAL; |
| 1046 | } |
| 1047 | return get_prop_batt_present(the_chip); |
| 1048 | } |
| 1049 | EXPORT_SYMBOL(pm8921_is_battery_present); |
| 1050 | |
| 1051 | int pm8921_set_max_battery_charge_current(int ma) |
| 1052 | { |
| 1053 | if (!the_chip) { |
| 1054 | pr_err("called before init\n"); |
| 1055 | return -EINVAL; |
| 1056 | } |
| 1057 | return pm_chg_ibatmax_set(the_chip, ma); |
| 1058 | } |
| 1059 | EXPORT_SYMBOL(pm8921_set_max_battery_charge_current); |
| 1060 | |
| 1061 | int pm8921_disable_source_current(bool disable) |
| 1062 | { |
| 1063 | if (!the_chip) { |
| 1064 | pr_err("called before init\n"); |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | if (disable) |
| 1068 | pr_warn("current drawn from chg=0, battery provides current\n"); |
| 1069 | return pm_chg_charge_dis(the_chip, disable); |
| 1070 | } |
| 1071 | EXPORT_SYMBOL(pm8921_disable_source_current); |
| 1072 | |
Abhijeet Dharmapurikar | 6fe50a8 | 2011-08-25 21:33:14 -0700 | [diff] [blame] | 1073 | int pm8921_regulate_input_voltage(int voltage) |
| 1074 | { |
| 1075 | if (!the_chip) { |
| 1076 | pr_err("called before init\n"); |
| 1077 | return -EINVAL; |
| 1078 | } |
| 1079 | return pm_chg_vinmin_set(the_chip, voltage); |
| 1080 | } |
| 1081 | |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1082 | bool pm8921_is_battery_charging(int *source) |
| 1083 | { |
| 1084 | int fsm_state, is_charging, dc_present, usb_present; |
| 1085 | |
| 1086 | if (!the_chip) { |
| 1087 | pr_err("called before init\n"); |
| 1088 | return -EINVAL; |
| 1089 | } |
| 1090 | fsm_state = pm_chg_get_fsm_state(the_chip); |
| 1091 | is_charging = is_battery_charging(fsm_state); |
| 1092 | if (is_charging == 0) { |
| 1093 | *source = PM8921_CHG_SRC_NONE; |
| 1094 | return is_charging; |
| 1095 | } |
| 1096 | |
| 1097 | if (source == NULL) |
| 1098 | return is_charging; |
| 1099 | |
| 1100 | /* the battery is charging, the source is requested, find it */ |
| 1101 | dc_present = is_dc_chg_plugged_in(the_chip); |
| 1102 | usb_present = is_usb_chg_plugged_in(the_chip); |
| 1103 | |
| 1104 | if (dc_present && !usb_present) |
| 1105 | *source = PM8921_CHG_SRC_DC; |
| 1106 | |
| 1107 | if (usb_present && !dc_present) |
| 1108 | *source = PM8921_CHG_SRC_USB; |
| 1109 | |
| 1110 | if (usb_present && dc_present) |
| 1111 | /* |
| 1112 | * The system always chooses dc for charging since it has |
| 1113 | * higher priority. |
| 1114 | */ |
| 1115 | *source = PM8921_CHG_SRC_DC; |
| 1116 | |
| 1117 | return is_charging; |
| 1118 | } |
| 1119 | EXPORT_SYMBOL(pm8921_is_battery_charging); |
| 1120 | |
| 1121 | int pm8921_batt_temperature(void) |
| 1122 | { |
| 1123 | if (!the_chip) { |
| 1124 | pr_err("called before init\n"); |
| 1125 | return -EINVAL; |
| 1126 | } |
| 1127 | return get_prop_batt_temp(the_chip); |
| 1128 | } |
| 1129 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1130 | static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip) |
| 1131 | { |
| 1132 | int usb_present; |
| 1133 | |
| 1134 | usb_present = is_usb_chg_plugged_in(chip); |
| 1135 | if (chip->usb_present ^ usb_present) { |
| 1136 | notify_usb_of_the_plugin_event(usb_present); |
| 1137 | chip->usb_present = usb_present; |
| 1138 | power_supply_changed(&chip->usb_psy); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | static void handle_dc_removal_insertion(struct pm8921_chg_chip *chip) |
| 1143 | { |
| 1144 | int dc_present; |
| 1145 | |
| 1146 | dc_present = is_dc_chg_plugged_in(chip); |
| 1147 | if (chip->dc_present ^ dc_present) { |
| 1148 | chip->dc_present = dc_present; |
| 1149 | power_supply_changed(&chip->dc_psy); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | static irqreturn_t usbin_valid_irq_handler(int irq, void *data) |
| 1154 | { |
| 1155 | handle_usb_insertion_removal(data); |
| 1156 | return IRQ_HANDLED; |
| 1157 | } |
| 1158 | |
| 1159 | static irqreturn_t usbin_ov_irq_handler(int irq, void *data) |
| 1160 | { |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 1161 | pr_err("USB OverVoltage\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1162 | handle_usb_insertion_removal(data); |
| 1163 | return IRQ_HANDLED; |
| 1164 | } |
| 1165 | |
| 1166 | static irqreturn_t batt_inserted_irq_handler(int irq, void *data) |
| 1167 | { |
| 1168 | struct pm8921_chg_chip *chip = data; |
| 1169 | int status; |
| 1170 | |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 1171 | status = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ); |
| 1172 | schedule_work(&chip->battery_id_valid_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1173 | pr_debug("battery present=%d", status); |
| 1174 | power_supply_changed(&chip->batt_psy); |
| 1175 | return IRQ_HANDLED; |
| 1176 | } |
| 1177 | /* this interrupt used to restart charging a battery */ |
| 1178 | static irqreturn_t vbatdet_low_irq_handler(int irq, void *data) |
| 1179 | { |
| 1180 | struct pm8921_chg_chip *chip = data; |
| 1181 | |
| 1182 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1183 | power_supply_changed(&chip->batt_psy); |
| 1184 | power_supply_changed(&chip->usb_psy); |
| 1185 | power_supply_changed(&chip->dc_psy); |
| 1186 | return IRQ_HANDLED; |
| 1187 | } |
| 1188 | |
| 1189 | static irqreturn_t usbin_uv_irq_handler(int irq, void *data) |
| 1190 | { |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 1191 | pr_err("USB UnderVoltage\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1192 | handle_usb_insertion_removal(data); |
| 1193 | return IRQ_HANDLED; |
| 1194 | } |
| 1195 | |
| 1196 | static irqreturn_t vbat_ov_irq_handler(int irq, void *data) |
| 1197 | { |
| 1198 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1199 | return IRQ_HANDLED; |
| 1200 | } |
| 1201 | |
| 1202 | static irqreturn_t chgwdog_irq_handler(int irq, void *data) |
| 1203 | { |
| 1204 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1205 | return IRQ_HANDLED; |
| 1206 | } |
| 1207 | |
| 1208 | static irqreturn_t vcp_irq_handler(int irq, void *data) |
| 1209 | { |
| 1210 | pr_warning("VCP triggered BATDET forced on\n"); |
| 1211 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1212 | return IRQ_HANDLED; |
| 1213 | } |
| 1214 | |
| 1215 | static irqreturn_t atcdone_irq_handler(int irq, void *data) |
| 1216 | { |
| 1217 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1218 | return IRQ_HANDLED; |
| 1219 | } |
| 1220 | |
| 1221 | static irqreturn_t atcfail_irq_handler(int irq, void *data) |
| 1222 | { |
| 1223 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1224 | return IRQ_HANDLED; |
| 1225 | } |
| 1226 | |
| 1227 | static irqreturn_t chgdone_irq_handler(int irq, void *data) |
| 1228 | { |
| 1229 | struct pm8921_chg_chip *chip = data; |
| 1230 | |
| 1231 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1232 | power_supply_changed(&chip->batt_psy); |
| 1233 | power_supply_changed(&chip->usb_psy); |
| 1234 | power_supply_changed(&chip->dc_psy); |
| 1235 | return IRQ_HANDLED; |
| 1236 | } |
| 1237 | |
| 1238 | static irqreturn_t chgfail_irq_handler(int irq, void *data) |
| 1239 | { |
| 1240 | struct pm8921_chg_chip *chip = data; |
| 1241 | |
| 1242 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1243 | power_supply_changed(&chip->batt_psy); |
| 1244 | power_supply_changed(&chip->usb_psy); |
| 1245 | power_supply_changed(&chip->dc_psy); |
| 1246 | return IRQ_HANDLED; |
| 1247 | } |
| 1248 | |
| 1249 | static irqreturn_t chgstate_irq_handler(int irq, void *data) |
| 1250 | { |
| 1251 | struct pm8921_chg_chip *chip = data; |
| 1252 | int new_is_charging = 0, fsm_state; |
| 1253 | |
| 1254 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1255 | power_supply_changed(&chip->batt_psy); |
| 1256 | power_supply_changed(&chip->usb_psy); |
| 1257 | power_supply_changed(&chip->dc_psy); |
| 1258 | |
| 1259 | fsm_state = pm_chg_get_fsm_state(chip); |
| 1260 | new_is_charging = is_battery_charging(fsm_state); |
| 1261 | |
| 1262 | if (chip->bms_notify.is_charging ^ new_is_charging) { |
| 1263 | chip->bms_notify.is_charging = new_is_charging; |
| 1264 | schedule_work(&(chip->bms_notify.work)); |
| 1265 | } |
| 1266 | return IRQ_HANDLED; |
| 1267 | } |
| 1268 | |
| 1269 | static irqreturn_t loop_change_irq_handler(int irq, void *data) |
| 1270 | { |
| 1271 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1272 | return IRQ_HANDLED; |
| 1273 | } |
| 1274 | |
| 1275 | static irqreturn_t fastchg_irq_handler(int irq, void *data) |
| 1276 | { |
| 1277 | struct pm8921_chg_chip *chip = data; |
| 1278 | |
| 1279 | power_supply_changed(&chip->batt_psy); |
| 1280 | return IRQ_HANDLED; |
| 1281 | } |
| 1282 | |
| 1283 | static irqreturn_t trklchg_irq_handler(int irq, void *data) |
| 1284 | { |
| 1285 | struct pm8921_chg_chip *chip = data; |
| 1286 | |
| 1287 | power_supply_changed(&chip->batt_psy); |
| 1288 | return IRQ_HANDLED; |
| 1289 | } |
| 1290 | |
| 1291 | static irqreturn_t batt_removed_irq_handler(int irq, void *data) |
| 1292 | { |
| 1293 | struct pm8921_chg_chip *chip = data; |
| 1294 | int status; |
| 1295 | |
| 1296 | status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ); |
| 1297 | pr_debug("battery present=%d state=%d", !status, |
| 1298 | pm_chg_get_fsm_state(data)); |
| 1299 | power_supply_changed(&chip->batt_psy); |
| 1300 | return IRQ_HANDLED; |
| 1301 | } |
| 1302 | |
| 1303 | static irqreturn_t batttemp_hot_irq_handler(int irq, void *data) |
| 1304 | { |
| 1305 | struct pm8921_chg_chip *chip = data; |
| 1306 | |
| 1307 | power_supply_changed(&chip->batt_psy); |
| 1308 | return IRQ_HANDLED; |
| 1309 | } |
| 1310 | |
| 1311 | static irqreturn_t chghot_irq_handler(int irq, void *data) |
| 1312 | { |
| 1313 | struct pm8921_chg_chip *chip = data; |
| 1314 | |
| 1315 | pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1316 | power_supply_changed(&chip->batt_psy); |
| 1317 | power_supply_changed(&chip->usb_psy); |
| 1318 | power_supply_changed(&chip->dc_psy); |
| 1319 | return IRQ_HANDLED; |
| 1320 | } |
| 1321 | |
| 1322 | static irqreturn_t batttemp_cold_irq_handler(int irq, void *data) |
| 1323 | { |
| 1324 | struct pm8921_chg_chip *chip = data; |
| 1325 | |
| 1326 | pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1327 | power_supply_changed(&chip->batt_psy); |
| 1328 | power_supply_changed(&chip->usb_psy); |
| 1329 | power_supply_changed(&chip->dc_psy); |
| 1330 | return IRQ_HANDLED; |
| 1331 | } |
| 1332 | |
| 1333 | static irqreturn_t chg_gone_irq_handler(int irq, void *data) |
| 1334 | { |
| 1335 | struct pm8921_chg_chip *chip = data; |
| 1336 | |
| 1337 | pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1338 | power_supply_changed(&chip->batt_psy); |
| 1339 | power_supply_changed(&chip->usb_psy); |
| 1340 | power_supply_changed(&chip->dc_psy); |
| 1341 | return IRQ_HANDLED; |
| 1342 | } |
| 1343 | |
| 1344 | static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data) |
| 1345 | { |
| 1346 | struct pm8921_chg_chip *chip = data; |
| 1347 | |
| 1348 | pr_debug("batt temp ok fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1349 | power_supply_changed(&chip->batt_psy); |
| 1350 | power_supply_changed(&chip->usb_psy); |
| 1351 | power_supply_changed(&chip->dc_psy); |
| 1352 | return IRQ_HANDLED; |
| 1353 | } |
| 1354 | |
| 1355 | static irqreturn_t coarse_det_low_irq_handler(int irq, void *data) |
| 1356 | { |
| 1357 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1358 | return IRQ_HANDLED; |
| 1359 | } |
| 1360 | |
| 1361 | static irqreturn_t vdd_loop_irq_handler(int irq, void *data) |
| 1362 | { |
| 1363 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1364 | return IRQ_HANDLED; |
| 1365 | } |
| 1366 | |
| 1367 | static irqreturn_t vreg_ov_irq_handler(int irq, void *data) |
| 1368 | { |
| 1369 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1370 | return IRQ_HANDLED; |
| 1371 | } |
| 1372 | |
| 1373 | static irqreturn_t vbatdet_irq_handler(int irq, void *data) |
| 1374 | { |
| 1375 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1376 | return IRQ_HANDLED; |
| 1377 | } |
| 1378 | |
| 1379 | static irqreturn_t batfet_irq_handler(int irq, void *data) |
| 1380 | { |
| 1381 | struct pm8921_chg_chip *chip = data; |
| 1382 | |
| 1383 | pr_debug("vreg ov\n"); |
| 1384 | power_supply_changed(&chip->batt_psy); |
| 1385 | return IRQ_HANDLED; |
| 1386 | } |
| 1387 | |
| 1388 | static irqreturn_t dcin_valid_irq_handler(int irq, void *data) |
| 1389 | { |
| 1390 | handle_dc_removal_insertion(data); |
| 1391 | return IRQ_HANDLED; |
| 1392 | } |
| 1393 | |
| 1394 | static irqreturn_t dcin_ov_irq_handler(int irq, void *data) |
| 1395 | { |
| 1396 | handle_dc_removal_insertion(data); |
| 1397 | return IRQ_HANDLED; |
| 1398 | } |
| 1399 | |
| 1400 | static irqreturn_t dcin_uv_irq_handler(int irq, void *data) |
| 1401 | { |
| 1402 | handle_dc_removal_insertion(data); |
| 1403 | return IRQ_HANDLED; |
| 1404 | } |
| 1405 | |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 1406 | static void btm_configure_work(struct work_struct *work) |
| 1407 | { |
| 1408 | int rc; |
| 1409 | |
| 1410 | rc = pm8921_adc_btm_configure(&btm_config); |
| 1411 | if (rc) |
| 1412 | pr_err("failed to configure btm rc=%d", rc); |
| 1413 | } |
| 1414 | |
| 1415 | DECLARE_WORK(btm_config_work, btm_configure_work); |
| 1416 | |
| 1417 | #define TEMP_HYSTERISIS_DEGC 2 |
| 1418 | static void battery_cool(bool enter) |
| 1419 | { |
| 1420 | pr_debug("enter = %d\n", enter); |
| 1421 | if (enter) { |
| 1422 | btm_config.low_thr_temp = |
| 1423 | the_chip->cool_temp + TEMP_HYSTERISIS_DEGC; |
| 1424 | pm_chg_ibatmax_set(the_chip, the_chip->cool_bat_chg_current); |
| 1425 | pm_chg_vddmax_set(the_chip, the_chip->cool_bat_voltage); |
| 1426 | } else { |
| 1427 | btm_config.low_thr_temp = the_chip->cool_temp; |
| 1428 | pm_chg_ibatmax_set(the_chip, the_chip->max_bat_chg_current); |
| 1429 | pm_chg_vddmax_set(the_chip, the_chip->max_voltage); |
| 1430 | } |
| 1431 | schedule_work(&btm_config_work); |
| 1432 | } |
| 1433 | |
| 1434 | static void battery_warm(bool enter) |
| 1435 | { |
| 1436 | pr_debug("enter = %d\n", enter); |
| 1437 | if (enter) { |
| 1438 | btm_config.high_thr_temp = |
| 1439 | the_chip->warm_temp - TEMP_HYSTERISIS_DEGC; |
| 1440 | pm_chg_ibatmax_set(the_chip, the_chip->warm_bat_chg_current); |
| 1441 | pm_chg_vddmax_set(the_chip, the_chip->warm_bat_voltage); |
| 1442 | } else { |
| 1443 | btm_config.high_thr_temp = the_chip->warm_temp; |
| 1444 | pm_chg_ibatmax_set(the_chip, the_chip->max_bat_chg_current); |
| 1445 | pm_chg_vddmax_set(the_chip, the_chip->max_voltage); |
| 1446 | } |
| 1447 | schedule_work(&btm_config_work); |
| 1448 | } |
| 1449 | |
| 1450 | static int configure_btm(struct pm8921_chg_chip *chip) |
| 1451 | { |
| 1452 | int rc; |
| 1453 | |
| 1454 | btm_config.btm_warm_fn = battery_warm; |
| 1455 | btm_config.btm_cool_fn = battery_cool; |
| 1456 | btm_config.low_thr_temp = chip->cool_temp; |
| 1457 | btm_config.high_thr_temp = chip->warm_temp; |
| 1458 | btm_config.interval = chip->temp_check_period; |
| 1459 | rc = pm8921_adc_btm_configure(&btm_config); |
| 1460 | if (rc) |
| 1461 | pr_err("failed to configure btm rc = %d\n", rc); |
| 1462 | rc = pm8921_adc_btm_start(); |
| 1463 | if (rc) |
| 1464 | pr_err("failed to start btm rc = %d\n", rc); |
| 1465 | |
| 1466 | return rc; |
| 1467 | } |
| 1468 | |
Abhijeet Dharmapurikar | 1f52c41 | 2011-08-12 17:11:45 -0700 | [diff] [blame] | 1469 | /** |
| 1470 | * set_disable_status_param - |
| 1471 | * |
| 1472 | * Internal function to disable battery charging and also disable drawing |
| 1473 | * any current from the source. The device is forced to run on a battery |
| 1474 | * after this. |
| 1475 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1476 | static int set_disable_status_param(const char *val, struct kernel_param *kp) |
| 1477 | { |
| 1478 | int ret; |
| 1479 | struct pm8921_chg_chip *chip = the_chip; |
| 1480 | |
| 1481 | ret = param_set_int(val, kp); |
| 1482 | if (ret) { |
| 1483 | pr_err("error setting value %d\n", ret); |
| 1484 | return ret; |
| 1485 | } |
| 1486 | pr_info("factory set disable param to %d\n", charging_disabled); |
| 1487 | if (chip) { |
| 1488 | pm_chg_auto_enable(chip, !charging_disabled); |
| 1489 | pm_chg_charge_dis(chip, charging_disabled); |
| 1490 | } |
| 1491 | return 0; |
| 1492 | } |
| 1493 | module_param_call(disabled, set_disable_status_param, param_get_uint, |
| 1494 | &charging_disabled, 0644); |
| 1495 | |
Abhijeet Dharmapurikar | ad74236 | 2011-08-29 19:50:02 -0700 | [diff] [blame^] | 1496 | /** |
| 1497 | * set_thermal_mitigation_level - |
| 1498 | * |
| 1499 | * Internal function to control battery charging current to reduce |
| 1500 | * temperature |
| 1501 | */ |
| 1502 | static int set_therm_mitigation_level(const char *val, struct kernel_param *kp) |
| 1503 | { |
| 1504 | int ret; |
| 1505 | struct pm8921_chg_chip *chip = the_chip; |
| 1506 | |
| 1507 | ret = param_set_int(val, kp); |
| 1508 | if (ret) { |
| 1509 | pr_err("error setting value %d\n", ret); |
| 1510 | return ret; |
| 1511 | } |
| 1512 | |
| 1513 | if (!chip) { |
| 1514 | pr_err("called before init\n"); |
| 1515 | return -EINVAL; |
| 1516 | } |
| 1517 | |
| 1518 | if (!chip->thermal_mitigation) { |
| 1519 | pr_err("no thermal mitigation\n"); |
| 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | |
| 1523 | if (thermal_mitigation < 0 |
| 1524 | || thermal_mitigation >= chip->thermal_levels) { |
| 1525 | pr_err("out of bound level selected\n"); |
| 1526 | return -EINVAL; |
| 1527 | } |
| 1528 | |
| 1529 | ret = pm_chg_ibatmax_set(chip, |
| 1530 | chip->thermal_mitigation[thermal_mitigation]); |
| 1531 | return ret; |
| 1532 | } |
| 1533 | module_param_call(thermal_mitigation, set_therm_mitigation_level, |
| 1534 | param_get_uint, |
| 1535 | &thermal_mitigation, 0644); |
| 1536 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1537 | static void free_irqs(struct pm8921_chg_chip *chip) |
| 1538 | { |
| 1539 | int i; |
| 1540 | |
| 1541 | for (i = 0; i < PM_CHG_MAX_INTS; i++) |
| 1542 | if (chip->pmic_chg_irq[i]) { |
| 1543 | free_irq(chip->pmic_chg_irq[i], chip); |
| 1544 | chip->pmic_chg_irq[i] = 0; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | /* determines the initial present states and notifies msm_charger */ |
| 1549 | static void __devinit determine_initial_state(struct pm8921_chg_chip *chip) |
| 1550 | { |
| 1551 | unsigned long flags; |
| 1552 | int fsm_state; |
| 1553 | |
| 1554 | chip->dc_present = !!is_dc_chg_plugged_in(chip); |
| 1555 | chip->usb_present = !!is_usb_chg_plugged_in(chip); |
| 1556 | |
| 1557 | notify_usb_of_the_plugin_event(chip->usb_present); |
| 1558 | |
| 1559 | pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ); |
| 1560 | pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ); |
| 1561 | pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ); |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1562 | pm8921_chg_enable_irq(chip, BATT_INSERTED_IRQ); |
| 1563 | pm8921_chg_enable_irq(chip, USBIN_OV_IRQ); |
| 1564 | pm8921_chg_enable_irq(chip, USBIN_UV_IRQ); |
| 1565 | pm8921_chg_enable_irq(chip, DCIN_OV_IRQ); |
| 1566 | pm8921_chg_enable_irq(chip, DCIN_UV_IRQ); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1567 | pm8921_chg_enable_irq(chip, CHGSTATE_IRQ); |
| 1568 | |
| 1569 | spin_lock_irqsave(&vbus_lock, flags); |
| 1570 | if (usb_chg_current) { |
| 1571 | /* reissue a vbus draw call */ |
| 1572 | __pm8921_charger_vbus_draw(usb_chg_current); |
| 1573 | } |
| 1574 | spin_unlock_irqrestore(&vbus_lock, flags); |
| 1575 | |
| 1576 | fsm_state = pm_chg_get_fsm_state(chip); |
| 1577 | if (is_battery_charging(fsm_state)) { |
| 1578 | chip->bms_notify.is_charging = 1; |
| 1579 | pm8921_bms_charging_began(); |
| 1580 | } |
| 1581 | |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 1582 | check_battery_valid(chip); |
| 1583 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1584 | pr_debug("usb = %d, dc = %d batt = %d state=%d\n", |
| 1585 | chip->usb_present, |
| 1586 | chip->dc_present, |
| 1587 | get_prop_batt_present(chip), |
| 1588 | fsm_state); |
| 1589 | } |
| 1590 | |
| 1591 | struct pm_chg_irq_init_data { |
| 1592 | unsigned int irq_id; |
| 1593 | char *name; |
| 1594 | unsigned long flags; |
| 1595 | irqreturn_t (*handler)(int, void *); |
| 1596 | }; |
| 1597 | |
| 1598 | #define CHG_IRQ(_id, _flags, _handler) \ |
| 1599 | { \ |
| 1600 | .irq_id = _id, \ |
| 1601 | .name = #_id, \ |
| 1602 | .flags = _flags, \ |
| 1603 | .handler = _handler, \ |
| 1604 | } |
| 1605 | struct pm_chg_irq_init_data chg_irq_data[] = { |
| 1606 | CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1607 | usbin_valid_irq_handler), |
| 1608 | CHG_IRQ(USBIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1609 | usbin_ov_irq_handler), |
| 1610 | CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1611 | batt_inserted_irq_handler), |
| 1612 | CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_RISING, vbatdet_low_irq_handler), |
| 1613 | CHG_IRQ(USBIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1614 | usbin_uv_irq_handler), |
| 1615 | CHG_IRQ(VBAT_OV_IRQ, IRQF_TRIGGER_RISING, vbat_ov_irq_handler), |
| 1616 | CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler), |
| 1617 | CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler), |
| 1618 | CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler), |
| 1619 | CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler), |
| 1620 | CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler), |
| 1621 | CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler), |
| 1622 | CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler), |
| 1623 | CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler), |
| 1624 | CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_RISING, fastchg_irq_handler), |
| 1625 | CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler), |
| 1626 | CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING, |
| 1627 | batt_removed_irq_handler), |
| 1628 | CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING, |
| 1629 | batttemp_hot_irq_handler), |
| 1630 | CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler), |
| 1631 | CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING, |
| 1632 | batttemp_cold_irq_handler), |
| 1633 | CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING, chg_gone_irq_handler), |
| 1634 | CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING, bat_temp_ok_irq_handler), |
| 1635 | CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING, |
| 1636 | coarse_det_low_irq_handler), |
| 1637 | CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler), |
| 1638 | CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler), |
| 1639 | CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler), |
| 1640 | CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1641 | batfet_irq_handler), |
| 1642 | CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1643 | dcin_valid_irq_handler), |
| 1644 | CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1645 | dcin_ov_irq_handler), |
| 1646 | CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1647 | dcin_uv_irq_handler), |
| 1648 | }; |
| 1649 | |
| 1650 | static int __devinit request_irqs(struct pm8921_chg_chip *chip, |
| 1651 | struct platform_device *pdev) |
| 1652 | { |
| 1653 | struct resource *res; |
| 1654 | int ret, i; |
| 1655 | |
| 1656 | ret = 0; |
| 1657 | bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS); |
| 1658 | |
| 1659 | for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) { |
| 1660 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1661 | chg_irq_data[i].name); |
| 1662 | if (res == NULL) { |
| 1663 | pr_err("couldn't find %s\n", chg_irq_data[i].name); |
| 1664 | goto err_out; |
| 1665 | } |
| 1666 | ret = request_irq(res->start, chg_irq_data[i].handler, |
| 1667 | chg_irq_data[i].flags, |
| 1668 | chg_irq_data[i].name, chip); |
| 1669 | if (ret < 0) { |
| 1670 | pr_err("couldn't request %d (%s) %d\n", res->start, |
| 1671 | chg_irq_data[i].name, ret); |
| 1672 | goto err_out; |
| 1673 | } |
| 1674 | chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start; |
| 1675 | pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id); |
| 1676 | } |
| 1677 | return 0; |
| 1678 | |
| 1679 | err_out: |
| 1680 | free_irqs(chip); |
| 1681 | return -EINVAL; |
| 1682 | } |
| 1683 | |
| 1684 | #define ENUM_TIMER_STOP_BIT BIT(1) |
| 1685 | #define BOOT_DONE_BIT BIT(6) |
| 1686 | #define CHG_BATFET_ON_BIT BIT(3) |
| 1687 | #define CHG_VCP_EN BIT(0) |
| 1688 | #define CHG_BAT_TEMP_DIS_BIT BIT(2) |
| 1689 | #define SAFE_CURRENT_MA 1500 |
| 1690 | static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip) |
| 1691 | { |
| 1692 | int rc; |
| 1693 | |
| 1694 | rc = pm_chg_masked_write(chip, SYS_CONFIG_2, |
| 1695 | BOOT_DONE_BIT, BOOT_DONE_BIT); |
| 1696 | if (rc) { |
| 1697 | pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc); |
| 1698 | return rc; |
| 1699 | } |
| 1700 | |
| 1701 | rc = pm_chg_vddsafe_set(chip, chip->max_voltage); |
| 1702 | if (rc) { |
| 1703 | pr_err("Failed to set safe voltage to %d rc=%d\n", |
| 1704 | chip->max_voltage, rc); |
| 1705 | return rc; |
| 1706 | } |
| 1707 | rc = pm_chg_vbatdet_set(chip, chip->resume_voltage); |
| 1708 | if (rc) { |
| 1709 | pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n", |
| 1710 | chip->resume_voltage, rc); |
| 1711 | return rc; |
| 1712 | } |
| 1713 | |
| 1714 | rc = pm_chg_vddmax_set(chip, chip->max_voltage); |
| 1715 | if (rc) { |
| 1716 | pr_err("Failed to set max voltage to %d rc=%d\n", |
| 1717 | chip->max_voltage, rc); |
| 1718 | return rc; |
| 1719 | } |
| 1720 | rc = pm_chg_ibatsafe_set(chip, SAFE_CURRENT_MA); |
| 1721 | if (rc) { |
| 1722 | pr_err("Failed to set max voltage to %d rc=%d\n", |
| 1723 | SAFE_CURRENT_MA, rc); |
| 1724 | return rc; |
| 1725 | } |
| 1726 | |
| 1727 | /* TODO needs to be changed as per the temeperature of the battery */ |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 1728 | rc = pm_chg_ibatmax_set(chip, chip->max_bat_chg_current); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1729 | if (rc) { |
| 1730 | pr_err("Failed to set max current to 400 rc=%d\n", rc); |
| 1731 | return rc; |
| 1732 | } |
| 1733 | |
| 1734 | rc = pm_chg_iterm_set(chip, chip->term_current); |
| 1735 | if (rc) { |
| 1736 | pr_err("Failed to set term current to %d rc=%d\n", |
| 1737 | chip->term_current, rc); |
| 1738 | return rc; |
| 1739 | } |
| 1740 | |
| 1741 | /* Disable the ENUM TIMER */ |
| 1742 | rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT, |
| 1743 | ENUM_TIMER_STOP_BIT); |
| 1744 | if (rc) { |
| 1745 | pr_err("Failed to set enum timer stop rc=%d\n", rc); |
| 1746 | return rc; |
| 1747 | } |
| 1748 | |
| 1749 | /* init with the lowest USB current */ |
| 1750 | rc = pm_chg_iusbmax_set(chip, usb_ma_table[0].chg_iusb_value); |
| 1751 | if (rc) { |
| 1752 | pr_err("Failed to set usb max to %d rc=%d\n", |
| 1753 | usb_ma_table[0].chg_iusb_value, rc); |
| 1754 | return rc; |
| 1755 | } |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1756 | |
| 1757 | if (chip->safety_time != 0) { |
| 1758 | rc = pm_chg_tchg_max_set(chip, chip->safety_time); |
| 1759 | if (rc) { |
| 1760 | pr_err("Failed to set max time to %d minutes rc=%d\n", |
| 1761 | chip->safety_time, rc); |
| 1762 | return rc; |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | if (chip->ttrkl_time != 0) { |
| 1767 | rc = pm_chg_ttrkl_max_set(chip, chip->safety_time); |
| 1768 | if (rc) { |
| 1769 | pr_err("Failed to set trkl time to %d minutes rc=%d\n", |
| 1770 | chip->safety_time, rc); |
| 1771 | return rc; |
| 1772 | } |
| 1773 | } |
| 1774 | |
Abhijeet Dharmapurikar | 6fe50a8 | 2011-08-25 21:33:14 -0700 | [diff] [blame] | 1775 | if (chip->vin_min != 0) { |
| 1776 | rc = pm_chg_vinmin_set(chip, chip->vin_min); |
| 1777 | if (rc) { |
| 1778 | pr_err("Failed to set vin min to %d mV rc=%d\n", |
| 1779 | chip->vin_min, rc); |
| 1780 | return rc; |
| 1781 | } |
| 1782 | } |
| 1783 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1784 | rc = pm_chg_disable_wd(chip); |
| 1785 | if (rc) { |
| 1786 | pr_err("Failed to disable wd rc=%d\n", rc); |
| 1787 | return rc; |
| 1788 | } |
| 1789 | |
| 1790 | rc = pm_chg_masked_write(chip, CHG_CNTRL_2, |
| 1791 | CHG_BAT_TEMP_DIS_BIT, 0); |
| 1792 | if (rc) { |
| 1793 | pr_err("Failed to enable temp control chg rc=%d\n", rc); |
| 1794 | return rc; |
| 1795 | } |
| 1796 | /* switch to a 3.2Mhz for the buck */ |
| 1797 | rc = pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CLOCK_CTRL, 0x15); |
| 1798 | if (rc) { |
| 1799 | pr_err("Failed to switch buck clk rc=%d\n", rc); |
| 1800 | return rc; |
| 1801 | } |
| 1802 | |
Abhijeet Dharmapurikar | 26cef9c | 2011-08-25 19:16:42 -0700 | [diff] [blame] | 1803 | if (chip->trkl_voltage != 0) { |
| 1804 | rc = pm_chg_vtrkl_low_set(chip, chip->trkl_voltage); |
| 1805 | if (rc) { |
| 1806 | pr_err("Failed to set trkl voltage to %dmv rc=%d\n", |
| 1807 | chip->trkl_voltage, rc); |
| 1808 | return rc; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | if (chip->weak_voltage != 0) { |
| 1813 | rc = pm_chg_vweak_set(chip, chip->weak_voltage); |
| 1814 | if (rc) { |
| 1815 | pr_err("Failed to set weak voltage to %dmv rc=%d\n", |
| 1816 | chip->weak_voltage, rc); |
| 1817 | return rc; |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | if (chip->trkl_current != 0) { |
| 1822 | rc = pm_chg_itrkl_set(chip, chip->trkl_current); |
| 1823 | if (rc) { |
| 1824 | pr_err("Failed to set trkl current to %dmA rc=%d\n", |
| 1825 | chip->trkl_voltage, rc); |
| 1826 | return rc; |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | if (chip->weak_current != 0) { |
| 1831 | rc = pm_chg_iweak_set(chip, chip->weak_current); |
| 1832 | if (rc) { |
| 1833 | pr_err("Failed to set weak current to %dmA rc=%d\n", |
| 1834 | chip->weak_current, rc); |
| 1835 | return rc; |
| 1836 | } |
| 1837 | } |
| 1838 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1839 | /* Workarounds for die 1.1 and 1.0 */ |
| 1840 | if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) { |
| 1841 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST2, 0xF1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1842 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xCE); |
| 1843 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD8); |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 1844 | |
| 1845 | /* software workaround for correct battery_id detection */ |
| 1846 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_0, 0xFF); |
| 1847 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_1, 0xFF); |
| 1848 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_2, 0xFF); |
| 1849 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_3, 0xFF); |
| 1850 | pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0D); |
| 1851 | udelay(100); |
| 1852 | pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0C); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | rc = pm_chg_charge_dis(chip, charging_disabled); |
| 1856 | if (rc) { |
| 1857 | pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc); |
| 1858 | return rc; |
| 1859 | } |
| 1860 | |
| 1861 | rc = pm_chg_auto_enable(chip, !charging_disabled); |
| 1862 | if (rc) { |
| 1863 | pr_err("Failed to enable charging rc=%d\n", rc); |
| 1864 | return rc; |
| 1865 | } |
| 1866 | |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
| 1870 | static int get_rt_status(void *data, u64 * val) |
| 1871 | { |
| 1872 | int i = (int)data; |
| 1873 | int ret; |
| 1874 | |
| 1875 | /* global irq number is passed in via data */ |
| 1876 | ret = pm_chg_get_rt_status(the_chip, i); |
| 1877 | *val = ret; |
| 1878 | return 0; |
| 1879 | } |
| 1880 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 1881 | |
| 1882 | static int get_fsm_status(void *data, u64 * val) |
| 1883 | { |
| 1884 | u8 temp; |
| 1885 | |
| 1886 | temp = pm_chg_get_fsm_state(the_chip); |
| 1887 | *val = temp; |
| 1888 | return 0; |
| 1889 | } |
| 1890 | DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n"); |
| 1891 | |
| 1892 | static int get_reg(void *data, u64 * val) |
| 1893 | { |
| 1894 | int addr = (int)data; |
| 1895 | int ret; |
| 1896 | u8 temp; |
| 1897 | |
| 1898 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 1899 | if (ret) { |
| 1900 | pr_err("pm8xxx_readb to %x value =%d errored = %d\n", |
| 1901 | addr, temp, ret); |
| 1902 | return -EAGAIN; |
| 1903 | } |
| 1904 | *val = temp; |
| 1905 | return 0; |
| 1906 | } |
| 1907 | |
| 1908 | static int set_reg(void *data, u64 val) |
| 1909 | { |
| 1910 | int addr = (int)data; |
| 1911 | int ret; |
| 1912 | u8 temp; |
| 1913 | |
| 1914 | temp = (u8) val; |
| 1915 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 1916 | if (ret) { |
| 1917 | pr_err("pm8xxx_writeb to %x value =%d errored = %d\n", |
| 1918 | addr, temp, ret); |
| 1919 | return -EAGAIN; |
| 1920 | } |
| 1921 | return 0; |
| 1922 | } |
| 1923 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 1924 | |
| 1925 | static void create_debugfs_entries(struct pm8921_chg_chip *chip) |
| 1926 | { |
| 1927 | int i; |
| 1928 | |
| 1929 | chip->dent = debugfs_create_dir("pm8921_chg", NULL); |
| 1930 | |
| 1931 | if (IS_ERR(chip->dent)) { |
| 1932 | pr_err("pmic charger couldnt create debugfs dir\n"); |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | debugfs_create_file("CHG_CNTRL", 0644, chip->dent, |
| 1937 | (void *)CHG_CNTRL, ®_fops); |
| 1938 | debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent, |
| 1939 | (void *)CHG_CNTRL_2, ®_fops); |
| 1940 | debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent, |
| 1941 | (void *)CHG_CNTRL_3, ®_fops); |
| 1942 | debugfs_create_file("PBL_ACCESS1", 0644, chip->dent, |
| 1943 | (void *)PBL_ACCESS1, ®_fops); |
| 1944 | debugfs_create_file("PBL_ACCESS2", 0644, chip->dent, |
| 1945 | (void *)PBL_ACCESS2, ®_fops); |
| 1946 | debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent, |
| 1947 | (void *)SYS_CONFIG_1, ®_fops); |
| 1948 | debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent, |
| 1949 | (void *)SYS_CONFIG_2, ®_fops); |
| 1950 | debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent, |
| 1951 | (void *)CHG_VDD_MAX, ®_fops); |
| 1952 | debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent, |
| 1953 | (void *)CHG_VDD_SAFE, ®_fops); |
| 1954 | debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent, |
| 1955 | (void *)CHG_VBAT_DET, ®_fops); |
| 1956 | debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent, |
| 1957 | (void *)CHG_IBAT_MAX, ®_fops); |
| 1958 | debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent, |
| 1959 | (void *)CHG_IBAT_SAFE, ®_fops); |
| 1960 | debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent, |
| 1961 | (void *)CHG_VIN_MIN, ®_fops); |
| 1962 | debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent, |
| 1963 | (void *)CHG_VTRICKLE, ®_fops); |
| 1964 | debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent, |
| 1965 | (void *)CHG_ITRICKLE, ®_fops); |
| 1966 | debugfs_create_file("CHG_ITERM", 0644, chip->dent, |
| 1967 | (void *)CHG_ITERM, ®_fops); |
| 1968 | debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent, |
| 1969 | (void *)CHG_TCHG_MAX, ®_fops); |
| 1970 | debugfs_create_file("CHG_TWDOG", 0644, chip->dent, |
| 1971 | (void *)CHG_TWDOG, ®_fops); |
| 1972 | debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent, |
| 1973 | (void *)CHG_TEMP_THRESH, ®_fops); |
| 1974 | debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent, |
| 1975 | (void *)CHG_COMP_OVR, ®_fops); |
| 1976 | debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent, |
| 1977 | (void *)CHG_BUCK_CTRL_TEST1, ®_fops); |
| 1978 | debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent, |
| 1979 | (void *)CHG_BUCK_CTRL_TEST2, ®_fops); |
| 1980 | debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent, |
| 1981 | (void *)CHG_BUCK_CTRL_TEST3, ®_fops); |
| 1982 | debugfs_create_file("CHG_TEST", 0644, chip->dent, |
| 1983 | (void *)CHG_TEST, ®_fops); |
| 1984 | |
| 1985 | debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL, |
| 1986 | &fsm_fops); |
| 1987 | |
| 1988 | for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) { |
| 1989 | if (chip->pmic_chg_irq[chg_irq_data[i].irq_id]) |
| 1990 | debugfs_create_file(chg_irq_data[i].name, 0444, |
| 1991 | chip->dent, |
| 1992 | (void *)chg_irq_data[i].irq_id, |
| 1993 | &rt_fops); |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | static int __devinit pm8921_charger_probe(struct platform_device *pdev) |
| 1998 | { |
| 1999 | int rc = 0; |
| 2000 | struct pm8921_chg_chip *chip; |
| 2001 | const struct pm8921_charger_platform_data *pdata |
| 2002 | = pdev->dev.platform_data; |
| 2003 | |
| 2004 | if (!pdata) { |
| 2005 | pr_err("missing platform data\n"); |
| 2006 | return -EINVAL; |
| 2007 | } |
| 2008 | |
| 2009 | chip = kzalloc(sizeof(struct pm8921_chg_chip), |
| 2010 | GFP_KERNEL); |
| 2011 | if (!chip) { |
| 2012 | pr_err("Cannot allocate pm_chg_chip\n"); |
| 2013 | return -ENOMEM; |
| 2014 | } |
| 2015 | |
| 2016 | chip->dev = &pdev->dev; |
| 2017 | chip->safety_time = pdata->safety_time; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 2018 | chip->ttrkl_time = pdata->ttrkl_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2019 | chip->update_time = pdata->update_time; |
| 2020 | chip->max_voltage = pdata->max_voltage; |
| 2021 | chip->min_voltage = pdata->min_voltage; |
| 2022 | chip->resume_voltage = pdata->resume_voltage; |
| 2023 | chip->term_current = pdata->term_current; |
| 2024 | chip->vbat_channel = pdata->charger_cdata.vbat_channel; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 2025 | chip->batt_temp_channel = pdata->charger_cdata.batt_temp_channel; |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 2026 | chip->batt_id_channel = pdata->charger_cdata.batt_id_channel; |
| 2027 | chip->batt_id_min = pdata->batt_id_min; |
| 2028 | chip->batt_id_max = pdata->batt_id_max; |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 2029 | chip->cool_temp = pdata->cool_temp; |
| 2030 | chip->warm_temp = pdata->warm_temp; |
| 2031 | chip->temp_check_period = pdata->temp_check_period; |
| 2032 | chip->max_bat_chg_current = pdata->max_bat_chg_current; |
| 2033 | chip->cool_bat_chg_current = pdata->cool_bat_chg_current; |
| 2034 | chip->warm_bat_chg_current = pdata->warm_bat_chg_current; |
| 2035 | chip->cool_bat_voltage = pdata->cool_bat_voltage; |
| 2036 | chip->warm_bat_voltage = pdata->warm_bat_voltage; |
Abhijeet Dharmapurikar | 26cef9c | 2011-08-25 19:16:42 -0700 | [diff] [blame] | 2037 | chip->trkl_voltage = pdata->trkl_voltage; |
| 2038 | chip->weak_voltage = pdata->weak_voltage; |
| 2039 | chip->trkl_current = pdata->trkl_current; |
| 2040 | chip->weak_current = pdata->weak_current; |
Abhijeet Dharmapurikar | 6fe50a8 | 2011-08-25 21:33:14 -0700 | [diff] [blame] | 2041 | chip->vin_min = pdata->vin_min; |
Abhijeet Dharmapurikar | ad74236 | 2011-08-29 19:50:02 -0700 | [diff] [blame^] | 2042 | chip->thermal_mitigation = pdata->thermal_mitigation; |
| 2043 | chip->thermal_levels = pdata->thermal_levels; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2044 | |
| 2045 | rc = pm8921_chg_hw_init(chip); |
| 2046 | if (rc) { |
| 2047 | pr_err("couldn't init hardware rc=%d\n", rc); |
| 2048 | goto free_chip; |
| 2049 | } |
| 2050 | |
| 2051 | chip->usb_psy.name = "usb", |
| 2052 | chip->usb_psy.type = POWER_SUPPLY_TYPE_USB, |
| 2053 | chip->usb_psy.supplied_to = pm_power_supplied_to, |
| 2054 | chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to), |
| 2055 | chip->usb_psy.properties = pm_power_props, |
| 2056 | chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props), |
| 2057 | chip->usb_psy.get_property = pm_power_get_property, |
| 2058 | |
| 2059 | chip->dc_psy.name = "ac", |
| 2060 | chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS, |
| 2061 | chip->dc_psy.supplied_to = pm_power_supplied_to, |
| 2062 | chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to), |
| 2063 | chip->dc_psy.properties = pm_power_props, |
| 2064 | chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props), |
| 2065 | chip->dc_psy.get_property = pm_power_get_property, |
| 2066 | |
| 2067 | chip->batt_psy.name = "battery", |
| 2068 | chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY, |
| 2069 | chip->batt_psy.properties = msm_batt_power_props, |
| 2070 | chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props), |
| 2071 | chip->batt_psy.get_property = pm_batt_power_get_property, |
| 2072 | |
| 2073 | rc = power_supply_register(chip->dev, &chip->usb_psy); |
| 2074 | if (rc < 0) { |
| 2075 | pr_err("power_supply_register usb failed rc = %d\n", rc); |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 2076 | goto free_chip; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | rc = power_supply_register(chip->dev, &chip->dc_psy); |
| 2080 | if (rc < 0) { |
| 2081 | pr_err("power_supply_register dc failed rc = %d\n", rc); |
| 2082 | goto unregister_usb; |
| 2083 | } |
| 2084 | |
| 2085 | rc = power_supply_register(chip->dev, &chip->batt_psy); |
| 2086 | if (rc < 0) { |
| 2087 | pr_err("power_supply_register batt failed rc = %d\n", rc); |
| 2088 | goto unregister_dc; |
| 2089 | } |
| 2090 | |
| 2091 | rc = request_irqs(chip, pdev); |
| 2092 | if (rc) { |
| 2093 | pr_err("couldn't register interrupts rc=%d\n", rc); |
| 2094 | goto unregister_batt; |
| 2095 | } |
| 2096 | |
Abhijeet Dharmapurikar | e27d809 | 2011-08-12 17:16:29 -0700 | [diff] [blame] | 2097 | enable_irq_wake(chip->pmic_chg_irq[USBIN_VALID_IRQ]); |
| 2098 | enable_irq_wake(chip->pmic_chg_irq[USBIN_OV_IRQ]); |
| 2099 | enable_irq_wake(chip->pmic_chg_irq[USBIN_UV_IRQ]); |
Abhijeet Dharmapurikar | 86eea30 | 2011-08-15 13:55:18 -0700 | [diff] [blame] | 2100 | /* |
| 2101 | * if both the cool_temp and warm_temp are zero the device doesnt |
| 2102 | * care for jeita compliance |
| 2103 | */ |
| 2104 | if (!(chip->cool_temp == 0 && chip->warm_temp == 0)) { |
| 2105 | rc = configure_btm(chip); |
| 2106 | if (rc) { |
| 2107 | pr_err("couldn't register with btm rc=%d\n", rc); |
| 2108 | goto free_irq; |
| 2109 | } |
| 2110 | } |
Abhijeet Dharmapurikar | e27d809 | 2011-08-12 17:16:29 -0700 | [diff] [blame] | 2111 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2112 | platform_set_drvdata(pdev, chip); |
| 2113 | the_chip = chip; |
| 2114 | create_debugfs_entries(chip); |
| 2115 | |
| 2116 | INIT_WORK(&chip->bms_notify.work, bms_notify); |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame] | 2117 | INIT_WORK(&chip->battery_id_valid_work, battery_id_valid); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2118 | /* determine what state the charger is in */ |
| 2119 | determine_initial_state(chip); |
| 2120 | |
| 2121 | return 0; |
| 2122 | |
| 2123 | free_irq: |
| 2124 | free_irqs(chip); |
| 2125 | unregister_batt: |
| 2126 | power_supply_unregister(&chip->batt_psy); |
| 2127 | unregister_dc: |
| 2128 | power_supply_unregister(&chip->dc_psy); |
| 2129 | unregister_usb: |
| 2130 | power_supply_unregister(&chip->usb_psy); |
| 2131 | free_chip: |
| 2132 | kfree(chip); |
| 2133 | return rc; |
| 2134 | } |
| 2135 | |
| 2136 | static int __devexit pm8921_charger_remove(struct platform_device *pdev) |
| 2137 | { |
| 2138 | struct pm8921_chg_chip *chip = platform_get_drvdata(pdev); |
| 2139 | |
| 2140 | free_irqs(chip); |
| 2141 | platform_set_drvdata(pdev, NULL); |
| 2142 | the_chip = NULL; |
| 2143 | kfree(chip); |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | static struct platform_driver pm8921_charger_driver = { |
| 2148 | .probe = pm8921_charger_probe, |
| 2149 | .remove = __devexit_p(pm8921_charger_remove), |
| 2150 | .driver = { |
| 2151 | .name = PM8921_CHARGER_DEV_NAME, |
| 2152 | .owner = THIS_MODULE, |
| 2153 | }, |
| 2154 | }; |
| 2155 | |
| 2156 | static int __init pm8921_charger_init(void) |
| 2157 | { |
| 2158 | return platform_driver_register(&pm8921_charger_driver); |
| 2159 | } |
| 2160 | |
| 2161 | static void __exit pm8921_charger_exit(void) |
| 2162 | { |
| 2163 | platform_driver_unregister(&pm8921_charger_driver); |
| 2164 | } |
| 2165 | |
| 2166 | late_initcall(pm8921_charger_init); |
| 2167 | module_exit(pm8921_charger_exit); |
| 2168 | |
| 2169 | MODULE_LICENSE("GPL v2"); |
| 2170 | MODULE_DESCRIPTION("PMIC8921 charger/battery driver"); |
| 2171 | MODULE_VERSION("1.0"); |
| 2172 | MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME); |