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