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; |
| 168 | unsigned int pmic_chg_irq[PM_CHG_MAX_INTS]; |
| 169 | unsigned int safety_time; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 170 | unsigned int ttrkl_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | unsigned int update_time; |
| 172 | unsigned int max_voltage; |
| 173 | unsigned int min_voltage; |
| 174 | unsigned int resume_voltage; |
| 175 | unsigned int term_current; |
| 176 | unsigned int vbat_channel; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 177 | unsigned int batt_temp_channel; |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 178 | unsigned int batt_id_channel; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 179 | struct power_supply usb_psy; |
| 180 | struct power_supply dc_psy; |
| 181 | struct power_supply batt_psy; |
| 182 | struct dentry *dent; |
| 183 | struct bms_notify bms_notify; |
| 184 | DECLARE_BITMAP(enabled_irqs, PM_CHG_MAX_INTS); |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 185 | struct work_struct battery_id_valid_work; |
| 186 | int64_t batt_id_min; |
| 187 | int64_t batt_id_max; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | static int charging_disabled; |
| 191 | |
| 192 | static struct pm8921_chg_chip *the_chip; |
| 193 | |
| 194 | static int pm_chg_masked_write(struct pm8921_chg_chip *chip, u16 addr, |
| 195 | u8 mask, u8 val) |
| 196 | { |
| 197 | int rc; |
| 198 | u8 reg; |
| 199 | |
| 200 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 201 | if (rc) { |
| 202 | pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n", addr, rc); |
| 203 | return rc; |
| 204 | } |
| 205 | reg &= ~mask; |
| 206 | reg |= val & mask; |
| 207 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 208 | if (rc) { |
| 209 | pr_err("pm8xxx_writeb failed: addr=%03X, rc=%d\n", addr, rc); |
| 210 | return rc; |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | #define CAPTURE_FSM_STATE_CMD 0xC2 |
| 216 | #define READ_BANK_7 0x70 |
| 217 | #define READ_BANK_4 0x40 |
| 218 | static int pm_chg_get_fsm_state(struct pm8921_chg_chip *chip) |
| 219 | { |
| 220 | u8 temp; |
| 221 | int err, ret = 0; |
| 222 | |
| 223 | temp = CAPTURE_FSM_STATE_CMD; |
| 224 | err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp); |
| 225 | if (err) { |
| 226 | pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST); |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | temp = READ_BANK_7; |
| 231 | err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp); |
| 232 | if (err) { |
| 233 | pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST); |
| 234 | return err; |
| 235 | } |
| 236 | |
| 237 | err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp); |
| 238 | if (err) { |
| 239 | pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err); |
| 240 | return err; |
| 241 | } |
| 242 | /* get the lower 4 bits */ |
| 243 | ret = temp & 0xF; |
| 244 | |
| 245 | temp = READ_BANK_4; |
| 246 | err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp); |
| 247 | if (err) { |
| 248 | pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST); |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp); |
| 253 | if (err) { |
| 254 | pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err); |
| 255 | return err; |
| 256 | } |
| 257 | /* get the upper 1 bit */ |
| 258 | ret |= (temp & 0x1) << 4; |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | #define CHG_USB_SUSPEND_BIT BIT(2) |
| 263 | static int pm_chg_usb_suspend_enable(struct pm8921_chg_chip *chip, int enable) |
| 264 | { |
| 265 | return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_USB_SUSPEND_BIT, |
| 266 | enable ? CHG_USB_SUSPEND_BIT : 0); |
| 267 | } |
| 268 | |
| 269 | #define CHG_EN_BIT BIT(7) |
| 270 | static int pm_chg_auto_enable(struct pm8921_chg_chip *chip, int enable) |
| 271 | { |
| 272 | return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_EN_BIT, |
| 273 | enable ? CHG_EN_BIT : 0); |
| 274 | } |
| 275 | |
| 276 | #define CHG_CHARGE_DIS_BIT BIT(1) |
| 277 | static int pm_chg_charge_dis(struct pm8921_chg_chip *chip, int disable) |
| 278 | { |
| 279 | return pm_chg_masked_write(chip, CHG_CNTRL, CHG_CHARGE_DIS_BIT, |
| 280 | disable ? CHG_CHARGE_DIS_BIT : 0); |
| 281 | } |
| 282 | |
| 283 | #define PM8921_CHG_V_MIN_MV 3240 |
| 284 | #define PM8921_CHG_V_STEP_MV 20 |
| 285 | #define PM8921_CHG_VDDMAX_MAX 4500 |
| 286 | #define PM8921_CHG_VDDMAX_MIN 3400 |
| 287 | #define PM8921_CHG_V_MASK 0x7F |
| 288 | static int pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage) |
| 289 | { |
| 290 | u8 temp; |
| 291 | |
| 292 | if (voltage < PM8921_CHG_VDDMAX_MIN |
| 293 | || voltage > PM8921_CHG_VDDMAX_MAX) { |
| 294 | pr_err("bad mV=%d asked to set\n", voltage); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV; |
| 298 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 299 | return pm_chg_masked_write(chip, CHG_VDD_MAX, PM8921_CHG_V_MASK, temp); |
| 300 | } |
| 301 | |
| 302 | #define PM8921_CHG_VDDSAFE_MIN 3400 |
| 303 | #define PM8921_CHG_VDDSAFE_MAX 4500 |
| 304 | static int pm_chg_vddsafe_set(struct pm8921_chg_chip *chip, int voltage) |
| 305 | { |
| 306 | u8 temp; |
| 307 | |
| 308 | if (voltage < PM8921_CHG_VDDSAFE_MIN |
| 309 | || voltage > PM8921_CHG_VDDSAFE_MAX) { |
| 310 | pr_err("bad mV=%d asked to set\n", voltage); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV; |
| 314 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 315 | return pm_chg_masked_write(chip, CHG_VDD_SAFE, PM8921_CHG_V_MASK, temp); |
| 316 | } |
| 317 | |
| 318 | #define PM8921_CHG_VBATDET_MIN 3240 |
| 319 | #define PM8921_CHG_VBATDET_MAX 5780 |
| 320 | static int pm_chg_vbatdet_set(struct pm8921_chg_chip *chip, int voltage) |
| 321 | { |
| 322 | u8 temp; |
| 323 | |
| 324 | if (voltage < PM8921_CHG_VBATDET_MIN |
| 325 | || voltage > PM8921_CHG_VBATDET_MAX) { |
| 326 | pr_err("bad mV=%d asked to set\n", voltage); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV; |
| 330 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 331 | return pm_chg_masked_write(chip, CHG_VBAT_DET, PM8921_CHG_V_MASK, temp); |
| 332 | } |
| 333 | |
| 334 | #define PM8921_CHG_IBATMAX_MIN 325 |
| 335 | #define PM8921_CHG_IBATMAX_MAX 2000 |
| 336 | #define PM8921_CHG_I_MIN_MA 225 |
| 337 | #define PM8921_CHG_I_STEP_MA 50 |
| 338 | #define PM8921_CHG_I_MASK 0x3F |
| 339 | static int pm_chg_ibatmax_set(struct pm8921_chg_chip *chip, int chg_current) |
| 340 | { |
| 341 | u8 temp; |
| 342 | |
| 343 | if (chg_current < PM8921_CHG_IBATMAX_MIN |
| 344 | || chg_current > PM8921_CHG_IBATMAX_MAX) { |
| 345 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA; |
| 349 | return pm_chg_masked_write(chip, CHG_IBAT_MAX, PM8921_CHG_I_MASK, temp); |
| 350 | } |
| 351 | |
| 352 | #define PM8921_CHG_IBATSAFE_MIN 225 |
| 353 | #define PM8921_CHG_IBATSAFE_MAX 3375 |
| 354 | static int pm_chg_ibatsafe_set(struct pm8921_chg_chip *chip, int chg_current) |
| 355 | { |
| 356 | u8 temp; |
| 357 | |
| 358 | if (chg_current < PM8921_CHG_IBATSAFE_MIN |
| 359 | || chg_current > PM8921_CHG_IBATSAFE_MAX) { |
| 360 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA; |
| 364 | return pm_chg_masked_write(chip, CHG_IBAT_SAFE, |
| 365 | PM8921_CHG_I_MASK, temp); |
| 366 | } |
| 367 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 368 | #define PM8921_CHG_ITERM_MIN_MA 50 |
Abhijeet Dharmapurikar | 71816fd | 2011-08-12 17:14:10 -0700 | [diff] [blame] | 369 | #define PM8921_CHG_ITERM_MAX_MA 200 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 370 | #define PM8921_CHG_ITERM_STEP_MA 10 |
| 371 | #define PM8921_CHG_ITERM_MASK 0xF |
| 372 | static int pm_chg_iterm_set(struct pm8921_chg_chip *chip, int chg_current) |
| 373 | { |
| 374 | u8 temp; |
| 375 | |
Abhijeet Dharmapurikar | 71816fd | 2011-08-12 17:14:10 -0700 | [diff] [blame] | 376 | if (chg_current < PM8921_CHG_ITERM_MIN_MA |
| 377 | || chg_current > PM8921_CHG_ITERM_MAX_MA) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 378 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | temp = (chg_current - PM8921_CHG_ITERM_MIN_MA) |
| 383 | / PM8921_CHG_ITERM_STEP_MA; |
Abhijeet Dharmapurikar | 71816fd | 2011-08-12 17:14:10 -0700 | [diff] [blame] | 384 | return pm_chg_masked_write(chip, CHG_ITERM, PM8921_CHG_ITERM_MASK, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 385 | temp); |
| 386 | } |
| 387 | |
| 388 | #define PM8921_CHG_IUSB_MASK 0x1C |
| 389 | #define PM8921_CHG_IUSB_MAX 7 |
| 390 | #define PM8921_CHG_IUSB_MIN 0 |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 391 | 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] | 392 | { |
| 393 | u8 temp; |
| 394 | |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 395 | if (reg_val < PM8921_CHG_IUSB_MIN || reg_val > PM8921_CHG_IUSB_MAX) { |
| 396 | pr_err("bad mA=%d asked to set\n", reg_val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 397 | return -EINVAL; |
| 398 | } |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 399 | temp = reg_val << 2; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 400 | return pm_chg_masked_write(chip, PBL_ACCESS2, PM8921_CHG_IUSB_MASK, |
| 401 | temp); |
| 402 | } |
| 403 | |
| 404 | #define PM8921_CHG_WD_MASK 0x1F |
| 405 | static int pm_chg_disable_wd(struct pm8921_chg_chip *chip) |
| 406 | { |
| 407 | /* writing 0 to the wd timer disables it */ |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 408 | return pm_chg_masked_write(chip, CHG_TWDOG, PM8921_CHG_WD_MASK, 0); |
| 409 | } |
| 410 | |
| 411 | #define PM8921_CHG_TCHG_MASK 0x3F |
| 412 | #define PM8921_CHG_TCHG_MIN 4 |
| 413 | #define PM8921_CHG_TCHG_MAX 512 |
| 414 | #define PM8921_CHG_TCHG_STEP 4 |
| 415 | static int pm_chg_tchg_max_set(struct pm8921_chg_chip *chip, int minutes) |
| 416 | { |
| 417 | u8 temp; |
| 418 | |
| 419 | if (minutes < PM8921_CHG_TCHG_MIN || minutes > PM8921_CHG_TCHG_MAX) { |
| 420 | pr_err("bad max minutes =%d asked to set\n", minutes); |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | temp = (minutes - 1)/PM8921_CHG_TCHG_STEP; |
| 425 | return pm_chg_masked_write(chip, CHG_TCHG_MAX, PM8921_CHG_TCHG_MASK, |
| 426 | temp); |
| 427 | } |
| 428 | |
| 429 | #define PM8921_CHG_TTRKL_MASK 0x1F |
| 430 | #define PM8921_CHG_TTRKL_MIN 1 |
| 431 | #define PM8921_CHG_TTRKL_MAX 64 |
| 432 | static int pm_chg_ttrkl_max_set(struct pm8921_chg_chip *chip, int minutes) |
| 433 | { |
| 434 | u8 temp; |
| 435 | |
| 436 | if (minutes < PM8921_CHG_TTRKL_MIN || minutes > PM8921_CHG_TTRKL_MAX) { |
| 437 | pr_err("bad max minutes =%d asked to set\n", minutes); |
| 438 | return -EINVAL; |
| 439 | } |
| 440 | |
| 441 | temp = minutes - 1; |
| 442 | return pm_chg_masked_write(chip, CHG_TTRKL_MAX, PM8921_CHG_TTRKL_MASK, |
| 443 | temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 446 | static int64_t read_battery_id(struct pm8921_chg_chip *chip) |
| 447 | { |
| 448 | int rc; |
| 449 | struct pm8921_adc_chan_result result; |
| 450 | |
| 451 | rc = pm8921_adc_read(chip->batt_id_channel, &result); |
| 452 | if (rc) { |
| 453 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 454 | chip->vbat_channel, rc); |
| 455 | return rc; |
| 456 | } |
| 457 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 458 | result.measurement); |
| 459 | return result.physical; |
| 460 | } |
| 461 | |
| 462 | static int is_battery_valid(struct pm8921_chg_chip *chip) |
| 463 | { |
| 464 | int64_t rc; |
| 465 | |
| 466 | if (chip->batt_id_min == 0 && chip->batt_id_max == 0) |
| 467 | return 1; |
| 468 | |
| 469 | rc = read_battery_id(chip); |
| 470 | if (rc < 0) { |
| 471 | pr_err("error reading batt id channel = %d, rc = %lld\n", |
| 472 | chip->vbat_channel, rc); |
| 473 | /* assume battery id is valid when adc error happens */ |
| 474 | return 1; |
| 475 | } |
| 476 | |
| 477 | if (rc < chip->batt_id_min || rc > chip->batt_id_max) { |
| 478 | pr_err("batt_id phy =%lld is not valid\n", rc); |
| 479 | return 0; |
| 480 | } |
| 481 | return 1; |
| 482 | } |
| 483 | |
| 484 | static void check_battery_valid(struct pm8921_chg_chip *chip) |
| 485 | { |
| 486 | if (is_battery_valid(chip) == 0) { |
| 487 | pr_err("batt_id not valid, disbling charging\n"); |
| 488 | pm_chg_auto_enable(chip, 0); |
| 489 | } else { |
| 490 | pm_chg_auto_enable(chip, !charging_disabled); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | static void battery_id_valid(struct work_struct *work) |
| 495 | { |
| 496 | struct pm8921_chg_chip *chip = container_of(work, |
| 497 | struct pm8921_chg_chip, battery_id_valid_work); |
| 498 | |
| 499 | check_battery_valid(chip); |
| 500 | } |
| 501 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 502 | static void pm8921_chg_enable_irq(struct pm8921_chg_chip *chip, int interrupt) |
| 503 | { |
| 504 | if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) { |
| 505 | dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]); |
| 506 | enable_irq(chip->pmic_chg_irq[interrupt]); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static void pm8921_chg_disable_irq(struct pm8921_chg_chip *chip, int interrupt) |
| 511 | { |
| 512 | if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) { |
| 513 | dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]); |
| 514 | disable_irq_nosync(chip->pmic_chg_irq[interrupt]); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static int pm_chg_get_rt_status(struct pm8921_chg_chip *chip, int irq_id) |
| 519 | { |
| 520 | return pm8xxx_read_irq_stat(chip->dev->parent, |
| 521 | chip->pmic_chg_irq[irq_id]); |
| 522 | } |
| 523 | |
| 524 | /* Treat OverVoltage/UnderVoltage as source missing */ |
| 525 | static int is_usb_chg_plugged_in(struct pm8921_chg_chip *chip) |
| 526 | { |
| 527 | int pres, ov, uv; |
| 528 | |
| 529 | pres = pm_chg_get_rt_status(chip, USBIN_VALID_IRQ); |
| 530 | ov = pm_chg_get_rt_status(chip, USBIN_OV_IRQ); |
| 531 | uv = pm_chg_get_rt_status(chip, USBIN_UV_IRQ); |
| 532 | |
| 533 | return pres && !ov && !uv; |
| 534 | } |
| 535 | |
| 536 | /* Treat OverVoltage/UnderVoltage as source missing */ |
| 537 | static int is_dc_chg_plugged_in(struct pm8921_chg_chip *chip) |
| 538 | { |
| 539 | int pres, ov, uv; |
| 540 | |
| 541 | pres = pm_chg_get_rt_status(chip, DCIN_VALID_IRQ); |
| 542 | ov = pm_chg_get_rt_status(chip, DCIN_OV_IRQ); |
| 543 | uv = pm_chg_get_rt_status(chip, DCIN_UV_IRQ); |
| 544 | |
| 545 | return pres && !ov && !uv; |
| 546 | } |
| 547 | |
| 548 | static int is_battery_charging(int fsm_state) |
| 549 | { |
| 550 | switch (fsm_state) { |
| 551 | case FSM_STATE_ATC_2A: |
| 552 | case FSM_STATE_ATC_2B: |
| 553 | case FSM_STATE_ON_CHG_AND_BAT_6: |
| 554 | case FSM_STATE_FAST_CHG_7: |
| 555 | case FSM_STATE_TRKL_CHG_8: |
| 556 | return 1; |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static void bms_notify(struct work_struct *work) |
| 562 | { |
| 563 | struct bms_notify *n = container_of(work, struct bms_notify, work); |
| 564 | |
| 565 | if (n->is_charging) |
| 566 | pm8921_bms_charging_began(); |
| 567 | else |
| 568 | pm8921_bms_charging_end(); |
| 569 | } |
| 570 | |
| 571 | static enum power_supply_property pm_power_props[] = { |
| 572 | POWER_SUPPLY_PROP_PRESENT, |
Willie Ruan | 9ad6d83 | 2011-08-19 11:58:42 -0700 | [diff] [blame] | 573 | POWER_SUPPLY_PROP_ONLINE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | static char *pm_power_supplied_to[] = { |
| 577 | "battery", |
| 578 | }; |
| 579 | |
| 580 | static int pm_power_get_property(struct power_supply *psy, |
| 581 | enum power_supply_property psp, |
| 582 | union power_supply_propval *val) |
| 583 | { |
| 584 | struct pm8921_chg_chip *chip; |
| 585 | |
| 586 | switch (psp) { |
| 587 | case POWER_SUPPLY_PROP_PRESENT: |
Willie Ruan | 9ad6d83 | 2011-08-19 11:58:42 -0700 | [diff] [blame] | 588 | case POWER_SUPPLY_PROP_ONLINE: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 589 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) { |
| 590 | chip = container_of(psy, struct pm8921_chg_chip, |
| 591 | dc_psy); |
| 592 | val->intval = is_dc_chg_plugged_in(chip); |
| 593 | } |
| 594 | if (psy->type == POWER_SUPPLY_TYPE_USB) { |
| 595 | chip = container_of(psy, struct pm8921_chg_chip, |
| 596 | usb_psy); |
| 597 | val->intval = is_usb_chg_plugged_in(chip); |
| 598 | } |
| 599 | break; |
| 600 | default: |
| 601 | return -EINVAL; |
| 602 | } |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static enum power_supply_property msm_batt_power_props[] = { |
| 607 | POWER_SUPPLY_PROP_STATUS, |
| 608 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 609 | POWER_SUPPLY_PROP_HEALTH, |
| 610 | POWER_SUPPLY_PROP_PRESENT, |
| 611 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 612 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 613 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 614 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 615 | POWER_SUPPLY_PROP_CAPACITY, |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 616 | POWER_SUPPLY_PROP_CURRENT_NOW, |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 617 | POWER_SUPPLY_PROP_TEMP, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | static int get_prop_battery_mvolts(struct pm8921_chg_chip *chip) |
| 621 | { |
| 622 | int rc; |
| 623 | struct pm8921_adc_chan_result result; |
| 624 | |
| 625 | rc = pm8921_adc_read(chip->vbat_channel, &result); |
| 626 | if (rc) { |
| 627 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 628 | chip->vbat_channel, rc); |
| 629 | return rc; |
| 630 | } |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 631 | pr_debug("mvolts phy = %lld meas = 0x%llx\n", result.physical, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 632 | result.measurement); |
| 633 | return (int)result.physical; |
| 634 | } |
| 635 | |
| 636 | static int get_prop_batt_capacity(struct pm8921_chg_chip *chip) |
| 637 | { |
Abhijeet Dharmapurikar | 5a7df4e | 2011-07-27 13:16:24 -0700 | [diff] [blame] | 638 | int percent_soc = pm8921_bms_get_percent_charge(); |
| 639 | |
| 640 | if (percent_soc <= 10) |
| 641 | pr_warn("low battery charge = %d%%\n", percent_soc); |
| 642 | |
| 643 | return percent_soc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 646 | static int get_prop_batt_current(struct pm8921_chg_chip *chip) |
| 647 | { |
| 648 | int result_ma, rc; |
| 649 | |
| 650 | rc = pm8921_bms_get_battery_current(&result_ma); |
| 651 | if (rc) { |
| 652 | pr_err("unable to get batt current rc = %d\n", rc); |
| 653 | return rc; |
| 654 | } else { |
| 655 | return result_ma; |
| 656 | } |
| 657 | } |
| 658 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 659 | static int get_prop_batt_health(struct pm8921_chg_chip *chip) |
| 660 | { |
| 661 | int temp; |
| 662 | |
| 663 | temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ); |
| 664 | if (temp) |
| 665 | return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 666 | |
| 667 | temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ); |
| 668 | if (temp) |
| 669 | return POWER_SUPPLY_HEALTH_COLD; |
| 670 | |
| 671 | return POWER_SUPPLY_HEALTH_GOOD; |
| 672 | } |
| 673 | |
| 674 | static int get_prop_batt_present(struct pm8921_chg_chip *chip) |
| 675 | { |
| 676 | return pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ); |
| 677 | } |
| 678 | |
| 679 | static int get_prop_charge_type(struct pm8921_chg_chip *chip) |
| 680 | { |
| 681 | int temp; |
| 682 | |
| 683 | temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ); |
| 684 | if (temp) |
| 685 | return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 686 | |
| 687 | temp = pm_chg_get_rt_status(chip, FASTCHG_IRQ); |
| 688 | if (temp) |
| 689 | return POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 690 | |
| 691 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 692 | } |
| 693 | |
| 694 | static int get_prop_batt_status(struct pm8921_chg_chip *chip) |
| 695 | { |
| 696 | int temp = 0; |
| 697 | |
| 698 | /* TODO reading the FSM state is more reliable */ |
| 699 | temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ); |
| 700 | |
| 701 | temp |= pm_chg_get_rt_status(chip, FASTCHG_IRQ); |
| 702 | if (temp) |
| 703 | return POWER_SUPPLY_STATUS_CHARGING; |
| 704 | /* |
| 705 | * The battery is not charging |
| 706 | * check the FET - if on battery is discharging |
| 707 | * - if off battery is isolated(full) and the system |
| 708 | * is being driven from a charger |
| 709 | */ |
| 710 | temp = pm_chg_get_rt_status(chip, BATFET_IRQ); |
| 711 | if (temp) |
| 712 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 713 | |
| 714 | return POWER_SUPPLY_STATUS_FULL; |
| 715 | } |
| 716 | |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 717 | static int get_prop_batt_temp(struct pm8921_chg_chip *chip) |
| 718 | { |
| 719 | int rc; |
| 720 | struct pm8921_adc_chan_result result; |
| 721 | |
| 722 | rc = pm8921_adc_read(chip->batt_temp_channel, &result); |
| 723 | if (rc) { |
| 724 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 725 | chip->vbat_channel, rc); |
| 726 | return rc; |
| 727 | } |
| 728 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 729 | result.measurement); |
| 730 | return (int)result.physical; |
| 731 | } |
| 732 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 733 | static int pm_batt_power_get_property(struct power_supply *psy, |
| 734 | enum power_supply_property psp, |
| 735 | union power_supply_propval *val) |
| 736 | { |
| 737 | struct pm8921_chg_chip *chip = container_of(psy, struct pm8921_chg_chip, |
| 738 | batt_psy); |
| 739 | |
| 740 | switch (psp) { |
| 741 | case POWER_SUPPLY_PROP_STATUS: |
| 742 | val->intval = get_prop_batt_status(chip); |
| 743 | break; |
| 744 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 745 | val->intval = get_prop_charge_type(chip); |
| 746 | break; |
| 747 | case POWER_SUPPLY_PROP_HEALTH: |
| 748 | val->intval = get_prop_batt_health(chip); |
| 749 | break; |
| 750 | case POWER_SUPPLY_PROP_PRESENT: |
| 751 | val->intval = get_prop_batt_present(chip); |
| 752 | break; |
| 753 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 754 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 755 | break; |
| 756 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 757 | val->intval = chip->max_voltage; |
| 758 | break; |
| 759 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 760 | val->intval = chip->min_voltage; |
| 761 | break; |
| 762 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 763 | val->intval = get_prop_battery_mvolts(chip); |
| 764 | break; |
| 765 | case POWER_SUPPLY_PROP_CAPACITY: |
| 766 | val->intval = get_prop_batt_capacity(chip); |
| 767 | break; |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 768 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 769 | val->intval = get_prop_batt_current(chip); |
| 770 | break; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 771 | case POWER_SUPPLY_PROP_TEMP: |
| 772 | val->intval = get_prop_batt_temp(chip); |
| 773 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 774 | default: |
| 775 | return -EINVAL; |
| 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static void (*notify_vbus_state_func_ptr)(int); |
| 782 | static int usb_chg_current; |
| 783 | static DEFINE_SPINLOCK(vbus_lock); |
| 784 | |
| 785 | int pm8921_charger_register_vbus_sn(void (*callback)(int)) |
| 786 | { |
| 787 | pr_debug("%p\n", callback); |
| 788 | notify_vbus_state_func_ptr = callback; |
| 789 | return 0; |
| 790 | } |
| 791 | EXPORT_SYMBOL_GPL(pm8921_charger_register_vbus_sn); |
| 792 | |
| 793 | /* this is passed to the hsusb via platform_data msm_otg_pdata */ |
| 794 | void pm8921_charger_unregister_vbus_sn(void (*callback)(int)) |
| 795 | { |
| 796 | pr_debug("%p\n", callback); |
| 797 | notify_vbus_state_func_ptr = NULL; |
| 798 | } |
| 799 | EXPORT_SYMBOL_GPL(pm8921_charger_unregister_vbus_sn); |
| 800 | |
| 801 | static void notify_usb_of_the_plugin_event(int plugin) |
| 802 | { |
| 803 | plugin = !!plugin; |
| 804 | if (notify_vbus_state_func_ptr) { |
| 805 | pr_debug("notifying plugin\n"); |
| 806 | (*notify_vbus_state_func_ptr) (plugin); |
| 807 | } else { |
| 808 | pr_debug("unable to notify plugin\n"); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | struct usb_ma_limit_entry { |
| 813 | int usb_ma; |
| 814 | u8 chg_iusb_value; |
| 815 | }; |
| 816 | |
| 817 | static struct usb_ma_limit_entry usb_ma_table[] = { |
| 818 | {100, 0}, |
| 819 | {500, 1}, |
| 820 | {700, 2}, |
| 821 | {850, 3}, |
| 822 | {900, 4}, |
| 823 | {1100, 5}, |
| 824 | {1300, 6}, |
| 825 | {1500, 7}, |
| 826 | }; |
| 827 | |
| 828 | /* assumes vbus_lock is held */ |
| 829 | static void __pm8921_charger_vbus_draw(unsigned int mA) |
| 830 | { |
| 831 | int i, rc; |
| 832 | |
| 833 | if (mA > 0 && mA <= 2) { |
| 834 | usb_chg_current = 0; |
| 835 | rc = pm_chg_iusbmax_set(the_chip, |
| 836 | usb_ma_table[0].chg_iusb_value); |
| 837 | if (rc) { |
| 838 | pr_err("unable to set iusb to %d rc = %d\n", |
| 839 | usb_ma_table[0].chg_iusb_value, rc); |
| 840 | } |
| 841 | rc = pm_chg_usb_suspend_enable(the_chip, 1); |
| 842 | if (rc) |
| 843 | pr_err("fail to set suspend bit rc=%d\n", rc); |
| 844 | } else { |
| 845 | rc = pm_chg_usb_suspend_enable(the_chip, 0); |
| 846 | if (rc) |
| 847 | pr_err("fail to reset suspend bit rc=%d\n", rc); |
| 848 | for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) { |
| 849 | if (usb_ma_table[i].usb_ma <= mA) |
| 850 | break; |
| 851 | } |
| 852 | if (i < 0) |
| 853 | i = 0; |
| 854 | rc = pm_chg_iusbmax_set(the_chip, |
| 855 | usb_ma_table[i].chg_iusb_value); |
| 856 | if (rc) { |
| 857 | pr_err("unable to set iusb to %d rc = %d\n", |
| 858 | usb_ma_table[i].chg_iusb_value, rc); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* USB calls these to tell us how much max usb current the system can draw */ |
| 864 | void pm8921_charger_vbus_draw(unsigned int mA) |
| 865 | { |
| 866 | unsigned long flags; |
| 867 | |
| 868 | pr_debug("Enter charge=%d\n", mA); |
| 869 | spin_lock_irqsave(&vbus_lock, flags); |
| 870 | if (the_chip) { |
| 871 | __pm8921_charger_vbus_draw(mA); |
| 872 | } else { |
| 873 | /* |
| 874 | * called before pmic initialized, |
| 875 | * save this value and use it at probe |
| 876 | */ |
| 877 | usb_chg_current = mA; |
| 878 | } |
| 879 | spin_unlock_irqrestore(&vbus_lock, flags); |
| 880 | } |
| 881 | EXPORT_SYMBOL_GPL(pm8921_charger_vbus_draw); |
| 882 | |
Abhijeet Dharmapurikar | 1f52c41 | 2011-08-12 17:11:45 -0700 | [diff] [blame] | 883 | int pm8921_charger_enable(bool enable) |
| 884 | { |
| 885 | int rc; |
| 886 | |
| 887 | if (!the_chip) { |
| 888 | pr_err("called before init\n"); |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | enable = !!enable; |
| 892 | rc = pm_chg_auto_enable(the_chip, enable); |
| 893 | if (rc) |
| 894 | pr_err("Failed rc=%d\n", rc); |
| 895 | return rc; |
| 896 | } |
| 897 | EXPORT_SYMBOL(pm8921_charger_enable); |
| 898 | |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 899 | int pm8921_is_usb_chg_plugged_in(void) |
| 900 | { |
| 901 | if (!the_chip) { |
| 902 | pr_err("called before init\n"); |
| 903 | return -EINVAL; |
| 904 | } |
| 905 | return is_usb_chg_plugged_in(the_chip); |
| 906 | } |
| 907 | EXPORT_SYMBOL(pm8921_is_usb_chg_plugged_in); |
| 908 | |
| 909 | int pm8921_is_dc_chg_plugged_in(void) |
| 910 | { |
| 911 | if (!the_chip) { |
| 912 | pr_err("called before init\n"); |
| 913 | return -EINVAL; |
| 914 | } |
| 915 | return is_dc_chg_plugged_in(the_chip); |
| 916 | } |
| 917 | EXPORT_SYMBOL(pm8921_is_dc_chg_plugged_in); |
| 918 | |
| 919 | int pm8921_is_battery_present(void) |
| 920 | { |
| 921 | if (!the_chip) { |
| 922 | pr_err("called before init\n"); |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | return get_prop_batt_present(the_chip); |
| 926 | } |
| 927 | EXPORT_SYMBOL(pm8921_is_battery_present); |
| 928 | |
| 929 | int pm8921_set_max_battery_charge_current(int ma) |
| 930 | { |
| 931 | if (!the_chip) { |
| 932 | pr_err("called before init\n"); |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | return pm_chg_ibatmax_set(the_chip, ma); |
| 936 | } |
| 937 | EXPORT_SYMBOL(pm8921_set_max_battery_charge_current); |
| 938 | |
| 939 | int pm8921_disable_source_current(bool disable) |
| 940 | { |
| 941 | if (!the_chip) { |
| 942 | pr_err("called before init\n"); |
| 943 | return -EINVAL; |
| 944 | } |
| 945 | if (disable) |
| 946 | pr_warn("current drawn from chg=0, battery provides current\n"); |
| 947 | return pm_chg_charge_dis(the_chip, disable); |
| 948 | } |
| 949 | EXPORT_SYMBOL(pm8921_disable_source_current); |
| 950 | |
| 951 | bool pm8921_is_battery_charging(int *source) |
| 952 | { |
| 953 | int fsm_state, is_charging, dc_present, usb_present; |
| 954 | |
| 955 | if (!the_chip) { |
| 956 | pr_err("called before init\n"); |
| 957 | return -EINVAL; |
| 958 | } |
| 959 | fsm_state = pm_chg_get_fsm_state(the_chip); |
| 960 | is_charging = is_battery_charging(fsm_state); |
| 961 | if (is_charging == 0) { |
| 962 | *source = PM8921_CHG_SRC_NONE; |
| 963 | return is_charging; |
| 964 | } |
| 965 | |
| 966 | if (source == NULL) |
| 967 | return is_charging; |
| 968 | |
| 969 | /* the battery is charging, the source is requested, find it */ |
| 970 | dc_present = is_dc_chg_plugged_in(the_chip); |
| 971 | usb_present = is_usb_chg_plugged_in(the_chip); |
| 972 | |
| 973 | if (dc_present && !usb_present) |
| 974 | *source = PM8921_CHG_SRC_DC; |
| 975 | |
| 976 | if (usb_present && !dc_present) |
| 977 | *source = PM8921_CHG_SRC_USB; |
| 978 | |
| 979 | if (usb_present && dc_present) |
| 980 | /* |
| 981 | * The system always chooses dc for charging since it has |
| 982 | * higher priority. |
| 983 | */ |
| 984 | *source = PM8921_CHG_SRC_DC; |
| 985 | |
| 986 | return is_charging; |
| 987 | } |
| 988 | EXPORT_SYMBOL(pm8921_is_battery_charging); |
| 989 | |
| 990 | int pm8921_batt_temperature(void) |
| 991 | { |
| 992 | if (!the_chip) { |
| 993 | pr_err("called before init\n"); |
| 994 | return -EINVAL; |
| 995 | } |
| 996 | return get_prop_batt_temp(the_chip); |
| 997 | } |
| 998 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 999 | static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip) |
| 1000 | { |
| 1001 | int usb_present; |
| 1002 | |
| 1003 | usb_present = is_usb_chg_plugged_in(chip); |
| 1004 | if (chip->usb_present ^ usb_present) { |
| 1005 | notify_usb_of_the_plugin_event(usb_present); |
| 1006 | chip->usb_present = usb_present; |
| 1007 | power_supply_changed(&chip->usb_psy); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | static void handle_dc_removal_insertion(struct pm8921_chg_chip *chip) |
| 1012 | { |
| 1013 | int dc_present; |
| 1014 | |
| 1015 | dc_present = is_dc_chg_plugged_in(chip); |
| 1016 | if (chip->dc_present ^ dc_present) { |
| 1017 | chip->dc_present = dc_present; |
| 1018 | power_supply_changed(&chip->dc_psy); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | static irqreturn_t usbin_valid_irq_handler(int irq, void *data) |
| 1023 | { |
| 1024 | handle_usb_insertion_removal(data); |
| 1025 | return IRQ_HANDLED; |
| 1026 | } |
| 1027 | |
| 1028 | static irqreturn_t usbin_ov_irq_handler(int irq, void *data) |
| 1029 | { |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 1030 | pr_err("USB OverVoltage\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1031 | handle_usb_insertion_removal(data); |
| 1032 | return IRQ_HANDLED; |
| 1033 | } |
| 1034 | |
| 1035 | static irqreturn_t batt_inserted_irq_handler(int irq, void *data) |
| 1036 | { |
| 1037 | struct pm8921_chg_chip *chip = data; |
| 1038 | int status; |
| 1039 | |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 1040 | status = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ); |
| 1041 | schedule_work(&chip->battery_id_valid_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1042 | pr_debug("battery present=%d", status); |
| 1043 | power_supply_changed(&chip->batt_psy); |
| 1044 | return IRQ_HANDLED; |
| 1045 | } |
| 1046 | /* this interrupt used to restart charging a battery */ |
| 1047 | static irqreturn_t vbatdet_low_irq_handler(int irq, void *data) |
| 1048 | { |
| 1049 | struct pm8921_chg_chip *chip = data; |
| 1050 | |
| 1051 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1052 | power_supply_changed(&chip->batt_psy); |
| 1053 | power_supply_changed(&chip->usb_psy); |
| 1054 | power_supply_changed(&chip->dc_psy); |
| 1055 | return IRQ_HANDLED; |
| 1056 | } |
| 1057 | |
| 1058 | static irqreturn_t usbin_uv_irq_handler(int irq, void *data) |
| 1059 | { |
Abhijeet Dharmapurikar | 485a053 | 2011-08-12 17:18:10 -0700 | [diff] [blame] | 1060 | pr_err("USB UnderVoltage\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1061 | handle_usb_insertion_removal(data); |
| 1062 | return IRQ_HANDLED; |
| 1063 | } |
| 1064 | |
| 1065 | static irqreturn_t vbat_ov_irq_handler(int irq, void *data) |
| 1066 | { |
| 1067 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1068 | return IRQ_HANDLED; |
| 1069 | } |
| 1070 | |
| 1071 | static irqreturn_t chgwdog_irq_handler(int irq, void *data) |
| 1072 | { |
| 1073 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1074 | return IRQ_HANDLED; |
| 1075 | } |
| 1076 | |
| 1077 | static irqreturn_t vcp_irq_handler(int irq, void *data) |
| 1078 | { |
| 1079 | pr_warning("VCP triggered BATDET forced on\n"); |
| 1080 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1081 | return IRQ_HANDLED; |
| 1082 | } |
| 1083 | |
| 1084 | static irqreturn_t atcdone_irq_handler(int irq, void *data) |
| 1085 | { |
| 1086 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1087 | return IRQ_HANDLED; |
| 1088 | } |
| 1089 | |
| 1090 | static irqreturn_t atcfail_irq_handler(int irq, void *data) |
| 1091 | { |
| 1092 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1093 | return IRQ_HANDLED; |
| 1094 | } |
| 1095 | |
| 1096 | static irqreturn_t chgdone_irq_handler(int irq, void *data) |
| 1097 | { |
| 1098 | struct pm8921_chg_chip *chip = data; |
| 1099 | |
| 1100 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1101 | power_supply_changed(&chip->batt_psy); |
| 1102 | power_supply_changed(&chip->usb_psy); |
| 1103 | power_supply_changed(&chip->dc_psy); |
| 1104 | return IRQ_HANDLED; |
| 1105 | } |
| 1106 | |
| 1107 | static irqreturn_t chgfail_irq_handler(int irq, void *data) |
| 1108 | { |
| 1109 | struct pm8921_chg_chip *chip = data; |
| 1110 | |
| 1111 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1112 | power_supply_changed(&chip->batt_psy); |
| 1113 | power_supply_changed(&chip->usb_psy); |
| 1114 | power_supply_changed(&chip->dc_psy); |
| 1115 | return IRQ_HANDLED; |
| 1116 | } |
| 1117 | |
| 1118 | static irqreturn_t chgstate_irq_handler(int irq, void *data) |
| 1119 | { |
| 1120 | struct pm8921_chg_chip *chip = data; |
| 1121 | int new_is_charging = 0, fsm_state; |
| 1122 | |
| 1123 | pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data)); |
| 1124 | power_supply_changed(&chip->batt_psy); |
| 1125 | power_supply_changed(&chip->usb_psy); |
| 1126 | power_supply_changed(&chip->dc_psy); |
| 1127 | |
| 1128 | fsm_state = pm_chg_get_fsm_state(chip); |
| 1129 | new_is_charging = is_battery_charging(fsm_state); |
| 1130 | |
| 1131 | if (chip->bms_notify.is_charging ^ new_is_charging) { |
| 1132 | chip->bms_notify.is_charging = new_is_charging; |
| 1133 | schedule_work(&(chip->bms_notify.work)); |
| 1134 | } |
| 1135 | return IRQ_HANDLED; |
| 1136 | } |
| 1137 | |
| 1138 | static irqreturn_t loop_change_irq_handler(int irq, void *data) |
| 1139 | { |
| 1140 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1141 | return IRQ_HANDLED; |
| 1142 | } |
| 1143 | |
| 1144 | static irqreturn_t fastchg_irq_handler(int irq, void *data) |
| 1145 | { |
| 1146 | struct pm8921_chg_chip *chip = data; |
| 1147 | |
| 1148 | power_supply_changed(&chip->batt_psy); |
| 1149 | return IRQ_HANDLED; |
| 1150 | } |
| 1151 | |
| 1152 | static irqreturn_t trklchg_irq_handler(int irq, void *data) |
| 1153 | { |
| 1154 | struct pm8921_chg_chip *chip = data; |
| 1155 | |
| 1156 | power_supply_changed(&chip->batt_psy); |
| 1157 | return IRQ_HANDLED; |
| 1158 | } |
| 1159 | |
| 1160 | static irqreturn_t batt_removed_irq_handler(int irq, void *data) |
| 1161 | { |
| 1162 | struct pm8921_chg_chip *chip = data; |
| 1163 | int status; |
| 1164 | |
| 1165 | status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ); |
| 1166 | pr_debug("battery present=%d state=%d", !status, |
| 1167 | pm_chg_get_fsm_state(data)); |
| 1168 | power_supply_changed(&chip->batt_psy); |
| 1169 | return IRQ_HANDLED; |
| 1170 | } |
| 1171 | |
| 1172 | static irqreturn_t batttemp_hot_irq_handler(int irq, void *data) |
| 1173 | { |
| 1174 | struct pm8921_chg_chip *chip = data; |
| 1175 | |
| 1176 | power_supply_changed(&chip->batt_psy); |
| 1177 | return IRQ_HANDLED; |
| 1178 | } |
| 1179 | |
| 1180 | static irqreturn_t chghot_irq_handler(int irq, void *data) |
| 1181 | { |
| 1182 | struct pm8921_chg_chip *chip = data; |
| 1183 | |
| 1184 | pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1185 | power_supply_changed(&chip->batt_psy); |
| 1186 | power_supply_changed(&chip->usb_psy); |
| 1187 | power_supply_changed(&chip->dc_psy); |
| 1188 | return IRQ_HANDLED; |
| 1189 | } |
| 1190 | |
| 1191 | static irqreturn_t batttemp_cold_irq_handler(int irq, void *data) |
| 1192 | { |
| 1193 | struct pm8921_chg_chip *chip = data; |
| 1194 | |
| 1195 | pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1196 | power_supply_changed(&chip->batt_psy); |
| 1197 | power_supply_changed(&chip->usb_psy); |
| 1198 | power_supply_changed(&chip->dc_psy); |
| 1199 | return IRQ_HANDLED; |
| 1200 | } |
| 1201 | |
| 1202 | static irqreturn_t chg_gone_irq_handler(int irq, void *data) |
| 1203 | { |
| 1204 | struct pm8921_chg_chip *chip = data; |
| 1205 | |
| 1206 | pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1207 | power_supply_changed(&chip->batt_psy); |
| 1208 | power_supply_changed(&chip->usb_psy); |
| 1209 | power_supply_changed(&chip->dc_psy); |
| 1210 | return IRQ_HANDLED; |
| 1211 | } |
| 1212 | |
| 1213 | static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data) |
| 1214 | { |
| 1215 | struct pm8921_chg_chip *chip = data; |
| 1216 | |
| 1217 | pr_debug("batt temp ok fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1218 | power_supply_changed(&chip->batt_psy); |
| 1219 | power_supply_changed(&chip->usb_psy); |
| 1220 | power_supply_changed(&chip->dc_psy); |
| 1221 | return IRQ_HANDLED; |
| 1222 | } |
| 1223 | |
| 1224 | static irqreturn_t coarse_det_low_irq_handler(int irq, void *data) |
| 1225 | { |
| 1226 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1227 | return IRQ_HANDLED; |
| 1228 | } |
| 1229 | |
| 1230 | static irqreturn_t vdd_loop_irq_handler(int irq, void *data) |
| 1231 | { |
| 1232 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1233 | return IRQ_HANDLED; |
| 1234 | } |
| 1235 | |
| 1236 | static irqreturn_t vreg_ov_irq_handler(int irq, void *data) |
| 1237 | { |
| 1238 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1239 | return IRQ_HANDLED; |
| 1240 | } |
| 1241 | |
| 1242 | static irqreturn_t vbatdet_irq_handler(int irq, void *data) |
| 1243 | { |
| 1244 | pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data)); |
| 1245 | return IRQ_HANDLED; |
| 1246 | } |
| 1247 | |
| 1248 | static irqreturn_t batfet_irq_handler(int irq, void *data) |
| 1249 | { |
| 1250 | struct pm8921_chg_chip *chip = data; |
| 1251 | |
| 1252 | pr_debug("vreg ov\n"); |
| 1253 | power_supply_changed(&chip->batt_psy); |
| 1254 | return IRQ_HANDLED; |
| 1255 | } |
| 1256 | |
| 1257 | static irqreturn_t dcin_valid_irq_handler(int irq, void *data) |
| 1258 | { |
| 1259 | handle_dc_removal_insertion(data); |
| 1260 | return IRQ_HANDLED; |
| 1261 | } |
| 1262 | |
| 1263 | static irqreturn_t dcin_ov_irq_handler(int irq, void *data) |
| 1264 | { |
| 1265 | handle_dc_removal_insertion(data); |
| 1266 | return IRQ_HANDLED; |
| 1267 | } |
| 1268 | |
| 1269 | static irqreturn_t dcin_uv_irq_handler(int irq, void *data) |
| 1270 | { |
| 1271 | handle_dc_removal_insertion(data); |
| 1272 | return IRQ_HANDLED; |
| 1273 | } |
| 1274 | |
Abhijeet Dharmapurikar | 1f52c41 | 2011-08-12 17:11:45 -0700 | [diff] [blame] | 1275 | /** |
| 1276 | * set_disable_status_param - |
| 1277 | * |
| 1278 | * Internal function to disable battery charging and also disable drawing |
| 1279 | * any current from the source. The device is forced to run on a battery |
| 1280 | * after this. |
| 1281 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1282 | static int set_disable_status_param(const char *val, struct kernel_param *kp) |
| 1283 | { |
| 1284 | int ret; |
| 1285 | struct pm8921_chg_chip *chip = the_chip; |
| 1286 | |
| 1287 | ret = param_set_int(val, kp); |
| 1288 | if (ret) { |
| 1289 | pr_err("error setting value %d\n", ret); |
| 1290 | return ret; |
| 1291 | } |
| 1292 | pr_info("factory set disable param to %d\n", charging_disabled); |
| 1293 | if (chip) { |
| 1294 | pm_chg_auto_enable(chip, !charging_disabled); |
| 1295 | pm_chg_charge_dis(chip, charging_disabled); |
| 1296 | } |
| 1297 | return 0; |
| 1298 | } |
| 1299 | module_param_call(disabled, set_disable_status_param, param_get_uint, |
| 1300 | &charging_disabled, 0644); |
| 1301 | |
| 1302 | static void free_irqs(struct pm8921_chg_chip *chip) |
| 1303 | { |
| 1304 | int i; |
| 1305 | |
| 1306 | for (i = 0; i < PM_CHG_MAX_INTS; i++) |
| 1307 | if (chip->pmic_chg_irq[i]) { |
| 1308 | free_irq(chip->pmic_chg_irq[i], chip); |
| 1309 | chip->pmic_chg_irq[i] = 0; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | /* determines the initial present states and notifies msm_charger */ |
| 1314 | static void __devinit determine_initial_state(struct pm8921_chg_chip *chip) |
| 1315 | { |
| 1316 | unsigned long flags; |
| 1317 | int fsm_state; |
| 1318 | |
| 1319 | chip->dc_present = !!is_dc_chg_plugged_in(chip); |
| 1320 | chip->usb_present = !!is_usb_chg_plugged_in(chip); |
| 1321 | |
| 1322 | notify_usb_of_the_plugin_event(chip->usb_present); |
| 1323 | |
| 1324 | pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ); |
| 1325 | pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ); |
| 1326 | pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ); |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1327 | pm8921_chg_enable_irq(chip, BATT_INSERTED_IRQ); |
| 1328 | pm8921_chg_enable_irq(chip, USBIN_OV_IRQ); |
| 1329 | pm8921_chg_enable_irq(chip, USBIN_UV_IRQ); |
| 1330 | pm8921_chg_enable_irq(chip, DCIN_OV_IRQ); |
| 1331 | pm8921_chg_enable_irq(chip, DCIN_UV_IRQ); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1332 | pm8921_chg_enable_irq(chip, CHGSTATE_IRQ); |
| 1333 | |
| 1334 | spin_lock_irqsave(&vbus_lock, flags); |
| 1335 | if (usb_chg_current) { |
| 1336 | /* reissue a vbus draw call */ |
| 1337 | __pm8921_charger_vbus_draw(usb_chg_current); |
| 1338 | } |
| 1339 | spin_unlock_irqrestore(&vbus_lock, flags); |
| 1340 | |
| 1341 | fsm_state = pm_chg_get_fsm_state(chip); |
| 1342 | if (is_battery_charging(fsm_state)) { |
| 1343 | chip->bms_notify.is_charging = 1; |
| 1344 | pm8921_bms_charging_began(); |
| 1345 | } |
| 1346 | |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 1347 | check_battery_valid(chip); |
| 1348 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1349 | pr_debug("usb = %d, dc = %d batt = %d state=%d\n", |
| 1350 | chip->usb_present, |
| 1351 | chip->dc_present, |
| 1352 | get_prop_batt_present(chip), |
| 1353 | fsm_state); |
| 1354 | } |
| 1355 | |
| 1356 | struct pm_chg_irq_init_data { |
| 1357 | unsigned int irq_id; |
| 1358 | char *name; |
| 1359 | unsigned long flags; |
| 1360 | irqreturn_t (*handler)(int, void *); |
| 1361 | }; |
| 1362 | |
| 1363 | #define CHG_IRQ(_id, _flags, _handler) \ |
| 1364 | { \ |
| 1365 | .irq_id = _id, \ |
| 1366 | .name = #_id, \ |
| 1367 | .flags = _flags, \ |
| 1368 | .handler = _handler, \ |
| 1369 | } |
| 1370 | struct pm_chg_irq_init_data chg_irq_data[] = { |
| 1371 | CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1372 | usbin_valid_irq_handler), |
| 1373 | CHG_IRQ(USBIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1374 | usbin_ov_irq_handler), |
| 1375 | CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1376 | batt_inserted_irq_handler), |
| 1377 | CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_RISING, vbatdet_low_irq_handler), |
| 1378 | CHG_IRQ(USBIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1379 | usbin_uv_irq_handler), |
| 1380 | CHG_IRQ(VBAT_OV_IRQ, IRQF_TRIGGER_RISING, vbat_ov_irq_handler), |
| 1381 | CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler), |
| 1382 | CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler), |
| 1383 | CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler), |
| 1384 | CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler), |
| 1385 | CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler), |
| 1386 | CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler), |
| 1387 | CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler), |
| 1388 | CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler), |
| 1389 | CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_RISING, fastchg_irq_handler), |
| 1390 | CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler), |
| 1391 | CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING, |
| 1392 | batt_removed_irq_handler), |
| 1393 | CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING, |
| 1394 | batttemp_hot_irq_handler), |
| 1395 | CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler), |
| 1396 | CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING, |
| 1397 | batttemp_cold_irq_handler), |
| 1398 | CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING, chg_gone_irq_handler), |
| 1399 | CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING, bat_temp_ok_irq_handler), |
| 1400 | CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING, |
| 1401 | coarse_det_low_irq_handler), |
| 1402 | CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler), |
| 1403 | CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler), |
| 1404 | CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler), |
| 1405 | CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1406 | batfet_irq_handler), |
| 1407 | CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1408 | dcin_valid_irq_handler), |
| 1409 | CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1410 | dcin_ov_irq_handler), |
| 1411 | CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 1412 | dcin_uv_irq_handler), |
| 1413 | }; |
| 1414 | |
| 1415 | static int __devinit request_irqs(struct pm8921_chg_chip *chip, |
| 1416 | struct platform_device *pdev) |
| 1417 | { |
| 1418 | struct resource *res; |
| 1419 | int ret, i; |
| 1420 | |
| 1421 | ret = 0; |
| 1422 | bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS); |
| 1423 | |
| 1424 | for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) { |
| 1425 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1426 | chg_irq_data[i].name); |
| 1427 | if (res == NULL) { |
| 1428 | pr_err("couldn't find %s\n", chg_irq_data[i].name); |
| 1429 | goto err_out; |
| 1430 | } |
| 1431 | ret = request_irq(res->start, chg_irq_data[i].handler, |
| 1432 | chg_irq_data[i].flags, |
| 1433 | chg_irq_data[i].name, chip); |
| 1434 | if (ret < 0) { |
| 1435 | pr_err("couldn't request %d (%s) %d\n", res->start, |
| 1436 | chg_irq_data[i].name, ret); |
| 1437 | goto err_out; |
| 1438 | } |
| 1439 | chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start; |
| 1440 | pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id); |
| 1441 | } |
| 1442 | return 0; |
| 1443 | |
| 1444 | err_out: |
| 1445 | free_irqs(chip); |
| 1446 | return -EINVAL; |
| 1447 | } |
| 1448 | |
| 1449 | #define ENUM_TIMER_STOP_BIT BIT(1) |
| 1450 | #define BOOT_DONE_BIT BIT(6) |
| 1451 | #define CHG_BATFET_ON_BIT BIT(3) |
| 1452 | #define CHG_VCP_EN BIT(0) |
| 1453 | #define CHG_BAT_TEMP_DIS_BIT BIT(2) |
| 1454 | #define SAFE_CURRENT_MA 1500 |
| 1455 | static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip) |
| 1456 | { |
| 1457 | int rc; |
| 1458 | |
| 1459 | rc = pm_chg_masked_write(chip, SYS_CONFIG_2, |
| 1460 | BOOT_DONE_BIT, BOOT_DONE_BIT); |
| 1461 | if (rc) { |
| 1462 | pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc); |
| 1463 | return rc; |
| 1464 | } |
| 1465 | |
| 1466 | rc = pm_chg_vddsafe_set(chip, chip->max_voltage); |
| 1467 | if (rc) { |
| 1468 | pr_err("Failed to set safe voltage to %d rc=%d\n", |
| 1469 | chip->max_voltage, rc); |
| 1470 | return rc; |
| 1471 | } |
| 1472 | rc = pm_chg_vbatdet_set(chip, chip->resume_voltage); |
| 1473 | if (rc) { |
| 1474 | pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n", |
| 1475 | chip->resume_voltage, rc); |
| 1476 | return rc; |
| 1477 | } |
| 1478 | |
| 1479 | rc = pm_chg_vddmax_set(chip, chip->max_voltage); |
| 1480 | if (rc) { |
| 1481 | pr_err("Failed to set max voltage to %d rc=%d\n", |
| 1482 | chip->max_voltage, rc); |
| 1483 | return rc; |
| 1484 | } |
| 1485 | rc = pm_chg_ibatsafe_set(chip, SAFE_CURRENT_MA); |
| 1486 | if (rc) { |
| 1487 | pr_err("Failed to set max voltage to %d rc=%d\n", |
| 1488 | SAFE_CURRENT_MA, rc); |
| 1489 | return rc; |
| 1490 | } |
| 1491 | |
| 1492 | /* TODO needs to be changed as per the temeperature of the battery */ |
| 1493 | rc = pm_chg_ibatmax_set(chip, 400); |
| 1494 | if (rc) { |
| 1495 | pr_err("Failed to set max current to 400 rc=%d\n", rc); |
| 1496 | return rc; |
| 1497 | } |
| 1498 | |
| 1499 | rc = pm_chg_iterm_set(chip, chip->term_current); |
| 1500 | if (rc) { |
| 1501 | pr_err("Failed to set term current to %d rc=%d\n", |
| 1502 | chip->term_current, rc); |
| 1503 | return rc; |
| 1504 | } |
| 1505 | |
| 1506 | /* Disable the ENUM TIMER */ |
| 1507 | rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT, |
| 1508 | ENUM_TIMER_STOP_BIT); |
| 1509 | if (rc) { |
| 1510 | pr_err("Failed to set enum timer stop rc=%d\n", rc); |
| 1511 | return rc; |
| 1512 | } |
| 1513 | |
| 1514 | /* init with the lowest USB current */ |
| 1515 | rc = pm_chg_iusbmax_set(chip, usb_ma_table[0].chg_iusb_value); |
| 1516 | if (rc) { |
| 1517 | pr_err("Failed to set usb max to %d rc=%d\n", |
| 1518 | usb_ma_table[0].chg_iusb_value, rc); |
| 1519 | return rc; |
| 1520 | } |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1521 | |
| 1522 | if (chip->safety_time != 0) { |
| 1523 | rc = pm_chg_tchg_max_set(chip, chip->safety_time); |
| 1524 | if (rc) { |
| 1525 | pr_err("Failed to set max time to %d minutes rc=%d\n", |
| 1526 | chip->safety_time, rc); |
| 1527 | return rc; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | if (chip->ttrkl_time != 0) { |
| 1532 | rc = pm_chg_ttrkl_max_set(chip, chip->safety_time); |
| 1533 | if (rc) { |
| 1534 | pr_err("Failed to set trkl time to %d minutes rc=%d\n", |
| 1535 | chip->safety_time, rc); |
| 1536 | return rc; |
| 1537 | } |
| 1538 | } |
| 1539 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1540 | rc = pm_chg_disable_wd(chip); |
| 1541 | if (rc) { |
| 1542 | pr_err("Failed to disable wd rc=%d\n", rc); |
| 1543 | return rc; |
| 1544 | } |
| 1545 | |
| 1546 | rc = pm_chg_masked_write(chip, CHG_CNTRL_2, |
| 1547 | CHG_BAT_TEMP_DIS_BIT, 0); |
| 1548 | if (rc) { |
| 1549 | pr_err("Failed to enable temp control chg rc=%d\n", rc); |
| 1550 | return rc; |
| 1551 | } |
| 1552 | /* switch to a 3.2Mhz for the buck */ |
| 1553 | rc = pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CLOCK_CTRL, 0x15); |
| 1554 | if (rc) { |
| 1555 | pr_err("Failed to switch buck clk rc=%d\n", rc); |
| 1556 | return rc; |
| 1557 | } |
| 1558 | |
| 1559 | /* Workarounds for die 1.1 and 1.0 */ |
| 1560 | if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) { |
| 1561 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST2, 0xF1); |
| 1562 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0x8C); |
| 1563 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xCE); |
| 1564 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD8); |
Abhijeet Dharmapurikar | 305d702 | 2011-08-10 17:07:44 -0700 | [diff] [blame] | 1565 | pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xF1); |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 1566 | |
| 1567 | /* software workaround for correct battery_id detection */ |
| 1568 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_0, 0xFF); |
| 1569 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_1, 0xFF); |
| 1570 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_2, 0xFF); |
| 1571 | pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_3, 0xFF); |
| 1572 | pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0D); |
| 1573 | udelay(100); |
| 1574 | pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0C); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | rc = pm_chg_charge_dis(chip, charging_disabled); |
| 1578 | if (rc) { |
| 1579 | pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc); |
| 1580 | return rc; |
| 1581 | } |
| 1582 | |
| 1583 | rc = pm_chg_auto_enable(chip, !charging_disabled); |
| 1584 | if (rc) { |
| 1585 | pr_err("Failed to enable charging rc=%d\n", rc); |
| 1586 | return rc; |
| 1587 | } |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | static int get_rt_status(void *data, u64 * val) |
| 1593 | { |
| 1594 | int i = (int)data; |
| 1595 | int ret; |
| 1596 | |
| 1597 | /* global irq number is passed in via data */ |
| 1598 | ret = pm_chg_get_rt_status(the_chip, i); |
| 1599 | *val = ret; |
| 1600 | return 0; |
| 1601 | } |
| 1602 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 1603 | |
| 1604 | static int get_fsm_status(void *data, u64 * val) |
| 1605 | { |
| 1606 | u8 temp; |
| 1607 | |
| 1608 | temp = pm_chg_get_fsm_state(the_chip); |
| 1609 | *val = temp; |
| 1610 | return 0; |
| 1611 | } |
| 1612 | DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n"); |
| 1613 | |
| 1614 | static int get_reg(void *data, u64 * val) |
| 1615 | { |
| 1616 | int addr = (int)data; |
| 1617 | int ret; |
| 1618 | u8 temp; |
| 1619 | |
| 1620 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 1621 | if (ret) { |
| 1622 | pr_err("pm8xxx_readb to %x value =%d errored = %d\n", |
| 1623 | addr, temp, ret); |
| 1624 | return -EAGAIN; |
| 1625 | } |
| 1626 | *val = temp; |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | static int set_reg(void *data, u64 val) |
| 1631 | { |
| 1632 | int addr = (int)data; |
| 1633 | int ret; |
| 1634 | u8 temp; |
| 1635 | |
| 1636 | temp = (u8) val; |
| 1637 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 1638 | if (ret) { |
| 1639 | pr_err("pm8xxx_writeb to %x value =%d errored = %d\n", |
| 1640 | addr, temp, ret); |
| 1641 | return -EAGAIN; |
| 1642 | } |
| 1643 | return 0; |
| 1644 | } |
| 1645 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 1646 | |
| 1647 | static void create_debugfs_entries(struct pm8921_chg_chip *chip) |
| 1648 | { |
| 1649 | int i; |
| 1650 | |
| 1651 | chip->dent = debugfs_create_dir("pm8921_chg", NULL); |
| 1652 | |
| 1653 | if (IS_ERR(chip->dent)) { |
| 1654 | pr_err("pmic charger couldnt create debugfs dir\n"); |
| 1655 | return; |
| 1656 | } |
| 1657 | |
| 1658 | debugfs_create_file("CHG_CNTRL", 0644, chip->dent, |
| 1659 | (void *)CHG_CNTRL, ®_fops); |
| 1660 | debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent, |
| 1661 | (void *)CHG_CNTRL_2, ®_fops); |
| 1662 | debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent, |
| 1663 | (void *)CHG_CNTRL_3, ®_fops); |
| 1664 | debugfs_create_file("PBL_ACCESS1", 0644, chip->dent, |
| 1665 | (void *)PBL_ACCESS1, ®_fops); |
| 1666 | debugfs_create_file("PBL_ACCESS2", 0644, chip->dent, |
| 1667 | (void *)PBL_ACCESS2, ®_fops); |
| 1668 | debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent, |
| 1669 | (void *)SYS_CONFIG_1, ®_fops); |
| 1670 | debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent, |
| 1671 | (void *)SYS_CONFIG_2, ®_fops); |
| 1672 | debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent, |
| 1673 | (void *)CHG_VDD_MAX, ®_fops); |
| 1674 | debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent, |
| 1675 | (void *)CHG_VDD_SAFE, ®_fops); |
| 1676 | debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent, |
| 1677 | (void *)CHG_VBAT_DET, ®_fops); |
| 1678 | debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent, |
| 1679 | (void *)CHG_IBAT_MAX, ®_fops); |
| 1680 | debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent, |
| 1681 | (void *)CHG_IBAT_SAFE, ®_fops); |
| 1682 | debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent, |
| 1683 | (void *)CHG_VIN_MIN, ®_fops); |
| 1684 | debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent, |
| 1685 | (void *)CHG_VTRICKLE, ®_fops); |
| 1686 | debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent, |
| 1687 | (void *)CHG_ITRICKLE, ®_fops); |
| 1688 | debugfs_create_file("CHG_ITERM", 0644, chip->dent, |
| 1689 | (void *)CHG_ITERM, ®_fops); |
| 1690 | debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent, |
| 1691 | (void *)CHG_TCHG_MAX, ®_fops); |
| 1692 | debugfs_create_file("CHG_TWDOG", 0644, chip->dent, |
| 1693 | (void *)CHG_TWDOG, ®_fops); |
| 1694 | debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent, |
| 1695 | (void *)CHG_TEMP_THRESH, ®_fops); |
| 1696 | debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent, |
| 1697 | (void *)CHG_COMP_OVR, ®_fops); |
| 1698 | debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent, |
| 1699 | (void *)CHG_BUCK_CTRL_TEST1, ®_fops); |
| 1700 | debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent, |
| 1701 | (void *)CHG_BUCK_CTRL_TEST2, ®_fops); |
| 1702 | debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent, |
| 1703 | (void *)CHG_BUCK_CTRL_TEST3, ®_fops); |
| 1704 | debugfs_create_file("CHG_TEST", 0644, chip->dent, |
| 1705 | (void *)CHG_TEST, ®_fops); |
| 1706 | |
| 1707 | debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL, |
| 1708 | &fsm_fops); |
| 1709 | |
| 1710 | for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) { |
| 1711 | if (chip->pmic_chg_irq[chg_irq_data[i].irq_id]) |
| 1712 | debugfs_create_file(chg_irq_data[i].name, 0444, |
| 1713 | chip->dent, |
| 1714 | (void *)chg_irq_data[i].irq_id, |
| 1715 | &rt_fops); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | static int __devinit pm8921_charger_probe(struct platform_device *pdev) |
| 1720 | { |
| 1721 | int rc = 0; |
| 1722 | struct pm8921_chg_chip *chip; |
| 1723 | const struct pm8921_charger_platform_data *pdata |
| 1724 | = pdev->dev.platform_data; |
| 1725 | |
| 1726 | if (!pdata) { |
| 1727 | pr_err("missing platform data\n"); |
| 1728 | return -EINVAL; |
| 1729 | } |
| 1730 | |
| 1731 | chip = kzalloc(sizeof(struct pm8921_chg_chip), |
| 1732 | GFP_KERNEL); |
| 1733 | if (!chip) { |
| 1734 | pr_err("Cannot allocate pm_chg_chip\n"); |
| 1735 | return -ENOMEM; |
| 1736 | } |
| 1737 | |
| 1738 | chip->dev = &pdev->dev; |
| 1739 | chip->safety_time = pdata->safety_time; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1740 | chip->ttrkl_time = pdata->ttrkl_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1741 | chip->update_time = pdata->update_time; |
| 1742 | chip->max_voltage = pdata->max_voltage; |
| 1743 | chip->min_voltage = pdata->min_voltage; |
| 1744 | chip->resume_voltage = pdata->resume_voltage; |
| 1745 | chip->term_current = pdata->term_current; |
| 1746 | chip->vbat_channel = pdata->charger_cdata.vbat_channel; |
Abhijeet Dharmapurikar | b24e2c3 | 2011-08-17 17:13:09 -0700 | [diff] [blame] | 1747 | chip->batt_temp_channel = pdata->charger_cdata.batt_temp_channel; |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 1748 | chip->batt_id_channel = pdata->charger_cdata.batt_id_channel; |
| 1749 | chip->batt_id_min = pdata->batt_id_min; |
| 1750 | chip->batt_id_max = pdata->batt_id_max; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1751 | |
| 1752 | rc = pm8921_chg_hw_init(chip); |
| 1753 | if (rc) { |
| 1754 | pr_err("couldn't init hardware rc=%d\n", rc); |
| 1755 | goto free_chip; |
| 1756 | } |
| 1757 | |
| 1758 | chip->usb_psy.name = "usb", |
| 1759 | chip->usb_psy.type = POWER_SUPPLY_TYPE_USB, |
| 1760 | chip->usb_psy.supplied_to = pm_power_supplied_to, |
| 1761 | chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to), |
| 1762 | chip->usb_psy.properties = pm_power_props, |
| 1763 | chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props), |
| 1764 | chip->usb_psy.get_property = pm_power_get_property, |
| 1765 | |
| 1766 | chip->dc_psy.name = "ac", |
| 1767 | chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS, |
| 1768 | chip->dc_psy.supplied_to = pm_power_supplied_to, |
| 1769 | chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to), |
| 1770 | chip->dc_psy.properties = pm_power_props, |
| 1771 | chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props), |
| 1772 | chip->dc_psy.get_property = pm_power_get_property, |
| 1773 | |
| 1774 | chip->batt_psy.name = "battery", |
| 1775 | chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY, |
| 1776 | chip->batt_psy.properties = msm_batt_power_props, |
| 1777 | chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props), |
| 1778 | chip->batt_psy.get_property = pm_batt_power_get_property, |
| 1779 | |
| 1780 | rc = power_supply_register(chip->dev, &chip->usb_psy); |
| 1781 | if (rc < 0) { |
| 1782 | pr_err("power_supply_register usb failed rc = %d\n", rc); |
| 1783 | goto free_irq; |
| 1784 | } |
| 1785 | |
| 1786 | rc = power_supply_register(chip->dev, &chip->dc_psy); |
| 1787 | if (rc < 0) { |
| 1788 | pr_err("power_supply_register dc failed rc = %d\n", rc); |
| 1789 | goto unregister_usb; |
| 1790 | } |
| 1791 | |
| 1792 | rc = power_supply_register(chip->dev, &chip->batt_psy); |
| 1793 | if (rc < 0) { |
| 1794 | pr_err("power_supply_register batt failed rc = %d\n", rc); |
| 1795 | goto unregister_dc; |
| 1796 | } |
| 1797 | |
| 1798 | rc = request_irqs(chip, pdev); |
| 1799 | if (rc) { |
| 1800 | pr_err("couldn't register interrupts rc=%d\n", rc); |
| 1801 | goto unregister_batt; |
| 1802 | } |
| 1803 | |
Abhijeet Dharmapurikar | e27d809 | 2011-08-12 17:16:29 -0700 | [diff] [blame] | 1804 | enable_irq_wake(chip->pmic_chg_irq[USBIN_VALID_IRQ]); |
| 1805 | enable_irq_wake(chip->pmic_chg_irq[USBIN_OV_IRQ]); |
| 1806 | enable_irq_wake(chip->pmic_chg_irq[USBIN_UV_IRQ]); |
| 1807 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1808 | platform_set_drvdata(pdev, chip); |
| 1809 | the_chip = chip; |
| 1810 | create_debugfs_entries(chip); |
| 1811 | |
| 1812 | INIT_WORK(&chip->bms_notify.work, bms_notify); |
Abhijeet Dharmapurikar | be6bd8c | 2011-08-19 12:15:06 -0700 | [diff] [blame^] | 1813 | INIT_WORK(&chip->battery_id_valid_work, battery_id_valid); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1814 | /* determine what state the charger is in */ |
| 1815 | determine_initial_state(chip); |
| 1816 | |
| 1817 | return 0; |
| 1818 | |
| 1819 | free_irq: |
| 1820 | free_irqs(chip); |
| 1821 | unregister_batt: |
| 1822 | power_supply_unregister(&chip->batt_psy); |
| 1823 | unregister_dc: |
| 1824 | power_supply_unregister(&chip->dc_psy); |
| 1825 | unregister_usb: |
| 1826 | power_supply_unregister(&chip->usb_psy); |
| 1827 | free_chip: |
| 1828 | kfree(chip); |
| 1829 | return rc; |
| 1830 | } |
| 1831 | |
| 1832 | static int __devexit pm8921_charger_remove(struct platform_device *pdev) |
| 1833 | { |
| 1834 | struct pm8921_chg_chip *chip = platform_get_drvdata(pdev); |
| 1835 | |
| 1836 | free_irqs(chip); |
| 1837 | platform_set_drvdata(pdev, NULL); |
| 1838 | the_chip = NULL; |
| 1839 | kfree(chip); |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
| 1843 | static struct platform_driver pm8921_charger_driver = { |
| 1844 | .probe = pm8921_charger_probe, |
| 1845 | .remove = __devexit_p(pm8921_charger_remove), |
| 1846 | .driver = { |
| 1847 | .name = PM8921_CHARGER_DEV_NAME, |
| 1848 | .owner = THIS_MODULE, |
| 1849 | }, |
| 1850 | }; |
| 1851 | |
| 1852 | static int __init pm8921_charger_init(void) |
| 1853 | { |
| 1854 | return platform_driver_register(&pm8921_charger_driver); |
| 1855 | } |
| 1856 | |
| 1857 | static void __exit pm8921_charger_exit(void) |
| 1858 | { |
| 1859 | platform_driver_unregister(&pm8921_charger_driver); |
| 1860 | } |
| 1861 | |
| 1862 | late_initcall(pm8921_charger_init); |
| 1863 | module_exit(pm8921_charger_exit); |
| 1864 | |
| 1865 | MODULE_LICENSE("GPL v2"); |
| 1866 | MODULE_DESCRIPTION("PMIC8921 charger/battery driver"); |
| 1867 | MODULE_VERSION("1.0"); |
| 1868 | MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME); |