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