David Keitel | a8099ff | 2014-01-06 11:31:40 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 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/slab.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/spmi.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/radix-tree.h> |
| 22 | #include <linux/interrupt.h> |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 24 | #include <linux/qpnp/qpnp-adc.h> |
| 25 | #include <linux/power_supply.h> |
| 26 | #include <linux/bitops.h> |
Abhijeet Dharmapurikar | 53ce35a | 2013-03-29 16:14:16 -0700 | [diff] [blame] | 27 | #include <linux/ratelimit.h> |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 28 | #include <linux/regulator/driver.h> |
| 29 | #include <linux/regulator/of_regulator.h> |
| 30 | #include <linux/regulator/machine.h> |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 31 | #include <linux/of_batterydata.h> |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 32 | #include <linux/qpnp-revid.h> |
| 33 | #include <linux/android_alarm.h> |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 35 | #include <linux/gpio.h> |
| 36 | #include <linux/of_gpio.h> |
| 37 | #include <linux/qpnp/pin.h> |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 38 | |
| 39 | /* Interrupt offsets */ |
| 40 | #define INT_RT_STS(base) (base + 0x10) |
| 41 | #define INT_SET_TYPE(base) (base + 0x11) |
| 42 | #define INT_POLARITY_HIGH(base) (base + 0x12) |
| 43 | #define INT_POLARITY_LOW(base) (base + 0x13) |
| 44 | #define INT_LATCHED_CLR(base) (base + 0x14) |
| 45 | #define INT_EN_SET(base) (base + 0x15) |
| 46 | #define INT_EN_CLR(base) (base + 0x16) |
| 47 | #define INT_LATCHED_STS(base) (base + 0x18) |
| 48 | #define INT_PENDING_STS(base) (base + 0x19) |
| 49 | #define INT_MID_SEL(base) (base + 0x1A) |
| 50 | #define INT_PRIORITY(base) (base + 0x1B) |
| 51 | |
| 52 | /* Peripheral register offsets */ |
| 53 | #define CHGR_CHG_OPTION 0x08 |
| 54 | #define CHGR_ATC_STATUS 0x0A |
| 55 | #define CHGR_VBAT_STATUS 0x0B |
| 56 | #define CHGR_IBAT_BMS 0x0C |
| 57 | #define CHGR_IBAT_STS 0x0D |
| 58 | #define CHGR_VDD_MAX 0x40 |
| 59 | #define CHGR_VDD_SAFE 0x41 |
| 60 | #define CHGR_VDD_MAX_STEP 0x42 |
| 61 | #define CHGR_IBAT_MAX 0x44 |
| 62 | #define CHGR_IBAT_SAFE 0x45 |
| 63 | #define CHGR_VIN_MIN 0x47 |
| 64 | #define CHGR_VIN_MIN_STEP 0x48 |
| 65 | #define CHGR_CHG_CTRL 0x49 |
| 66 | #define CHGR_CHG_FAILED 0x4A |
| 67 | #define CHGR_ATC_CTRL 0x4B |
| 68 | #define CHGR_ATC_FAILED 0x4C |
| 69 | #define CHGR_VBAT_TRKL 0x50 |
| 70 | #define CHGR_VBAT_WEAK 0x52 |
| 71 | #define CHGR_IBAT_ATC_A 0x54 |
| 72 | #define CHGR_IBAT_ATC_B 0x55 |
| 73 | #define CHGR_IBAT_TERM_CHGR 0x5B |
| 74 | #define CHGR_IBAT_TERM_BMS 0x5C |
| 75 | #define CHGR_VBAT_DET 0x5D |
Abhijeet Dharmapurikar | 11e6156 | 2013-11-19 12:01:29 -0800 | [diff] [blame] | 76 | #define CHGR_TTRKL_MAX_EN 0x5E |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 77 | #define CHGR_TTRKL_MAX 0x5F |
Abhijeet Dharmapurikar | 11e6156 | 2013-11-19 12:01:29 -0800 | [diff] [blame] | 78 | #define CHGR_TCHG_MAX_EN 0x60 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 79 | #define CHGR_TCHG_MAX 0x61 |
| 80 | #define CHGR_CHG_WDOG_TIME 0x62 |
| 81 | #define CHGR_CHG_WDOG_DLY 0x63 |
| 82 | #define CHGR_CHG_WDOG_PET 0x64 |
| 83 | #define CHGR_CHG_WDOG_EN 0x65 |
David Keitel | 9201df3 | 2013-01-10 18:38:34 -0800 | [diff] [blame] | 84 | #define CHGR_IR_DROP_COMPEN 0x67 |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 85 | #define CHGR_I_MAX_REG 0x44 |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 86 | #define CHGR_USB_USB_SUSP 0x47 |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 87 | #define CHGR_USB_USB_OTG_CTL 0x48 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 88 | #define CHGR_USB_ENUM_T_STOP 0x4E |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 89 | #define CHGR_USB_TRIM 0xF1 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 90 | #define CHGR_CHG_TEMP_THRESH 0x66 |
| 91 | #define CHGR_BAT_IF_PRES_STATUS 0x08 |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 92 | #define CHGR_STATUS 0x09 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 93 | #define CHGR_BAT_IF_VCP 0x42 |
| 94 | #define CHGR_BAT_IF_BATFET_CTRL1 0x90 |
David Keitel | 9eca8ac | 2013-09-06 14:18:36 -0700 | [diff] [blame] | 95 | #define CHGR_BAT_IF_BATFET_CTRL4 0x93 |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 96 | #define CHGR_BAT_IF_SPARE 0xDF |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 97 | #define CHGR_MISC_BOOT_DONE 0x42 |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 98 | #define CHGR_BUCK_PSTG_CTRL 0x73 |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 99 | #define CHGR_BUCK_COMPARATOR_OVRIDE_1 0xEB |
Fenglin Wu | c64e994 | 2014-08-22 13:47:34 +0800 | [diff] [blame] | 100 | #define CHGR_BUCK_COMPARATOR_OVRIDE_2 0xEC |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 101 | #define CHGR_BUCK_COMPARATOR_OVRIDE_3 0xED |
David Keitel | 79e0832 | 2014-04-21 14:17:05 -0700 | [diff] [blame] | 102 | #define CHG_OVR0 0xED |
| 103 | #define CHG_TRICKLE_CLAMP 0xE3 |
David Keitel | 9201df3 | 2013-01-10 18:38:34 -0800 | [diff] [blame] | 104 | #define CHGR_BUCK_BCK_VBAT_REG_MODE 0x74 |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 105 | #define MISC_REVISION2 0x01 |
David Keitel | 5c3a770 | 2012-12-20 11:13:21 -0800 | [diff] [blame] | 106 | #define USB_OVP_CTL 0x42 |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 107 | #define USB_CHG_GONE_REV_BST 0xED |
| 108 | #define BUCK_VCHG_OV 0x77 |
| 109 | #define BUCK_TEST_SMBC_MODES 0xE6 |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 110 | #define BUCK_CTRL_TRIM1 0xF1 |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 111 | #define BUCK_CTRL_TRIM3 0xF3 |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 112 | #define SEC_ACCESS 0xD0 |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 113 | #define BAT_IF_VREF_BAT_THM_CTRL 0x4A |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 114 | #define BAT_IF_BPD_CTRL 0x48 |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 115 | #define BOOST_VSET 0x41 |
| 116 | #define BOOST_ENABLE_CONTROL 0x46 |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 117 | #define COMP_OVR1 0xEA |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 118 | #define BAT_IF_BTC_CTRL 0x49 |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 119 | #define USB_OCP_THR 0x52 |
| 120 | #define USB_OCP_CLR 0x53 |
Zhenhua Huang | 2a0ee35 | 2013-07-23 01:55:43 +0800 | [diff] [blame] | 121 | #define BAT_IF_TEMP_STATUS 0x09 |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 122 | #define BOOST_ILIM 0x78 |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 123 | #define USB_SPARE 0xDF |
| 124 | #define DC_COMP_OVR1 0xE9 |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 125 | #define CHGR_COMP_OVR1 0xEE |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 126 | #define USB_CHGPTH_CTL 0x40 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 127 | #define REG_OFFSET_PERP_SUBTYPE 0x05 |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 128 | |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 129 | /* SMBB peripheral subtype values */ |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 130 | #define SMBB_CHGR_SUBTYPE 0x01 |
| 131 | #define SMBB_BUCK_SUBTYPE 0x02 |
| 132 | #define SMBB_BAT_IF_SUBTYPE 0x03 |
| 133 | #define SMBB_USB_CHGPTH_SUBTYPE 0x04 |
| 134 | #define SMBB_DC_CHGPTH_SUBTYPE 0x05 |
| 135 | #define SMBB_BOOST_SUBTYPE 0x06 |
| 136 | #define SMBB_MISC_SUBTYPE 0x07 |
| 137 | |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 138 | /* SMBB peripheral subtype values */ |
| 139 | #define SMBBP_CHGR_SUBTYPE 0x31 |
| 140 | #define SMBBP_BUCK_SUBTYPE 0x32 |
| 141 | #define SMBBP_BAT_IF_SUBTYPE 0x33 |
| 142 | #define SMBBP_USB_CHGPTH_SUBTYPE 0x34 |
| 143 | #define SMBBP_BOOST_SUBTYPE 0x36 |
| 144 | #define SMBBP_MISC_SUBTYPE 0x37 |
| 145 | |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 146 | /* SMBCL peripheral subtype values */ |
| 147 | #define SMBCL_CHGR_SUBTYPE 0x41 |
| 148 | #define SMBCL_BUCK_SUBTYPE 0x42 |
| 149 | #define SMBCL_BAT_IF_SUBTYPE 0x43 |
| 150 | #define SMBCL_USB_CHGPTH_SUBTYPE 0x44 |
| 151 | #define SMBCL_MISC_SUBTYPE 0x47 |
| 152 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 153 | #define QPNP_CHARGER_DEV_NAME "qcom,qpnp-charger" |
| 154 | |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 155 | /* Status bits and masks */ |
| 156 | #define CHGR_BOOT_DONE BIT(7) |
| 157 | #define CHGR_CHG_EN BIT(7) |
| 158 | #define CHGR_ON_BAT_FORCE_BIT BIT(0) |
David Keitel | 5c3a770 | 2012-12-20 11:13:21 -0800 | [diff] [blame] | 159 | #define USB_VALID_DEB_20MS 0x03 |
David Keitel | 9201df3 | 2013-01-10 18:38:34 -0800 | [diff] [blame] | 160 | #define BUCK_VBAT_REG_NODE_SEL_BIT BIT(0) |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 161 | #define VREF_BATT_THERM_FORCE_ON 0xC0 |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 162 | #define BAT_IF_BPD_CTRL_SEL 0x03 |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 163 | #define VREF_BAT_THM_ENABLED_FSM 0x80 |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 164 | #define REV_BST_DETECTED BIT(0) |
| 165 | #define BAT_THM_EN BIT(1) |
| 166 | #define BAT_ID_EN BIT(0) |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 167 | #define BOOST_PWR_EN BIT(7) |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 168 | #define OCP_CLR_BIT BIT(7) |
| 169 | #define OCP_THR_MASK 0x03 |
| 170 | #define OCP_THR_900_MA 0x02 |
| 171 | #define OCP_THR_500_MA 0x01 |
| 172 | #define OCP_THR_200_MA 0x00 |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 173 | #define DC_HIGHER_PRIORITY BIT(7) |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 174 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 175 | /* Interrupt definitions */ |
| 176 | /* smbb_chg_interrupts */ |
| 177 | #define CHG_DONE_IRQ BIT(7) |
| 178 | #define CHG_FAILED_IRQ BIT(6) |
| 179 | #define FAST_CHG_ON_IRQ BIT(5) |
| 180 | #define TRKL_CHG_ON_IRQ BIT(4) |
| 181 | #define STATE_CHANGE_ON_IR BIT(3) |
| 182 | #define CHGWDDOG_IRQ BIT(2) |
| 183 | #define VBAT_DET_HI_IRQ BIT(1) |
| 184 | #define VBAT_DET_LOW_IRQ BIT(0) |
| 185 | |
| 186 | /* smbb_buck_interrupts */ |
| 187 | #define VDD_LOOP_IRQ BIT(6) |
| 188 | #define IBAT_LOOP_IRQ BIT(5) |
| 189 | #define ICHG_LOOP_IRQ BIT(4) |
| 190 | #define VCHG_LOOP_IRQ BIT(3) |
| 191 | #define OVERTEMP_IRQ BIT(2) |
| 192 | #define VREF_OV_IRQ BIT(1) |
| 193 | #define VBAT_OV_IRQ BIT(0) |
| 194 | |
| 195 | /* smbb_bat_if_interrupts */ |
| 196 | #define PSI_IRQ BIT(4) |
| 197 | #define VCP_ON_IRQ BIT(3) |
| 198 | #define BAT_FET_ON_IRQ BIT(2) |
| 199 | #define BAT_TEMP_OK_IRQ BIT(1) |
| 200 | #define BATT_PRES_IRQ BIT(0) |
| 201 | |
| 202 | /* smbb_usb_interrupts */ |
| 203 | #define CHG_GONE_IRQ BIT(2) |
| 204 | #define USBIN_VALID_IRQ BIT(1) |
| 205 | #define COARSE_DET_USB_IRQ BIT(0) |
| 206 | |
| 207 | /* smbb_dc_interrupts */ |
| 208 | #define DCIN_VALID_IRQ BIT(1) |
| 209 | #define COARSE_DET_DC_IRQ BIT(0) |
| 210 | |
| 211 | /* smbb_boost_interrupts */ |
| 212 | #define LIMIT_ERROR_IRQ BIT(1) |
| 213 | #define BOOST_PWR_OK_IRQ BIT(0) |
| 214 | |
| 215 | /* smbb_misc_interrupts */ |
| 216 | #define TFTWDOG_IRQ BIT(0) |
| 217 | |
David Keitel | fe51cb9 | 2013-04-02 19:42:58 -0700 | [diff] [blame] | 218 | /* SMBB types */ |
| 219 | #define SMBB BIT(1) |
| 220 | #define SMBBP BIT(2) |
| 221 | #define SMBCL BIT(3) |
| 222 | |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 223 | /* Workaround flags */ |
| 224 | #define CHG_FLAGS_VCP_WA BIT(0) |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 225 | #define BOOST_FLASH_WA BIT(1) |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 226 | #define POWER_STAGE_WA BIT(2) |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 227 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 228 | struct qpnp_chg_irq { |
David Keitel | 9879e2a | 2013-06-28 18:40:22 -0700 | [diff] [blame] | 229 | int irq; |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 230 | unsigned long disabled; |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 231 | unsigned long wake_enable; |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 234 | struct qpnp_chg_regulator { |
| 235 | struct regulator_desc rdesc; |
| 236 | struct regulator_dev *rdev; |
| 237 | }; |
| 238 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 239 | /** |
| 240 | * struct qpnp_chg_chip - device information |
| 241 | * @dev: device pointer to access the parent |
| 242 | * @spmi: spmi pointer to access spmi information |
| 243 | * @chgr_base: charger peripheral base address |
| 244 | * @buck_base: buck peripheral base address |
| 245 | * @bat_if_base: battery interface peripheral base address |
| 246 | * @usb_chgpth_base: USB charge path peripheral base address |
| 247 | * @dc_chgpth_base: DC charge path peripheral base address |
| 248 | * @boost_base: boost peripheral base address |
| 249 | * @misc_base: misc peripheral base address |
| 250 | * @freq_base: freq peripheral base address |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 251 | * @bat_is_cool: indicates that battery is cool |
| 252 | * @bat_is_warm: indicates that battery is warm |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 253 | * @chg_done: indicates that charging is completed |
| 254 | * @usb_present: present status of usb |
| 255 | * @dc_present: present status of dc |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 256 | * @batt_present: present status of battery |
David Keitel | 3dd5e0f | 2012-12-12 18:12:36 -0800 | [diff] [blame] | 257 | * @use_default_batt_values: flag to report default battery properties |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 258 | * @btc_disabled Flag to disable btc (disables hot and cold irqs) |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 259 | * @max_voltage_mv: the max volts the batt should be charged up to |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 260 | * @min_voltage_mv: min battery voltage before turning the FET on |
Prasad Sodagudi | c367801 | 2013-09-23 12:01:19 +0530 | [diff] [blame] | 261 | * @batt_weak_voltage_mv: Weak battery voltage threshold |
David Keitel | bdbae45 | 2014-01-14 17:19:50 -0800 | [diff] [blame] | 262 | * @vbatdet_max_err_mv resume voltage hysterisis |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 263 | * @max_bat_chg_current: maximum battery charge current in mA |
| 264 | * @warm_bat_chg_ma: warm battery maximum charge current in mA |
| 265 | * @cool_bat_chg_ma: cool battery maximum charge current in mA |
| 266 | * @warm_bat_mv: warm temperature battery target voltage |
| 267 | * @cool_bat_mv: cool temperature battery target voltage |
| 268 | * @resume_delta_mv: voltage delta at which battery resumes charging |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 269 | * @term_current: the charging based term current |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 270 | * @safe_current: battery safety current setting |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 271 | * @maxinput_usb_ma: Maximum Input current USB |
| 272 | * @maxinput_dc_ma: Maximum Input current DC |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 273 | * @hot_batt_p Hot battery threshold setting |
| 274 | * @cold_batt_p Cold battery threshold setting |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 275 | * @warm_bat_decidegc Warm battery temperature in degree Celsius |
| 276 | * @cool_bat_decidegc Cool battery temperature in degree Celsius |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 277 | * @revision: PMIC revision |
David Keitel | fe51cb9 | 2013-04-02 19:42:58 -0700 | [diff] [blame] | 278 | * @type: SMBB type |
| 279 | * @tchg_mins maximum allowed software initiated charge time |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 280 | * @thermal_levels amount of thermal mitigation levels |
| 281 | * @thermal_mitigation thermal mitigation level values |
| 282 | * @therm_lvl_sel thermal mitigation level selection |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 283 | * @dc_psy power supply to export information to userspace |
| 284 | * @usb_psy power supply to export information to userspace |
| 285 | * @bms_psy power supply to export information to userspace |
| 286 | * @batt_psy: power supply to export information to userspace |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 287 | * @flags: flags to activate specific workarounds |
| 288 | * throughout the driver |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 289 | * |
| 290 | */ |
| 291 | struct qpnp_chg_chip { |
| 292 | struct device *dev; |
| 293 | struct spmi_device *spmi; |
| 294 | u16 chgr_base; |
| 295 | u16 buck_base; |
| 296 | u16 bat_if_base; |
| 297 | u16 usb_chgpth_base; |
| 298 | u16 dc_chgpth_base; |
| 299 | u16 boost_base; |
| 300 | u16 misc_base; |
| 301 | u16 freq_base; |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 302 | struct qpnp_chg_irq usbin_valid; |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 303 | struct qpnp_chg_irq usb_ocp; |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 304 | struct qpnp_chg_irq dcin_valid; |
| 305 | struct qpnp_chg_irq chg_gone; |
| 306 | struct qpnp_chg_irq chg_fastchg; |
| 307 | struct qpnp_chg_irq chg_trklchg; |
| 308 | struct qpnp_chg_irq chg_failed; |
| 309 | struct qpnp_chg_irq chg_vbatdet_lo; |
| 310 | struct qpnp_chg_irq batt_pres; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 311 | struct qpnp_chg_irq batt_temp_ok; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 312 | struct qpnp_chg_irq coarse_det_usb; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 313 | bool bat_is_cool; |
| 314 | bool bat_is_warm; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 315 | bool chg_done; |
David Keitel | 7ac385e | 2013-08-19 18:02:40 -0700 | [diff] [blame] | 316 | bool charger_monitor_checked; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 317 | bool usb_present; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 318 | u8 usbin_health; |
| 319 | bool usb_coarse_det; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 320 | bool dc_present; |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 321 | bool batt_present; |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 322 | bool charging_disabled; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 323 | bool ovp_monitor_enable; |
| 324 | bool usb_valid_check_ovp; |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 325 | bool btc_disabled; |
David Keitel | 3dd5e0f | 2012-12-12 18:12:36 -0800 | [diff] [blame] | 326 | bool use_default_batt_values; |
David Keitel | 8b68d2d | 2013-05-14 23:36:51 -0700 | [diff] [blame] | 327 | bool duty_cycle_100p; |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 328 | bool ibat_calibration_enabled; |
| 329 | bool aicl_settled; |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 330 | bool use_external_rsense; |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 331 | bool fastchg_on; |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 332 | bool parallel_ovp_mode; |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 333 | unsigned int bpd_detection; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 334 | unsigned int max_bat_chg_current; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 335 | unsigned int warm_bat_chg_ma; |
| 336 | unsigned int cool_bat_chg_ma; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 337 | unsigned int safe_voltage_mv; |
| 338 | unsigned int max_voltage_mv; |
| 339 | unsigned int min_voltage_mv; |
Prasad Sodagudi | c367801 | 2013-09-23 12:01:19 +0530 | [diff] [blame] | 340 | unsigned int batt_weak_voltage_mv; |
David Keitel | bdbae45 | 2014-01-14 17:19:50 -0800 | [diff] [blame] | 341 | unsigned int vbatdet_max_err_mv; |
David Keitel | fd305ee | 2013-07-19 20:19:06 -0700 | [diff] [blame] | 342 | int prev_usb_max_ma; |
Xiaozhe Shi | 79a8c49 | 2013-05-24 13:09:23 -0700 | [diff] [blame] | 343 | int set_vddmax_mv; |
| 344 | int delta_vddmax_mv; |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 345 | u8 trim_center; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 346 | unsigned int warm_bat_mv; |
| 347 | unsigned int cool_bat_mv; |
| 348 | unsigned int resume_delta_mv; |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 349 | int insertion_ocv_uv; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 350 | int term_current; |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 351 | int soc_resume_limit; |
| 352 | bool resuming_charging; |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 353 | unsigned int maxinput_usb_ma; |
| 354 | unsigned int maxinput_dc_ma; |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 355 | unsigned int hot_batt_p; |
| 356 | unsigned int cold_batt_p; |
David Keitel | 27a97e6 | 2013-06-24 13:57:12 -0700 | [diff] [blame] | 357 | int warm_bat_decidegc; |
| 358 | int cool_bat_decidegc; |
Xiaozhe Shi | 78797cf | 2013-11-12 13:18:42 -0800 | [diff] [blame] | 359 | int fake_battery_soc; |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 360 | unsigned int safe_current; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 361 | unsigned int revision; |
David Keitel | fe51cb9 | 2013-04-02 19:42:58 -0700 | [diff] [blame] | 362 | unsigned int type; |
| 363 | unsigned int tchg_mins; |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 364 | unsigned int thermal_levels; |
| 365 | unsigned int therm_lvl_sel; |
| 366 | unsigned int *thermal_mitigation; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 367 | struct power_supply dc_psy; |
| 368 | struct power_supply *usb_psy; |
| 369 | struct power_supply *bms_psy; |
| 370 | struct power_supply batt_psy; |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 371 | uint32_t flags; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 372 | struct qpnp_adc_tm_btm_param adc_param; |
David Keitel | 79f4c93 | 2013-04-03 16:08:39 -0700 | [diff] [blame] | 373 | struct work_struct adc_measure_work; |
David Keitel | ecab15a | 2013-08-06 15:41:39 -0700 | [diff] [blame] | 374 | struct work_struct adc_disable_work; |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 375 | struct delayed_work arb_stop_work; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 376 | struct delayed_work eoc_work; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 377 | struct delayed_work usbin_health_check; |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 378 | struct work_struct soc_check_work; |
David Keitel | 7ac385e | 2013-08-19 18:02:40 -0700 | [diff] [blame] | 379 | struct delayed_work aicl_check_work; |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 380 | struct work_struct insertion_ocv_work; |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 381 | struct work_struct ocp_clear_work; |
Chunmei Cai | 661a7cd | 2014-08-05 17:31:54 +0800 | [diff] [blame] | 382 | struct qpnp_chg_regulator flash_wa_vreg; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 383 | struct qpnp_chg_regulator otg_vreg; |
| 384 | struct qpnp_chg_regulator boost_vreg; |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 385 | struct qpnp_chg_regulator batfet_vreg; |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 386 | bool batfet_ext_en; |
| 387 | struct work_struct batfet_lcl_work; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 388 | struct qpnp_vadc_chip *vadc_dev; |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 389 | struct qpnp_iadc_chip *iadc_dev; |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 390 | struct qpnp_adc_tm_chip *adc_tm_dev; |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 391 | struct mutex jeita_configure_lock; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 392 | spinlock_t usbin_health_monitor_lock; |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 393 | struct mutex batfet_vreg_lock; |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 394 | struct alarm reduce_power_stage_alarm; |
| 395 | struct work_struct reduce_power_stage_work; |
| 396 | bool power_stage_workaround_running; |
Zhenhua Huang | dc33551 | 2013-10-30 11:45:15 +0800 | [diff] [blame] | 397 | bool power_stage_workaround_enable; |
Chunmei Cai | 661a7cd | 2014-08-05 17:31:54 +0800 | [diff] [blame] | 398 | bool is_flash_wa_reg_enabled; |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 399 | bool ext_ovp_ic_gpio_enabled; |
| 400 | unsigned int ext_ovp_isns_gpio; |
| 401 | unsigned int usb_trim_default; |
Chunmei Cai | 661a7cd | 2014-08-05 17:31:54 +0800 | [diff] [blame] | 402 | u8 chg_temp_thresh_default; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 403 | }; |
| 404 | |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 405 | static void |
| 406 | qpnp_chg_set_appropriate_battery_current(struct qpnp_chg_chip *chip); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 407 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 408 | static struct of_device_id qpnp_charger_match_table[] = { |
| 409 | { .compatible = QPNP_CHARGER_DEV_NAME, }, |
| 410 | {} |
| 411 | }; |
| 412 | |
David Keitel | 0ef282b | 2013-05-29 16:04:57 -0700 | [diff] [blame] | 413 | enum bpd_type { |
| 414 | BPD_TYPE_BAT_ID, |
| 415 | BPD_TYPE_BAT_THM, |
| 416 | BPD_TYPE_BAT_THM_BAT_ID, |
| 417 | }; |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 418 | |
David Keitel | 0ef282b | 2013-05-29 16:04:57 -0700 | [diff] [blame] | 419 | static const char * const bpd_label[] = { |
| 420 | [BPD_TYPE_BAT_ID] = "bpd_id", |
| 421 | [BPD_TYPE_BAT_THM] = "bpd_thm", |
| 422 | [BPD_TYPE_BAT_THM_BAT_ID] = "bpd_thm_id", |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 423 | }; |
| 424 | |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 425 | enum btc_type { |
| 426 | HOT_THD_25_PCT = 25, |
| 427 | HOT_THD_35_PCT = 35, |
| 428 | COLD_THD_70_PCT = 70, |
| 429 | COLD_THD_80_PCT = 80, |
| 430 | }; |
| 431 | |
| 432 | static u8 btc_value[] = { |
| 433 | [HOT_THD_25_PCT] = 0x0, |
| 434 | [HOT_THD_35_PCT] = BIT(0), |
| 435 | [COLD_THD_70_PCT] = 0x0, |
| 436 | [COLD_THD_80_PCT] = BIT(1), |
| 437 | }; |
| 438 | |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 439 | enum usbin_health { |
| 440 | USBIN_UNKNOW, |
| 441 | USBIN_OK, |
| 442 | USBIN_OVP, |
| 443 | }; |
| 444 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 445 | static int ext_ovp_isns_present; |
| 446 | module_param(ext_ovp_isns_present, int, 0444); |
| 447 | static int ext_ovp_isns_r; |
| 448 | module_param(ext_ovp_isns_r, int, 0444); |
| 449 | |
| 450 | static bool ext_ovp_isns_online; |
| 451 | static long ext_ovp_isns_ua; |
| 452 | #define MAX_CURRENT_LENGTH_9A 10 |
| 453 | #define ISNS_CURRENT_RATIO 2500 |
| 454 | static int ext_ovp_isns_read(char *buffer, const struct kernel_param *kp) |
| 455 | { |
| 456 | int rc; |
| 457 | struct qpnp_vadc_result results; |
| 458 | struct power_supply *batt_psy = power_supply_get_by_name("battery"); |
| 459 | struct qpnp_chg_chip *chip = container_of(batt_psy, |
| 460 | struct qpnp_chg_chip, batt_psy); |
| 461 | |
| 462 | if (!ext_ovp_isns_present) |
| 463 | return 0; |
| 464 | |
| 465 | rc = qpnp_vadc_read(chip->vadc_dev, P_MUX7_1_1, &results); |
| 466 | if (rc) { |
| 467 | pr_err("Unable to read vbat rc=%d\n", rc); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | pr_debug("voltage %lld uV, current: %d\n mA", results.physical, |
| 472 | ((int) results.physical / |
| 473 | (ext_ovp_isns_r / ISNS_CURRENT_RATIO))); |
| 474 | |
| 475 | return snprintf(buffer, MAX_CURRENT_LENGTH_9A, "%d\n", |
| 476 | ((int)results.physical / |
| 477 | (ext_ovp_isns_r / ISNS_CURRENT_RATIO))); |
| 478 | } |
| 479 | |
| 480 | static int ext_ovp_isns_enable(const char *val, const struct kernel_param *kp) |
| 481 | { |
| 482 | int rc; |
| 483 | struct power_supply *batt_psy = power_supply_get_by_name("battery"); |
| 484 | struct qpnp_chg_chip *chip = container_of(batt_psy, |
| 485 | struct qpnp_chg_chip, batt_psy); |
| 486 | |
| 487 | rc = param_set_bool(val, kp); |
| 488 | if (rc) { |
| 489 | pr_err("Unable to set gpio en: %d\n", rc); |
| 490 | return rc; |
| 491 | } |
| 492 | |
| 493 | if (*(bool *)kp->arg) { |
| 494 | gpio_direction_output( |
| 495 | chip->ext_ovp_isns_gpio, 1); |
| 496 | chip->ext_ovp_ic_gpio_enabled = 1; |
| 497 | pr_debug("enabled GPIO\n"); |
| 498 | } else { |
| 499 | gpio_direction_output( |
| 500 | chip->ext_ovp_isns_gpio, 0); |
| 501 | chip->ext_ovp_ic_gpio_enabled = 0; |
| 502 | pr_debug("disabled GPIO\n"); |
| 503 | } |
| 504 | |
| 505 | return rc; |
| 506 | } |
| 507 | |
| 508 | static struct kernel_param_ops ext_ovp_isns_ops = { |
| 509 | .get = ext_ovp_isns_read, |
| 510 | }; |
| 511 | module_param_cb(ext_ovp_isns_ua, &ext_ovp_isns_ops, &ext_ovp_isns_ua, 0644); |
| 512 | |
| 513 | static struct kernel_param_ops ext_ovp_en_ops = { |
| 514 | .set = ext_ovp_isns_enable, |
| 515 | .get = param_get_bool, |
| 516 | }; |
| 517 | module_param_cb(ext_ovp_isns_online, &ext_ovp_en_ops, |
| 518 | &ext_ovp_isns_online, 0664); |
| 519 | |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 520 | static inline int |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 521 | get_bpd(const char *name) |
| 522 | { |
| 523 | int i = 0; |
David Keitel | 0ef282b | 2013-05-29 16:04:57 -0700 | [diff] [blame] | 524 | for (i = 0; i < ARRAY_SIZE(bpd_label); i++) { |
| 525 | if (strcmp(bpd_label[i], name) == 0) |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 526 | return i; |
| 527 | } |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 531 | static bool |
| 532 | is_within_range(int value, int left, int right) |
| 533 | { |
| 534 | if (left >= right && left >= value && value >= right) |
| 535 | return 1; |
| 536 | if (left <= right && left <= value && value <= right) |
| 537 | return 1; |
| 538 | return 0; |
| 539 | } |
| 540 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 541 | static int |
| 542 | qpnp_chg_read(struct qpnp_chg_chip *chip, u8 *val, |
| 543 | u16 base, int count) |
| 544 | { |
David Keitel | 3909357 | 2013-06-05 15:32:30 -0700 | [diff] [blame] | 545 | int rc = 0; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 546 | struct spmi_device *spmi = chip->spmi; |
| 547 | |
David Keitel | 3909357 | 2013-06-05 15:32:30 -0700 | [diff] [blame] | 548 | if (base == 0) { |
| 549 | pr_err("base cannot be zero base=0x%02x sid=0x%02x rc=%d\n", |
| 550 | base, spmi->sid, rc); |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | |
| 554 | rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 555 | if (rc) { |
| 556 | pr_err("SPMI read failed base=0x%02x sid=0x%02x rc=%d\n", base, |
| 557 | spmi->sid, rc); |
| 558 | return rc; |
| 559 | } |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int |
| 564 | qpnp_chg_write(struct qpnp_chg_chip *chip, u8 *val, |
| 565 | u16 base, int count) |
| 566 | { |
David Keitel | 3909357 | 2013-06-05 15:32:30 -0700 | [diff] [blame] | 567 | int rc = 0; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 568 | struct spmi_device *spmi = chip->spmi; |
| 569 | |
David Keitel | 3909357 | 2013-06-05 15:32:30 -0700 | [diff] [blame] | 570 | if (base == 0) { |
| 571 | pr_err("base cannot be zero base=0x%02x sid=0x%02x rc=%d\n", |
| 572 | base, spmi->sid, rc); |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | |
| 576 | rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 577 | if (rc) { |
| 578 | pr_err("write failed base=0x%02x sid=0x%02x rc=%d\n", |
| 579 | base, spmi->sid, rc); |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int |
| 587 | qpnp_chg_masked_write(struct qpnp_chg_chip *chip, u16 base, |
| 588 | u8 mask, u8 val, int count) |
| 589 | { |
| 590 | int rc; |
| 591 | u8 reg; |
| 592 | |
| 593 | rc = qpnp_chg_read(chip, ®, base, count); |
| 594 | if (rc) { |
| 595 | pr_err("spmi read failed: addr=%03X, rc=%d\n", base, rc); |
| 596 | return rc; |
| 597 | } |
| 598 | pr_debug("addr = 0x%x read 0x%x\n", base, reg); |
| 599 | |
| 600 | reg &= ~mask; |
| 601 | reg |= val & mask; |
| 602 | |
| 603 | pr_debug("Writing 0x%x\n", reg); |
| 604 | |
| 605 | rc = qpnp_chg_write(chip, ®, base, count); |
| 606 | if (rc) { |
| 607 | pr_err("spmi write failed: addr=%03X, rc=%d\n", base, rc); |
| 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 614 | static void |
| 615 | qpnp_chg_enable_irq(struct qpnp_chg_irq *irq) |
| 616 | { |
| 617 | if (__test_and_clear_bit(0, &irq->disabled)) { |
| 618 | pr_debug("number = %d\n", irq->irq); |
| 619 | enable_irq(irq->irq); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | qpnp_chg_disable_irq(struct qpnp_chg_irq *irq) |
| 625 | { |
| 626 | if (!__test_and_set_bit(0, &irq->disabled)) { |
| 627 | pr_debug("number = %d\n", irq->irq); |
| 628 | disable_irq_nosync(irq->irq); |
| 629 | } |
| 630 | } |
| 631 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 632 | static void |
| 633 | qpnp_chg_irq_wake_enable(struct qpnp_chg_irq *irq) |
| 634 | { |
| 635 | if (!__test_and_set_bit(0, &irq->wake_enable)) { |
| 636 | pr_debug("number = %d\n", irq->irq); |
| 637 | enable_irq_wake(irq->irq); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static void |
| 642 | qpnp_chg_irq_wake_disable(struct qpnp_chg_irq *irq) |
| 643 | { |
| 644 | if (__test_and_clear_bit(0, &irq->wake_enable)) { |
| 645 | pr_debug("number = %d\n", irq->irq); |
| 646 | disable_irq_wake(irq->irq); |
| 647 | } |
| 648 | } |
| 649 | |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 650 | #define USB_OTG_EN_BIT BIT(0) |
| 651 | static int |
| 652 | qpnp_chg_is_otg_en_set(struct qpnp_chg_chip *chip) |
| 653 | { |
| 654 | u8 usb_otg_en; |
| 655 | int rc; |
| 656 | |
| 657 | rc = qpnp_chg_read(chip, &usb_otg_en, |
| 658 | chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL, |
| 659 | 1); |
| 660 | |
| 661 | if (rc) { |
| 662 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 663 | chip->usb_chgpth_base + CHGR_STATUS, rc); |
| 664 | return rc; |
| 665 | } |
| 666 | pr_debug("usb otg en 0x%x\n", usb_otg_en); |
| 667 | |
| 668 | return (usb_otg_en & USB_OTG_EN_BIT) ? 1 : 0; |
| 669 | } |
| 670 | |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 671 | static int |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 672 | qpnp_chg_is_boost_en_set(struct qpnp_chg_chip *chip) |
| 673 | { |
| 674 | u8 boost_en_ctl; |
| 675 | int rc; |
| 676 | |
| 677 | rc = qpnp_chg_read(chip, &boost_en_ctl, |
| 678 | chip->boost_base + BOOST_ENABLE_CONTROL, 1); |
| 679 | if (rc) { |
| 680 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 681 | chip->boost_base + BOOST_ENABLE_CONTROL, rc); |
| 682 | return rc; |
| 683 | } |
| 684 | |
| 685 | pr_debug("boost en 0x%x\n", boost_en_ctl); |
| 686 | |
| 687 | return (boost_en_ctl & BOOST_PWR_EN) ? 1 : 0; |
| 688 | } |
| 689 | |
| 690 | static int |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 691 | qpnp_chg_is_batt_temp_ok(struct qpnp_chg_chip *chip) |
| 692 | { |
| 693 | u8 batt_rt_sts; |
| 694 | int rc; |
| 695 | |
| 696 | rc = qpnp_chg_read(chip, &batt_rt_sts, |
| 697 | INT_RT_STS(chip->bat_if_base), 1); |
| 698 | if (rc) { |
| 699 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 700 | INT_RT_STS(chip->bat_if_base), rc); |
| 701 | return rc; |
| 702 | } |
| 703 | |
| 704 | return (batt_rt_sts & BAT_TEMP_OK_IRQ) ? 1 : 0; |
| 705 | } |
| 706 | |
| 707 | static int |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 708 | qpnp_chg_is_batt_present(struct qpnp_chg_chip *chip) |
| 709 | { |
| 710 | u8 batt_pres_rt_sts; |
| 711 | int rc; |
| 712 | |
| 713 | rc = qpnp_chg_read(chip, &batt_pres_rt_sts, |
| 714 | INT_RT_STS(chip->bat_if_base), 1); |
| 715 | if (rc) { |
| 716 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 717 | INT_RT_STS(chip->bat_if_base), rc); |
| 718 | return rc; |
| 719 | } |
| 720 | |
| 721 | return (batt_pres_rt_sts & BATT_PRES_IRQ) ? 1 : 0; |
| 722 | } |
| 723 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 724 | static int |
| 725 | qpnp_chg_is_batfet_closed(struct qpnp_chg_chip *chip) |
| 726 | { |
| 727 | u8 batfet_closed_rt_sts; |
| 728 | int rc; |
| 729 | |
| 730 | rc = qpnp_chg_read(chip, &batfet_closed_rt_sts, |
| 731 | INT_RT_STS(chip->bat_if_base), 1); |
| 732 | if (rc) { |
| 733 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 734 | INT_RT_STS(chip->bat_if_base), rc); |
| 735 | return rc; |
| 736 | } |
| 737 | |
| 738 | return (batfet_closed_rt_sts & BAT_FET_ON_IRQ) ? 1 : 0; |
| 739 | } |
| 740 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 741 | static int |
| 742 | qpnp_chg_is_usb_chg_plugged_in(struct qpnp_chg_chip *chip) |
| 743 | { |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 744 | u8 usb_chgpth_rt_sts; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 745 | int rc; |
| 746 | |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 747 | rc = qpnp_chg_read(chip, &usb_chgpth_rt_sts, |
| 748 | INT_RT_STS(chip->usb_chgpth_base), 1); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 749 | |
| 750 | if (rc) { |
| 751 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 752 | INT_RT_STS(chip->usb_chgpth_base), rc); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 753 | return rc; |
| 754 | } |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 755 | pr_debug("chgr usb sts 0x%x\n", usb_chgpth_rt_sts); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 756 | |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 757 | return (usb_chgpth_rt_sts & USBIN_VALID_IRQ) ? 1 : 0; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 760 | static bool |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 761 | qpnp_is_dc_higher_prio(struct qpnp_chg_chip *chip) |
| 762 | { |
| 763 | int rc; |
| 764 | u8 usb_ctl; |
| 765 | |
| 766 | if (!chip->type == SMBB) |
| 767 | return false; |
| 768 | |
| 769 | rc = qpnp_chg_read(chip, &usb_ctl, |
| 770 | chip->usb_chgpth_base + USB_CHGPTH_CTL, 1); |
| 771 | if (rc) { |
| 772 | pr_err("failed to read usb ctl rc=%d\n", rc); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | return !!(usb_ctl & DC_HIGHER_PRIORITY); |
| 777 | } |
| 778 | |
| 779 | static bool |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 780 | qpnp_chg_is_ibat_loop_active(struct qpnp_chg_chip *chip) |
| 781 | { |
| 782 | int rc; |
| 783 | u8 buck_sts; |
| 784 | |
| 785 | rc = qpnp_chg_read(chip, &buck_sts, |
| 786 | INT_RT_STS(chip->buck_base), 1); |
| 787 | if (rc) { |
| 788 | pr_err("failed to read buck RT status rc=%d\n", rc); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | return !!(buck_sts & IBAT_LOOP_IRQ); |
| 793 | } |
| 794 | |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 795 | #define USB_VALID_MASK 0xC0 |
| 796 | #define USB_VALID_IN_MASK BIT(7) |
| 797 | #define USB_COARSE_DET 0x10 |
| 798 | #define USB_VALID_OVP_VALUE 0x40 |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 799 | static int |
| 800 | qpnp_chg_check_usb_coarse_det(struct qpnp_chg_chip *chip) |
| 801 | { |
| 802 | u8 usbin_chg_rt_sts; |
| 803 | int rc; |
| 804 | rc = qpnp_chg_read(chip, &usbin_chg_rt_sts, |
| 805 | chip->usb_chgpth_base + CHGR_STATUS , 1); |
| 806 | if (rc) { |
| 807 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 808 | chip->usb_chgpth_base + CHGR_STATUS, rc); |
| 809 | return rc; |
| 810 | } |
| 811 | return (usbin_chg_rt_sts & USB_COARSE_DET) ? 1 : 0; |
| 812 | } |
| 813 | |
| 814 | static int |
| 815 | qpnp_chg_check_usbin_health(struct qpnp_chg_chip *chip) |
| 816 | { |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 817 | u8 usbin_chg_rt_sts, usb_chgpth_rt_sts; |
| 818 | u8 usbin_health = 0; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 819 | int rc; |
| 820 | |
| 821 | rc = qpnp_chg_read(chip, &usbin_chg_rt_sts, |
| 822 | chip->usb_chgpth_base + CHGR_STATUS , 1); |
| 823 | |
| 824 | if (rc) { |
| 825 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 826 | chip->usb_chgpth_base + CHGR_STATUS, rc); |
| 827 | return rc; |
| 828 | } |
| 829 | |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 830 | rc = qpnp_chg_read(chip, &usb_chgpth_rt_sts, |
| 831 | INT_RT_STS(chip->usb_chgpth_base) , 1); |
| 832 | |
| 833 | if (rc) { |
| 834 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 835 | INT_RT_STS(chip->usb_chgpth_base), rc); |
| 836 | return rc; |
| 837 | } |
| 838 | |
| 839 | pr_debug("chgr usb sts 0x%x, chgpth rt sts 0x%x\n", |
| 840 | usbin_chg_rt_sts, usb_chgpth_rt_sts); |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 841 | if ((usbin_chg_rt_sts & USB_COARSE_DET) == USB_COARSE_DET) { |
| 842 | if ((usbin_chg_rt_sts & USB_VALID_MASK) |
| 843 | == USB_VALID_OVP_VALUE) { |
| 844 | usbin_health = USBIN_OVP; |
| 845 | pr_err("Over voltage charger inserted\n"); |
Zhenhua Huang | f116387 | 2014-03-03 10:51:25 +0800 | [diff] [blame] | 846 | } else if ((usb_chgpth_rt_sts & USBIN_VALID_IRQ) != 0) { |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 847 | usbin_health = USBIN_OK; |
| 848 | pr_debug("Valid charger inserted\n"); |
| 849 | } |
| 850 | } else { |
| 851 | usbin_health = USBIN_UNKNOW; |
| 852 | pr_debug("Charger plug out\n"); |
| 853 | } |
| 854 | |
| 855 | return usbin_health; |
| 856 | } |
| 857 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 858 | static int |
| 859 | qpnp_chg_is_dc_chg_plugged_in(struct qpnp_chg_chip *chip) |
| 860 | { |
| 861 | u8 dcin_valid_rt_sts; |
| 862 | int rc; |
| 863 | |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 864 | if (!chip->dc_chgpth_base) |
| 865 | return 0; |
| 866 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 867 | rc = qpnp_chg_read(chip, &dcin_valid_rt_sts, |
| 868 | INT_RT_STS(chip->dc_chgpth_base), 1); |
| 869 | if (rc) { |
| 870 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 871 | INT_RT_STS(chip->dc_chgpth_base), rc); |
| 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | return (dcin_valid_rt_sts & DCIN_VALID_IRQ) ? 1 : 0; |
| 876 | } |
| 877 | |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 878 | static int |
| 879 | qpnp_chg_is_ichg_loop_active(struct qpnp_chg_chip *chip) |
| 880 | { |
| 881 | u8 buck_sts; |
| 882 | int rc; |
| 883 | |
| 884 | rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1); |
| 885 | |
| 886 | if (rc) { |
| 887 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 888 | INT_RT_STS(chip->buck_base), rc); |
| 889 | return rc; |
| 890 | } |
| 891 | pr_debug("buck usb sts 0x%x\n", buck_sts); |
| 892 | |
| 893 | return (buck_sts & ICHG_LOOP_IRQ) ? 1 : 0; |
| 894 | } |
| 895 | |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 896 | #define QPNP_CHG_I_MAX_MIN_100 100 |
| 897 | #define QPNP_CHG_I_MAX_MIN_150 150 |
| 898 | #define QPNP_CHG_I_MAX_MIN_MA 200 |
| 899 | #define QPNP_CHG_I_MAX_MAX_MA 2500 |
| 900 | #define QPNP_CHG_I_MAXSTEP_MA 100 |
| 901 | static int |
| 902 | qpnp_chg_idcmax_set(struct qpnp_chg_chip *chip, int mA) |
| 903 | { |
| 904 | int rc = 0; |
| 905 | u8 dc = 0; |
| 906 | |
| 907 | if (mA < QPNP_CHG_I_MAX_MIN_100 |
| 908 | || mA > QPNP_CHG_I_MAX_MAX_MA) { |
| 909 | pr_err("bad mA=%d asked to set\n", mA); |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
| 913 | if (mA == QPNP_CHG_I_MAX_MIN_100) { |
| 914 | dc = 0x00; |
| 915 | pr_debug("current=%d setting %02x\n", mA, dc); |
| 916 | return qpnp_chg_write(chip, &dc, |
| 917 | chip->dc_chgpth_base + CHGR_I_MAX_REG, 1); |
| 918 | } else if (mA == QPNP_CHG_I_MAX_MIN_150) { |
| 919 | dc = 0x01; |
| 920 | pr_debug("current=%d setting %02x\n", mA, dc); |
| 921 | return qpnp_chg_write(chip, &dc, |
| 922 | chip->dc_chgpth_base + CHGR_I_MAX_REG, 1); |
| 923 | } |
| 924 | |
| 925 | dc = mA / QPNP_CHG_I_MAXSTEP_MA; |
| 926 | |
| 927 | pr_debug("current=%d setting 0x%x\n", mA, dc); |
| 928 | rc = qpnp_chg_write(chip, &dc, |
| 929 | chip->dc_chgpth_base + CHGR_I_MAX_REG, 1); |
| 930 | |
| 931 | return rc; |
| 932 | } |
| 933 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 934 | static int |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 935 | qpnp_chg_iusb_trim_get(struct qpnp_chg_chip *chip) |
| 936 | { |
| 937 | int rc = 0; |
| 938 | u8 trim_reg; |
| 939 | |
| 940 | rc = qpnp_chg_read(chip, &trim_reg, |
| 941 | chip->usb_chgpth_base + CHGR_USB_TRIM, 1); |
| 942 | if (rc) { |
| 943 | pr_err("failed to read USB_TRIM rc=%d\n", rc); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | return trim_reg; |
| 948 | } |
| 949 | |
| 950 | static int |
| 951 | qpnp_chg_iusb_trim_set(struct qpnp_chg_chip *chip, int trim) |
| 952 | { |
| 953 | int rc = 0; |
| 954 | |
| 955 | rc = qpnp_chg_masked_write(chip, |
| 956 | chip->usb_chgpth_base + SEC_ACCESS, |
| 957 | 0xFF, |
| 958 | 0xA5, 1); |
| 959 | if (rc) { |
| 960 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 961 | return rc; |
| 962 | } |
| 963 | |
| 964 | rc = qpnp_chg_masked_write(chip, |
| 965 | chip->usb_chgpth_base + CHGR_USB_TRIM, |
| 966 | 0xFF, |
| 967 | trim, 1); |
| 968 | if (rc) { |
| 969 | pr_err("failed to write USB TRIM rc=%d\n", rc); |
| 970 | return rc; |
| 971 | } |
| 972 | |
| 973 | return rc; |
| 974 | } |
| 975 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 976 | #define IOVP_USB_WALL_TRSH_MA 150 |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 977 | static int |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 978 | qpnp_chg_iusbmax_set(struct qpnp_chg_chip *chip, int mA) |
| 979 | { |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 980 | int rc = 0; |
| 981 | u8 usb_reg = 0, temp = 8; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 982 | |
David Keitel | 7b056848 | 2013-12-02 14:47:42 -0800 | [diff] [blame] | 983 | if (mA < 0 || mA > QPNP_CHG_I_MAX_MAX_MA) { |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 984 | pr_err("bad mA=%d asked to set\n", mA); |
| 985 | return -EINVAL; |
| 986 | } |
| 987 | |
David Keitel | 7b056848 | 2013-12-02 14:47:42 -0800 | [diff] [blame] | 988 | if (mA <= QPNP_CHG_I_MAX_MIN_100) { |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 989 | usb_reg = 0x00; |
| 990 | pr_debug("current=%d setting %02x\n", mA, usb_reg); |
| 991 | return qpnp_chg_write(chip, &usb_reg, |
| 992 | chip->usb_chgpth_base + CHGR_I_MAX_REG, 1); |
| 993 | } else if (mA == QPNP_CHG_I_MAX_MIN_150) { |
| 994 | usb_reg = 0x01; |
| 995 | pr_debug("current=%d setting %02x\n", mA, usb_reg); |
| 996 | return qpnp_chg_write(chip, &usb_reg, |
| 997 | chip->usb_chgpth_base + CHGR_I_MAX_REG, 1); |
| 998 | } |
| 999 | |
| 1000 | /* Impose input current limit */ |
| 1001 | if (chip->maxinput_usb_ma) |
| 1002 | mA = (chip->maxinput_usb_ma) <= mA ? chip->maxinput_usb_ma : mA; |
| 1003 | |
| 1004 | usb_reg = mA / QPNP_CHG_I_MAXSTEP_MA; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1005 | |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 1006 | if (chip->flags & CHG_FLAGS_VCP_WA) { |
| 1007 | temp = 0xA5; |
| 1008 | rc = qpnp_chg_write(chip, &temp, |
| 1009 | chip->buck_base + SEC_ACCESS, 1); |
| 1010 | rc = qpnp_chg_masked_write(chip, |
| 1011 | chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_3, |
| 1012 | 0x0C, 0x0C, 1); |
| 1013 | } |
| 1014 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1015 | pr_debug("current=%d setting 0x%x\n", mA, usb_reg); |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 1016 | rc = qpnp_chg_write(chip, &usb_reg, |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 1017 | chip->usb_chgpth_base + CHGR_I_MAX_REG, 1); |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 1018 | |
| 1019 | if (chip->flags & CHG_FLAGS_VCP_WA) { |
| 1020 | temp = 0xA5; |
| 1021 | udelay(200); |
| 1022 | rc = qpnp_chg_write(chip, &temp, |
| 1023 | chip->buck_base + SEC_ACCESS, 1); |
| 1024 | rc = qpnp_chg_masked_write(chip, |
| 1025 | chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_3, |
| 1026 | 0x0C, 0x00, 1); |
| 1027 | } |
| 1028 | |
| 1029 | return rc; |
| 1030 | } |
| 1031 | |
David Keitel | 25544a2 | 2013-12-06 17:59:24 -0800 | [diff] [blame] | 1032 | #define QPNP_CHG_VINMIN_MIN_MV 4000 |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 1033 | #define QPNP_CHG_VINMIN_HIGH_MIN_MV 5600 |
| 1034 | #define QPNP_CHG_VINMIN_HIGH_MIN_VAL 0x2B |
| 1035 | #define QPNP_CHG_VINMIN_MAX_MV 9600 |
| 1036 | #define QPNP_CHG_VINMIN_STEP_MV 50 |
| 1037 | #define QPNP_CHG_VINMIN_STEP_HIGH_MV 200 |
Prasad Sodagudi | 95624b6 | 2013-09-26 16:40:59 +0530 | [diff] [blame] | 1038 | #define QPNP_CHG_VINMIN_MASK 0x3F |
David Keitel | 25544a2 | 2013-12-06 17:59:24 -0800 | [diff] [blame] | 1039 | #define QPNP_CHG_VINMIN_MIN_VAL 0x0C |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 1040 | static int |
| 1041 | qpnp_chg_vinmin_set(struct qpnp_chg_chip *chip, int voltage) |
| 1042 | { |
| 1043 | u8 temp; |
| 1044 | |
David Keitel | 25544a2 | 2013-12-06 17:59:24 -0800 | [diff] [blame] | 1045 | if ((voltage < QPNP_CHG_VINMIN_MIN_MV) |
| 1046 | || (voltage > QPNP_CHG_VINMIN_MAX_MV)) { |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 1047 | pr_err("bad mV=%d asked to set\n", voltage); |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | if (voltage >= QPNP_CHG_VINMIN_HIGH_MIN_MV) { |
| 1051 | temp = QPNP_CHG_VINMIN_HIGH_MIN_VAL; |
David Keitel | 289fcf8 | 2013-10-24 11:33:00 -0700 | [diff] [blame] | 1052 | temp += (voltage - QPNP_CHG_VINMIN_HIGH_MIN_MV) |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 1053 | / QPNP_CHG_VINMIN_STEP_HIGH_MV; |
| 1054 | } else { |
| 1055 | temp = QPNP_CHG_VINMIN_MIN_VAL; |
| 1056 | temp += (voltage - QPNP_CHG_VINMIN_MIN_MV) |
| 1057 | / QPNP_CHG_VINMIN_STEP_MV; |
| 1058 | } |
| 1059 | |
| 1060 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 1061 | return qpnp_chg_masked_write(chip, |
| 1062 | chip->chgr_base + CHGR_VIN_MIN, |
| 1063 | QPNP_CHG_VINMIN_MASK, temp, 1); |
| 1064 | } |
| 1065 | |
| 1066 | static int |
| 1067 | qpnp_chg_vinmin_get(struct qpnp_chg_chip *chip) |
| 1068 | { |
| 1069 | int rc, vin_min_mv; |
| 1070 | u8 vin_min; |
| 1071 | |
| 1072 | rc = qpnp_chg_read(chip, &vin_min, chip->chgr_base + CHGR_VIN_MIN, 1); |
| 1073 | if (rc) { |
| 1074 | pr_err("failed to read VIN_MIN rc=%d\n", rc); |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | if (vin_min == 0) |
| 1079 | vin_min_mv = QPNP_CHG_I_MAX_MIN_100; |
David Keitel | 289fcf8 | 2013-10-24 11:33:00 -0700 | [diff] [blame] | 1080 | else if (vin_min >= QPNP_CHG_VINMIN_HIGH_MIN_VAL) |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 1081 | vin_min_mv = QPNP_CHG_VINMIN_HIGH_MIN_MV + |
| 1082 | (vin_min - QPNP_CHG_VINMIN_HIGH_MIN_VAL) |
| 1083 | * QPNP_CHG_VINMIN_STEP_HIGH_MV; |
| 1084 | else |
| 1085 | vin_min_mv = QPNP_CHG_VINMIN_MIN_MV + |
| 1086 | (vin_min - QPNP_CHG_VINMIN_MIN_VAL) |
| 1087 | * QPNP_CHG_VINMIN_STEP_MV; |
| 1088 | pr_debug("vin_min= 0x%02x, ma = %d\n", vin_min, vin_min_mv); |
| 1089 | |
| 1090 | return vin_min_mv; |
| 1091 | } |
| 1092 | |
Prasad Sodagudi | c367801 | 2013-09-23 12:01:19 +0530 | [diff] [blame] | 1093 | #define QPNP_CHG_VBATWEAK_MIN_MV 2100 |
| 1094 | #define QPNP_CHG_VBATWEAK_MAX_MV 3600 |
| 1095 | #define QPNP_CHG_VBATWEAK_STEP_MV 100 |
| 1096 | static int |
| 1097 | qpnp_chg_vbatweak_set(struct qpnp_chg_chip *chip, int vbatweak_mv) |
| 1098 | { |
| 1099 | u8 temp; |
| 1100 | |
| 1101 | if (vbatweak_mv < QPNP_CHG_VBATWEAK_MIN_MV |
| 1102 | || vbatweak_mv > QPNP_CHG_VBATWEAK_MAX_MV) |
| 1103 | return -EINVAL; |
| 1104 | |
| 1105 | temp = (vbatweak_mv - QPNP_CHG_VBATWEAK_MIN_MV) |
| 1106 | / QPNP_CHG_VBATWEAK_STEP_MV; |
| 1107 | |
| 1108 | pr_debug("voltage=%d setting %02x\n", vbatweak_mv, temp); |
| 1109 | return qpnp_chg_write(chip, &temp, |
| 1110 | chip->chgr_base + CHGR_VBAT_WEAK, 1); |
| 1111 | } |
| 1112 | |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 1113 | static int |
| 1114 | qpnp_chg_usb_iusbmax_get(struct qpnp_chg_chip *chip) |
| 1115 | { |
| 1116 | int rc, iusbmax_ma; |
| 1117 | u8 iusbmax; |
| 1118 | |
| 1119 | rc = qpnp_chg_read(chip, &iusbmax, |
| 1120 | chip->usb_chgpth_base + CHGR_I_MAX_REG, 1); |
| 1121 | if (rc) { |
| 1122 | pr_err("failed to read IUSB_MAX rc=%d\n", rc); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | if (iusbmax == 0) |
| 1127 | iusbmax_ma = QPNP_CHG_I_MAX_MIN_100; |
| 1128 | else if (iusbmax == 0x01) |
| 1129 | iusbmax_ma = QPNP_CHG_I_MAX_MIN_150; |
| 1130 | else |
| 1131 | iusbmax_ma = iusbmax * QPNP_CHG_I_MAXSTEP_MA; |
| 1132 | |
| 1133 | pr_debug("iusbmax = 0x%02x, ma = %d\n", iusbmax, iusbmax_ma); |
| 1134 | |
| 1135 | return iusbmax_ma; |
| 1136 | } |
| 1137 | |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 1138 | #define ILIMIT_OVR_0 0x02 |
| 1139 | static int |
| 1140 | override_dcin_ilimit(struct qpnp_chg_chip *chip, bool override) |
| 1141 | { |
| 1142 | int rc; |
| 1143 | |
| 1144 | pr_debug("override %d\n", override); |
| 1145 | rc = qpnp_chg_masked_write(chip, |
| 1146 | chip->dc_chgpth_base + SEC_ACCESS, |
| 1147 | 0xA5, |
| 1148 | 0xA5, 1); |
| 1149 | rc |= qpnp_chg_masked_write(chip, |
| 1150 | chip->dc_chgpth_base + DC_COMP_OVR1, |
| 1151 | 0xFF, |
| 1152 | override ? ILIMIT_OVR_0 : 0, 1); |
| 1153 | if (rc) { |
| 1154 | pr_err("Failed to override dc ilimit rc = %d\n", rc); |
| 1155 | return rc; |
| 1156 | } |
| 1157 | |
| 1158 | return rc; |
| 1159 | } |
| 1160 | |
| 1161 | #define DUAL_PATH_EN BIT(7) |
| 1162 | static int |
| 1163 | switch_parallel_ovp_mode(struct qpnp_chg_chip *chip, bool enable) |
| 1164 | { |
| 1165 | int rc = 0; |
| 1166 | |
| 1167 | if (!chip->usb_chgpth_base || !chip->dc_chgpth_base) |
| 1168 | return rc; |
| 1169 | |
| 1170 | pr_debug("enable %d\n", enable); |
| 1171 | rc = override_dcin_ilimit(chip, 1); |
| 1172 | udelay(10); |
| 1173 | |
| 1174 | /* enable/disable dual path mode */ |
| 1175 | rc = qpnp_chg_masked_write(chip, |
| 1176 | chip->usb_chgpth_base + SEC_ACCESS, |
| 1177 | 0xA5, |
| 1178 | 0xA5, 1); |
| 1179 | rc |= qpnp_chg_masked_write(chip, |
| 1180 | chip->usb_chgpth_base + USB_SPARE, |
| 1181 | 0xFF, |
| 1182 | enable ? DUAL_PATH_EN : 0, 1); |
| 1183 | if (rc) { |
| 1184 | pr_err("Failed to turn on usb ovp rc = %d\n", rc); |
| 1185 | return rc; |
| 1186 | } |
| 1187 | |
David Keitel | bf2edba | 2014-03-05 10:49:32 -0800 | [diff] [blame] | 1188 | if (enable) |
| 1189 | rc = override_dcin_ilimit(chip, 0); |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 1190 | return rc; |
| 1191 | } |
| 1192 | |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 1193 | #define USB_SUSPEND_BIT BIT(0) |
| 1194 | static int |
| 1195 | qpnp_chg_usb_suspend_enable(struct qpnp_chg_chip *chip, int enable) |
| 1196 | { |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 1197 | /* Turn off DC OVP FET when going into USB suspend */ |
| 1198 | if (chip->parallel_ovp_mode && enable) |
| 1199 | switch_parallel_ovp_mode(chip, 0); |
| 1200 | |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 1201 | return qpnp_chg_masked_write(chip, |
| 1202 | chip->usb_chgpth_base + CHGR_USB_USB_SUSP, |
| 1203 | USB_SUSPEND_BIT, |
| 1204 | enable ? USB_SUSPEND_BIT : 0, 1); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1207 | static int |
| 1208 | qpnp_chg_charge_en(struct qpnp_chg_chip *chip, int enable) |
| 1209 | { |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 1210 | if (chip->insertion_ocv_uv == 0 && enable) { |
| 1211 | pr_debug("Battery not present, skipping\n"); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | pr_debug("charging %s\n", enable ? "enabled" : "disabled"); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1215 | return qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_CHG_CTRL, |
| 1216 | CHGR_CHG_EN, |
| 1217 | enable ? CHGR_CHG_EN : 0, 1); |
| 1218 | } |
| 1219 | |
| 1220 | static int |
| 1221 | qpnp_chg_force_run_on_batt(struct qpnp_chg_chip *chip, int disable) |
| 1222 | { |
| 1223 | /* Don't run on battery for batteryless hardware */ |
| 1224 | if (chip->use_default_batt_values) |
| 1225 | return 0; |
David Keitel | 4d66ea0 | 2013-04-30 10:57:58 -0700 | [diff] [blame] | 1226 | /* Don't force on battery if battery is not present */ |
| 1227 | if (!qpnp_chg_is_batt_present(chip)) |
| 1228 | return 0; |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1229 | |
| 1230 | /* This bit forces the charger to run off of the battery rather |
| 1231 | * than a connected charger */ |
| 1232 | return qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_CHG_CTRL, |
| 1233 | CHGR_ON_BAT_FORCE_BIT, |
| 1234 | disable ? CHGR_ON_BAT_FORCE_BIT : 0, 1); |
| 1235 | } |
| 1236 | |
David Keitel | 8b68d2d | 2013-05-14 23:36:51 -0700 | [diff] [blame] | 1237 | #define BUCK_DUTY_MASK_100P 0x30 |
| 1238 | static int |
| 1239 | qpnp_buck_set_100_duty_cycle_enable(struct qpnp_chg_chip *chip, int enable) |
| 1240 | { |
| 1241 | int rc; |
| 1242 | |
| 1243 | pr_debug("enable: %d\n", enable); |
| 1244 | |
| 1245 | rc = qpnp_chg_masked_write(chip, |
| 1246 | chip->buck_base + SEC_ACCESS, 0xA5, 0xA5, 1); |
| 1247 | if (rc) { |
| 1248 | pr_debug("failed to write sec access rc=%d\n", rc); |
| 1249 | return rc; |
| 1250 | } |
| 1251 | |
| 1252 | rc = qpnp_chg_masked_write(chip, |
| 1253 | chip->buck_base + BUCK_TEST_SMBC_MODES, |
| 1254 | BUCK_DUTY_MASK_100P, enable ? 0x00 : 0x10, 1); |
| 1255 | if (rc) { |
| 1256 | pr_debug("failed enable 100p duty cycle rc=%d\n", rc); |
| 1257 | return rc; |
| 1258 | } |
| 1259 | |
| 1260 | return rc; |
| 1261 | } |
| 1262 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1263 | #define COMPATATOR_OVERRIDE_0 0x80 |
| 1264 | static int |
| 1265 | qpnp_chg_toggle_chg_done_logic(struct qpnp_chg_chip *chip, int enable) |
| 1266 | { |
| 1267 | int rc; |
| 1268 | |
| 1269 | pr_debug("toggle: %d\n", enable); |
| 1270 | |
| 1271 | rc = qpnp_chg_masked_write(chip, |
| 1272 | chip->buck_base + SEC_ACCESS, 0xA5, 0xA5, 1); |
| 1273 | if (rc) { |
| 1274 | pr_debug("failed to write sec access rc=%d\n", rc); |
| 1275 | return rc; |
| 1276 | } |
| 1277 | |
| 1278 | rc = qpnp_chg_masked_write(chip, |
| 1279 | chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_1, |
| 1280 | 0xC0, enable ? 0x00 : COMPATATOR_OVERRIDE_0, 1); |
| 1281 | if (rc) { |
| 1282 | pr_debug("failed to toggle chg done override rc=%d\n", rc); |
| 1283 | return rc; |
| 1284 | } |
| 1285 | |
| 1286 | return rc; |
| 1287 | } |
| 1288 | |
| 1289 | #define QPNP_CHG_VBATDET_MIN_MV 3240 |
| 1290 | #define QPNP_CHG_VBATDET_MAX_MV 5780 |
| 1291 | #define QPNP_CHG_VBATDET_STEP_MV 20 |
| 1292 | static int |
| 1293 | qpnp_chg_vbatdet_set(struct qpnp_chg_chip *chip, int vbatdet_mv) |
| 1294 | { |
| 1295 | u8 temp; |
| 1296 | |
| 1297 | if (vbatdet_mv < QPNP_CHG_VBATDET_MIN_MV |
| 1298 | || vbatdet_mv > QPNP_CHG_VBATDET_MAX_MV) { |
| 1299 | pr_err("bad mV=%d asked to set\n", vbatdet_mv); |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | temp = (vbatdet_mv - QPNP_CHG_VBATDET_MIN_MV) |
| 1303 | / QPNP_CHG_VBATDET_STEP_MV; |
| 1304 | |
| 1305 | pr_debug("voltage=%d setting %02x\n", vbatdet_mv, temp); |
| 1306 | return qpnp_chg_write(chip, &temp, |
| 1307 | chip->chgr_base + CHGR_VBAT_DET, 1); |
| 1308 | } |
| 1309 | |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1310 | static void |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 1311 | qpnp_chg_set_appropriate_vbatdet(struct qpnp_chg_chip *chip) |
| 1312 | { |
| 1313 | if (chip->bat_is_cool) |
| 1314 | qpnp_chg_vbatdet_set(chip, chip->cool_bat_mv |
| 1315 | - chip->resume_delta_mv); |
| 1316 | else if (chip->bat_is_warm) |
| 1317 | qpnp_chg_vbatdet_set(chip, chip->warm_bat_mv |
| 1318 | - chip->resume_delta_mv); |
| 1319 | else if (chip->resuming_charging) |
| 1320 | qpnp_chg_vbatdet_set(chip, chip->max_voltage_mv |
| 1321 | + chip->resume_delta_mv); |
| 1322 | else |
| 1323 | qpnp_chg_vbatdet_set(chip, chip->max_voltage_mv |
| 1324 | - chip->resume_delta_mv); |
| 1325 | } |
| 1326 | |
| 1327 | static void |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1328 | qpnp_arb_stop_work(struct work_struct *work) |
| 1329 | { |
| 1330 | struct delayed_work *dwork = to_delayed_work(work); |
| 1331 | struct qpnp_chg_chip *chip = container_of(dwork, |
| 1332 | struct qpnp_chg_chip, arb_stop_work); |
| 1333 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1334 | if (!chip->chg_done) |
| 1335 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1336 | qpnp_chg_force_run_on_batt(chip, chip->charging_disabled); |
| 1337 | } |
| 1338 | |
| 1339 | static void |
| 1340 | qpnp_bat_if_adc_measure_work(struct work_struct *work) |
David Keitel | 79f4c93 | 2013-04-03 16:08:39 -0700 | [diff] [blame] | 1341 | { |
| 1342 | struct qpnp_chg_chip *chip = container_of(work, |
| 1343 | struct qpnp_chg_chip, adc_measure_work); |
| 1344 | |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 1345 | if (qpnp_adc_tm_channel_measure(chip->adc_tm_dev, &chip->adc_param)) |
David Keitel | 79f4c93 | 2013-04-03 16:08:39 -0700 | [diff] [blame] | 1346 | pr_err("request ADC error\n"); |
| 1347 | } |
| 1348 | |
David Keitel | ecab15a | 2013-08-06 15:41:39 -0700 | [diff] [blame] | 1349 | static void |
| 1350 | qpnp_bat_if_adc_disable_work(struct work_struct *work) |
| 1351 | { |
| 1352 | struct qpnp_chg_chip *chip = container_of(work, |
| 1353 | struct qpnp_chg_chip, adc_disable_work); |
| 1354 | |
| 1355 | qpnp_adc_tm_disable_chan_meas(chip->adc_tm_dev, &chip->adc_param); |
| 1356 | } |
| 1357 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1358 | #define EOC_CHECK_PERIOD_MS 10000 |
| 1359 | static irqreturn_t |
| 1360 | qpnp_chg_vbatdet_lo_irq_handler(int irq, void *_chip) |
| 1361 | { |
| 1362 | struct qpnp_chg_chip *chip = _chip; |
| 1363 | u8 chg_sts = 0; |
| 1364 | int rc; |
| 1365 | |
| 1366 | pr_debug("vbatdet-lo triggered\n"); |
| 1367 | |
| 1368 | rc = qpnp_chg_read(chip, &chg_sts, INT_RT_STS(chip->chgr_base), 1); |
| 1369 | if (rc) |
| 1370 | pr_err("failed to read chg_sts rc=%d\n", rc); |
| 1371 | |
| 1372 | pr_debug("chg_done chg_sts: 0x%x triggered\n", chg_sts); |
| 1373 | if (!chip->charging_disabled && (chg_sts & FAST_CHG_ON_IRQ)) { |
| 1374 | schedule_delayed_work(&chip->eoc_work, |
| 1375 | msecs_to_jiffies(EOC_CHECK_PERIOD_MS)); |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 1376 | pm_stay_awake(chip->dev); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1377 | } |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 1378 | qpnp_chg_disable_irq(&chip->chg_vbatdet_lo); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1379 | |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1380 | pr_debug("psy changed usb_psy\n"); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1381 | power_supply_changed(chip->usb_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1382 | if (chip->dc_chgpth_base) { |
| 1383 | pr_debug("psy changed dc_psy\n"); |
David Keitel | dbcef09 | 2013-05-14 14:48:30 -0700 | [diff] [blame] | 1384 | power_supply_changed(&chip->dc_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1385 | } |
| 1386 | if (chip->bat_if_base) { |
| 1387 | pr_debug("psy changed batt_psy\n"); |
David Keitel | dbcef09 | 2013-05-14 14:48:30 -0700 | [diff] [blame] | 1388 | power_supply_changed(&chip->batt_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1389 | } |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1390 | return IRQ_HANDLED; |
| 1391 | } |
| 1392 | |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1393 | #define ARB_STOP_WORK_MS 1000 |
| 1394 | static irqreturn_t |
| 1395 | qpnp_chg_usb_chg_gone_irq_handler(int irq, void *_chip) |
| 1396 | { |
| 1397 | struct qpnp_chg_chip *chip = _chip; |
David Keitel | 7e38f51 | 2013-11-19 11:15:11 -0800 | [diff] [blame] | 1398 | u8 usb_sts; |
| 1399 | int rc; |
| 1400 | |
| 1401 | rc = qpnp_chg_read(chip, &usb_sts, |
| 1402 | INT_RT_STS(chip->usb_chgpth_base), 1); |
| 1403 | if (rc) |
| 1404 | pr_err("failed to read usb_chgpth_sts rc=%d\n", rc); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1405 | |
| 1406 | pr_debug("chg_gone triggered\n"); |
David Keitel | d53183a | 2014-01-07 17:17:44 -0800 | [diff] [blame] | 1407 | if ((qpnp_chg_is_usb_chg_plugged_in(chip) |
| 1408 | || qpnp_chg_is_dc_chg_plugged_in(chip)) |
| 1409 | && (usb_sts & CHG_GONE_IRQ)) { |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 1410 | if (ext_ovp_isns_present) { |
| 1411 | pr_debug("EXT OVP IC ISNS disabled due to ARB WA\n"); |
| 1412 | gpio_direction_output(chip->ext_ovp_isns_gpio, 0); |
| 1413 | } |
| 1414 | |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1415 | qpnp_chg_charge_en(chip, 0); |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 1416 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1417 | qpnp_chg_force_run_on_batt(chip, 1); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1418 | schedule_delayed_work(&chip->arb_stop_work, |
| 1419 | msecs_to_jiffies(ARB_STOP_WORK_MS)); |
| 1420 | } |
| 1421 | |
| 1422 | return IRQ_HANDLED; |
| 1423 | } |
| 1424 | |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 1425 | static irqreturn_t |
| 1426 | qpnp_chg_usb_usb_ocp_irq_handler(int irq, void *_chip) |
| 1427 | { |
| 1428 | struct qpnp_chg_chip *chip = _chip; |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 1429 | |
| 1430 | pr_debug("usb-ocp triggered\n"); |
| 1431 | |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 1432 | schedule_work(&chip->ocp_clear_work); |
| 1433 | |
| 1434 | return IRQ_HANDLED; |
| 1435 | } |
| 1436 | |
| 1437 | #define BOOST_ILIMIT_MIN 0x07 |
| 1438 | #define BOOST_ILIMIT_DEF 0x02 |
| 1439 | #define BOOST_ILIMT_MASK 0xFF |
| 1440 | static void |
| 1441 | qpnp_chg_ocp_clear_work(struct work_struct *work) |
| 1442 | { |
| 1443 | int rc; |
| 1444 | u8 usb_sts; |
| 1445 | struct qpnp_chg_chip *chip = container_of(work, |
| 1446 | struct qpnp_chg_chip, ocp_clear_work); |
| 1447 | |
| 1448 | if (chip->type == SMBBP) { |
| 1449 | rc = qpnp_chg_masked_write(chip, |
| 1450 | chip->boost_base + BOOST_ILIM, |
| 1451 | BOOST_ILIMT_MASK, |
| 1452 | BOOST_ILIMIT_MIN, 1); |
| 1453 | if (rc) { |
| 1454 | pr_err("Failed to turn configure ilim rc = %d\n", rc); |
| 1455 | return; |
| 1456 | } |
| 1457 | } |
| 1458 | |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 1459 | rc = qpnp_chg_masked_write(chip, |
| 1460 | chip->usb_chgpth_base + USB_OCP_CLR, |
| 1461 | OCP_CLR_BIT, |
| 1462 | OCP_CLR_BIT, 1); |
| 1463 | if (rc) |
| 1464 | pr_err("Failed to clear OCP bit rc = %d\n", rc); |
| 1465 | |
| 1466 | /* force usb ovp fet off */ |
| 1467 | rc = qpnp_chg_masked_write(chip, |
| 1468 | chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL, |
| 1469 | USB_OTG_EN_BIT, |
| 1470 | USB_OTG_EN_BIT, 1); |
| 1471 | if (rc) |
| 1472 | pr_err("Failed to turn off usb ovp rc = %d\n", rc); |
| 1473 | |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 1474 | if (chip->type == SMBBP) { |
| 1475 | /* Wait for OCP circuitry to be powered up */ |
| 1476 | msleep(100); |
| 1477 | rc = qpnp_chg_read(chip, &usb_sts, |
| 1478 | INT_RT_STS(chip->usb_chgpth_base), 1); |
| 1479 | if (rc) { |
| 1480 | pr_err("failed to read interrupt sts %d\n", rc); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | if (usb_sts & COARSE_DET_USB_IRQ) { |
| 1485 | rc = qpnp_chg_masked_write(chip, |
| 1486 | chip->boost_base + BOOST_ILIM, |
| 1487 | BOOST_ILIMT_MASK, |
| 1488 | BOOST_ILIMIT_DEF, 1); |
| 1489 | if (rc) { |
| 1490 | pr_err("Failed to set ilim rc = %d\n", rc); |
| 1491 | return; |
| 1492 | } |
| 1493 | } else { |
| 1494 | pr_warn_ratelimited("USB short to GND detected!\n"); |
| 1495 | } |
| 1496 | } |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 1499 | #define QPNP_CHG_VDDMAX_MIN 3400 |
| 1500 | #define QPNP_CHG_V_MIN_MV 3240 |
| 1501 | #define QPNP_CHG_V_MAX_MV 4500 |
| 1502 | #define QPNP_CHG_V_STEP_MV 10 |
| 1503 | #define QPNP_CHG_BUCK_TRIM1_STEP 10 |
| 1504 | #define QPNP_CHG_BUCK_VDD_TRIM_MASK 0xF0 |
| 1505 | static int |
| 1506 | qpnp_chg_vddmax_and_trim_set(struct qpnp_chg_chip *chip, |
| 1507 | int voltage, int trim_mv) |
| 1508 | { |
| 1509 | int rc, trim_set; |
| 1510 | u8 vddmax = 0, trim = 0; |
| 1511 | |
| 1512 | if (voltage < QPNP_CHG_VDDMAX_MIN |
| 1513 | || voltage > QPNP_CHG_V_MAX_MV) { |
| 1514 | pr_err("bad mV=%d asked to set\n", voltage); |
| 1515 | return -EINVAL; |
| 1516 | } |
| 1517 | |
| 1518 | vddmax = (voltage - QPNP_CHG_V_MIN_MV) / QPNP_CHG_V_STEP_MV; |
| 1519 | rc = qpnp_chg_write(chip, &vddmax, chip->chgr_base + CHGR_VDD_MAX, 1); |
| 1520 | if (rc) { |
| 1521 | pr_err("Failed to write vddmax: %d\n", rc); |
| 1522 | return rc; |
| 1523 | } |
| 1524 | |
| 1525 | rc = qpnp_chg_masked_write(chip, |
| 1526 | chip->buck_base + SEC_ACCESS, |
| 1527 | 0xFF, |
| 1528 | 0xA5, 1); |
| 1529 | if (rc) { |
| 1530 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 1531 | return rc; |
| 1532 | } |
| 1533 | trim_set = clamp((int)chip->trim_center |
| 1534 | + (trim_mv / QPNP_CHG_BUCK_TRIM1_STEP), |
| 1535 | 0, 0xF); |
| 1536 | trim = (u8)trim_set << 4; |
| 1537 | rc = qpnp_chg_masked_write(chip, |
| 1538 | chip->buck_base + BUCK_CTRL_TRIM1, |
| 1539 | QPNP_CHG_BUCK_VDD_TRIM_MASK, |
| 1540 | trim, 1); |
| 1541 | if (rc) { |
| 1542 | pr_err("Failed to write buck trim1: %d\n", rc); |
| 1543 | return rc; |
| 1544 | } |
| 1545 | pr_debug("voltage=%d+%d setting vddmax: %02x, trim: %02x\n", |
| 1546 | voltage, trim_mv, vddmax, trim); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Xiaozhe Shi | e850448 | 2013-10-30 18:10:31 -0700 | [diff] [blame] | 1550 | static int |
| 1551 | qpnp_chg_vddmax_get(struct qpnp_chg_chip *chip) |
| 1552 | { |
| 1553 | int rc; |
| 1554 | u8 vddmax = 0; |
| 1555 | |
| 1556 | rc = qpnp_chg_read(chip, &vddmax, chip->chgr_base + CHGR_VDD_MAX, 1); |
| 1557 | if (rc) { |
| 1558 | pr_err("Failed to write vddmax: %d\n", rc); |
| 1559 | return rc; |
| 1560 | } |
| 1561 | |
| 1562 | return QPNP_CHG_V_MIN_MV + (int)vddmax * QPNP_CHG_V_STEP_MV; |
| 1563 | } |
| 1564 | |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 1565 | /* JEITA compliance logic */ |
| 1566 | static void |
| 1567 | qpnp_chg_set_appropriate_vddmax(struct qpnp_chg_chip *chip) |
| 1568 | { |
| 1569 | if (chip->bat_is_cool) |
| 1570 | qpnp_chg_vddmax_and_trim_set(chip, chip->cool_bat_mv, |
| 1571 | chip->delta_vddmax_mv); |
| 1572 | else if (chip->bat_is_warm) |
| 1573 | qpnp_chg_vddmax_and_trim_set(chip, chip->warm_bat_mv, |
| 1574 | chip->delta_vddmax_mv); |
| 1575 | else |
| 1576 | qpnp_chg_vddmax_and_trim_set(chip, chip->max_voltage_mv, |
| 1577 | chip->delta_vddmax_mv); |
| 1578 | } |
| 1579 | |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1580 | static void |
| 1581 | qpnp_usbin_health_check_work(struct work_struct *work) |
| 1582 | { |
| 1583 | int usbin_health = 0; |
| 1584 | u8 psy_health_sts = 0; |
| 1585 | struct delayed_work *dwork = to_delayed_work(work); |
| 1586 | struct qpnp_chg_chip *chip = container_of(dwork, |
| 1587 | struct qpnp_chg_chip, usbin_health_check); |
| 1588 | |
| 1589 | usbin_health = qpnp_chg_check_usbin_health(chip); |
| 1590 | spin_lock(&chip->usbin_health_monitor_lock); |
| 1591 | if (chip->usbin_health != usbin_health) { |
| 1592 | pr_debug("health_check_work: pr_usbin_health = %d, usbin_health = %d", |
| 1593 | chip->usbin_health, usbin_health); |
| 1594 | chip->usbin_health = usbin_health; |
| 1595 | if (usbin_health == USBIN_OVP) |
| 1596 | psy_health_sts = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 1597 | else if (usbin_health == USBIN_OK) |
| 1598 | psy_health_sts = POWER_SUPPLY_HEALTH_GOOD; |
| 1599 | power_supply_set_health_state(chip->usb_psy, psy_health_sts); |
| 1600 | power_supply_changed(chip->usb_psy); |
| 1601 | } |
| 1602 | /* enable OVP monitor in usb valid after coarse-det complete */ |
| 1603 | chip->usb_valid_check_ovp = true; |
| 1604 | spin_unlock(&chip->usbin_health_monitor_lock); |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | #define USB_VALID_DEBOUNCE_TIME_MASK 0x3 |
| 1609 | #define USB_DEB_BYPASS 0x0 |
| 1610 | #define USB_DEB_5MS 0x1 |
| 1611 | #define USB_DEB_10MS 0x2 |
| 1612 | #define USB_DEB_20MS 0x3 |
| 1613 | static irqreturn_t |
| 1614 | qpnp_chg_coarse_det_usb_irq_handler(int irq, void *_chip) |
| 1615 | { |
| 1616 | struct qpnp_chg_chip *chip = _chip; |
| 1617 | int host_mode, rc = 0; |
| 1618 | int debounce[] = { |
| 1619 | [USB_DEB_BYPASS] = 0, |
| 1620 | [USB_DEB_5MS] = 5, |
| 1621 | [USB_DEB_10MS] = 10, |
| 1622 | [USB_DEB_20MS] = 20 }; |
| 1623 | u8 ovp_ctl; |
| 1624 | bool usb_coarse_det; |
| 1625 | |
| 1626 | host_mode = qpnp_chg_is_otg_en_set(chip); |
| 1627 | usb_coarse_det = qpnp_chg_check_usb_coarse_det(chip); |
| 1628 | pr_debug("usb coarse-det triggered: %d host_mode: %d\n", |
| 1629 | usb_coarse_det, host_mode); |
| 1630 | |
| 1631 | if (host_mode) |
| 1632 | return IRQ_HANDLED; |
| 1633 | /* ignore to monitor OVP in usbin valid irq handler |
| 1634 | if the coarse-det fired first, do the OVP state monitor |
| 1635 | in the usbin_health_check work, and after the work, |
| 1636 | enable monitor OVP in usbin valid irq handler */ |
| 1637 | chip->usb_valid_check_ovp = false; |
| 1638 | if (chip->usb_coarse_det ^ usb_coarse_det) { |
| 1639 | chip->usb_coarse_det = usb_coarse_det; |
| 1640 | if (usb_coarse_det) { |
| 1641 | /* usb coarse-det rising edge, check the usbin_valid |
| 1642 | debounce time setting, and start a delay work to |
| 1643 | check the OVP status*/ |
| 1644 | rc = qpnp_chg_read(chip, &ovp_ctl, |
| 1645 | chip->usb_chgpth_base + USB_OVP_CTL, 1); |
| 1646 | |
| 1647 | if (rc) { |
| 1648 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 1649 | chip->usb_chgpth_base + USB_OVP_CTL, |
| 1650 | rc); |
| 1651 | return rc; |
| 1652 | } |
| 1653 | ovp_ctl = ovp_ctl & USB_VALID_DEBOUNCE_TIME_MASK; |
| 1654 | schedule_delayed_work(&chip->usbin_health_check, |
| 1655 | msecs_to_jiffies(debounce[ovp_ctl])); |
| 1656 | } else { |
| 1657 | /* usb coarse-det rising edge, set the usb psy health |
| 1658 | status to unknown */ |
| 1659 | pr_debug("usb coarse det clear, set usb health to unknown\n"); |
| 1660 | chip->usbin_health = USBIN_UNKNOW; |
| 1661 | power_supply_set_health_state(chip->usb_psy, |
| 1662 | POWER_SUPPLY_HEALTH_UNKNOWN); |
| 1663 | power_supply_changed(chip->usb_psy); |
| 1664 | } |
| 1665 | |
| 1666 | } |
| 1667 | return IRQ_HANDLED; |
| 1668 | } |
| 1669 | |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 1670 | #define BATFET_LPM_MASK 0xC0 |
| 1671 | #define BATFET_LPM 0x40 |
| 1672 | #define BATFET_NO_LPM 0x00 |
| 1673 | static int |
| 1674 | qpnp_chg_regulator_batfet_set(struct qpnp_chg_chip *chip, bool enable) |
| 1675 | { |
| 1676 | int rc = 0; |
| 1677 | |
David Keitel | cadbc28 | 2013-09-26 11:39:14 -0700 | [diff] [blame] | 1678 | if (chip->charging_disabled || !chip->bat_if_base) |
| 1679 | return rc; |
| 1680 | |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 1681 | if (chip->type == SMBB) |
| 1682 | rc = qpnp_chg_masked_write(chip, |
| 1683 | chip->bat_if_base + CHGR_BAT_IF_SPARE, |
| 1684 | BATFET_LPM_MASK, |
| 1685 | enable ? BATFET_NO_LPM : BATFET_LPM, 1); |
| 1686 | else |
| 1687 | rc = qpnp_chg_masked_write(chip, |
| 1688 | chip->bat_if_base + CHGR_BAT_IF_BATFET_CTRL4, |
| 1689 | BATFET_LPM_MASK, |
| 1690 | enable ? BATFET_NO_LPM : BATFET_LPM, 1); |
| 1691 | |
| 1692 | return rc; |
| 1693 | } |
| 1694 | |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 1695 | #define USB_WALL_THRESHOLD_MA 500 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1696 | #define ENUM_T_STOP_BIT BIT(0) |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 1697 | #define USB_5V_UV 5000000 |
| 1698 | #define USB_9V_UV 9000000 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1699 | static irqreturn_t |
| 1700 | qpnp_chg_usb_usbin_valid_irq_handler(int irq, void *_chip) |
| 1701 | { |
| 1702 | struct qpnp_chg_chip *chip = _chip; |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1703 | int usb_present, host_mode, usbin_health; |
| 1704 | u8 psy_health_sts; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1705 | |
| 1706 | usb_present = qpnp_chg_is_usb_chg_plugged_in(chip); |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 1707 | host_mode = qpnp_chg_is_otg_en_set(chip); |
| 1708 | pr_debug("usbin-valid triggered: %d host_mode: %d\n", |
| 1709 | usb_present, host_mode); |
| 1710 | |
| 1711 | /* In host mode notifications cmoe from USB supply */ |
| 1712 | if (host_mode) |
| 1713 | return IRQ_HANDLED; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1714 | |
| 1715 | if (chip->usb_present ^ usb_present) { |
Abhijeet Dharmapurikar | 02e5956 | 2014-08-12 19:31:31 -0700 | [diff] [blame] | 1716 | chip->aicl_settled = false; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1717 | chip->usb_present = usb_present; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1718 | if (!usb_present) { |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1719 | /* when a valid charger inserted, and increase the |
| 1720 | * charger voltage to OVP threshold, then |
| 1721 | * usb_in_valid falling edge interrupt triggers. |
| 1722 | * So we handle the OVP monitor here, and ignore |
| 1723 | * other health state changes */ |
| 1724 | if (chip->ovp_monitor_enable && |
| 1725 | (chip->usb_valid_check_ovp)) { |
| 1726 | usbin_health = |
| 1727 | qpnp_chg_check_usbin_health(chip); |
Wu Fenglin | 15c907c | 2013-10-18 18:08:33 +0800 | [diff] [blame] | 1728 | if ((chip->usbin_health != usbin_health) |
| 1729 | && (usbin_health == USBIN_OVP)) { |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1730 | chip->usbin_health = usbin_health; |
Wu Fenglin | 15c907c | 2013-10-18 18:08:33 +0800 | [diff] [blame] | 1731 | psy_health_sts = |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1732 | POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 1733 | power_supply_set_health_state( |
| 1734 | chip->usb_psy, |
| 1735 | psy_health_sts); |
| 1736 | power_supply_changed(chip->usb_psy); |
| 1737 | } |
| 1738 | } |
Xiaozhe Shi | 9e887be | 2014-05-21 15:18:58 -0700 | [diff] [blame] | 1739 | if (!qpnp_chg_is_dc_chg_plugged_in(chip)) |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 1740 | chip->chg_done = false; |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 1741 | |
| 1742 | if (!qpnp_is_dc_higher_prio(chip)) |
| 1743 | qpnp_chg_idcmax_set(chip, chip->maxinput_dc_ma); |
| 1744 | |
David Keitel | a918560 | 2013-11-12 17:55:48 -0800 | [diff] [blame] | 1745 | qpnp_chg_usb_suspend_enable(chip, 0); |
David Keitel | 7b056848 | 2013-12-02 14:47:42 -0800 | [diff] [blame] | 1746 | qpnp_chg_iusbmax_set(chip, QPNP_CHG_I_MAX_MIN_100); |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 1747 | qpnp_chg_iusb_trim_set(chip, chip->usb_trim_default); |
David Keitel | fd305ee | 2013-07-19 20:19:06 -0700 | [diff] [blame] | 1748 | chip->prev_usb_max_ma = -EINVAL; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1749 | } else { |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1750 | /* when OVP clamped usbin, and then decrease |
| 1751 | * the charger voltage to lower than the OVP |
| 1752 | * threshold, a usbin_valid rising edge |
| 1753 | * interrupt triggered. So we change the usb |
| 1754 | * psy health state back to good */ |
| 1755 | if (chip->ovp_monitor_enable && |
| 1756 | (chip->usb_valid_check_ovp)) { |
| 1757 | usbin_health = |
| 1758 | qpnp_chg_check_usbin_health(chip); |
Wu Fenglin | 15c907c | 2013-10-18 18:08:33 +0800 | [diff] [blame] | 1759 | if ((chip->usbin_health != usbin_health) |
| 1760 | && (usbin_health == USBIN_OK)) { |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1761 | chip->usbin_health = usbin_health; |
Wu Fenglin | 15c907c | 2013-10-18 18:08:33 +0800 | [diff] [blame] | 1762 | psy_health_sts = |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 1763 | POWER_SUPPLY_HEALTH_GOOD; |
| 1764 | power_supply_set_health_state( |
| 1765 | chip->usb_psy, |
| 1766 | psy_health_sts); |
| 1767 | power_supply_changed(chip->usb_psy); |
| 1768 | } |
| 1769 | } |
| 1770 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1771 | schedule_delayed_work(&chip->eoc_work, |
| 1772 | msecs_to_jiffies(EOC_CHECK_PERIOD_MS)); |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 1773 | schedule_work(&chip->soc_check_work); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1774 | } |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 1775 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1776 | power_supply_set_present(chip->usb_psy, chip->usb_present); |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 1777 | schedule_work(&chip->batfet_lcl_work); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | return IRQ_HANDLED; |
| 1781 | } |
| 1782 | |
Fenglin Wu | c64e994 | 2014-08-22 13:47:34 +0800 | [diff] [blame] | 1783 | #define BUCK_VIN_LOOP_CMP_OVRD_MASK 0x30 |
| 1784 | static int |
| 1785 | qpnp_chg_bypass_vchg_loop_debouncer(struct qpnp_chg_chip *chip, bool bypass) |
| 1786 | { |
| 1787 | int rc; |
| 1788 | u8 value = bypass ? 0x10 : 0; |
| 1789 | |
| 1790 | pr_debug("bypass vchg_loop debouncer: %d\n", bypass); |
| 1791 | |
| 1792 | rc = qpnp_chg_masked_write(chip, chip->buck_base + SEC_ACCESS, |
| 1793 | 0xFF, 0xA5, 1); |
| 1794 | if (rc) { |
| 1795 | pr_err("failed to write SEC_ACCESS register, rc = %d\n", rc); |
| 1796 | return rc; |
| 1797 | } |
| 1798 | |
| 1799 | rc = qpnp_chg_masked_write(chip, |
| 1800 | chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_2, |
| 1801 | BUCK_VIN_LOOP_CMP_OVRD_MASK, value, 1); |
| 1802 | if (rc) |
| 1803 | pr_err("failed to write BUCK_COMP_OVRIDE_2, rc = %d\n", rc); |
| 1804 | |
| 1805 | return rc; |
| 1806 | } |
| 1807 | |
| 1808 | static int |
| 1809 | qpnp_chg_vchg_loop_debouncer_setting_get(struct qpnp_chg_chip *chip) |
| 1810 | { |
| 1811 | int rc; |
| 1812 | u8 value; |
| 1813 | |
| 1814 | rc = qpnp_chg_read(chip, &value, |
| 1815 | chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_2, 1); |
| 1816 | if (rc) { |
| 1817 | pr_err("failed to read BUCK_CMP_OVERIDE_2, rc = %d\n", rc); |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | return value & BUCK_VIN_LOOP_CMP_OVRD_MASK; |
| 1822 | } |
| 1823 | |
Xiaozhe Shi | 6c72665 | 2014-01-03 15:45:14 -0800 | [diff] [blame] | 1824 | #define TEST_EN_SMBC_LOOP 0xE5 |
| 1825 | #define IBAT_REGULATION_DISABLE BIT(2) |
David Keitel | 7450dcd | 2013-01-29 18:41:41 -0800 | [diff] [blame] | 1826 | static irqreturn_t |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1827 | qpnp_chg_bat_if_batt_temp_irq_handler(int irq, void *_chip) |
| 1828 | { |
| 1829 | struct qpnp_chg_chip *chip = _chip; |
Xiaozhe Shi | 8783186 | 2014-01-20 17:44:40 -0800 | [diff] [blame] | 1830 | int batt_temp_good, batt_present, rc; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1831 | |
| 1832 | batt_temp_good = qpnp_chg_is_batt_temp_ok(chip); |
| 1833 | pr_debug("batt-temp triggered: %d\n", batt_temp_good); |
| 1834 | |
Xiaozhe Shi | 8783186 | 2014-01-20 17:44:40 -0800 | [diff] [blame] | 1835 | batt_present = qpnp_chg_is_batt_present(chip); |
| 1836 | if (batt_present) { |
| 1837 | rc = qpnp_chg_masked_write(chip, |
| 1838 | chip->buck_base + SEC_ACCESS, |
| 1839 | 0xFF, |
| 1840 | 0xA5, 1); |
| 1841 | if (rc) { |
| 1842 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 1843 | return rc; |
| 1844 | } |
Xiaozhe Shi | 6c72665 | 2014-01-03 15:45:14 -0800 | [diff] [blame] | 1845 | |
Xiaozhe Shi | 8783186 | 2014-01-20 17:44:40 -0800 | [diff] [blame] | 1846 | rc = qpnp_chg_masked_write(chip, |
| 1847 | chip->buck_base + TEST_EN_SMBC_LOOP, |
| 1848 | IBAT_REGULATION_DISABLE, |
| 1849 | batt_temp_good ? 0 : IBAT_REGULATION_DISABLE, 1); |
| 1850 | if (rc) { |
| 1851 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 1852 | return rc; |
| 1853 | } |
Xiaozhe Shi | 6c72665 | 2014-01-03 15:45:14 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1856 | pr_debug("psy changed batt_psy\n"); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1857 | power_supply_changed(&chip->batt_psy); |
| 1858 | return IRQ_HANDLED; |
| 1859 | } |
| 1860 | |
| 1861 | static irqreturn_t |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 1862 | qpnp_chg_bat_if_batt_pres_irq_handler(int irq, void *_chip) |
| 1863 | { |
| 1864 | struct qpnp_chg_chip *chip = _chip; |
Xiaozhe Shi | 8783186 | 2014-01-20 17:44:40 -0800 | [diff] [blame] | 1865 | int batt_present, batt_temp_good, rc; |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 1866 | |
| 1867 | batt_present = qpnp_chg_is_batt_present(chip); |
| 1868 | pr_debug("batt-pres triggered: %d\n", batt_present); |
| 1869 | |
| 1870 | if (chip->batt_present ^ batt_present) { |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 1871 | if (batt_present) { |
Xiaozhe Shi | 8783186 | 2014-01-20 17:44:40 -0800 | [diff] [blame] | 1872 | batt_temp_good = qpnp_chg_is_batt_temp_ok(chip); |
| 1873 | rc = qpnp_chg_masked_write(chip, |
| 1874 | chip->buck_base + SEC_ACCESS, |
| 1875 | 0xFF, |
| 1876 | 0xA5, 1); |
| 1877 | if (rc) { |
| 1878 | pr_err("failed to write SEC_ACCESS: %d\n", rc); |
| 1879 | return rc; |
| 1880 | } |
| 1881 | |
| 1882 | rc = qpnp_chg_masked_write(chip, |
| 1883 | chip->buck_base + TEST_EN_SMBC_LOOP, |
| 1884 | IBAT_REGULATION_DISABLE, |
| 1885 | batt_temp_good |
| 1886 | ? 0 : IBAT_REGULATION_DISABLE, 1); |
| 1887 | if (rc) { |
| 1888 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 1889 | return rc; |
| 1890 | } |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 1891 | schedule_work(&chip->insertion_ocv_work); |
| 1892 | } else { |
Xiaozhe Shi | 8783186 | 2014-01-20 17:44:40 -0800 | [diff] [blame] | 1893 | rc = qpnp_chg_masked_write(chip, |
| 1894 | chip->buck_base + SEC_ACCESS, |
| 1895 | 0xFF, |
| 1896 | 0xA5, 1); |
| 1897 | if (rc) { |
| 1898 | pr_err("failed to write SEC_ACCESS: %d\n", rc); |
| 1899 | return rc; |
| 1900 | } |
| 1901 | |
| 1902 | rc = qpnp_chg_masked_write(chip, |
| 1903 | chip->buck_base + TEST_EN_SMBC_LOOP, |
| 1904 | IBAT_REGULATION_DISABLE, |
| 1905 | 0, 1); |
| 1906 | if (rc) { |
| 1907 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 1908 | return rc; |
| 1909 | } |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 1910 | chip->insertion_ocv_uv = 0; |
| 1911 | qpnp_chg_charge_en(chip, 0); |
| 1912 | } |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 1913 | chip->batt_present = batt_present; |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1914 | pr_debug("psy changed batt_psy\n"); |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 1915 | power_supply_changed(&chip->batt_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1916 | pr_debug("psy changed usb_psy\n"); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1917 | power_supply_changed(chip->usb_psy); |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 1918 | |
Zhenhua Huang | 4c593ec | 2013-08-13 17:22:58 +0800 | [diff] [blame] | 1919 | if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc) |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 1920 | && batt_present) { |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 1921 | pr_debug("enabling vadc notifications\n"); |
David Keitel | 79f4c93 | 2013-04-03 16:08:39 -0700 | [diff] [blame] | 1922 | schedule_work(&chip->adc_measure_work); |
Zhenhua Huang | 4c593ec | 2013-08-13 17:22:58 +0800 | [diff] [blame] | 1923 | } else if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc) |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 1924 | && !batt_present) { |
David Keitel | ecab15a | 2013-08-06 15:41:39 -0700 | [diff] [blame] | 1925 | schedule_work(&chip->adc_disable_work); |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 1926 | pr_debug("disabling vadc notifications\n"); |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 1927 | } |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | return IRQ_HANDLED; |
| 1931 | } |
| 1932 | |
| 1933 | static irqreturn_t |
David Keitel | 7450dcd | 2013-01-29 18:41:41 -0800 | [diff] [blame] | 1934 | qpnp_chg_dc_dcin_valid_irq_handler(int irq, void *_chip) |
| 1935 | { |
| 1936 | struct qpnp_chg_chip *chip = _chip; |
| 1937 | int dc_present; |
| 1938 | |
| 1939 | dc_present = qpnp_chg_is_dc_chg_plugged_in(chip); |
| 1940 | pr_debug("dcin-valid triggered: %d\n", dc_present); |
| 1941 | |
| 1942 | if (chip->dc_present ^ dc_present) { |
| 1943 | chip->dc_present = dc_present; |
David Keitel | 6d4b9da | 2013-09-05 17:38:40 -0700 | [diff] [blame] | 1944 | if (qpnp_chg_is_otg_en_set(chip)) |
| 1945 | qpnp_chg_force_run_on_batt(chip, !dc_present ? 1 : 0); |
David Keitel | 86628cc | 2014-06-09 13:24:00 -0700 | [diff] [blame] | 1946 | if (!dc_present && (!qpnp_chg_is_usb_chg_plugged_in(chip) || |
| 1947 | qpnp_chg_is_otg_en_set(chip))) { |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1948 | chip->chg_done = false; |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 1949 | } else { |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1950 | schedule_delayed_work(&chip->eoc_work, |
| 1951 | msecs_to_jiffies(EOC_CHECK_PERIOD_MS)); |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 1952 | schedule_work(&chip->soc_check_work); |
| 1953 | } |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 1954 | |
| 1955 | if (qpnp_is_dc_higher_prio(chip)) { |
| 1956 | pr_debug("dc has higher priority\n"); |
| 1957 | if (dc_present) { |
| 1958 | qpnp_chg_iusbmax_set(chip, |
| 1959 | QPNP_CHG_I_MAX_MIN_100); |
| 1960 | power_supply_set_voltage_limit(chip->usb_psy, |
| 1961 | USB_5V_UV); |
| 1962 | } else { |
| 1963 | chip->aicl_settled = false; |
| 1964 | qpnp_chg_iusbmax_set(chip, |
| 1965 | USB_WALL_THRESHOLD_MA); |
| 1966 | power_supply_set_voltage_limit(chip->usb_psy, |
| 1967 | USB_9V_UV); |
| 1968 | } |
| 1969 | } |
| 1970 | |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1971 | pr_debug("psy changed dc_psy\n"); |
David Keitel | 7450dcd | 2013-01-29 18:41:41 -0800 | [diff] [blame] | 1972 | power_supply_changed(&chip->dc_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1973 | pr_debug("psy changed batt_psy\n"); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1974 | power_supply_changed(&chip->batt_psy); |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 1975 | schedule_work(&chip->batfet_lcl_work); |
David Keitel | 7450dcd | 2013-01-29 18:41:41 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | return IRQ_HANDLED; |
| 1979 | } |
| 1980 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1981 | #define CHGR_CHG_FAILED_BIT BIT(7) |
| 1982 | static irqreturn_t |
| 1983 | qpnp_chg_chgr_chg_failed_irq_handler(int irq, void *_chip) |
| 1984 | { |
| 1985 | struct qpnp_chg_chip *chip = _chip; |
David Keitel | 4429b1f | 2012-10-18 10:42:50 -0700 | [diff] [blame] | 1986 | int rc; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1987 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 1988 | pr_debug("chg_failed triggered\n"); |
| 1989 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1990 | rc = qpnp_chg_masked_write(chip, |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 1991 | chip->chgr_base + CHGR_CHG_FAILED, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 1992 | CHGR_CHG_FAILED_BIT, |
| 1993 | CHGR_CHG_FAILED_BIT, 1); |
| 1994 | if (rc) |
| 1995 | pr_err("Failed to write chg_fail clear bit!\n"); |
| 1996 | |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 1997 | if (chip->bat_if_base) { |
| 1998 | pr_debug("psy changed batt_psy\n"); |
David Keitel | dbcef09 | 2013-05-14 14:48:30 -0700 | [diff] [blame] | 1999 | power_supply_changed(&chip->batt_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2000 | } |
| 2001 | pr_debug("psy changed usb_psy\n"); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 2002 | power_supply_changed(chip->usb_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2003 | if (chip->dc_chgpth_base) { |
| 2004 | pr_debug("psy changed dc_psy\n"); |
David Keitel | dbcef09 | 2013-05-14 14:48:30 -0700 | [diff] [blame] | 2005 | power_supply_changed(&chip->dc_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2006 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2007 | return IRQ_HANDLED; |
| 2008 | } |
| 2009 | |
| 2010 | static irqreturn_t |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 2011 | qpnp_chg_chgr_chg_trklchg_irq_handler(int irq, void *_chip) |
| 2012 | { |
| 2013 | struct qpnp_chg_chip *chip = _chip; |
| 2014 | |
| 2015 | pr_debug("TRKL IRQ triggered\n"); |
David Keitel | c9ffe84 | 2013-01-25 19:37:51 -0800 | [diff] [blame] | 2016 | |
| 2017 | chip->chg_done = false; |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2018 | if (chip->bat_if_base) { |
| 2019 | pr_debug("psy changed batt_psy\n"); |
David Keitel | dbcef09 | 2013-05-14 14:48:30 -0700 | [diff] [blame] | 2020 | power_supply_changed(&chip->batt_psy); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2021 | } |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 2022 | |
| 2023 | return IRQ_HANDLED; |
| 2024 | } |
| 2025 | |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2026 | static int qpnp_chg_is_fastchg_on(struct qpnp_chg_chip *chip) |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 2027 | { |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2028 | u8 chgr_sts; |
| 2029 | int rc; |
| 2030 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 2031 | qpnp_chg_irq_wake_disable(&chip->chg_fastchg); |
| 2032 | |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2033 | rc = qpnp_chg_read(chip, &chgr_sts, INT_RT_STS(chip->chgr_base), 1); |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2034 | if (rc) { |
| 2035 | pr_err("failed to read interrupt status %d\n", rc); |
| 2036 | return rc; |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2037 | } |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2038 | pr_debug("chgr_sts 0x%x\n", chgr_sts); |
| 2039 | return (chgr_sts & FAST_CHG_ON_IRQ) ? 1 : 0; |
| 2040 | } |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2041 | |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 2042 | #define VBATDET_BYPASS 0x01 |
| 2043 | static int |
| 2044 | bypass_vbatdet_comp(struct qpnp_chg_chip *chip, bool bypass) |
| 2045 | { |
| 2046 | int rc; |
| 2047 | |
| 2048 | pr_debug("bypass %d\n", bypass); |
| 2049 | rc = qpnp_chg_masked_write(chip, |
| 2050 | chip->chgr_base + SEC_ACCESS, |
| 2051 | 0xA5, |
| 2052 | 0xA5, 1); |
| 2053 | rc |= qpnp_chg_masked_write(chip, |
| 2054 | chip->chgr_base + CHGR_COMP_OVR1, |
| 2055 | 0xFF, |
| 2056 | bypass ? VBATDET_BYPASS : 0, 1); |
| 2057 | if (rc) { |
| 2058 | pr_err("Failed to bypass vbatdet comp rc = %d\n", rc); |
| 2059 | return rc; |
| 2060 | } |
| 2061 | |
| 2062 | return rc; |
| 2063 | } |
| 2064 | |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2065 | static irqreturn_t |
| 2066 | qpnp_chg_chgr_chg_fastchg_irq_handler(int irq, void *_chip) |
| 2067 | { |
| 2068 | struct qpnp_chg_chip *chip = _chip; |
| 2069 | bool fastchg_on = false; |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2070 | |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2071 | fastchg_on = qpnp_chg_is_fastchg_on(chip); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2072 | |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2073 | pr_debug("FAST_CHG IRQ triggered, fastchg_on: %d\n", fastchg_on); |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2074 | |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2075 | if (chip->fastchg_on ^ fastchg_on) { |
| 2076 | chip->fastchg_on = fastchg_on; |
| 2077 | if (chip->bat_if_base) { |
| 2078 | pr_debug("psy changed batt_psy\n"); |
| 2079 | power_supply_changed(&chip->batt_psy); |
| 2080 | } |
| 2081 | |
| 2082 | pr_debug("psy changed usb_psy\n"); |
| 2083 | power_supply_changed(chip->usb_psy); |
| 2084 | |
| 2085 | if (chip->dc_chgpth_base) { |
| 2086 | pr_debug("psy changed dc_psy\n"); |
| 2087 | power_supply_changed(&chip->dc_psy); |
| 2088 | } |
| 2089 | |
| 2090 | if (fastchg_on) { |
| 2091 | chip->chg_done = false; |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 2092 | bypass_vbatdet_comp(chip, 1); |
| 2093 | if (chip->bat_is_warm || chip->bat_is_cool) { |
| 2094 | qpnp_chg_set_appropriate_vddmax(chip); |
| 2095 | qpnp_chg_set_appropriate_battery_current(chip); |
| 2096 | } |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2097 | |
| 2098 | if (chip->resuming_charging) { |
| 2099 | chip->resuming_charging = false; |
| 2100 | qpnp_chg_set_appropriate_vbatdet(chip); |
| 2101 | } |
| 2102 | |
| 2103 | if (!chip->charging_disabled) { |
| 2104 | schedule_delayed_work(&chip->eoc_work, |
| 2105 | msecs_to_jiffies(EOC_CHECK_PERIOD_MS)); |
| 2106 | pm_stay_awake(chip->dev); |
| 2107 | } |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 2108 | if (chip->parallel_ovp_mode) |
| 2109 | switch_parallel_ovp_mode(chip, 1); |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 2110 | |
| 2111 | if (ext_ovp_isns_present && |
| 2112 | chip->ext_ovp_ic_gpio_enabled) { |
| 2113 | pr_debug("EXT OVP IC ISNS enabled\n"); |
| 2114 | gpio_direction_output( |
| 2115 | chip->ext_ovp_isns_gpio, 1); |
| 2116 | } |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 2117 | } else { |
| 2118 | if (chip->parallel_ovp_mode) |
| 2119 | switch_parallel_ovp_mode(chip, 0); |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 2120 | if (!chip->bat_is_warm && !chip->bat_is_cool) |
| 2121 | bypass_vbatdet_comp(chip, 0); |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 2122 | } |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 2125 | qpnp_chg_enable_irq(&chip->chg_vbatdet_lo); |
David Keitel | 42ae0aa | 2013-03-08 16:20:10 -0800 | [diff] [blame] | 2126 | |
| 2127 | return IRQ_HANDLED; |
| 2128 | } |
| 2129 | |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 2130 | static int |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 2131 | qpnp_dc_property_is_writeable(struct power_supply *psy, |
| 2132 | enum power_supply_property psp) |
| 2133 | { |
| 2134 | switch (psp) { |
| 2135 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 2136 | return 1; |
| 2137 | default: |
| 2138 | break; |
| 2139 | } |
| 2140 | |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | static int |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 2145 | qpnp_batt_property_is_writeable(struct power_supply *psy, |
| 2146 | enum power_supply_property psp) |
| 2147 | { |
| 2148 | switch (psp) { |
| 2149 | case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 2150 | case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL: |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2151 | case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX: |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 2152 | case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM: |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 2153 | case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED: |
Fenglin Wu | c64e994 | 2014-08-22 13:47:34 +0800 | [diff] [blame] | 2154 | case POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS: |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2155 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 2156 | case POWER_SUPPLY_PROP_COOL_TEMP: |
| 2157 | case POWER_SUPPLY_PROP_WARM_TEMP: |
Xiaozhe Shi | 78797cf | 2013-11-12 13:18:42 -0800 | [diff] [blame] | 2158 | case POWER_SUPPLY_PROP_CAPACITY: |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 2159 | return 1; |
| 2160 | default: |
| 2161 | break; |
| 2162 | } |
| 2163 | |
| 2164 | return 0; |
| 2165 | } |
| 2166 | |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2167 | static int |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 2168 | qpnp_chg_buck_control(struct qpnp_chg_chip *chip, int enable) |
| 2169 | { |
| 2170 | int rc; |
| 2171 | |
| 2172 | if (chip->charging_disabled && enable) { |
| 2173 | pr_debug("Charging disabled\n"); |
| 2174 | return 0; |
| 2175 | } |
| 2176 | |
| 2177 | rc = qpnp_chg_charge_en(chip, enable); |
| 2178 | if (rc) { |
| 2179 | pr_err("Failed to control charging %d\n", rc); |
| 2180 | return rc; |
| 2181 | } |
| 2182 | |
| 2183 | rc = qpnp_chg_force_run_on_batt(chip, !enable); |
| 2184 | if (rc) |
| 2185 | pr_err("Failed to control charging %d\n", rc); |
| 2186 | |
| 2187 | return rc; |
| 2188 | } |
| 2189 | |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 2190 | static int |
| 2191 | switch_usb_to_charge_mode(struct qpnp_chg_chip *chip) |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2192 | { |
| 2193 | int rc; |
| 2194 | |
| 2195 | pr_debug("switch to charge mode\n"); |
| 2196 | if (!qpnp_chg_is_otg_en_set(chip)) |
| 2197 | return 0; |
| 2198 | |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 2199 | if (chip->type == SMBBP) { |
| 2200 | rc = qpnp_chg_masked_write(chip, |
| 2201 | chip->boost_base + BOOST_ILIM, |
| 2202 | BOOST_ILIMT_MASK, |
| 2203 | BOOST_ILIMIT_DEF, 1); |
| 2204 | if (rc) { |
| 2205 | pr_err("Failed to set ilim rc = %d\n", rc); |
| 2206 | return rc; |
| 2207 | } |
| 2208 | } |
| 2209 | |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2210 | /* enable usb ovp fet */ |
| 2211 | rc = qpnp_chg_masked_write(chip, |
| 2212 | chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL, |
| 2213 | USB_OTG_EN_BIT, |
| 2214 | 0, 1); |
| 2215 | if (rc) { |
| 2216 | pr_err("Failed to turn on usb ovp rc = %d\n", rc); |
| 2217 | return rc; |
| 2218 | } |
| 2219 | |
| 2220 | rc = qpnp_chg_force_run_on_batt(chip, chip->charging_disabled); |
| 2221 | if (rc) { |
| 2222 | pr_err("Failed re-enable charging rc = %d\n", rc); |
| 2223 | return rc; |
| 2224 | } |
| 2225 | |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 2229 | static int |
| 2230 | switch_usb_to_host_mode(struct qpnp_chg_chip *chip) |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2231 | { |
| 2232 | int rc; |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 2233 | u8 usb_sts; |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2234 | |
| 2235 | pr_debug("switch to host mode\n"); |
| 2236 | if (qpnp_chg_is_otg_en_set(chip)) |
| 2237 | return 0; |
| 2238 | |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 2239 | if (chip->parallel_ovp_mode) |
| 2240 | switch_parallel_ovp_mode(chip, 0); |
| 2241 | |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 2242 | if (chip->type == SMBBP) { |
| 2243 | rc = qpnp_chg_masked_write(chip, |
| 2244 | chip->boost_base + BOOST_ILIM, |
| 2245 | BOOST_ILIMT_MASK, |
| 2246 | BOOST_ILIMIT_MIN, 1); |
| 2247 | if (rc) { |
| 2248 | pr_err("Failed to turn configure ilim rc = %d\n", rc); |
| 2249 | return rc; |
| 2250 | } |
| 2251 | } |
| 2252 | |
David Keitel | 6d4b9da | 2013-09-05 17:38:40 -0700 | [diff] [blame] | 2253 | if (!qpnp_chg_is_dc_chg_plugged_in(chip)) { |
| 2254 | rc = qpnp_chg_force_run_on_batt(chip, 1); |
| 2255 | if (rc) { |
| 2256 | pr_err("Failed to disable charging rc = %d\n", rc); |
| 2257 | return rc; |
| 2258 | } |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | /* force usb ovp fet off */ |
| 2262 | rc = qpnp_chg_masked_write(chip, |
| 2263 | chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL, |
| 2264 | USB_OTG_EN_BIT, |
| 2265 | USB_OTG_EN_BIT, 1); |
| 2266 | if (rc) { |
| 2267 | pr_err("Failed to turn off usb ovp rc = %d\n", rc); |
| 2268 | return rc; |
| 2269 | } |
| 2270 | |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 2271 | if (chip->type == SMBBP) { |
| 2272 | /* Wait for OCP circuitry to be powered up */ |
| 2273 | msleep(100); |
| 2274 | rc = qpnp_chg_read(chip, &usb_sts, |
| 2275 | INT_RT_STS(chip->usb_chgpth_base), 1); |
| 2276 | if (rc) { |
| 2277 | pr_err("failed to read interrupt sts %d\n", rc); |
| 2278 | return rc; |
| 2279 | } |
| 2280 | |
| 2281 | if (usb_sts & COARSE_DET_USB_IRQ) { |
| 2282 | rc = qpnp_chg_masked_write(chip, |
| 2283 | chip->boost_base + BOOST_ILIM, |
| 2284 | BOOST_ILIMT_MASK, |
| 2285 | BOOST_ILIMIT_DEF, 1); |
| 2286 | if (rc) { |
| 2287 | pr_err("Failed to set ilim rc = %d\n", rc); |
| 2288 | return rc; |
| 2289 | } |
| 2290 | } else { |
| 2291 | pr_warn_ratelimited("USB short to GND detected!\n"); |
| 2292 | } |
| 2293 | } |
| 2294 | |
David Keitel | 6f865cd | 2012-11-30 15:04:32 -0800 | [diff] [blame] | 2295 | return 0; |
| 2296 | } |
| 2297 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2298 | static enum power_supply_property pm_power_props_mains[] = { |
| 2299 | POWER_SUPPLY_PROP_PRESENT, |
| 2300 | POWER_SUPPLY_PROP_ONLINE, |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 2301 | POWER_SUPPLY_PROP_CURRENT_MAX, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2302 | }; |
| 2303 | |
| 2304 | static enum power_supply_property msm_batt_power_props[] = { |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 2305 | POWER_SUPPLY_PROP_CHARGING_ENABLED, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2306 | POWER_SUPPLY_PROP_STATUS, |
| 2307 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 2308 | POWER_SUPPLY_PROP_HEALTH, |
| 2309 | POWER_SUPPLY_PROP_PRESENT, |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 2310 | POWER_SUPPLY_PROP_ONLINE, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2311 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 2312 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 2313 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 2314 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 2315 | POWER_SUPPLY_PROP_CAPACITY, |
| 2316 | POWER_SUPPLY_PROP_CURRENT_NOW, |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2317 | POWER_SUPPLY_PROP_INPUT_CURRENT_MAX, |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 2318 | POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM, |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 2319 | POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED, |
Fenglin Wu | c64e994 | 2014-08-22 13:47:34 +0800 | [diff] [blame] | 2320 | POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS, |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2321 | POWER_SUPPLY_PROP_VOLTAGE_MIN, |
| 2322 | POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2323 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 2324 | POWER_SUPPLY_PROP_CHARGE_FULL, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2325 | POWER_SUPPLY_PROP_TEMP, |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 2326 | POWER_SUPPLY_PROP_COOL_TEMP, |
| 2327 | POWER_SUPPLY_PROP_WARM_TEMP, |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 2328 | POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL, |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 2329 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 2330 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2331 | }; |
| 2332 | |
| 2333 | static char *pm_power_supplied_to[] = { |
| 2334 | "battery", |
| 2335 | }; |
| 2336 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 2337 | static char *pm_batt_supplied_to[] = { |
| 2338 | "bms", |
| 2339 | }; |
| 2340 | |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2341 | static int charger_monitor; |
| 2342 | module_param(charger_monitor, int, 0644); |
| 2343 | |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 2344 | static int ext_ovp_present; |
| 2345 | module_param(ext_ovp_present, int, 0444); |
| 2346 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 2347 | #define OVP_USB_WALL_TRSH_MA 200 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2348 | static int |
| 2349 | qpnp_power_get_property_mains(struct power_supply *psy, |
| 2350 | enum power_supply_property psp, |
| 2351 | union power_supply_propval *val) |
| 2352 | { |
| 2353 | struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip, |
| 2354 | dc_psy); |
| 2355 | |
| 2356 | switch (psp) { |
| 2357 | case POWER_SUPPLY_PROP_PRESENT: |
| 2358 | case POWER_SUPPLY_PROP_ONLINE: |
| 2359 | val->intval = 0; |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 2360 | if (chip->charging_disabled) |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2361 | return 0; |
| 2362 | |
| 2363 | val->intval = qpnp_chg_is_dc_chg_plugged_in(chip); |
| 2364 | break; |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 2365 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
David Keitel | e2c9530 | 2013-07-11 11:15:06 -0700 | [diff] [blame] | 2366 | val->intval = chip->maxinput_dc_ma * 1000; |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 2367 | break; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2368 | default: |
| 2369 | return -EINVAL; |
| 2370 | } |
| 2371 | return 0; |
| 2372 | } |
| 2373 | |
David Keitel | 7ac385e | 2013-08-19 18:02:40 -0700 | [diff] [blame] | 2374 | static void |
| 2375 | qpnp_aicl_check_work(struct work_struct *work) |
| 2376 | { |
| 2377 | struct delayed_work *dwork = to_delayed_work(work); |
| 2378 | struct qpnp_chg_chip *chip = container_of(dwork, |
| 2379 | struct qpnp_chg_chip, aicl_check_work); |
| 2380 | union power_supply_propval ret = {0,}; |
| 2381 | |
| 2382 | if (!charger_monitor && qpnp_chg_is_usb_chg_plugged_in(chip)) { |
| 2383 | chip->usb_psy->get_property(chip->usb_psy, |
| 2384 | POWER_SUPPLY_PROP_CURRENT_MAX, &ret); |
| 2385 | if ((ret.intval / 1000) > USB_WALL_THRESHOLD_MA) { |
| 2386 | pr_debug("no charger_monitor present set iusbmax %d\n", |
| 2387 | ret.intval / 1000); |
| 2388 | qpnp_chg_iusbmax_set(chip, ret.intval / 1000); |
| 2389 | } |
| 2390 | } else { |
| 2391 | pr_debug("charger_monitor is present\n"); |
| 2392 | } |
| 2393 | chip->charger_monitor_checked = true; |
| 2394 | } |
| 2395 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2396 | static int |
| 2397 | get_prop_battery_voltage_now(struct qpnp_chg_chip *chip) |
| 2398 | { |
| 2399 | int rc = 0; |
| 2400 | struct qpnp_vadc_result results; |
| 2401 | |
David Keitel | fe51cb9 | 2013-04-02 19:42:58 -0700 | [diff] [blame] | 2402 | if (chip->revision == 0 && chip->type == SMBB) { |
| 2403 | pr_err("vbat reading not supported for 1.0 rc=%d\n", rc); |
| 2404 | return 0; |
| 2405 | } else { |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2406 | rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &results); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2407 | if (rc) { |
| 2408 | pr_err("Unable to read vbat rc=%d\n", rc); |
| 2409 | return 0; |
| 2410 | } |
| 2411 | return results.physical; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | #define BATT_PRES_BIT BIT(7) |
| 2416 | static int |
| 2417 | get_prop_batt_present(struct qpnp_chg_chip *chip) |
| 2418 | { |
| 2419 | u8 batt_present; |
| 2420 | int rc; |
| 2421 | |
| 2422 | rc = qpnp_chg_read(chip, &batt_present, |
| 2423 | chip->bat_if_base + CHGR_BAT_IF_PRES_STATUS, 1); |
| 2424 | if (rc) { |
| 2425 | pr_err("Couldn't read battery status read failed rc=%d\n", rc); |
| 2426 | return 0; |
| 2427 | }; |
| 2428 | return (batt_present & BATT_PRES_BIT) ? 1 : 0; |
| 2429 | } |
| 2430 | |
| 2431 | #define BATT_TEMP_HOT BIT(6) |
| 2432 | #define BATT_TEMP_OK BIT(7) |
| 2433 | static int |
| 2434 | get_prop_batt_health(struct qpnp_chg_chip *chip) |
| 2435 | { |
| 2436 | u8 batt_health; |
| 2437 | int rc; |
| 2438 | |
| 2439 | rc = qpnp_chg_read(chip, &batt_health, |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 2440 | chip->bat_if_base + CHGR_STATUS, 1); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2441 | if (rc) { |
| 2442 | pr_err("Couldn't read battery health read failed rc=%d\n", rc); |
| 2443 | return POWER_SUPPLY_HEALTH_UNKNOWN; |
| 2444 | }; |
| 2445 | |
| 2446 | if (BATT_TEMP_OK & batt_health) |
| 2447 | return POWER_SUPPLY_HEALTH_GOOD; |
| 2448 | if (BATT_TEMP_HOT & batt_health) |
| 2449 | return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 2450 | else |
| 2451 | return POWER_SUPPLY_HEALTH_COLD; |
| 2452 | } |
| 2453 | |
| 2454 | static int |
| 2455 | get_prop_charge_type(struct qpnp_chg_chip *chip) |
| 2456 | { |
| 2457 | int rc; |
| 2458 | u8 chgr_sts; |
| 2459 | |
| 2460 | if (!get_prop_batt_present(chip)) |
| 2461 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 2462 | |
| 2463 | rc = qpnp_chg_read(chip, &chgr_sts, |
| 2464 | INT_RT_STS(chip->chgr_base), 1); |
| 2465 | if (rc) { |
| 2466 | pr_err("failed to read interrupt sts %d\n", rc); |
| 2467 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 2468 | } |
| 2469 | |
| 2470 | if (chgr_sts & TRKL_CHG_ON_IRQ) |
| 2471 | return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 2472 | if (chgr_sts & FAST_CHG_ON_IRQ) |
| 2473 | return POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 2474 | |
| 2475 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 2476 | } |
| 2477 | |
Xiaozhe Shi | 72a72f2 | 2013-12-26 13:54:29 -0800 | [diff] [blame] | 2478 | #define DEFAULT_CAPACITY 50 |
| 2479 | static int |
| 2480 | get_batt_capacity(struct qpnp_chg_chip *chip) |
| 2481 | { |
| 2482 | union power_supply_propval ret = {0,}; |
| 2483 | |
| 2484 | if (chip->fake_battery_soc >= 0) |
| 2485 | return chip->fake_battery_soc; |
| 2486 | if (chip->use_default_batt_values || !get_prop_batt_present(chip)) |
| 2487 | return DEFAULT_CAPACITY; |
| 2488 | if (chip->bms_psy) { |
| 2489 | chip->bms_psy->get_property(chip->bms_psy, |
| 2490 | POWER_SUPPLY_PROP_CAPACITY, &ret); |
| 2491 | return ret.intval; |
| 2492 | } |
| 2493 | return DEFAULT_CAPACITY; |
| 2494 | } |
| 2495 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2496 | static int |
| 2497 | get_prop_batt_status(struct qpnp_chg_chip *chip) |
| 2498 | { |
| 2499 | int rc; |
Zhenhua Huang | 2a0ee35 | 2013-07-23 01:55:43 +0800 | [diff] [blame] | 2500 | u8 chgr_sts, bat_if_sts; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2501 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 2502 | rc = qpnp_chg_read(chip, &chgr_sts, INT_RT_STS(chip->chgr_base), 1); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2503 | if (rc) { |
| 2504 | pr_err("failed to read interrupt sts %d\n", rc); |
David Keitel | c9ffe84 | 2013-01-25 19:37:51 -0800 | [diff] [blame] | 2505 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
Zhenhua Huang | 2a0ee35 | 2013-07-23 01:55:43 +0800 | [diff] [blame] | 2508 | rc = qpnp_chg_read(chip, &bat_if_sts, INT_RT_STS(chip->bat_if_base), 1); |
| 2509 | if (rc) { |
| 2510 | pr_err("failed to read bat_if sts %d\n", rc); |
| 2511 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 2512 | } |
| 2513 | |
David Keitel | a8099ff | 2014-01-06 11:31:40 -0800 | [diff] [blame] | 2514 | if ((chgr_sts & TRKL_CHG_ON_IRQ) && !(bat_if_sts & BAT_FET_ON_IRQ)) |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2515 | return POWER_SUPPLY_STATUS_CHARGING; |
Zhenhua Huang | 2a0ee35 | 2013-07-23 01:55:43 +0800 | [diff] [blame] | 2516 | if (chgr_sts & FAST_CHG_ON_IRQ && bat_if_sts & BAT_FET_ON_IRQ) |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2517 | return POWER_SUPPLY_STATUS_CHARGING; |
| 2518 | |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 2519 | /* |
| 2520 | * Report full if state of charge is 100 or chg_done is true |
| 2521 | * when a charger is connected and boost is disabled |
| 2522 | */ |
Xiaozhe Shi | 72a72f2 | 2013-12-26 13:54:29 -0800 | [diff] [blame] | 2523 | if ((qpnp_chg_is_usb_chg_plugged_in(chip) || |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 2524 | qpnp_chg_is_dc_chg_plugged_in(chip)) && |
| 2525 | (chip->chg_done || get_batt_capacity(chip) == 100) |
| 2526 | && qpnp_chg_is_boost_en_set(chip) == 0) { |
Xiaozhe Shi | 72a72f2 | 2013-12-26 13:54:29 -0800 | [diff] [blame] | 2527 | return POWER_SUPPLY_STATUS_FULL; |
| 2528 | } |
| 2529 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2530 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 2531 | } |
| 2532 | |
| 2533 | static int |
| 2534 | get_prop_current_now(struct qpnp_chg_chip *chip) |
| 2535 | { |
| 2536 | union power_supply_propval ret = {0,}; |
| 2537 | |
| 2538 | if (chip->bms_psy) { |
| 2539 | chip->bms_psy->get_property(chip->bms_psy, |
| 2540 | POWER_SUPPLY_PROP_CURRENT_NOW, &ret); |
| 2541 | return ret.intval; |
| 2542 | } else { |
| 2543 | pr_debug("No BMS supply registered return 0\n"); |
| 2544 | } |
| 2545 | |
| 2546 | return 0; |
| 2547 | } |
| 2548 | |
| 2549 | static int |
| 2550 | get_prop_full_design(struct qpnp_chg_chip *chip) |
| 2551 | { |
| 2552 | union power_supply_propval ret = {0,}; |
| 2553 | |
| 2554 | if (chip->bms_psy) { |
| 2555 | chip->bms_psy->get_property(chip->bms_psy, |
| 2556 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, &ret); |
| 2557 | return ret.intval; |
| 2558 | } else { |
| 2559 | pr_debug("No BMS supply registered return 0\n"); |
| 2560 | } |
| 2561 | |
| 2562 | return 0; |
| 2563 | } |
| 2564 | |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 2565 | static int |
| 2566 | get_prop_charge_full(struct qpnp_chg_chip *chip) |
| 2567 | { |
| 2568 | union power_supply_propval ret = {0,}; |
| 2569 | |
| 2570 | if (chip->bms_psy) { |
| 2571 | chip->bms_psy->get_property(chip->bms_psy, |
| 2572 | POWER_SUPPLY_PROP_CHARGE_FULL, &ret); |
| 2573 | return ret.intval; |
| 2574 | } else { |
| 2575 | pr_debug("No BMS supply registered return 0\n"); |
| 2576 | } |
| 2577 | |
| 2578 | return 0; |
| 2579 | } |
| 2580 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2581 | static int |
| 2582 | get_prop_capacity(struct qpnp_chg_chip *chip) |
| 2583 | { |
| 2584 | union power_supply_propval ret = {0,}; |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2585 | int battery_status, bms_status, soc, charger_in; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2586 | |
Xiaozhe Shi | 78797cf | 2013-11-12 13:18:42 -0800 | [diff] [blame] | 2587 | if (chip->fake_battery_soc >= 0) |
| 2588 | return chip->fake_battery_soc; |
| 2589 | |
David Keitel | 3dd5e0f | 2012-12-12 18:12:36 -0800 | [diff] [blame] | 2590 | if (chip->use_default_batt_values || !get_prop_batt_present(chip)) |
| 2591 | return DEFAULT_CAPACITY; |
| 2592 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2593 | if (chip->bms_psy) { |
| 2594 | chip->bms_psy->get_property(chip->bms_psy, |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 2595 | POWER_SUPPLY_PROP_CAPACITY, &ret); |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2596 | soc = ret.intval; |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 2597 | battery_status = get_prop_batt_status(chip); |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2598 | chip->bms_psy->get_property(chip->bms_psy, |
| 2599 | POWER_SUPPLY_PROP_STATUS, &ret); |
| 2600 | bms_status = ret.intval; |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 2601 | charger_in = qpnp_chg_is_usb_chg_plugged_in(chip) || |
| 2602 | qpnp_chg_is_dc_chg_plugged_in(chip); |
| 2603 | |
| 2604 | if (battery_status != POWER_SUPPLY_STATUS_CHARGING |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2605 | && bms_status != POWER_SUPPLY_STATUS_CHARGING |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 2606 | && charger_in |
Xiaozhe Shi | e850448 | 2013-10-30 18:10:31 -0700 | [diff] [blame] | 2607 | && !chip->bat_is_cool |
| 2608 | && !chip->bat_is_warm |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 2609 | && !chip->resuming_charging |
| 2610 | && !chip->charging_disabled |
| 2611 | && chip->soc_resume_limit |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2612 | && soc <= chip->soc_resume_limit) { |
| 2613 | pr_debug("resuming charging at %d%% soc\n", soc); |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 2614 | chip->resuming_charging = true; |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 2615 | qpnp_chg_irq_wake_enable(&chip->chg_fastchg); |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 2616 | qpnp_chg_set_appropriate_vbatdet(chip); |
| 2617 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 2618 | } |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2619 | if (soc == 0) { |
David Keitel | eeca08f | 2013-05-17 16:40:46 -0700 | [diff] [blame] | 2620 | if (!qpnp_chg_is_usb_chg_plugged_in(chip) |
| 2621 | && !qpnp_chg_is_usb_chg_plugged_in(chip)) |
Abhijeet Dharmapurikar | 53ce35a | 2013-03-29 16:14:16 -0700 | [diff] [blame] | 2622 | pr_warn_ratelimited("Battery 0, CHG absent\n"); |
| 2623 | } |
Xiaozhe Shi | bda8499 | 2013-09-05 10:39:11 -0700 | [diff] [blame] | 2624 | return soc; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2625 | } else { |
| 2626 | pr_debug("No BMS supply registered return 50\n"); |
| 2627 | } |
| 2628 | |
| 2629 | /* return default capacity to avoid userspace |
| 2630 | * from shutting down unecessarily */ |
| 2631 | return DEFAULT_CAPACITY; |
| 2632 | } |
| 2633 | |
David Keitel | 3dd5e0f | 2012-12-12 18:12:36 -0800 | [diff] [blame] | 2634 | #define DEFAULT_TEMP 250 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2635 | #define MAX_TOLERABLE_BATT_TEMP_DDC 680 |
| 2636 | static int |
| 2637 | get_prop_batt_temp(struct qpnp_chg_chip *chip) |
| 2638 | { |
| 2639 | int rc = 0; |
| 2640 | struct qpnp_vadc_result results; |
| 2641 | |
David Keitel | 3dd5e0f | 2012-12-12 18:12:36 -0800 | [diff] [blame] | 2642 | if (chip->use_default_batt_values || !get_prop_batt_present(chip)) |
| 2643 | return DEFAULT_TEMP; |
| 2644 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2645 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &results); |
David Keitel | a2dbfc7 | 2013-06-07 11:10:14 -0700 | [diff] [blame] | 2646 | if (rc) { |
| 2647 | pr_debug("Unable to read batt temperature rc=%d\n", rc); |
| 2648 | return 0; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2649 | } |
Xiaozhe Shi | 4c8458a | 2013-11-26 13:00:56 -0800 | [diff] [blame] | 2650 | pr_debug("get_bat_temp %d, %lld\n", |
David Keitel | a2dbfc7 | 2013-06-07 11:10:14 -0700 | [diff] [blame] | 2651 | results.adc_code, results.physical); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2652 | |
David Keitel | a2dbfc7 | 2013-06-07 11:10:14 -0700 | [diff] [blame] | 2653 | return (int)results.physical; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 2656 | static int get_prop_cycle_count(struct qpnp_chg_chip *chip) |
| 2657 | { |
| 2658 | union power_supply_propval ret = {0,}; |
| 2659 | |
| 2660 | if (chip->bms_psy) |
| 2661 | chip->bms_psy->get_property(chip->bms_psy, |
| 2662 | POWER_SUPPLY_PROP_CYCLE_COUNT, &ret); |
| 2663 | return ret.intval; |
| 2664 | } |
| 2665 | |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2666 | static int get_prop_vchg_loop(struct qpnp_chg_chip *chip) |
| 2667 | { |
| 2668 | u8 buck_sts; |
| 2669 | int rc; |
| 2670 | |
| 2671 | rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1); |
| 2672 | |
| 2673 | if (rc) { |
| 2674 | pr_err("spmi read failed: addr=%03X, rc=%d\n", |
| 2675 | INT_RT_STS(chip->buck_base), rc); |
| 2676 | return rc; |
| 2677 | } |
| 2678 | pr_debug("buck usb sts 0x%x\n", buck_sts); |
| 2679 | |
| 2680 | return (buck_sts & VCHG_LOOP_IRQ) ? 1 : 0; |
| 2681 | } |
| 2682 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 2683 | static int get_prop_online(struct qpnp_chg_chip *chip) |
| 2684 | { |
| 2685 | return qpnp_chg_is_batfet_closed(chip); |
| 2686 | } |
| 2687 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2688 | static void |
| 2689 | qpnp_batt_external_power_changed(struct power_supply *psy) |
| 2690 | { |
| 2691 | struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip, |
| 2692 | batt_psy); |
| 2693 | union power_supply_propval ret = {0,}; |
| 2694 | |
| 2695 | if (!chip->bms_psy) |
| 2696 | chip->bms_psy = power_supply_get_by_name("bms"); |
| 2697 | |
| 2698 | chip->usb_psy->get_property(chip->usb_psy, |
| 2699 | POWER_SUPPLY_PROP_ONLINE, &ret); |
| 2700 | |
David Keitel | c69f2d6 | 2013-03-17 14:52:35 -0700 | [diff] [blame] | 2701 | /* Only honour requests while USB is present */ |
| 2702 | if (qpnp_chg_is_usb_chg_plugged_in(chip)) { |
David Keitel | 359ab65 | 2013-03-21 17:46:00 -0700 | [diff] [blame] | 2703 | chip->usb_psy->get_property(chip->usb_psy, |
| 2704 | POWER_SUPPLY_PROP_CURRENT_MAX, &ret); |
David Keitel | fd305ee | 2013-07-19 20:19:06 -0700 | [diff] [blame] | 2705 | |
| 2706 | if (chip->prev_usb_max_ma == ret.intval) |
| 2707 | goto skip_set_iusb_max; |
| 2708 | |
Zhenhua Huang | dd61974 | 2013-10-24 10:02:07 +0800 | [diff] [blame] | 2709 | chip->prev_usb_max_ma = ret.intval; |
| 2710 | |
David Keitel | 8747325 | 2013-03-21 14:39:45 -0700 | [diff] [blame] | 2711 | if (ret.intval <= 2 && !chip->use_default_batt_values && |
| 2712 | get_prop_batt_present(chip)) { |
David Keitel | a918560 | 2013-11-12 17:55:48 -0800 | [diff] [blame] | 2713 | if (ret.intval == 2) |
| 2714 | qpnp_chg_usb_suspend_enable(chip, 1); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 2715 | qpnp_chg_iusbmax_set(chip, QPNP_CHG_I_MAX_MIN_100); |
David Keitel | c69f2d6 | 2013-03-17 14:52:35 -0700 | [diff] [blame] | 2716 | } else { |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 2717 | qpnp_chg_usb_suspend_enable(chip, 0); |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 2718 | if (qpnp_is_dc_higher_prio(chip) |
| 2719 | && qpnp_chg_is_dc_chg_plugged_in(chip)) { |
| 2720 | pr_debug("dc has higher priority\n"); |
| 2721 | qpnp_chg_iusbmax_set(chip, |
| 2722 | QPNP_CHG_I_MAX_MIN_100); |
| 2723 | } else if (((ret.intval / 1000) > USB_WALL_THRESHOLD_MA) |
David Keitel | 7ac385e | 2013-08-19 18:02:40 -0700 | [diff] [blame] | 2724 | && (charger_monitor || |
| 2725 | !chip->charger_monitor_checked)) { |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 2726 | if (!qpnp_is_dc_higher_prio(chip)) |
| 2727 | qpnp_chg_idcmax_set(chip, |
| 2728 | QPNP_CHG_I_MAX_MIN_100); |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 2729 | if (unlikely(ext_ovp_present)) { |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 2730 | qpnp_chg_iusbmax_set(chip, |
| 2731 | OVP_USB_WALL_TRSH_MA); |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 2732 | } else if (unlikely( |
| 2733 | ext_ovp_isns_present)) { |
| 2734 | qpnp_chg_iusb_trim_set(chip, 0); |
| 2735 | qpnp_chg_iusbmax_set(chip, |
| 2736 | IOVP_USB_WALL_TRSH_MA); |
| 2737 | } else { |
| 2738 | qpnp_chg_iusbmax_set(chip, |
| 2739 | USB_WALL_THRESHOLD_MA); |
| 2740 | } |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2741 | } else { |
| 2742 | qpnp_chg_iusbmax_set(chip, ret.intval / 1000); |
| 2743 | } |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 2744 | |
| 2745 | if ((chip->flags & POWER_STAGE_WA) |
| 2746 | && ((ret.intval / 1000) > USB_WALL_THRESHOLD_MA) |
Zhenhua Huang | dc33551 | 2013-10-30 11:45:15 +0800 | [diff] [blame] | 2747 | && !chip->power_stage_workaround_running |
| 2748 | && chip->power_stage_workaround_enable) { |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 2749 | chip->power_stage_workaround_running = true; |
| 2750 | pr_debug("usb wall chg inserted starting power stage workaround charger_monitor = %d\n", |
| 2751 | charger_monitor); |
| 2752 | schedule_work(&chip->reduce_power_stage_work); |
| 2753 | } |
David Keitel | c69f2d6 | 2013-03-17 14:52:35 -0700 | [diff] [blame] | 2754 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
David Keitel | fd305ee | 2013-07-19 20:19:06 -0700 | [diff] [blame] | 2757 | skip_set_iusb_max: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2758 | pr_debug("end of power supply changed\n"); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 2759 | pr_debug("psy changed batt_psy\n"); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2760 | power_supply_changed(&chip->batt_psy); |
| 2761 | } |
| 2762 | |
| 2763 | static int |
| 2764 | qpnp_batt_power_get_property(struct power_supply *psy, |
| 2765 | enum power_supply_property psp, |
| 2766 | union power_supply_propval *val) |
| 2767 | { |
| 2768 | struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip, |
| 2769 | batt_psy); |
| 2770 | |
| 2771 | switch (psp) { |
| 2772 | case POWER_SUPPLY_PROP_STATUS: |
| 2773 | val->intval = get_prop_batt_status(chip); |
| 2774 | break; |
| 2775 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 2776 | val->intval = get_prop_charge_type(chip); |
| 2777 | break; |
| 2778 | case POWER_SUPPLY_PROP_HEALTH: |
| 2779 | val->intval = get_prop_batt_health(chip); |
| 2780 | break; |
| 2781 | case POWER_SUPPLY_PROP_PRESENT: |
| 2782 | val->intval = get_prop_batt_present(chip); |
| 2783 | break; |
| 2784 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 2785 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 2786 | break; |
| 2787 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 2788 | val->intval = chip->max_voltage_mv * 1000; |
| 2789 | break; |
| 2790 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 2791 | val->intval = chip->min_voltage_mv * 1000; |
| 2792 | break; |
| 2793 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 2794 | val->intval = get_prop_battery_voltage_now(chip); |
| 2795 | break; |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 2796 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 2797 | val->intval = chip->insertion_ocv_uv; |
| 2798 | break; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2799 | case POWER_SUPPLY_PROP_TEMP: |
| 2800 | val->intval = get_prop_batt_temp(chip); |
| 2801 | break; |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 2802 | case POWER_SUPPLY_PROP_COOL_TEMP: |
| 2803 | val->intval = chip->cool_bat_decidegc; |
| 2804 | break; |
| 2805 | case POWER_SUPPLY_PROP_WARM_TEMP: |
| 2806 | val->intval = chip->warm_bat_decidegc; |
| 2807 | break; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2808 | case POWER_SUPPLY_PROP_CAPACITY: |
| 2809 | val->intval = get_prop_capacity(chip); |
| 2810 | break; |
| 2811 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 2812 | val->intval = get_prop_current_now(chip); |
| 2813 | break; |
| 2814 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 2815 | val->intval = get_prop_full_design(chip); |
| 2816 | break; |
Anirudh Ghayal | c9d981a | 2013-06-24 09:50:33 +0530 | [diff] [blame] | 2817 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 2818 | val->intval = get_prop_charge_full(chip); |
| 2819 | break; |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 2820 | case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 2821 | val->intval = !(chip->charging_disabled); |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 2822 | break; |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 2823 | case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL: |
| 2824 | val->intval = chip->therm_lvl_sel; |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 2825 | break; |
Anirudh Ghayal | 9dd582d | 2013-06-07 17:48:58 +0530 | [diff] [blame] | 2826 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 2827 | val->intval = get_prop_cycle_count(chip); |
| 2828 | break; |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2829 | case POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION: |
| 2830 | val->intval = get_prop_vchg_loop(chip); |
| 2831 | break; |
| 2832 | case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX: |
| 2833 | val->intval = qpnp_chg_usb_iusbmax_get(chip) * 1000; |
| 2834 | break; |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 2835 | case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM: |
| 2836 | val->intval = qpnp_chg_iusb_trim_get(chip); |
| 2837 | break; |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 2838 | case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED: |
| 2839 | val->intval = chip->aicl_settled; |
| 2840 | break; |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 2841 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
| 2842 | val->intval = qpnp_chg_vinmin_get(chip) * 1000; |
| 2843 | break; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 2844 | case POWER_SUPPLY_PROP_ONLINE: |
| 2845 | val->intval = get_prop_online(chip); |
| 2846 | break; |
Fenglin Wu | c64e994 | 2014-08-22 13:47:34 +0800 | [diff] [blame] | 2847 | case POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS: |
| 2848 | val->intval = qpnp_chg_vchg_loop_debouncer_setting_get(chip); |
| 2849 | break; |
| 2850 | |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 2851 | default: |
| 2852 | return -EINVAL; |
| 2853 | } |
| 2854 | |
David Keitel | b80eda8 | 2012-10-15 10:49:11 -0700 | [diff] [blame] | 2855 | return 0; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 2858 | #define BTC_CONFIG_ENABLED BIT(7) |
| 2859 | #define BTC_COLD BIT(1) |
| 2860 | #define BTC_HOT BIT(0) |
| 2861 | static int |
| 2862 | qpnp_chg_bat_if_configure_btc(struct qpnp_chg_chip *chip) |
| 2863 | { |
| 2864 | u8 btc_cfg = 0, mask = 0; |
| 2865 | |
| 2866 | /* Do nothing if battery peripheral not present */ |
| 2867 | if (!chip->bat_if_base) |
| 2868 | return 0; |
| 2869 | |
| 2870 | if ((chip->hot_batt_p == HOT_THD_25_PCT) |
| 2871 | || (chip->hot_batt_p == HOT_THD_35_PCT)) { |
| 2872 | btc_cfg |= btc_value[chip->hot_batt_p]; |
| 2873 | mask |= BTC_HOT; |
| 2874 | } |
| 2875 | |
| 2876 | if ((chip->cold_batt_p == COLD_THD_70_PCT) || |
| 2877 | (chip->cold_batt_p == COLD_THD_80_PCT)) { |
| 2878 | btc_cfg |= btc_value[chip->cold_batt_p]; |
| 2879 | mask |= BTC_COLD; |
| 2880 | } |
| 2881 | |
| 2882 | if (chip->btc_disabled) |
| 2883 | mask |= BTC_CONFIG_ENABLED; |
| 2884 | |
| 2885 | return qpnp_chg_masked_write(chip, |
| 2886 | chip->bat_if_base + BAT_IF_BTC_CTRL, |
| 2887 | mask, btc_cfg, 1); |
| 2888 | } |
| 2889 | |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 2890 | #define QPNP_CHG_IBATSAFE_MIN_MA 100 |
| 2891 | #define QPNP_CHG_IBATSAFE_MAX_MA 3250 |
| 2892 | #define QPNP_CHG_I_STEP_MA 50 |
| 2893 | #define QPNP_CHG_I_MIN_MA 100 |
| 2894 | #define QPNP_CHG_I_MASK 0x3F |
| 2895 | static int |
| 2896 | qpnp_chg_ibatsafe_set(struct qpnp_chg_chip *chip, int safe_current) |
| 2897 | { |
| 2898 | u8 temp; |
| 2899 | |
| 2900 | if (safe_current < QPNP_CHG_IBATSAFE_MIN_MA |
| 2901 | || safe_current > QPNP_CHG_IBATSAFE_MAX_MA) { |
| 2902 | pr_err("bad mA=%d asked to set\n", safe_current); |
| 2903 | return -EINVAL; |
| 2904 | } |
| 2905 | |
Zhenhua Huang | a2964fb | 2013-07-23 09:06:20 +0800 | [diff] [blame] | 2906 | temp = safe_current / QPNP_CHG_I_STEP_MA; |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 2907 | return qpnp_chg_masked_write(chip, |
| 2908 | chip->chgr_base + CHGR_IBAT_SAFE, |
| 2909 | QPNP_CHG_I_MASK, temp, 1); |
| 2910 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2911 | |
| 2912 | #define QPNP_CHG_ITERM_MIN_MA 100 |
| 2913 | #define QPNP_CHG_ITERM_MAX_MA 250 |
| 2914 | #define QPNP_CHG_ITERM_STEP_MA 50 |
| 2915 | #define QPNP_CHG_ITERM_MASK 0x03 |
| 2916 | static int |
| 2917 | qpnp_chg_ibatterm_set(struct qpnp_chg_chip *chip, int term_current) |
| 2918 | { |
| 2919 | u8 temp; |
| 2920 | |
| 2921 | if (term_current < QPNP_CHG_ITERM_MIN_MA |
| 2922 | || term_current > QPNP_CHG_ITERM_MAX_MA) { |
| 2923 | pr_err("bad mA=%d asked to set\n", term_current); |
| 2924 | return -EINVAL; |
| 2925 | } |
| 2926 | |
| 2927 | temp = (term_current - QPNP_CHG_ITERM_MIN_MA) |
| 2928 | / QPNP_CHG_ITERM_STEP_MA; |
| 2929 | return qpnp_chg_masked_write(chip, |
| 2930 | chip->chgr_base + CHGR_IBAT_TERM_CHGR, |
| 2931 | QPNP_CHG_ITERM_MASK, temp, 1); |
| 2932 | } |
| 2933 | |
David Keitel | ff5d047 | 2013-04-04 11:36:06 -0700 | [diff] [blame] | 2934 | #define QPNP_CHG_IBATMAX_MIN 50 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2935 | #define QPNP_CHG_IBATMAX_MAX 3250 |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2936 | static int |
| 2937 | qpnp_chg_ibatmax_set(struct qpnp_chg_chip *chip, int chg_current) |
| 2938 | { |
| 2939 | u8 temp; |
| 2940 | |
| 2941 | if (chg_current < QPNP_CHG_IBATMAX_MIN |
| 2942 | || chg_current > QPNP_CHG_IBATMAX_MAX) { |
| 2943 | pr_err("bad mA=%d asked to set\n", chg_current); |
| 2944 | return -EINVAL; |
| 2945 | } |
David Keitel | ff5d047 | 2013-04-04 11:36:06 -0700 | [diff] [blame] | 2946 | temp = chg_current / QPNP_CHG_I_STEP_MA; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 2947 | return qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_IBAT_MAX, |
| 2948 | QPNP_CHG_I_MASK, temp, 1); |
| 2949 | } |
| 2950 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 2951 | static int |
| 2952 | qpnp_chg_ibatmax_get(struct qpnp_chg_chip *chip, int *chg_current) |
| 2953 | { |
| 2954 | int rc; |
| 2955 | u8 temp; |
| 2956 | |
| 2957 | *chg_current = 0; |
| 2958 | rc = qpnp_chg_read(chip, &temp, chip->chgr_base + CHGR_IBAT_MAX, 1); |
| 2959 | if (rc) { |
| 2960 | pr_err("failed read ibat_max rc=%d\n", rc); |
| 2961 | return rc; |
| 2962 | } |
| 2963 | |
| 2964 | *chg_current = ((temp & QPNP_CHG_I_MASK) * QPNP_CHG_I_STEP_MA); |
| 2965 | |
| 2966 | return 0; |
| 2967 | } |
| 2968 | |
David Keitel | a4b7b59 | 2013-04-11 18:34:35 -0700 | [diff] [blame] | 2969 | #define QPNP_CHG_TCHG_MASK 0x7F |
Abhijeet Dharmapurikar | 11e6156 | 2013-11-19 12:01:29 -0800 | [diff] [blame] | 2970 | #define QPNP_CHG_TCHG_EN_MASK 0x80 |
David Keitel | a4b7b59 | 2013-04-11 18:34:35 -0700 | [diff] [blame] | 2971 | #define QPNP_CHG_TCHG_MIN 4 |
| 2972 | #define QPNP_CHG_TCHG_MAX 512 |
| 2973 | #define QPNP_CHG_TCHG_STEP 4 |
| 2974 | static int qpnp_chg_tchg_max_set(struct qpnp_chg_chip *chip, int minutes) |
| 2975 | { |
| 2976 | u8 temp; |
Abhijeet Dharmapurikar | 11e6156 | 2013-11-19 12:01:29 -0800 | [diff] [blame] | 2977 | int rc; |
David Keitel | a4b7b59 | 2013-04-11 18:34:35 -0700 | [diff] [blame] | 2978 | |
| 2979 | if (minutes < QPNP_CHG_TCHG_MIN || minutes > QPNP_CHG_TCHG_MAX) { |
| 2980 | pr_err("bad max minutes =%d asked to set\n", minutes); |
| 2981 | return -EINVAL; |
| 2982 | } |
| 2983 | |
Abhijeet Dharmapurikar | 11e6156 | 2013-11-19 12:01:29 -0800 | [diff] [blame] | 2984 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_TCHG_MAX_EN, |
| 2985 | QPNP_CHG_TCHG_EN_MASK, 0, 1); |
| 2986 | if (rc) { |
| 2987 | pr_err("failed write tchg_max_en rc=%d\n", rc); |
| 2988 | return rc; |
| 2989 | } |
| 2990 | |
| 2991 | temp = minutes / QPNP_CHG_TCHG_STEP - 1; |
| 2992 | |
| 2993 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_TCHG_MAX, |
David Keitel | a1d1644 | 2013-05-09 14:47:37 -0700 | [diff] [blame] | 2994 | QPNP_CHG_TCHG_MASK, temp, 1); |
Abhijeet Dharmapurikar | 11e6156 | 2013-11-19 12:01:29 -0800 | [diff] [blame] | 2995 | if (rc) { |
| 2996 | pr_err("failed write tchg_max_en rc=%d\n", rc); |
| 2997 | return rc; |
| 2998 | } |
| 2999 | |
| 3000 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_TCHG_MAX_EN, |
| 3001 | QPNP_CHG_TCHG_EN_MASK, QPNP_CHG_TCHG_EN_MASK, 1); |
| 3002 | if (rc) { |
| 3003 | pr_err("failed write tchg_max_en rc=%d\n", rc); |
| 3004 | return rc; |
| 3005 | } |
| 3006 | |
| 3007 | return 0; |
David Keitel | a4b7b59 | 2013-04-11 18:34:35 -0700 | [diff] [blame] | 3008 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 3009 | |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 3010 | static void |
| 3011 | qpnp_chg_set_appropriate_battery_current(struct qpnp_chg_chip *chip) |
| 3012 | { |
| 3013 | unsigned int chg_current = chip->max_bat_chg_current; |
| 3014 | |
| 3015 | if (chip->bat_is_cool) |
| 3016 | chg_current = min(chg_current, chip->cool_bat_chg_ma); |
| 3017 | |
| 3018 | if (chip->bat_is_warm) |
| 3019 | chg_current = min(chg_current, chip->warm_bat_chg_ma); |
| 3020 | |
| 3021 | if (chip->therm_lvl_sel != 0 && chip->thermal_mitigation) |
| 3022 | chg_current = min(chg_current, |
| 3023 | chip->thermal_mitigation[chip->therm_lvl_sel]); |
| 3024 | |
| 3025 | pr_debug("setting %d mA\n", chg_current); |
| 3026 | qpnp_chg_ibatmax_set(chip, chg_current); |
| 3027 | } |
| 3028 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 3029 | static int |
| 3030 | qpnp_chg_vddsafe_set(struct qpnp_chg_chip *chip, int voltage) |
| 3031 | { |
| 3032 | u8 temp; |
| 3033 | |
| 3034 | if (voltage < QPNP_CHG_V_MIN_MV |
| 3035 | || voltage > QPNP_CHG_V_MAX_MV) { |
| 3036 | pr_err("bad mV=%d asked to set\n", voltage); |
| 3037 | return -EINVAL; |
| 3038 | } |
| 3039 | temp = (voltage - QPNP_CHG_V_MIN_MV) / QPNP_CHG_V_STEP_MV; |
| 3040 | pr_debug("voltage=%d setting %02x\n", voltage, temp); |
| 3041 | return qpnp_chg_write(chip, &temp, |
| 3042 | chip->chgr_base + CHGR_VDD_SAFE, 1); |
| 3043 | } |
| 3044 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3045 | #define IBAT_TRIM_TGT_MA 500 |
| 3046 | #define IBAT_TRIM_OFFSET_MASK 0x7F |
| 3047 | #define IBAT_TRIM_GOOD_BIT BIT(7) |
| 3048 | #define IBAT_TRIM_LOW_LIM 20 |
| 3049 | #define IBAT_TRIM_HIGH_LIM 114 |
| 3050 | #define IBAT_TRIM_MEAN 64 |
| 3051 | |
| 3052 | static void |
| 3053 | qpnp_chg_trim_ibat(struct qpnp_chg_chip *chip, u8 ibat_trim) |
| 3054 | { |
| 3055 | int ibat_now_ma, ibat_diff_ma, rc; |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 3056 | struct qpnp_iadc_result i_result; |
| 3057 | enum qpnp_iadc_channels iadc_channel; |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3058 | |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 3059 | iadc_channel = chip->use_external_rsense ? |
| 3060 | EXTERNAL_RSENSE : INTERNAL_RSENSE; |
| 3061 | rc = qpnp_iadc_read(chip->iadc_dev, iadc_channel, &i_result); |
| 3062 | if (rc) { |
| 3063 | pr_err("Unable to read bat rc=%d\n", rc); |
| 3064 | return; |
| 3065 | } |
| 3066 | |
| 3067 | ibat_now_ma = i_result.result_ua / 1000; |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3068 | |
| 3069 | if (qpnp_chg_is_ibat_loop_active(chip)) { |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3070 | ibat_diff_ma = ibat_now_ma - IBAT_TRIM_TGT_MA; |
| 3071 | |
| 3072 | if (abs(ibat_diff_ma) > 50) { |
| 3073 | ibat_trim += (ibat_diff_ma / 20); |
| 3074 | ibat_trim &= IBAT_TRIM_OFFSET_MASK; |
| 3075 | /* reject new ibat_trim if it is outside limits */ |
| 3076 | if (!is_within_range(ibat_trim, IBAT_TRIM_LOW_LIM, |
| 3077 | IBAT_TRIM_HIGH_LIM)) |
| 3078 | return; |
| 3079 | } |
| 3080 | ibat_trim |= IBAT_TRIM_GOOD_BIT; |
| 3081 | rc = qpnp_chg_write(chip, &ibat_trim, |
| 3082 | chip->buck_base + BUCK_CTRL_TRIM3, 1); |
| 3083 | if (rc) |
| 3084 | pr_err("failed to set IBAT_TRIM rc=%d\n", rc); |
| 3085 | |
| 3086 | pr_debug("ibat_now=%dmA, itgt=%dmA, ibat_diff=%dmA, ibat_trim=%x\n", |
| 3087 | ibat_now_ma, IBAT_TRIM_TGT_MA, |
| 3088 | ibat_diff_ma, ibat_trim); |
| 3089 | } else { |
| 3090 | pr_debug("ibat loop not active - cannot calibrate ibat\n"); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | static int |
| 3095 | qpnp_chg_input_current_settled(struct qpnp_chg_chip *chip) |
| 3096 | { |
| 3097 | int rc, ibat_max_ma; |
| 3098 | u8 reg, chgr_sts, ibat_trim, i; |
Abhijeet Dharmapurikar | 02e5956 | 2014-08-12 19:31:31 -0700 | [diff] [blame] | 3099 | bool usb_present = qpnp_chg_is_usb_chg_plugged_in(chip); |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3100 | |
Abhijeet Dharmapurikar | 02e5956 | 2014-08-12 19:31:31 -0700 | [diff] [blame] | 3101 | if (!usb_present) { |
| 3102 | pr_debug("Ignoring AICL settled, since USB is removed\n"); |
| 3103 | return 0; |
| 3104 | } |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3105 | chip->aicl_settled = true; |
| 3106 | |
| 3107 | /* |
| 3108 | * Perform the ibat calibration. |
| 3109 | * This is for devices which have a IBAT_TRIM error |
| 3110 | * which can show IBAT_MAX out of spec. |
| 3111 | */ |
| 3112 | if (!chip->ibat_calibration_enabled) |
| 3113 | return 0; |
| 3114 | |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 3115 | if (chip->type != SMBB) |
| 3116 | return 0; |
| 3117 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3118 | rc = qpnp_chg_read(chip, ®, |
| 3119 | chip->buck_base + BUCK_CTRL_TRIM3, 1); |
| 3120 | if (rc) { |
| 3121 | pr_err("failed to read BUCK_CTRL_TRIM3 rc=%d\n", rc); |
| 3122 | return rc; |
| 3123 | } |
| 3124 | if (reg & IBAT_TRIM_GOOD_BIT) { |
| 3125 | pr_debug("IBAT_TRIM_GOOD bit already set. Quitting!\n"); |
| 3126 | return 0; |
| 3127 | } |
| 3128 | ibat_trim = reg & IBAT_TRIM_OFFSET_MASK; |
| 3129 | |
| 3130 | if (!is_within_range(ibat_trim, IBAT_TRIM_LOW_LIM, |
| 3131 | IBAT_TRIM_HIGH_LIM)) { |
| 3132 | pr_debug("Improper ibat_trim value=%x setting to value=%x\n", |
| 3133 | ibat_trim, IBAT_TRIM_MEAN); |
| 3134 | ibat_trim = IBAT_TRIM_MEAN; |
| 3135 | rc = qpnp_chg_masked_write(chip, |
| 3136 | chip->buck_base + BUCK_CTRL_TRIM3, |
| 3137 | IBAT_TRIM_OFFSET_MASK, ibat_trim, 1); |
| 3138 | if (rc) { |
| 3139 | pr_err("failed to set ibat_trim to %x rc=%d\n", |
| 3140 | IBAT_TRIM_MEAN, rc); |
| 3141 | return rc; |
| 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | rc = qpnp_chg_read(chip, &chgr_sts, |
| 3146 | INT_RT_STS(chip->chgr_base), 1); |
| 3147 | if (rc) { |
| 3148 | pr_err("failed to read interrupt sts rc=%d\n", rc); |
| 3149 | return rc; |
| 3150 | } |
| 3151 | if (!(chgr_sts & FAST_CHG_ON_IRQ)) { |
| 3152 | pr_debug("Not in fastchg\n"); |
| 3153 | return rc; |
| 3154 | } |
| 3155 | |
| 3156 | /* save the ibat_max to restore it later */ |
| 3157 | rc = qpnp_chg_ibatmax_get(chip, &ibat_max_ma); |
| 3158 | if (rc) { |
| 3159 | pr_debug("failed to save ibatmax rc=%d\n", rc); |
| 3160 | return rc; |
| 3161 | } |
| 3162 | |
| 3163 | rc = qpnp_chg_ibatmax_set(chip, IBAT_TRIM_TGT_MA); |
| 3164 | if (rc) { |
| 3165 | pr_err("failed to set ibatmax rc=%d\n", rc); |
| 3166 | return rc; |
| 3167 | } |
| 3168 | |
| 3169 | for (i = 0; i < 3; i++) { |
| 3170 | /* |
| 3171 | * ibat settling delay - to make sure the BMS controller |
| 3172 | * has sufficient time to sample ibat for the configured |
| 3173 | * ibat_max |
| 3174 | */ |
| 3175 | msleep(20); |
| 3176 | if (qpnp_chg_is_ibat_loop_active(chip)) |
| 3177 | qpnp_chg_trim_ibat(chip, ibat_trim); |
| 3178 | else |
| 3179 | pr_debug("ibat loop not active\n"); |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 3180 | |
| 3181 | /* read the adjusted ibat_trim for further adjustments */ |
| 3182 | rc = qpnp_chg_read(chip, &ibat_trim, |
| 3183 | chip->buck_base + BUCK_CTRL_TRIM3, 1); |
| 3184 | if (rc) { |
| 3185 | pr_err("failed to read BUCK_CTRL_TRIM3 rc=%d\n", rc); |
| 3186 | break; |
| 3187 | } |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3188 | } |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 3189 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 3190 | /* restore IBATMAX */ |
| 3191 | rc = qpnp_chg_ibatmax_set(chip, ibat_max_ma); |
| 3192 | if (rc) |
| 3193 | pr_err("failed to restore ibatmax rc=%d\n", rc); |
| 3194 | |
| 3195 | return rc; |
| 3196 | } |
| 3197 | |
| 3198 | |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3199 | #define BOOST_MIN_UV 4200000 |
| 3200 | #define BOOST_MAX_UV 5500000 |
| 3201 | #define BOOST_STEP_UV 50000 |
| 3202 | #define BOOST_MIN 16 |
| 3203 | #define N_BOOST_V ((BOOST_MAX_UV - BOOST_MIN_UV) / BOOST_STEP_UV + 1) |
| 3204 | static int |
| 3205 | qpnp_boost_vset(struct qpnp_chg_chip *chip, int voltage) |
| 3206 | { |
| 3207 | u8 reg = 0; |
| 3208 | |
| 3209 | if (voltage < BOOST_MIN_UV || voltage > BOOST_MAX_UV) { |
| 3210 | pr_err("invalid voltage requested %d uV\n", voltage); |
| 3211 | return -EINVAL; |
| 3212 | } |
| 3213 | |
| 3214 | reg = DIV_ROUND_UP(voltage - BOOST_MIN_UV, BOOST_STEP_UV) + BOOST_MIN; |
| 3215 | |
| 3216 | pr_debug("voltage=%d setting %02x\n", voltage, reg); |
| 3217 | return qpnp_chg_write(chip, ®, chip->boost_base + BOOST_VSET, 1); |
| 3218 | } |
| 3219 | |
| 3220 | static int |
| 3221 | qpnp_boost_vget_uv(struct qpnp_chg_chip *chip) |
| 3222 | { |
| 3223 | int rc; |
| 3224 | u8 boost_reg; |
| 3225 | |
| 3226 | rc = qpnp_chg_read(chip, &boost_reg, |
| 3227 | chip->boost_base + BOOST_VSET, 1); |
| 3228 | if (rc) { |
| 3229 | pr_err("failed to read BOOST_VSET rc=%d\n", rc); |
| 3230 | return rc; |
| 3231 | } |
| 3232 | |
| 3233 | if (boost_reg < BOOST_MIN) { |
| 3234 | pr_err("Invalid reading from 0x%x\n", boost_reg); |
| 3235 | return -EINVAL; |
| 3236 | } |
| 3237 | |
| 3238 | return BOOST_MIN_UV + ((boost_reg - BOOST_MIN) * BOOST_STEP_UV); |
| 3239 | } |
| 3240 | |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3241 | static void |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 3242 | qpnp_batt_system_temp_level_set(struct qpnp_chg_chip *chip, int lvl_sel) |
| 3243 | { |
| 3244 | if (lvl_sel >= 0 && lvl_sel < chip->thermal_levels) { |
| 3245 | chip->therm_lvl_sel = lvl_sel; |
| 3246 | if (lvl_sel == (chip->thermal_levels - 1)) { |
| 3247 | /* disable charging if highest value selected */ |
| 3248 | qpnp_chg_buck_control(chip, 0); |
| 3249 | } else { |
| 3250 | qpnp_chg_buck_control(chip, 1); |
| 3251 | qpnp_chg_set_appropriate_battery_current(chip); |
| 3252 | } |
| 3253 | } else { |
| 3254 | pr_err("Unsupported level selected %d\n", lvl_sel); |
| 3255 | } |
| 3256 | } |
| 3257 | |
Chunmei Cai | 661a7cd | 2014-08-05 17:31:54 +0800 | [diff] [blame] | 3258 | /* |
| 3259 | * Increase the SMBB/SMBBP charger overtemp threshold to 150C while firing |
| 3260 | * the flash (and/or torch for PM8x26) when the bharger is used as the |
| 3261 | * power source. |
| 3262 | */ |
| 3263 | static int |
| 3264 | qpnp_chg_temp_threshold_set(struct qpnp_chg_chip *chip, u8 value) |
| 3265 | { |
| 3266 | int rc; |
| 3267 | |
| 3268 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + |
| 3269 | CHGR_CHG_TEMP_THRESH , |
| 3270 | 0xFF, value, 1); |
| 3271 | if (rc) |
| 3272 | pr_err("set CHG_TEMP_THRESH_Flash failed, value = %d, rc = %d\n", |
| 3273 | value, rc); |
| 3274 | |
| 3275 | return rc; |
| 3276 | } |
| 3277 | |
| 3278 | #define CHG_TEMP_THRESH_FOR_FLASH 0xFD |
| 3279 | #define CHG_TEMP_THRESH_DEFAULT 0x94 |
| 3280 | static int |
| 3281 | qpnp_chg_regulator_flash_wa_enable(struct regulator_dev *rdev) |
| 3282 | { |
| 3283 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3284 | int rc = 0; |
| 3285 | |
| 3286 | if (chip->flags & BOOST_FLASH_WA) { |
| 3287 | rc = qpnp_chg_temp_threshold_set(chip, |
| 3288 | CHG_TEMP_THRESH_FOR_FLASH); |
| 3289 | if (rc) { |
| 3290 | pr_err("set chg temp threshold failed rc = %d\n", rc); |
| 3291 | return rc; |
| 3292 | } |
| 3293 | } |
| 3294 | chip->is_flash_wa_reg_enabled = true; |
| 3295 | |
| 3296 | return rc; |
| 3297 | } |
| 3298 | |
| 3299 | static int |
| 3300 | qpnp_chg_regulator_flash_wa_disable(struct regulator_dev *rdev) |
| 3301 | { |
| 3302 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3303 | int rc = 0; |
| 3304 | |
| 3305 | if (chip->flags & BOOST_FLASH_WA) { |
| 3306 | rc = qpnp_chg_temp_threshold_set(chip, |
| 3307 | chip->chg_temp_thresh_default); |
| 3308 | if (rc) { |
| 3309 | pr_err("set chg temp threshold failed rc = %d\n", rc); |
| 3310 | return rc; |
| 3311 | } |
| 3312 | |
| 3313 | } |
| 3314 | chip->is_flash_wa_reg_enabled = false; |
| 3315 | |
| 3316 | return rc; |
| 3317 | } |
| 3318 | |
| 3319 | static int |
| 3320 | qpnp_chg_regulator_flash_wa_is_enabled(struct regulator_dev *rdev) |
| 3321 | { |
| 3322 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3323 | |
| 3324 | return chip->is_flash_wa_reg_enabled; |
| 3325 | } |
| 3326 | |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3327 | /* OTG regulator operations */ |
| 3328 | static int |
| 3329 | qpnp_chg_regulator_otg_enable(struct regulator_dev *rdev) |
| 3330 | { |
| 3331 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3332 | |
| 3333 | return switch_usb_to_host_mode(chip); |
| 3334 | } |
| 3335 | |
| 3336 | static int |
| 3337 | qpnp_chg_regulator_otg_disable(struct regulator_dev *rdev) |
| 3338 | { |
| 3339 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3340 | |
| 3341 | return switch_usb_to_charge_mode(chip); |
| 3342 | } |
| 3343 | |
| 3344 | static int |
| 3345 | qpnp_chg_regulator_otg_is_enabled(struct regulator_dev *rdev) |
| 3346 | { |
| 3347 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3348 | |
| 3349 | return qpnp_chg_is_otg_en_set(chip); |
| 3350 | } |
| 3351 | |
| 3352 | static int |
| 3353 | qpnp_chg_regulator_boost_enable(struct regulator_dev *rdev) |
| 3354 | { |
| 3355 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 3356 | int usb_present = qpnp_chg_is_usb_chg_plugged_in(chip); |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3357 | int rc; |
| 3358 | |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 3359 | if (usb_present && (chip->flags & BOOST_FLASH_WA)) { |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 3360 | |
| 3361 | if (ext_ovp_isns_present && chip->ext_ovp_ic_gpio_enabled) { |
| 3362 | pr_debug("EXT OVP IC ISNS disabled\n"); |
| 3363 | gpio_direction_output(chip->ext_ovp_isns_gpio, 0); |
| 3364 | } |
| 3365 | |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3366 | qpnp_chg_usb_suspend_enable(chip, 1); |
| 3367 | |
| 3368 | rc = qpnp_chg_masked_write(chip, |
| 3369 | chip->usb_chgpth_base + SEC_ACCESS, |
| 3370 | 0xFF, |
| 3371 | 0xA5, 1); |
| 3372 | if (rc) { |
| 3373 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 3374 | return rc; |
| 3375 | } |
| 3376 | |
| 3377 | rc = qpnp_chg_masked_write(chip, |
| 3378 | chip->usb_chgpth_base + COMP_OVR1, |
| 3379 | 0xFF, |
| 3380 | 0x2F, 1); |
| 3381 | if (rc) { |
| 3382 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 3383 | return rc; |
| 3384 | } |
| 3385 | } |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3386 | |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 3387 | rc = qpnp_chg_masked_write(chip, |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3388 | chip->boost_base + BOOST_ENABLE_CONTROL, |
| 3389 | BOOST_PWR_EN, |
| 3390 | BOOST_PWR_EN, 1); |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 3391 | if (rc) { |
| 3392 | pr_err("failed to enable boost rc = %d\n", rc); |
| 3393 | return rc; |
| 3394 | } |
| 3395 | /* |
| 3396 | * update battery status when charger is connected and state is full |
| 3397 | */ |
| 3398 | if (usb_present && (chip->chg_done |
| 3399 | || (get_batt_capacity(chip) == 100) |
| 3400 | || (get_prop_batt_status(chip) == |
| 3401 | POWER_SUPPLY_STATUS_FULL))) |
| 3402 | power_supply_changed(&chip->batt_psy); |
| 3403 | |
| 3404 | return rc; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | /* Boost regulator operations */ |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3408 | #define ABOVE_VBAT_WEAK BIT(1) |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3409 | static int |
| 3410 | qpnp_chg_regulator_boost_disable(struct regulator_dev *rdev) |
| 3411 | { |
| 3412 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3413 | int rc; |
| 3414 | u8 vbat_sts; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3415 | |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3416 | rc = qpnp_chg_masked_write(chip, |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3417 | chip->boost_base + BOOST_ENABLE_CONTROL, |
| 3418 | BOOST_PWR_EN, |
| 3419 | 0, 1); |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3420 | if (rc) { |
| 3421 | pr_err("failed to disable boost rc=%d\n", rc); |
| 3422 | return rc; |
| 3423 | } |
| 3424 | |
| 3425 | rc = qpnp_chg_read(chip, &vbat_sts, |
| 3426 | chip->chgr_base + CHGR_VBAT_STATUS, 1); |
| 3427 | if (rc) { |
| 3428 | pr_err("failed to read bat sts rc=%d\n", rc); |
| 3429 | return rc; |
| 3430 | } |
| 3431 | |
| 3432 | if (!(vbat_sts & ABOVE_VBAT_WEAK) && (chip->flags & BOOST_FLASH_WA)) { |
| 3433 | rc = qpnp_chg_masked_write(chip, |
| 3434 | chip->chgr_base + SEC_ACCESS, |
| 3435 | 0xFF, |
| 3436 | 0xA5, 1); |
| 3437 | if (rc) { |
| 3438 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 3439 | return rc; |
| 3440 | } |
| 3441 | |
| 3442 | rc = qpnp_chg_masked_write(chip, |
| 3443 | chip->chgr_base + COMP_OVR1, |
| 3444 | 0xFF, |
| 3445 | 0x20, 1); |
| 3446 | if (rc) { |
| 3447 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 3448 | return rc; |
| 3449 | } |
| 3450 | |
| 3451 | usleep(2000); |
| 3452 | |
| 3453 | rc = qpnp_chg_masked_write(chip, |
| 3454 | chip->chgr_base + SEC_ACCESS, |
| 3455 | 0xFF, |
| 3456 | 0xA5, 1); |
| 3457 | if (rc) { |
| 3458 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 3459 | return rc; |
| 3460 | } |
| 3461 | |
| 3462 | rc = qpnp_chg_masked_write(chip, |
| 3463 | chip->chgr_base + COMP_OVR1, |
| 3464 | 0xFF, |
| 3465 | 0x00, 1); |
| 3466 | if (rc) { |
| 3467 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 3468 | return rc; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | if (qpnp_chg_is_usb_chg_plugged_in(chip) |
| 3473 | && (chip->flags & BOOST_FLASH_WA)) { |
| 3474 | rc = qpnp_chg_masked_write(chip, |
| 3475 | chip->usb_chgpth_base + SEC_ACCESS, |
| 3476 | 0xFF, |
| 3477 | 0xA5, 1); |
| 3478 | if (rc) { |
| 3479 | pr_err("failed to write SEC_ACCESS rc=%d\n", rc); |
| 3480 | return rc; |
| 3481 | } |
| 3482 | |
| 3483 | rc = qpnp_chg_masked_write(chip, |
| 3484 | chip->usb_chgpth_base + COMP_OVR1, |
| 3485 | 0xFF, |
| 3486 | 0x00, 1); |
| 3487 | if (rc) { |
| 3488 | pr_err("failed to write COMP_OVR1 rc=%d\n", rc); |
| 3489 | return rc; |
| 3490 | } |
| 3491 | |
| 3492 | usleep(1000); |
| 3493 | |
| 3494 | qpnp_chg_usb_suspend_enable(chip, 0); |
| 3495 | } |
| 3496 | |
Chunmei Cai | b3999b2 | 2014-07-11 15:38:13 +0800 | [diff] [blame] | 3497 | /* |
| 3498 | * When a charger is connected,if state of charge is not full |
| 3499 | * resumeing charging else update battery status |
| 3500 | */ |
| 3501 | if (qpnp_chg_is_usb_chg_plugged_in(chip)) { |
| 3502 | if (get_batt_capacity(chip) < 100 || !chip->chg_done) { |
| 3503 | chip->chg_done = false; |
| 3504 | chip->resuming_charging = true; |
| 3505 | qpnp_chg_set_appropriate_vbatdet(chip); |
| 3506 | } else if (chip->chg_done) { |
| 3507 | power_supply_changed(&chip->batt_psy); |
| 3508 | } |
| 3509 | } |
| 3510 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 3511 | if (ext_ovp_isns_present && chip->ext_ovp_ic_gpio_enabled) { |
| 3512 | pr_debug("EXT OVP IC ISNS enable\n"); |
| 3513 | gpio_direction_output(chip->ext_ovp_isns_gpio, 1); |
| 3514 | } |
| 3515 | |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 3516 | return rc; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | static int |
| 3520 | qpnp_chg_regulator_boost_is_enabled(struct regulator_dev *rdev) |
| 3521 | { |
| 3522 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3523 | |
| 3524 | return qpnp_chg_is_boost_en_set(chip); |
| 3525 | } |
| 3526 | |
| 3527 | static int |
| 3528 | qpnp_chg_regulator_boost_set_voltage(struct regulator_dev *rdev, |
| 3529 | int min_uV, int max_uV, unsigned *selector) |
| 3530 | { |
| 3531 | int uV = min_uV; |
| 3532 | int rc; |
| 3533 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3534 | |
| 3535 | if (uV < BOOST_MIN_UV && max_uV >= BOOST_MIN_UV) |
| 3536 | uV = BOOST_MIN_UV; |
| 3537 | |
| 3538 | |
| 3539 | if (uV < BOOST_MIN_UV || uV > BOOST_MAX_UV) { |
| 3540 | pr_err("request %d uV is out of bounds\n", uV); |
| 3541 | return -EINVAL; |
| 3542 | } |
| 3543 | |
| 3544 | *selector = DIV_ROUND_UP(uV - BOOST_MIN_UV, BOOST_STEP_UV); |
| 3545 | if ((*selector * BOOST_STEP_UV + BOOST_MIN_UV) > max_uV) { |
| 3546 | pr_err("no available setpoint [%d, %d] uV\n", min_uV, max_uV); |
| 3547 | return -EINVAL; |
| 3548 | } |
| 3549 | |
| 3550 | rc = qpnp_boost_vset(chip, uV); |
| 3551 | |
| 3552 | return rc; |
| 3553 | } |
| 3554 | |
| 3555 | static int |
| 3556 | qpnp_chg_regulator_boost_get_voltage(struct regulator_dev *rdev) |
| 3557 | { |
| 3558 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3559 | |
| 3560 | return qpnp_boost_vget_uv(chip); |
| 3561 | } |
| 3562 | |
| 3563 | static int |
| 3564 | qpnp_chg_regulator_boost_list_voltage(struct regulator_dev *rdev, |
| 3565 | unsigned selector) |
| 3566 | { |
| 3567 | if (selector >= N_BOOST_V) |
| 3568 | return 0; |
| 3569 | |
| 3570 | return BOOST_MIN_UV + (selector * BOOST_STEP_UV); |
| 3571 | } |
| 3572 | |
Chunmei Cai | 661a7cd | 2014-08-05 17:31:54 +0800 | [diff] [blame] | 3573 | static struct regulator_ops qpnp_chg_flash_wa_reg_ops = { |
| 3574 | .enable = qpnp_chg_regulator_flash_wa_enable, |
| 3575 | .disable = qpnp_chg_regulator_flash_wa_disable, |
| 3576 | .is_enabled = qpnp_chg_regulator_flash_wa_is_enabled, |
| 3577 | }; |
| 3578 | |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 3579 | static struct regulator_ops qpnp_chg_otg_reg_ops = { |
| 3580 | .enable = qpnp_chg_regulator_otg_enable, |
| 3581 | .disable = qpnp_chg_regulator_otg_disable, |
| 3582 | .is_enabled = qpnp_chg_regulator_otg_is_enabled, |
| 3583 | }; |
| 3584 | |
| 3585 | static struct regulator_ops qpnp_chg_boost_reg_ops = { |
| 3586 | .enable = qpnp_chg_regulator_boost_enable, |
| 3587 | .disable = qpnp_chg_regulator_boost_disable, |
| 3588 | .is_enabled = qpnp_chg_regulator_boost_is_enabled, |
| 3589 | .set_voltage = qpnp_chg_regulator_boost_set_voltage, |
| 3590 | .get_voltage = qpnp_chg_regulator_boost_get_voltage, |
| 3591 | .list_voltage = qpnp_chg_regulator_boost_list_voltage, |
| 3592 | }; |
| 3593 | |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3594 | static int |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 3595 | qpnp_chg_bat_if_batfet_reg_enabled(struct qpnp_chg_chip *chip) |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3596 | { |
David Keitel | 9eca8ac | 2013-09-06 14:18:36 -0700 | [diff] [blame] | 3597 | int rc = 0; |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 3598 | u8 reg = 0; |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3599 | |
David Keitel | cadbc28 | 2013-09-26 11:39:14 -0700 | [diff] [blame] | 3600 | if (!chip->bat_if_base) |
| 3601 | return rc; |
| 3602 | |
David Keitel | 9eca8ac | 2013-09-06 14:18:36 -0700 | [diff] [blame] | 3603 | if (chip->type == SMBB) |
| 3604 | rc = qpnp_chg_read(chip, ®, |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3605 | chip->bat_if_base + CHGR_BAT_IF_SPARE, 1); |
David Keitel | 9eca8ac | 2013-09-06 14:18:36 -0700 | [diff] [blame] | 3606 | else |
| 3607 | rc = qpnp_chg_read(chip, ®, |
| 3608 | chip->bat_if_base + CHGR_BAT_IF_BATFET_CTRL4, 1); |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 3609 | |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3610 | if (rc) { |
| 3611 | pr_err("failed to read batt_if rc=%d\n", rc); |
| 3612 | return rc; |
| 3613 | } |
| 3614 | |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 3615 | if ((reg & BATFET_LPM_MASK) == BATFET_NO_LPM) |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3616 | return 1; |
| 3617 | |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 3621 | static int |
| 3622 | qpnp_chg_regulator_batfet_enable(struct regulator_dev *rdev) |
| 3623 | { |
| 3624 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3625 | int rc = 0; |
| 3626 | |
| 3627 | mutex_lock(&chip->batfet_vreg_lock); |
| 3628 | /* Only enable if not already enabled */ |
| 3629 | if (!qpnp_chg_bat_if_batfet_reg_enabled(chip)) { |
| 3630 | rc = qpnp_chg_regulator_batfet_set(chip, 1); |
| 3631 | if (rc) |
| 3632 | pr_err("failed to write to batt_if rc=%d\n", rc); |
| 3633 | } |
| 3634 | |
| 3635 | chip->batfet_ext_en = true; |
| 3636 | mutex_unlock(&chip->batfet_vreg_lock); |
| 3637 | |
| 3638 | return rc; |
| 3639 | } |
| 3640 | |
| 3641 | static int |
| 3642 | qpnp_chg_regulator_batfet_disable(struct regulator_dev *rdev) |
| 3643 | { |
| 3644 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3645 | int rc = 0; |
| 3646 | |
| 3647 | mutex_lock(&chip->batfet_vreg_lock); |
| 3648 | /* Don't allow disable if charger connected */ |
| 3649 | if (!qpnp_chg_is_usb_chg_plugged_in(chip) && |
| 3650 | !qpnp_chg_is_dc_chg_plugged_in(chip)) { |
| 3651 | rc = qpnp_chg_regulator_batfet_set(chip, 0); |
| 3652 | if (rc) |
| 3653 | pr_err("failed to write to batt_if rc=%d\n", rc); |
| 3654 | } |
| 3655 | |
| 3656 | chip->batfet_ext_en = false; |
| 3657 | mutex_unlock(&chip->batfet_vreg_lock); |
| 3658 | |
| 3659 | return rc; |
| 3660 | } |
| 3661 | |
| 3662 | static int |
| 3663 | qpnp_chg_regulator_batfet_is_enabled(struct regulator_dev *rdev) |
| 3664 | { |
| 3665 | struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev); |
| 3666 | |
| 3667 | return chip->batfet_ext_en; |
| 3668 | } |
| 3669 | |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 3670 | static struct regulator_ops qpnp_chg_batfet_vreg_ops = { |
| 3671 | .enable = qpnp_chg_regulator_batfet_enable, |
| 3672 | .disable = qpnp_chg_regulator_batfet_disable, |
| 3673 | .is_enabled = qpnp_chg_regulator_batfet_is_enabled, |
| 3674 | }; |
| 3675 | |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 3676 | #define MIN_DELTA_MV_TO_INCREASE_VDD_MAX 8 |
| 3677 | #define MAX_DELTA_VDD_MAX_MV 80 |
| 3678 | #define VDD_MAX_CENTER_OFFSET 4 |
Xiaozhe Shi | 79a8c49 | 2013-05-24 13:09:23 -0700 | [diff] [blame] | 3679 | static void |
| 3680 | qpnp_chg_adjust_vddmax(struct qpnp_chg_chip *chip, int vbat_mv) |
| 3681 | { |
| 3682 | int delta_mv, closest_delta_mv, sign; |
| 3683 | |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 3684 | delta_mv = chip->max_voltage_mv - VDD_MAX_CENTER_OFFSET - vbat_mv; |
Xiaozhe Shi | 79a8c49 | 2013-05-24 13:09:23 -0700 | [diff] [blame] | 3685 | if (delta_mv > 0 && delta_mv < MIN_DELTA_MV_TO_INCREASE_VDD_MAX) { |
| 3686 | pr_debug("vbat is not low enough to increase vdd\n"); |
| 3687 | return; |
| 3688 | } |
| 3689 | |
| 3690 | sign = delta_mv > 0 ? 1 : -1; |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 3691 | closest_delta_mv = ((delta_mv + sign * QPNP_CHG_BUCK_TRIM1_STEP / 2) |
| 3692 | / QPNP_CHG_BUCK_TRIM1_STEP) * QPNP_CHG_BUCK_TRIM1_STEP; |
Xiaozhe Shi | 79a8c49 | 2013-05-24 13:09:23 -0700 | [diff] [blame] | 3693 | pr_debug("max_voltage = %d, vbat_mv = %d, delta_mv = %d, closest = %d\n", |
| 3694 | chip->max_voltage_mv, vbat_mv, |
| 3695 | delta_mv, closest_delta_mv); |
| 3696 | chip->delta_vddmax_mv = clamp(chip->delta_vddmax_mv + closest_delta_mv, |
| 3697 | -MAX_DELTA_VDD_MAX_MV, MAX_DELTA_VDD_MAX_MV); |
| 3698 | pr_debug("using delta_vddmax_mv = %d\n", chip->delta_vddmax_mv); |
| 3699 | qpnp_chg_set_appropriate_vddmax(chip); |
| 3700 | } |
| 3701 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3702 | #define CONSECUTIVE_COUNT 3 |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3703 | #define VBATDET_MAX_ERR_MV 50 |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3704 | static void |
| 3705 | qpnp_eoc_work(struct work_struct *work) |
| 3706 | { |
| 3707 | struct delayed_work *dwork = to_delayed_work(work); |
| 3708 | struct qpnp_chg_chip *chip = container_of(dwork, |
| 3709 | struct qpnp_chg_chip, eoc_work); |
| 3710 | static int count; |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3711 | static int vbat_low_count; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3712 | int ibat_ma, vbat_mv, rc = 0; |
| 3713 | u8 batt_sts = 0, buck_sts = 0, chg_sts = 0; |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3714 | bool vbat_lower_than_vbatdet; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3715 | |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3716 | pm_stay_awake(chip->dev); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3717 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 3718 | |
| 3719 | rc = qpnp_chg_read(chip, &batt_sts, INT_RT_STS(chip->bat_if_base), 1); |
| 3720 | if (rc) { |
| 3721 | pr_err("failed to read batt_if rc=%d\n", rc); |
| 3722 | return; |
| 3723 | } |
| 3724 | |
| 3725 | rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1); |
| 3726 | if (rc) { |
| 3727 | pr_err("failed to read buck rc=%d\n", rc); |
| 3728 | return; |
| 3729 | } |
| 3730 | |
| 3731 | rc = qpnp_chg_read(chip, &chg_sts, INT_RT_STS(chip->chgr_base), 1); |
| 3732 | if (rc) { |
| 3733 | pr_err("failed to read chg_sts rc=%d\n", rc); |
| 3734 | return; |
| 3735 | } |
| 3736 | |
| 3737 | pr_debug("chgr: 0x%x, bat_if: 0x%x, buck: 0x%x\n", |
| 3738 | chg_sts, batt_sts, buck_sts); |
| 3739 | |
| 3740 | if (!qpnp_chg_is_usb_chg_plugged_in(chip) && |
| 3741 | !qpnp_chg_is_dc_chg_plugged_in(chip)) { |
| 3742 | pr_debug("no chg connected, stopping\n"); |
| 3743 | goto stop_eoc; |
| 3744 | } |
| 3745 | |
| 3746 | if ((batt_sts & BAT_FET_ON_IRQ) && (chg_sts & FAST_CHG_ON_IRQ |
| 3747 | || chg_sts & TRKL_CHG_ON_IRQ)) { |
| 3748 | ibat_ma = get_prop_current_now(chip) / 1000; |
| 3749 | vbat_mv = get_prop_battery_voltage_now(chip) / 1000; |
Xiaozhe Shi | 22af5fe | 2013-05-29 10:38:30 -0700 | [diff] [blame] | 3750 | |
| 3751 | pr_debug("ibat_ma = %d vbat_mv = %d term_current_ma = %d\n", |
| 3752 | ibat_ma, vbat_mv, chip->term_current); |
| 3753 | |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3754 | vbat_lower_than_vbatdet = !(chg_sts & VBAT_DET_LOW_IRQ); |
| 3755 | if (vbat_lower_than_vbatdet && vbat_mv < |
| 3756 | (chip->max_voltage_mv - chip->resume_delta_mv |
David Keitel | bdbae45 | 2014-01-14 17:19:50 -0800 | [diff] [blame] | 3757 | - chip->vbatdet_max_err_mv)) { |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3758 | vbat_low_count++; |
| 3759 | pr_debug("woke up too early vbat_mv = %d, max_mv = %d, resume_mv = %d tolerance_mv = %d low_count = %d\n", |
| 3760 | vbat_mv, chip->max_voltage_mv, |
| 3761 | chip->resume_delta_mv, |
David Keitel | bdbae45 | 2014-01-14 17:19:50 -0800 | [diff] [blame] | 3762 | chip->vbatdet_max_err_mv, |
| 3763 | vbat_low_count); |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3764 | if (vbat_low_count >= CONSECUTIVE_COUNT) { |
| 3765 | pr_debug("woke up too early stopping\n"); |
| 3766 | qpnp_chg_enable_irq(&chip->chg_vbatdet_lo); |
| 3767 | goto stop_eoc; |
| 3768 | } else { |
| 3769 | goto check_again_later; |
| 3770 | } |
| 3771 | } else { |
| 3772 | vbat_low_count = 0; |
Xiaozhe Shi | 22af5fe | 2013-05-29 10:38:30 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
Xiaozhe Shi | 79a8c49 | 2013-05-24 13:09:23 -0700 | [diff] [blame] | 3775 | if (buck_sts & VDD_LOOP_IRQ) |
| 3776 | qpnp_chg_adjust_vddmax(chip, vbat_mv); |
| 3777 | |
Xiaozhe Shi | 22af5fe | 2013-05-29 10:38:30 -0700 | [diff] [blame] | 3778 | if (!(buck_sts & VDD_LOOP_IRQ)) { |
| 3779 | pr_debug("Not in CV\n"); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3780 | count = 0; |
Xiaozhe Shi | 22af5fe | 2013-05-29 10:38:30 -0700 | [diff] [blame] | 3781 | } else if ((ibat_ma * -1) > chip->term_current) { |
| 3782 | pr_debug("Not at EOC, battery current too high\n"); |
| 3783 | count = 0; |
| 3784 | } else if (ibat_ma > 0) { |
| 3785 | pr_debug("Charging but system demand increased\n"); |
| 3786 | count = 0; |
| 3787 | } else { |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3788 | if (count == CONSECUTIVE_COUNT) { |
Xiaozhe Shi | e850448 | 2013-10-30 18:10:31 -0700 | [diff] [blame] | 3789 | if (!chip->bat_is_cool && !chip->bat_is_warm) { |
| 3790 | pr_info("End of Charging\n"); |
| 3791 | chip->chg_done = true; |
| 3792 | } else { |
| 3793 | pr_info("stop charging: battery is %s, vddmax = %d reached\n", |
| 3794 | chip->bat_is_cool |
| 3795 | ? "cool" : "warm", |
| 3796 | qpnp_chg_vddmax_get(chip)); |
| 3797 | } |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3798 | qpnp_chg_charge_en(chip, 0); |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3799 | /* sleep for a second before enabling */ |
| 3800 | msleep(2000); |
| 3801 | qpnp_chg_charge_en(chip, |
| 3802 | !chip->charging_disabled); |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 3803 | pr_debug("psy changed batt_psy\n"); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3804 | power_supply_changed(&chip->batt_psy); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 3805 | qpnp_chg_enable_irq(&chip->chg_vbatdet_lo); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3806 | goto stop_eoc; |
| 3807 | } else { |
| 3808 | count += 1; |
| 3809 | pr_debug("EOC count = %d\n", count); |
| 3810 | } |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3811 | } |
| 3812 | } else { |
| 3813 | pr_debug("not charging\n"); |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 3814 | goto stop_eoc; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3817 | check_again_later: |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3818 | schedule_delayed_work(&chip->eoc_work, |
| 3819 | msecs_to_jiffies(EOC_CHECK_PERIOD_MS)); |
| 3820 | return; |
| 3821 | |
| 3822 | stop_eoc: |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3823 | vbat_low_count = 0; |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3824 | count = 0; |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 3825 | pm_relax(chip->dev); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 3828 | static void |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 3829 | qpnp_chg_insertion_ocv_work(struct work_struct *work) |
| 3830 | { |
| 3831 | struct qpnp_chg_chip *chip = container_of(work, |
| 3832 | struct qpnp_chg_chip, insertion_ocv_work); |
| 3833 | u8 bat_if_sts = 0, charge_en = 0; |
| 3834 | int rc; |
| 3835 | |
| 3836 | chip->insertion_ocv_uv = get_prop_battery_voltage_now(chip); |
| 3837 | |
| 3838 | rc = qpnp_chg_read(chip, &bat_if_sts, INT_RT_STS(chip->bat_if_base), 1); |
| 3839 | if (rc) |
| 3840 | pr_err("failed to read bat_if sts %d\n", rc); |
| 3841 | |
| 3842 | rc = qpnp_chg_read(chip, &charge_en, |
| 3843 | chip->chgr_base + CHGR_CHG_CTRL, 1); |
| 3844 | if (rc) |
| 3845 | pr_err("failed to read bat_if sts %d\n", rc); |
| 3846 | |
| 3847 | pr_debug("batfet sts = %02x, charge_en = %02x ocv = %d\n", |
| 3848 | bat_if_sts, charge_en, chip->insertion_ocv_uv); |
| 3849 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 3850 | pr_debug("psy changed batt_psy\n"); |
| 3851 | power_supply_changed(&chip->batt_psy); |
| 3852 | } |
| 3853 | |
| 3854 | static void |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 3855 | qpnp_chg_soc_check_work(struct work_struct *work) |
| 3856 | { |
| 3857 | struct qpnp_chg_chip *chip = container_of(work, |
| 3858 | struct qpnp_chg_chip, soc_check_work); |
| 3859 | |
| 3860 | get_prop_capacity(chip); |
| 3861 | } |
| 3862 | |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 3863 | #define HYSTERISIS_DECIDEGC 20 |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3864 | static void |
| 3865 | qpnp_chg_adc_notification(enum qpnp_tm_state state, void *ctx) |
| 3866 | { |
| 3867 | struct qpnp_chg_chip *chip = ctx; |
| 3868 | bool bat_warm = 0, bat_cool = 0; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3869 | int temp; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3870 | |
| 3871 | if (state >= ADC_TM_STATE_NUM) { |
| 3872 | pr_err("invalid notification %d\n", state); |
| 3873 | return; |
| 3874 | } |
| 3875 | |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3876 | temp = get_prop_batt_temp(chip); |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3877 | |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3878 | pr_debug("temp = %d state = %s\n", temp, |
| 3879 | state == ADC_TM_WARM_STATE ? "warm" : "cool"); |
| 3880 | |
| 3881 | if (state == ADC_TM_WARM_STATE) { |
David Keitel | 410ee0d | 2014-02-20 16:48:04 -0800 | [diff] [blame] | 3882 | if (temp >= chip->warm_bat_decidegc) { |
David Keitel | 3e37e5a | 2013-04-18 10:42:30 -0700 | [diff] [blame] | 3883 | /* Normal to warm */ |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3884 | bat_warm = true; |
| 3885 | bat_cool = false; |
| 3886 | chip->adc_param.low_temp = |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 3887 | chip->warm_bat_decidegc - HYSTERISIS_DECIDEGC; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3888 | chip->adc_param.state_request = |
| 3889 | ADC_TM_COOL_THR_ENABLE; |
David Keitel | 410ee0d | 2014-02-20 16:48:04 -0800 | [diff] [blame] | 3890 | } else if (temp >= |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3891 | chip->cool_bat_decidegc + HYSTERISIS_DECIDEGC){ |
David Keitel | 3e37e5a | 2013-04-18 10:42:30 -0700 | [diff] [blame] | 3892 | /* Cool to normal */ |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3893 | bat_warm = false; |
| 3894 | bat_cool = false; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3895 | |
| 3896 | chip->adc_param.low_temp = chip->cool_bat_decidegc; |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 3897 | chip->adc_param.high_temp = chip->warm_bat_decidegc; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3898 | chip->adc_param.state_request = |
| 3899 | ADC_TM_HIGH_LOW_THR_ENABLE; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3900 | } |
| 3901 | } else { |
David Keitel | 410ee0d | 2014-02-20 16:48:04 -0800 | [diff] [blame] | 3902 | if (temp <= chip->cool_bat_decidegc) { |
David Keitel | 3e37e5a | 2013-04-18 10:42:30 -0700 | [diff] [blame] | 3903 | /* Normal to cool */ |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3904 | bat_warm = false; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3905 | bat_cool = true; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3906 | chip->adc_param.high_temp = |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 3907 | chip->cool_bat_decidegc + HYSTERISIS_DECIDEGC; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3908 | chip->adc_param.state_request = |
| 3909 | ADC_TM_WARM_THR_ENABLE; |
David Keitel | 410ee0d | 2014-02-20 16:48:04 -0800 | [diff] [blame] | 3910 | } else if (temp <= |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3911 | chip->warm_bat_decidegc - HYSTERISIS_DECIDEGC){ |
David Keitel | 3e37e5a | 2013-04-18 10:42:30 -0700 | [diff] [blame] | 3912 | /* Warm to normal */ |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3913 | bat_warm = false; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3914 | bat_cool = false; |
| 3915 | |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 3916 | chip->adc_param.low_temp = chip->cool_bat_decidegc; |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3917 | chip->adc_param.high_temp = chip->warm_bat_decidegc; |
| 3918 | chip->adc_param.state_request = |
| 3919 | ADC_TM_HIGH_LOW_THR_ENABLE; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | if (chip->bat_is_cool ^ bat_cool || chip->bat_is_warm ^ bat_warm) { |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3924 | chip->bat_is_cool = bat_cool; |
| 3925 | chip->bat_is_warm = bat_warm; |
| 3926 | |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 3927 | /** |
| 3928 | * set appropriate voltages and currents. |
| 3929 | * |
| 3930 | * Note that when the battery is hot or cold, the charger |
| 3931 | * driver will not resume with SoC. Only vbatdet is used to |
| 3932 | * determine resume of charging. |
| 3933 | */ |
David Keitel | 59fbb76 | 2014-02-05 16:46:24 -0800 | [diff] [blame] | 3934 | if (bat_cool || bat_warm) { |
| 3935 | chip->resuming_charging = false; |
| 3936 | qpnp_chg_set_appropriate_vbatdet(chip); |
| 3937 | |
| 3938 | /* To avoid ARB, only vbatdet is configured in |
| 3939 | * warm/cold zones. Once vbat < vbatdet the |
| 3940 | * appropriate vddmax/ibatmax adjustments will |
| 3941 | * be made in the fast charge interrupt. */ |
| 3942 | bypass_vbatdet_comp(chip, 1); |
| 3943 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 3944 | qpnp_chg_charge_en(chip, chip->charging_disabled); |
| 3945 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 3946 | } else { |
| 3947 | bypass_vbatdet_comp(chip, 0); |
| 3948 | /* restore normal parameters */ |
| 3949 | qpnp_chg_set_appropriate_vbatdet(chip); |
| 3950 | qpnp_chg_set_appropriate_vddmax(chip); |
| 3951 | qpnp_chg_set_appropriate_battery_current(chip); |
| 3952 | } |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 3953 | } |
| 3954 | |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 3955 | pr_debug("warm %d, cool %d, low = %d deciDegC, high = %d deciDegC\n", |
| 3956 | chip->bat_is_warm, chip->bat_is_cool, |
| 3957 | chip->adc_param.low_temp, chip->adc_param.high_temp); |
| 3958 | |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 3959 | if (qpnp_adc_tm_channel_measure(chip->adc_tm_dev, &chip->adc_param)) |
David Keitel | 1219a80 | 2013-03-21 16:37:21 -0700 | [diff] [blame] | 3960 | pr_err("request ADC error\n"); |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 3961 | } |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 3962 | |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 3963 | #define MIN_COOL_TEMP -300 |
| 3964 | #define MAX_WARM_TEMP 1000 |
| 3965 | |
| 3966 | static int |
| 3967 | qpnp_chg_configure_jeita(struct qpnp_chg_chip *chip, |
| 3968 | enum power_supply_property psp, int temp_degc) |
| 3969 | { |
| 3970 | int rc = 0; |
| 3971 | |
| 3972 | if ((temp_degc < MIN_COOL_TEMP) || (temp_degc > MAX_WARM_TEMP)) { |
| 3973 | pr_err("Bad temperature request %d\n", temp_degc); |
| 3974 | return -EINVAL; |
| 3975 | } |
| 3976 | |
| 3977 | mutex_lock(&chip->jeita_configure_lock); |
| 3978 | switch (psp) { |
| 3979 | case POWER_SUPPLY_PROP_COOL_TEMP: |
| 3980 | if (temp_degc >= |
| 3981 | (chip->warm_bat_decidegc - HYSTERISIS_DECIDEGC)) { |
| 3982 | pr_err("Can't set cool %d higher than warm %d - hysterisis %d\n", |
| 3983 | temp_degc, chip->warm_bat_decidegc, |
| 3984 | HYSTERISIS_DECIDEGC); |
| 3985 | rc = -EINVAL; |
| 3986 | goto mutex_unlock; |
| 3987 | } |
| 3988 | if (chip->bat_is_cool) |
| 3989 | chip->adc_param.high_temp = |
| 3990 | temp_degc + HYSTERISIS_DECIDEGC; |
| 3991 | else if (!chip->bat_is_warm) |
| 3992 | chip->adc_param.low_temp = temp_degc; |
| 3993 | |
| 3994 | chip->cool_bat_decidegc = temp_degc; |
| 3995 | break; |
| 3996 | case POWER_SUPPLY_PROP_WARM_TEMP: |
| 3997 | if (temp_degc <= |
| 3998 | (chip->cool_bat_decidegc + HYSTERISIS_DECIDEGC)) { |
| 3999 | pr_err("Can't set warm %d higher than cool %d + hysterisis %d\n", |
| 4000 | temp_degc, chip->warm_bat_decidegc, |
| 4001 | HYSTERISIS_DECIDEGC); |
| 4002 | rc = -EINVAL; |
| 4003 | goto mutex_unlock; |
| 4004 | } |
| 4005 | if (chip->bat_is_warm) |
| 4006 | chip->adc_param.low_temp = |
| 4007 | temp_degc - HYSTERISIS_DECIDEGC; |
| 4008 | else if (!chip->bat_is_cool) |
| 4009 | chip->adc_param.high_temp = temp_degc; |
| 4010 | |
| 4011 | chip->warm_bat_decidegc = temp_degc; |
| 4012 | break; |
| 4013 | default: |
| 4014 | rc = -EINVAL; |
| 4015 | goto mutex_unlock; |
| 4016 | } |
| 4017 | |
| 4018 | schedule_work(&chip->adc_measure_work); |
| 4019 | |
| 4020 | mutex_unlock: |
| 4021 | mutex_unlock(&chip->jeita_configure_lock); |
| 4022 | return rc; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 4023 | } |
| 4024 | |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 4025 | #define POWER_STAGE_REDUCE_CHECK_PERIOD_SECONDS 20 |
| 4026 | #define POWER_STAGE_REDUCE_MAX_VBAT_UV 3900000 |
| 4027 | #define POWER_STAGE_REDUCE_MIN_VCHG_UV 4800000 |
| 4028 | #define POWER_STAGE_SEL_MASK 0x0F |
| 4029 | #define POWER_STAGE_REDUCED 0x01 |
| 4030 | #define POWER_STAGE_DEFAULT 0x0F |
| 4031 | static bool |
| 4032 | qpnp_chg_is_power_stage_reduced(struct qpnp_chg_chip *chip) |
| 4033 | { |
| 4034 | int rc; |
| 4035 | u8 reg; |
| 4036 | |
| 4037 | rc = qpnp_chg_read(chip, ®, |
| 4038 | chip->buck_base + CHGR_BUCK_PSTG_CTRL, |
| 4039 | 1); |
| 4040 | if (rc) { |
| 4041 | pr_err("Error %d reading power stage register\n", rc); |
| 4042 | return false; |
| 4043 | } |
| 4044 | |
| 4045 | if ((reg & POWER_STAGE_SEL_MASK) == POWER_STAGE_DEFAULT) |
| 4046 | return false; |
| 4047 | |
| 4048 | return true; |
| 4049 | } |
| 4050 | |
| 4051 | static int |
| 4052 | qpnp_chg_power_stage_set(struct qpnp_chg_chip *chip, bool reduce) |
| 4053 | { |
| 4054 | int rc; |
| 4055 | u8 reg = 0xA5; |
| 4056 | |
| 4057 | rc = qpnp_chg_write(chip, ®, |
| 4058 | chip->buck_base + SEC_ACCESS, |
| 4059 | 1); |
| 4060 | if (rc) { |
| 4061 | pr_err("Error %d writing 0xA5 to buck's 0x%x reg\n", |
| 4062 | rc, SEC_ACCESS); |
| 4063 | return rc; |
| 4064 | } |
| 4065 | |
| 4066 | reg = POWER_STAGE_DEFAULT; |
| 4067 | if (reduce) |
| 4068 | reg = POWER_STAGE_REDUCED; |
| 4069 | rc = qpnp_chg_write(chip, ®, |
| 4070 | chip->buck_base + CHGR_BUCK_PSTG_CTRL, |
| 4071 | 1); |
| 4072 | |
| 4073 | if (rc) |
| 4074 | pr_err("Error %d writing 0x%x power stage register\n", rc, reg); |
| 4075 | return rc; |
| 4076 | } |
| 4077 | |
| 4078 | static int |
| 4079 | qpnp_chg_get_vusbin_uv(struct qpnp_chg_chip *chip) |
| 4080 | { |
| 4081 | int rc = 0; |
| 4082 | struct qpnp_vadc_result results; |
| 4083 | |
| 4084 | rc = qpnp_vadc_read(chip->vadc_dev, USBIN, &results); |
| 4085 | if (rc) { |
| 4086 | pr_err("Unable to read vbat rc=%d\n", rc); |
| 4087 | return 0; |
| 4088 | } |
| 4089 | return results.physical; |
| 4090 | } |
| 4091 | |
| 4092 | static |
| 4093 | int get_vusb_averaged(struct qpnp_chg_chip *chip, int sample_count) |
| 4094 | { |
| 4095 | int vusb_uv = 0; |
| 4096 | int i; |
| 4097 | |
| 4098 | /* avoid overflows */ |
| 4099 | if (sample_count > 256) |
| 4100 | sample_count = 256; |
| 4101 | |
| 4102 | for (i = 0; i < sample_count; i++) |
| 4103 | vusb_uv += qpnp_chg_get_vusbin_uv(chip); |
| 4104 | |
| 4105 | vusb_uv = vusb_uv / sample_count; |
| 4106 | return vusb_uv; |
| 4107 | } |
| 4108 | |
| 4109 | static |
| 4110 | int get_vbat_averaged(struct qpnp_chg_chip *chip, int sample_count) |
| 4111 | { |
| 4112 | int vbat_uv = 0; |
| 4113 | int i; |
| 4114 | |
| 4115 | /* avoid overflows */ |
| 4116 | if (sample_count > 256) |
| 4117 | sample_count = 256; |
| 4118 | |
| 4119 | for (i = 0; i < sample_count; i++) |
| 4120 | vbat_uv += get_prop_battery_voltage_now(chip); |
| 4121 | |
| 4122 | vbat_uv = vbat_uv / sample_count; |
| 4123 | return vbat_uv; |
| 4124 | } |
| 4125 | |
| 4126 | static void |
| 4127 | qpnp_chg_reduce_power_stage(struct qpnp_chg_chip *chip) |
| 4128 | { |
| 4129 | struct timespec ts; |
| 4130 | bool power_stage_reduced_in_hw = qpnp_chg_is_power_stage_reduced(chip); |
| 4131 | bool reduce_power_stage = false; |
| 4132 | int vbat_uv = get_vbat_averaged(chip, 16); |
| 4133 | int vusb_uv = get_vusb_averaged(chip, 16); |
| 4134 | bool fast_chg = |
| 4135 | (get_prop_charge_type(chip) == POWER_SUPPLY_CHARGE_TYPE_FAST); |
| 4136 | static int count_restore_power_stage; |
| 4137 | static int count_reduce_power_stage; |
| 4138 | bool vchg_loop = get_prop_vchg_loop(chip); |
| 4139 | bool ichg_loop = qpnp_chg_is_ichg_loop_active(chip); |
| 4140 | bool usb_present = qpnp_chg_is_usb_chg_plugged_in(chip); |
| 4141 | bool usb_ma_above_wall = |
| 4142 | (qpnp_chg_usb_iusbmax_get(chip) > USB_WALL_THRESHOLD_MA); |
| 4143 | |
| 4144 | if (fast_chg |
| 4145 | && usb_present |
| 4146 | && usb_ma_above_wall |
| 4147 | && vbat_uv < POWER_STAGE_REDUCE_MAX_VBAT_UV |
| 4148 | && vusb_uv > POWER_STAGE_REDUCE_MIN_VCHG_UV) |
| 4149 | reduce_power_stage = true; |
| 4150 | |
| 4151 | if ((usb_present && usb_ma_above_wall) |
| 4152 | && (vchg_loop || ichg_loop)) |
| 4153 | reduce_power_stage = true; |
| 4154 | |
| 4155 | if (power_stage_reduced_in_hw && !reduce_power_stage) { |
| 4156 | count_restore_power_stage++; |
| 4157 | count_reduce_power_stage = 0; |
| 4158 | } else if (!power_stage_reduced_in_hw && reduce_power_stage) { |
| 4159 | count_reduce_power_stage++; |
| 4160 | count_restore_power_stage = 0; |
| 4161 | } else if (power_stage_reduced_in_hw == reduce_power_stage) { |
| 4162 | count_restore_power_stage = 0; |
| 4163 | count_reduce_power_stage = 0; |
| 4164 | } |
| 4165 | |
| 4166 | pr_debug("power_stage_hw = %d reduce_power_stage = %d usb_present = %d usb_ma_above_wall = %d vbat_uv(16) = %d vusb_uv(16) = %d fast_chg = %d , ichg = %d, vchg = %d, restore,reduce = %d, %d\n", |
| 4167 | power_stage_reduced_in_hw, reduce_power_stage, |
| 4168 | usb_present, usb_ma_above_wall, |
| 4169 | vbat_uv, vusb_uv, fast_chg, |
| 4170 | ichg_loop, vchg_loop, |
| 4171 | count_restore_power_stage, count_reduce_power_stage); |
| 4172 | |
| 4173 | if (!power_stage_reduced_in_hw && reduce_power_stage) { |
| 4174 | if (count_reduce_power_stage >= 2) { |
| 4175 | qpnp_chg_power_stage_set(chip, true); |
| 4176 | power_stage_reduced_in_hw = true; |
| 4177 | } |
| 4178 | } |
| 4179 | |
| 4180 | if (power_stage_reduced_in_hw && !reduce_power_stage) { |
| 4181 | if (count_restore_power_stage >= 6 |
| 4182 | || (!usb_present || !usb_ma_above_wall)) { |
| 4183 | qpnp_chg_power_stage_set(chip, false); |
| 4184 | power_stage_reduced_in_hw = false; |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | if (usb_present && usb_ma_above_wall) { |
| 4189 | getnstimeofday(&ts); |
| 4190 | ts.tv_sec += POWER_STAGE_REDUCE_CHECK_PERIOD_SECONDS; |
| 4191 | alarm_start_range(&chip->reduce_power_stage_alarm, |
| 4192 | timespec_to_ktime(ts), |
| 4193 | timespec_to_ktime(ts)); |
| 4194 | } else { |
| 4195 | pr_debug("stopping power stage workaround\n"); |
| 4196 | chip->power_stage_workaround_running = false; |
| 4197 | } |
| 4198 | } |
| 4199 | |
| 4200 | static void |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 4201 | qpnp_chg_batfet_lcl_work(struct work_struct *work) |
| 4202 | { |
| 4203 | struct qpnp_chg_chip *chip = container_of(work, |
| 4204 | struct qpnp_chg_chip, batfet_lcl_work); |
| 4205 | |
| 4206 | mutex_lock(&chip->batfet_vreg_lock); |
| 4207 | if (qpnp_chg_is_usb_chg_plugged_in(chip) || |
| 4208 | qpnp_chg_is_dc_chg_plugged_in(chip)) { |
| 4209 | qpnp_chg_regulator_batfet_set(chip, 1); |
| 4210 | pr_debug("disabled ULPM\n"); |
| 4211 | } else if (!chip->batfet_ext_en && !qpnp_chg_is_usb_chg_plugged_in(chip) |
| 4212 | && !qpnp_chg_is_dc_chg_plugged_in(chip)) { |
| 4213 | qpnp_chg_regulator_batfet_set(chip, 0); |
| 4214 | pr_debug("enabled ULPM\n"); |
| 4215 | } |
| 4216 | mutex_unlock(&chip->batfet_vreg_lock); |
| 4217 | } |
| 4218 | |
| 4219 | static void |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 4220 | qpnp_chg_reduce_power_stage_work(struct work_struct *work) |
| 4221 | { |
| 4222 | struct qpnp_chg_chip *chip = container_of(work, |
| 4223 | struct qpnp_chg_chip, reduce_power_stage_work); |
| 4224 | |
| 4225 | qpnp_chg_reduce_power_stage(chip); |
| 4226 | } |
| 4227 | |
| 4228 | static void |
| 4229 | qpnp_chg_reduce_power_stage_callback(struct alarm *alarm) |
| 4230 | { |
| 4231 | struct qpnp_chg_chip *chip = container_of(alarm, struct qpnp_chg_chip, |
| 4232 | reduce_power_stage_alarm); |
| 4233 | |
| 4234 | schedule_work(&chip->reduce_power_stage_work); |
| 4235 | } |
| 4236 | |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4237 | static int |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 4238 | qpnp_dc_power_set_property(struct power_supply *psy, |
| 4239 | enum power_supply_property psp, |
| 4240 | const union power_supply_propval *val) |
| 4241 | { |
| 4242 | struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip, |
| 4243 | dc_psy); |
| 4244 | int rc = 0; |
| 4245 | |
| 4246 | switch (psp) { |
| 4247 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 4248 | if (!val->intval) |
| 4249 | break; |
| 4250 | |
| 4251 | rc = qpnp_chg_idcmax_set(chip, val->intval / 1000); |
| 4252 | if (rc) { |
| 4253 | pr_err("Error setting idcmax property %d\n", rc); |
| 4254 | return rc; |
| 4255 | } |
| 4256 | chip->maxinput_dc_ma = (val->intval / 1000); |
| 4257 | |
| 4258 | break; |
| 4259 | default: |
| 4260 | return -EINVAL; |
| 4261 | } |
| 4262 | |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 4263 | pr_debug("psy changed dc_psy\n"); |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 4264 | power_supply_changed(&chip->dc_psy); |
| 4265 | return rc; |
| 4266 | } |
| 4267 | |
| 4268 | static int |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4269 | qpnp_batt_power_set_property(struct power_supply *psy, |
| 4270 | enum power_supply_property psp, |
| 4271 | const union power_supply_propval *val) |
| 4272 | { |
| 4273 | struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip, |
| 4274 | batt_psy); |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 4275 | int rc = 0; |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4276 | |
| 4277 | switch (psp) { |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 4278 | case POWER_SUPPLY_PROP_COOL_TEMP: |
| 4279 | rc = qpnp_chg_configure_jeita(chip, psp, val->intval); |
| 4280 | break; |
| 4281 | case POWER_SUPPLY_PROP_WARM_TEMP: |
| 4282 | rc = qpnp_chg_configure_jeita(chip, psp, val->intval); |
| 4283 | break; |
Xiaozhe Shi | 78797cf | 2013-11-12 13:18:42 -0800 | [diff] [blame] | 4284 | case POWER_SUPPLY_PROP_CAPACITY: |
| 4285 | chip->fake_battery_soc = val->intval; |
| 4286 | power_supply_changed(&chip->batt_psy); |
| 4287 | break; |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4288 | case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
| 4289 | chip->charging_disabled = !(val->intval); |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 4290 | if (chip->charging_disabled) { |
| 4291 | /* disable charging */ |
| 4292 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 4293 | qpnp_chg_force_run_on_batt(chip, |
| 4294 | chip->charging_disabled); |
| 4295 | } else { |
| 4296 | /* enable charging */ |
| 4297 | qpnp_chg_force_run_on_batt(chip, |
| 4298 | chip->charging_disabled); |
| 4299 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
| 4300 | } |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4301 | break; |
| 4302 | case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL: |
| 4303 | qpnp_batt_system_temp_level_set(chip, val->intval); |
| 4304 | break; |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 4305 | case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX: |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 4306 | if (qpnp_chg_is_usb_chg_plugged_in(chip) && |
| 4307 | !(qpnp_is_dc_higher_prio(chip) |
| 4308 | && qpnp_chg_is_dc_chg_plugged_in(chip))) |
David Keitel | 7b056848 | 2013-12-02 14:47:42 -0800 | [diff] [blame] | 4309 | qpnp_chg_iusbmax_set(chip, val->intval / 1000); |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 4310 | break; |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 4311 | case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM: |
| 4312 | qpnp_chg_iusb_trim_set(chip, val->intval); |
| 4313 | break; |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 4314 | case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED: |
David Keitel | 820a90d | 2014-03-25 18:18:04 -0700 | [diff] [blame] | 4315 | if (val->intval) |
| 4316 | qpnp_chg_input_current_settled(chip); |
| 4317 | else |
| 4318 | chip->aicl_settled = false; |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 4319 | break; |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 4320 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
| 4321 | qpnp_chg_vinmin_set(chip, val->intval / 1000); |
| 4322 | break; |
Fenglin Wu | c64e994 | 2014-08-22 13:47:34 +0800 | [diff] [blame] | 4323 | case POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS: |
| 4324 | rc = qpnp_chg_bypass_vchg_loop_debouncer(chip, !!val->intval); |
| 4325 | break; |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4326 | default: |
| 4327 | return -EINVAL; |
| 4328 | } |
| 4329 | |
Abhijeet Dharmapurikar | ba936ec | 2013-08-12 13:45:50 -0700 | [diff] [blame] | 4330 | pr_debug("psy changed batt_psy\n"); |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4331 | power_supply_changed(&chip->batt_psy); |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 4332 | return rc; |
David Keitel | be20825 | 2013-01-31 14:49:25 -0800 | [diff] [blame] | 4333 | } |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 4334 | |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 4335 | static int |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 4336 | qpnp_chg_setup_flags(struct qpnp_chg_chip *chip) |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 4337 | { |
David Keitel | fe51cb9 | 2013-04-02 19:42:58 -0700 | [diff] [blame] | 4338 | if (chip->revision > 0 && chip->type == SMBB) |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 4339 | chip->flags |= CHG_FLAGS_VCP_WA; |
David Keitel | 33f5895 | 2013-05-20 16:17:36 -0700 | [diff] [blame] | 4340 | if (chip->type == SMBB) |
| 4341 | chip->flags |= BOOST_FLASH_WA; |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 4342 | if (chip->type == SMBBP) { |
| 4343 | struct device_node *revid_dev_node; |
| 4344 | struct pmic_revid_data *revid_data; |
| 4345 | |
| 4346 | chip->flags |= BOOST_FLASH_WA; |
| 4347 | |
| 4348 | revid_dev_node = of_parse_phandle(chip->spmi->dev.of_node, |
| 4349 | "qcom,pmic-revid", 0); |
| 4350 | if (!revid_dev_node) { |
| 4351 | pr_err("Missing qcom,pmic-revid property\n"); |
| 4352 | return -EINVAL; |
| 4353 | } |
| 4354 | revid_data = get_revid_data(revid_dev_node); |
| 4355 | if (IS_ERR(revid_data)) { |
| 4356 | pr_err("Couldnt get revid data rc = %ld\n", |
| 4357 | PTR_ERR(revid_data)); |
| 4358 | return PTR_ERR(revid_data); |
| 4359 | } |
| 4360 | |
| 4361 | if (revid_data->rev4 < PM8226_V2P1_REV4 |
| 4362 | || ((revid_data->rev4 == PM8226_V2P1_REV4) |
| 4363 | && (revid_data->rev3 <= PM8226_V2P1_REV3))) { |
| 4364 | chip->flags |= POWER_STAGE_WA; |
| 4365 | } |
| 4366 | } |
| 4367 | return 0; |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4370 | static int |
| 4371 | qpnp_chg_request_irqs(struct qpnp_chg_chip *chip) |
| 4372 | { |
| 4373 | int rc = 0; |
| 4374 | struct resource *resource; |
| 4375 | struct spmi_resource *spmi_resource; |
| 4376 | u8 subtype; |
| 4377 | struct spmi_device *spmi = chip->spmi; |
| 4378 | |
| 4379 | spmi_for_each_container_dev(spmi_resource, chip->spmi) { |
| 4380 | if (!spmi_resource) { |
| 4381 | pr_err("qpnp_chg: spmi resource absent\n"); |
| 4382 | return rc; |
| 4383 | } |
| 4384 | |
| 4385 | resource = spmi_get_resource(spmi, spmi_resource, |
| 4386 | IORESOURCE_MEM, 0); |
| 4387 | if (!(resource && resource->start)) { |
| 4388 | pr_err("node %s IO resource absent!\n", |
| 4389 | spmi->dev.of_node->full_name); |
| 4390 | return rc; |
| 4391 | } |
| 4392 | |
| 4393 | rc = qpnp_chg_read(chip, &subtype, |
| 4394 | resource->start + REG_OFFSET_PERP_SUBTYPE, 1); |
| 4395 | if (rc) { |
| 4396 | pr_err("Peripheral subtype read failed rc=%d\n", rc); |
| 4397 | return rc; |
| 4398 | } |
| 4399 | |
| 4400 | switch (subtype) { |
| 4401 | case SMBB_CHGR_SUBTYPE: |
| 4402 | case SMBBP_CHGR_SUBTYPE: |
| 4403 | case SMBCL_CHGR_SUBTYPE: |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4404 | chip->chg_fastchg.irq = spmi_get_irq_byname(spmi, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4405 | spmi_resource, "fast-chg-on"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4406 | if (chip->chg_fastchg.irq < 0) { |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4407 | pr_err("Unable to get fast-chg-on irq\n"); |
| 4408 | return rc; |
| 4409 | } |
| 4410 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4411 | chip->chg_trklchg.irq = spmi_get_irq_byname(spmi, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4412 | spmi_resource, "trkl-chg-on"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4413 | if (chip->chg_trklchg.irq < 0) { |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4414 | pr_err("Unable to get trkl-chg-on irq\n"); |
| 4415 | return rc; |
| 4416 | } |
| 4417 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4418 | chip->chg_failed.irq = spmi_get_irq_byname(spmi, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4419 | spmi_resource, "chg-failed"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4420 | if (chip->chg_failed.irq < 0) { |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4421 | pr_err("Unable to get chg_failed irq\n"); |
| 4422 | return rc; |
| 4423 | } |
| 4424 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4425 | chip->chg_vbatdet_lo.irq = spmi_get_irq_byname(spmi, |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4426 | spmi_resource, "vbat-det-lo"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4427 | if (chip->chg_vbatdet_lo.irq < 0) { |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4428 | pr_err("Unable to get fast-chg-on irq\n"); |
| 4429 | return rc; |
| 4430 | } |
| 4431 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4432 | rc |= devm_request_irq(chip->dev, chip->chg_failed.irq, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4433 | qpnp_chg_chgr_chg_failed_irq_handler, |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4434 | IRQF_TRIGGER_RISING, "chg-failed", chip); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4435 | if (rc < 0) { |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4436 | pr_err("Can't request %d chg-failed: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4437 | chip->chg_failed.irq, rc); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4438 | return rc; |
| 4439 | } |
| 4440 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4441 | rc |= devm_request_irq(chip->dev, chip->chg_fastchg.irq, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4442 | qpnp_chg_chgr_chg_fastchg_irq_handler, |
Wu Fenglin | 64fb1ac | 2013-11-08 10:20:14 +0800 | [diff] [blame] | 4443 | IRQF_TRIGGER_RISING | |
| 4444 | IRQF_TRIGGER_FALLING, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4445 | "fast-chg-on", chip); |
| 4446 | if (rc < 0) { |
| 4447 | pr_err("Can't request %d fast-chg-on: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4448 | chip->chg_fastchg.irq, rc); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4449 | return rc; |
| 4450 | } |
| 4451 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4452 | rc |= devm_request_irq(chip->dev, chip->chg_trklchg.irq, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4453 | qpnp_chg_chgr_chg_trklchg_irq_handler, |
| 4454 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4455 | "trkl-chg-on", chip); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4456 | if (rc < 0) { |
| 4457 | pr_err("Can't request %d trkl-chg-on: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4458 | chip->chg_trklchg.irq, rc); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4459 | return rc; |
| 4460 | } |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4461 | |
| 4462 | rc |= devm_request_irq(chip->dev, |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4463 | chip->chg_vbatdet_lo.irq, |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4464 | qpnp_chg_vbatdet_lo_irq_handler, |
Abhijeet Dharmapurikar | 8a68b17 | 2013-08-12 13:55:58 -0700 | [diff] [blame] | 4465 | IRQF_TRIGGER_RISING, |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4466 | "vbat-det-lo", chip); |
| 4467 | if (rc < 0) { |
| 4468 | pr_err("Can't request %d vbat-det-lo: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4469 | chip->chg_vbatdet_lo.irq, rc); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4470 | return rc; |
| 4471 | } |
| 4472 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4473 | qpnp_chg_irq_wake_enable(&chip->chg_trklchg); |
| 4474 | qpnp_chg_irq_wake_enable(&chip->chg_failed); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4475 | qpnp_chg_disable_irq(&chip->chg_vbatdet_lo); |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4476 | qpnp_chg_irq_wake_enable(&chip->chg_vbatdet_lo); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4477 | |
| 4478 | break; |
| 4479 | case SMBB_BAT_IF_SUBTYPE: |
| 4480 | case SMBBP_BAT_IF_SUBTYPE: |
| 4481 | case SMBCL_BAT_IF_SUBTYPE: |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4482 | chip->batt_pres.irq = spmi_get_irq_byname(spmi, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4483 | spmi_resource, "batt-pres"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4484 | if (chip->batt_pres.irq < 0) { |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4485 | pr_err("Unable to get batt-pres irq\n"); |
| 4486 | return rc; |
| 4487 | } |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4488 | rc = devm_request_irq(chip->dev, chip->batt_pres.irq, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4489 | qpnp_chg_bat_if_batt_pres_irq_handler, |
David Keitel | 69193cc | 2013-06-24 18:12:22 -0700 | [diff] [blame] | 4490 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
| 4491 | | IRQF_SHARED | IRQF_ONESHOT, |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4492 | "batt-pres", chip); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4493 | if (rc < 0) { |
| 4494 | pr_err("Can't request %d batt-pres irq: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4495 | chip->batt_pres.irq, rc); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4496 | return rc; |
| 4497 | } |
| 4498 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4499 | qpnp_chg_irq_wake_enable(&chip->batt_pres); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 4500 | |
| 4501 | chip->batt_temp_ok.irq = spmi_get_irq_byname(spmi, |
| 4502 | spmi_resource, "bat-temp-ok"); |
| 4503 | if (chip->batt_temp_ok.irq < 0) { |
| 4504 | pr_err("Unable to get bat-temp-ok irq\n"); |
| 4505 | return rc; |
| 4506 | } |
| 4507 | rc = devm_request_irq(chip->dev, chip->batt_temp_ok.irq, |
| 4508 | qpnp_chg_bat_if_batt_temp_irq_handler, |
| 4509 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 4510 | "bat-temp-ok", chip); |
| 4511 | if (rc < 0) { |
| 4512 | pr_err("Can't request %d bat-temp-ok irq: %d\n", |
| 4513 | chip->batt_temp_ok.irq, rc); |
| 4514 | return rc; |
| 4515 | } |
Xiaozhe Shi | 6c72665 | 2014-01-03 15:45:14 -0800 | [diff] [blame] | 4516 | qpnp_chg_bat_if_batt_temp_irq_handler(0, chip); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 4517 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4518 | qpnp_chg_irq_wake_enable(&chip->batt_temp_ok); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 4519 | |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4520 | break; |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 4521 | case SMBB_BUCK_SUBTYPE: |
| 4522 | case SMBBP_BUCK_SUBTYPE: |
| 4523 | case SMBCL_BUCK_SUBTYPE: |
David Keitel | 337bd86 | 2013-07-15 21:42:58 -0700 | [diff] [blame] | 4524 | break; |
| 4525 | |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4526 | case SMBB_USB_CHGPTH_SUBTYPE: |
| 4527 | case SMBBP_USB_CHGPTH_SUBTYPE: |
| 4528 | case SMBCL_USB_CHGPTH_SUBTYPE: |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 4529 | if (chip->ovp_monitor_enable) { |
| 4530 | chip->coarse_det_usb.irq = |
| 4531 | spmi_get_irq_byname(spmi, |
| 4532 | spmi_resource, "coarse-det-usb"); |
| 4533 | if (chip->coarse_det_usb.irq < 0) { |
| 4534 | pr_err("Can't get coarse-det irq\n"); |
| 4535 | return rc; |
| 4536 | } |
| 4537 | rc = devm_request_irq(chip->dev, |
| 4538 | chip->coarse_det_usb.irq, |
| 4539 | qpnp_chg_coarse_det_usb_irq_handler, |
| 4540 | IRQF_TRIGGER_RISING | |
| 4541 | IRQF_TRIGGER_FALLING, |
| 4542 | "coarse-det-usb", chip); |
| 4543 | if (rc < 0) { |
| 4544 | pr_err("Can't req %d coarse-det: %d\n", |
| 4545 | chip->coarse_det_usb.irq, rc); |
| 4546 | return rc; |
| 4547 | } |
| 4548 | } |
| 4549 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4550 | chip->usbin_valid.irq = spmi_get_irq_byname(spmi, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4551 | spmi_resource, "usbin-valid"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4552 | if (chip->usbin_valid.irq < 0) { |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4553 | pr_err("Unable to get usbin irq\n"); |
| 4554 | return rc; |
| 4555 | } |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4556 | rc = devm_request_irq(chip->dev, chip->usbin_valid.irq, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4557 | qpnp_chg_usb_usbin_valid_irq_handler, |
| 4558 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4559 | "usbin-valid", chip); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4560 | if (rc < 0) { |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4561 | pr_err("Can't request %d usbin-valid: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4562 | chip->usbin_valid.irq, rc); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4563 | return rc; |
| 4564 | } |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4565 | |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4566 | chip->chg_gone.irq = spmi_get_irq_byname(spmi, |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4567 | spmi_resource, "chg-gone"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4568 | if (chip->chg_gone.irq < 0) { |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4569 | pr_err("Unable to get chg-gone irq\n"); |
| 4570 | return rc; |
| 4571 | } |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4572 | rc = devm_request_irq(chip->dev, chip->chg_gone.irq, |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4573 | qpnp_chg_usb_chg_gone_irq_handler, |
| 4574 | IRQF_TRIGGER_RISING, |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4575 | "chg-gone", chip); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4576 | if (rc < 0) { |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4577 | pr_err("Can't request %d chg-gone: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4578 | chip->chg_gone.irq, rc); |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4579 | return rc; |
| 4580 | } |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4581 | |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 4582 | if ((subtype == SMBBP_USB_CHGPTH_SUBTYPE) || |
| 4583 | (subtype == SMBCL_USB_CHGPTH_SUBTYPE)) { |
| 4584 | chip->usb_ocp.irq = spmi_get_irq_byname(spmi, |
| 4585 | spmi_resource, "usb-ocp"); |
| 4586 | if (chip->usb_ocp.irq < 0) { |
| 4587 | pr_err("Unable to get usbin irq\n"); |
| 4588 | return rc; |
| 4589 | } |
| 4590 | rc = devm_request_irq(chip->dev, |
| 4591 | chip->usb_ocp.irq, |
| 4592 | qpnp_chg_usb_usb_ocp_irq_handler, |
| 4593 | IRQF_TRIGGER_RISING, "usb-ocp", chip); |
| 4594 | if (rc < 0) { |
| 4595 | pr_err("Can't request %d usb-ocp: %d\n", |
| 4596 | chip->usb_ocp.irq, rc); |
| 4597 | return rc; |
| 4598 | } |
| 4599 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4600 | qpnp_chg_irq_wake_enable(&chip->usb_ocp); |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 4601 | } |
| 4602 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4603 | qpnp_chg_irq_wake_enable(&chip->usbin_valid); |
| 4604 | qpnp_chg_irq_wake_enable(&chip->chg_gone); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4605 | break; |
| 4606 | case SMBB_DC_CHGPTH_SUBTYPE: |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4607 | chip->dcin_valid.irq = spmi_get_irq_byname(spmi, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4608 | spmi_resource, "dcin-valid"); |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4609 | if (chip->dcin_valid.irq < 0) { |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4610 | pr_err("Unable to get dcin irq\n"); |
| 4611 | return -rc; |
| 4612 | } |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4613 | rc = devm_request_irq(chip->dev, chip->dcin_valid.irq, |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4614 | qpnp_chg_dc_dcin_valid_irq_handler, |
| 4615 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4616 | "dcin-valid", chip); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4617 | if (rc < 0) { |
David Keitel | c9f1917 | 2013-04-29 11:01:26 -0700 | [diff] [blame] | 4618 | pr_err("Can't request %d dcin-valid: %d\n", |
David Keitel | 47185a6 | 2013-05-15 18:54:10 -0700 | [diff] [blame] | 4619 | chip->dcin_valid.irq, rc); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4620 | return rc; |
| 4621 | } |
| 4622 | |
David Keitel | bf6a106 | 2014-01-23 10:57:36 -0800 | [diff] [blame] | 4623 | qpnp_chg_irq_wake_enable(&chip->dcin_valid); |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 4624 | break; |
| 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | return rc; |
| 4629 | } |
| 4630 | |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 4631 | static int |
| 4632 | qpnp_chg_load_battery_data(struct qpnp_chg_chip *chip) |
| 4633 | { |
| 4634 | struct bms_battery_data batt_data; |
| 4635 | struct device_node *node; |
| 4636 | struct qpnp_vadc_result result; |
| 4637 | int rc; |
| 4638 | |
| 4639 | node = of_find_node_by_name(chip->spmi->dev.of_node, |
| 4640 | "qcom,battery-data"); |
| 4641 | if (node) { |
| 4642 | memset(&batt_data, 0, sizeof(struct bms_battery_data)); |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 4643 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result); |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 4644 | if (rc) { |
| 4645 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 4646 | LR_MUX2_BAT_ID, rc); |
| 4647 | return rc; |
| 4648 | } |
| 4649 | |
Xiaozhe Shi | 23174ea | 2013-07-30 17:51:09 -0700 | [diff] [blame] | 4650 | batt_data.max_voltage_uv = -1; |
| 4651 | batt_data.iterm_ua = -1; |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 4652 | rc = of_batterydata_read_data(node, |
| 4653 | &batt_data, result.physical); |
| 4654 | if (rc) { |
| 4655 | pr_err("failed to read battery data: %d\n", rc); |
| 4656 | return rc; |
| 4657 | } |
| 4658 | |
Xiaozhe Shi | 34e56823 | 2013-07-24 12:47:34 -0700 | [diff] [blame] | 4659 | if (batt_data.max_voltage_uv >= 0) { |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 4660 | chip->max_voltage_mv = batt_data.max_voltage_uv / 1000; |
Xiaozhe Shi | 34e56823 | 2013-07-24 12:47:34 -0700 | [diff] [blame] | 4661 | chip->safe_voltage_mv = chip->max_voltage_mv |
| 4662 | + MAX_DELTA_VDD_MAX_MV; |
| 4663 | } |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 4664 | if (batt_data.iterm_ua >= 0) |
| 4665 | chip->term_current = batt_data.iterm_ua / 1000; |
| 4666 | } |
| 4667 | |
| 4668 | return 0; |
| 4669 | } |
| 4670 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4671 | #define WDOG_EN_BIT BIT(7) |
| 4672 | static int |
| 4673 | qpnp_chg_hwinit(struct qpnp_chg_chip *chip, u8 subtype, |
| 4674 | struct spmi_resource *spmi_resource) |
| 4675 | { |
| 4676 | int rc = 0; |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 4677 | u8 reg = 0; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 4678 | struct regulator_init_data *init_data; |
| 4679 | struct regulator_desc *rdesc; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4680 | |
| 4681 | switch (subtype) { |
| 4682 | case SMBB_CHGR_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 4683 | case SMBBP_CHGR_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 4684 | case SMBCL_CHGR_SUBTYPE: |
Prasad Sodagudi | c367801 | 2013-09-23 12:01:19 +0530 | [diff] [blame] | 4685 | qpnp_chg_vbatweak_set(chip, chip->batt_weak_voltage_mv); |
| 4686 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4687 | rc = qpnp_chg_vinmin_set(chip, chip->min_voltage_mv); |
| 4688 | if (rc) { |
| 4689 | pr_debug("failed setting min_voltage rc=%d\n", rc); |
| 4690 | return rc; |
| 4691 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4692 | rc = qpnp_chg_vddsafe_set(chip, chip->safe_voltage_mv); |
| 4693 | if (rc) { |
| 4694 | pr_debug("failed setting safe_voltage rc=%d\n", rc); |
| 4695 | return rc; |
| 4696 | } |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 4697 | rc = qpnp_chg_vbatdet_set(chip, |
| 4698 | chip->max_voltage_mv - chip->resume_delta_mv); |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 4699 | if (rc) { |
| 4700 | pr_debug("failed setting resume_voltage rc=%d\n", rc); |
| 4701 | return rc; |
| 4702 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4703 | rc = qpnp_chg_ibatmax_set(chip, chip->max_bat_chg_current); |
| 4704 | if (rc) { |
| 4705 | pr_debug("failed setting ibatmax rc=%d\n", rc); |
| 4706 | return rc; |
| 4707 | } |
David Keitel | 365c4c4 | 2013-03-08 16:20:40 -0800 | [diff] [blame] | 4708 | if (chip->term_current) { |
| 4709 | rc = qpnp_chg_ibatterm_set(chip, chip->term_current); |
| 4710 | if (rc) { |
| 4711 | pr_debug("failed setting ibatterm rc=%d\n", rc); |
| 4712 | return rc; |
| 4713 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4714 | } |
David Keitel | 5d44fa5 | 2012-12-03 16:37:31 -0800 | [diff] [blame] | 4715 | rc = qpnp_chg_ibatsafe_set(chip, chip->safe_current); |
| 4716 | if (rc) { |
| 4717 | pr_debug("failed setting ibat_Safe rc=%d\n", rc); |
| 4718 | return rc; |
| 4719 | } |
David Keitel | a4b7b59 | 2013-04-11 18:34:35 -0700 | [diff] [blame] | 4720 | rc = qpnp_chg_tchg_max_set(chip, chip->tchg_mins); |
| 4721 | if (rc) { |
| 4722 | pr_debug("failed setting tchg_mins rc=%d\n", rc); |
| 4723 | return rc; |
| 4724 | } |
| 4725 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4726 | /* HACK: Disable wdog */ |
| 4727 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + 0x62, |
| 4728 | 0xFF, 0xA0, 1); |
| 4729 | |
David Keitel | b4e4354 | 2013-04-09 17:30:41 -0700 | [diff] [blame] | 4730 | /* HACK: use analog EOC */ |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4731 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + |
| 4732 | CHGR_IBAT_TERM_CHGR, |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4733 | 0xFF, 0x08, 1); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4734 | |
David Keitel | 79e0832 | 2014-04-21 14:17:05 -0700 | [diff] [blame] | 4735 | /* HACK: trkl stuck workaround */ |
| 4736 | |
| 4737 | rc = qpnp_chg_masked_write(chip, |
| 4738 | chip->chgr_base + SEC_ACCESS, |
| 4739 | 0xFF, |
| 4740 | 0xA5, 1); |
| 4741 | |
| 4742 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + |
| 4743 | CHG_OVR0, |
| 4744 | 0xFF, 0x00, 1); |
| 4745 | |
| 4746 | rc = qpnp_chg_masked_write(chip, |
| 4747 | chip->chgr_base + SEC_ACCESS, |
| 4748 | 0xFF, |
| 4749 | 0xA5, 1); |
| 4750 | |
| 4751 | rc = qpnp_chg_masked_write(chip, chip->chgr_base + |
| 4752 | CHG_TRICKLE_CLAMP, |
| 4753 | 0xFF, 0x00, 1); |
Chunmei Cai | 661a7cd | 2014-08-05 17:31:54 +0800 | [diff] [blame] | 4754 | |
| 4755 | rc = qpnp_chg_read(chip, &chip->chg_temp_thresh_default, |
| 4756 | chip->chgr_base + CHGR_CHG_TEMP_THRESH, 1); |
| 4757 | if (rc) { |
| 4758 | pr_debug("read CHG_TEMP_THRESH failed, rc = %d\n", rc); |
| 4759 | chip->chg_temp_thresh_default = |
| 4760 | CHG_TEMP_THRESH_DEFAULT; |
| 4761 | } |
| 4762 | |
| 4763 | init_data = of_get_regulator_init_data(chip->dev, |
| 4764 | spmi_resource->of_node); |
| 4765 | if (!init_data) { |
| 4766 | pr_err("unable to get regulator init data for flash_wa\n"); |
| 4767 | return -ENOMEM; |
| 4768 | } |
| 4769 | |
| 4770 | if (init_data->constraints.name) { |
| 4771 | rdesc = &(chip->flash_wa_vreg.rdesc); |
| 4772 | rdesc->owner = THIS_MODULE; |
| 4773 | rdesc->type = REGULATOR_VOLTAGE; |
| 4774 | rdesc->ops = &qpnp_chg_flash_wa_reg_ops; |
| 4775 | rdesc->name = init_data->constraints.name; |
| 4776 | |
| 4777 | init_data->constraints.valid_ops_mask |
| 4778 | |= REGULATOR_CHANGE_STATUS; |
| 4779 | |
| 4780 | chip->flash_wa_vreg.rdev = |
| 4781 | regulator_register(rdesc, chip->dev, init_data, |
| 4782 | chip, spmi_resource->of_node); |
| 4783 | if (IS_ERR(chip->flash_wa_vreg.rdev)) { |
| 4784 | rc = PTR_ERR(chip->flash_wa_vreg.rdev); |
| 4785 | chip->flash_wa_vreg.rdev = NULL; |
| 4786 | pr_err("Flash wa failed, rc=%d\n", rc); |
| 4787 | return rc; |
| 4788 | } |
| 4789 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4790 | break; |
| 4791 | case SMBB_BUCK_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 4792 | case SMBBP_BUCK_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 4793 | case SMBCL_BUCK_SUBTYPE: |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4794 | rc = qpnp_chg_toggle_chg_done_logic(chip, 0); |
| 4795 | if (rc) |
| 4796 | return rc; |
| 4797 | |
David Keitel | 9201df3 | 2013-01-10 18:38:34 -0800 | [diff] [blame] | 4798 | rc = qpnp_chg_masked_write(chip, |
David Keitel | 5adc794 | 2013-05-21 13:45:17 -0700 | [diff] [blame] | 4799 | chip->buck_base + CHGR_BUCK_BCK_VBAT_REG_MODE, |
David Keitel | 9201df3 | 2013-01-10 18:38:34 -0800 | [diff] [blame] | 4800 | BUCK_VBAT_REG_NODE_SEL_BIT, |
| 4801 | BUCK_VBAT_REG_NODE_SEL_BIT, 1); |
| 4802 | if (rc) { |
| 4803 | pr_debug("failed to enable IR drop comp rc=%d\n", rc); |
| 4804 | return rc; |
| 4805 | } |
Xiaozhe Shi | 20b7abf | 2013-08-15 13:36:55 -0700 | [diff] [blame] | 4806 | |
| 4807 | rc = qpnp_chg_read(chip, &chip->trim_center, |
| 4808 | chip->buck_base + BUCK_CTRL_TRIM1, 1); |
| 4809 | if (rc) { |
| 4810 | pr_debug("failed to read trim center rc=%d\n", rc); |
| 4811 | return rc; |
| 4812 | } |
| 4813 | chip->trim_center >>= 4; |
| 4814 | pr_debug("trim center = %02x\n", chip->trim_center); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4815 | break; |
| 4816 | case SMBB_BAT_IF_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 4817 | case SMBBP_BAT_IF_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 4818 | case SMBCL_BAT_IF_SUBTYPE: |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 4819 | /* Select battery presence detection */ |
David Keitel | 0ef282b | 2013-05-29 16:04:57 -0700 | [diff] [blame] | 4820 | switch (chip->bpd_detection) { |
| 4821 | case BPD_TYPE_BAT_THM: |
| 4822 | reg = BAT_THM_EN; |
| 4823 | break; |
| 4824 | case BPD_TYPE_BAT_ID: |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 4825 | reg = BAT_ID_EN; |
David Keitel | 0ef282b | 2013-05-29 16:04:57 -0700 | [diff] [blame] | 4826 | break; |
| 4827 | case BPD_TYPE_BAT_THM_BAT_ID: |
| 4828 | reg = BAT_THM_EN | BAT_ID_EN; |
| 4829 | break; |
| 4830 | default: |
| 4831 | reg = BAT_THM_EN; |
| 4832 | break; |
| 4833 | } |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 4834 | |
| 4835 | rc = qpnp_chg_masked_write(chip, |
| 4836 | chip->bat_if_base + BAT_IF_BPD_CTRL, |
| 4837 | BAT_IF_BPD_CTRL_SEL, |
| 4838 | reg, 1); |
| 4839 | if (rc) { |
| 4840 | pr_debug("failed to chose BPD rc=%d\n", rc); |
| 4841 | return rc; |
| 4842 | } |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 4843 | /* Force on VREF_BAT_THM */ |
| 4844 | rc = qpnp_chg_masked_write(chip, |
| 4845 | chip->bat_if_base + BAT_IF_VREF_BAT_THM_CTRL, |
| 4846 | VREF_BATT_THERM_FORCE_ON, |
| 4847 | VREF_BATT_THERM_FORCE_ON, 1); |
| 4848 | if (rc) { |
| 4849 | pr_debug("failed to force on VREF_BAT_THM rc=%d\n", rc); |
| 4850 | return rc; |
| 4851 | } |
Abhijeet Dharmapurikar | e17309f1 | 2013-08-05 12:50:21 -0700 | [diff] [blame] | 4852 | |
| 4853 | init_data = of_get_regulator_init_data(chip->dev, |
| 4854 | spmi_resource->of_node); |
| 4855 | |
| 4856 | if (init_data->constraints.name) { |
| 4857 | rdesc = &(chip->batfet_vreg.rdesc); |
| 4858 | rdesc->owner = THIS_MODULE; |
| 4859 | rdesc->type = REGULATOR_VOLTAGE; |
| 4860 | rdesc->ops = &qpnp_chg_batfet_vreg_ops; |
| 4861 | rdesc->name = init_data->constraints.name; |
| 4862 | |
| 4863 | init_data->constraints.valid_ops_mask |
| 4864 | |= REGULATOR_CHANGE_STATUS; |
| 4865 | |
| 4866 | chip->batfet_vreg.rdev = regulator_register(rdesc, |
| 4867 | chip->dev, init_data, chip, |
| 4868 | spmi_resource->of_node); |
| 4869 | if (IS_ERR(chip->batfet_vreg.rdev)) { |
| 4870 | rc = PTR_ERR(chip->batfet_vreg.rdev); |
| 4871 | chip->batfet_vreg.rdev = NULL; |
| 4872 | if (rc != -EPROBE_DEFER) |
| 4873 | pr_err("batfet reg failed, rc=%d\n", |
| 4874 | rc); |
| 4875 | return rc; |
| 4876 | } |
| 4877 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4878 | break; |
| 4879 | case SMBB_USB_CHGPTH_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 4880 | case SMBBP_USB_CHGPTH_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 4881 | case SMBCL_USB_CHGPTH_SUBTYPE: |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 4882 | if (qpnp_chg_is_usb_chg_plugged_in(chip)) { |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4883 | rc = qpnp_chg_masked_write(chip, |
| 4884 | chip->usb_chgpth_base + CHGR_USB_ENUM_T_STOP, |
| 4885 | ENUM_T_STOP_BIT, |
| 4886 | ENUM_T_STOP_BIT, 1); |
| 4887 | if (rc) { |
| 4888 | pr_err("failed to write enum stop rc=%d\n", rc); |
| 4889 | return -ENXIO; |
| 4890 | } |
| 4891 | } |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 4892 | |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 4893 | init_data = of_get_regulator_init_data(chip->dev, |
| 4894 | spmi_resource->of_node); |
| 4895 | if (!init_data) { |
| 4896 | pr_err("unable to allocate memory\n"); |
| 4897 | return -ENOMEM; |
| 4898 | } |
| 4899 | |
| 4900 | if (init_data->constraints.name) { |
| 4901 | if (of_get_property(chip->dev->of_node, |
| 4902 | "otg-parent-supply", NULL)) |
| 4903 | init_data->supply_regulator = "otg-parent"; |
| 4904 | |
| 4905 | rdesc = &(chip->otg_vreg.rdesc); |
| 4906 | rdesc->owner = THIS_MODULE; |
| 4907 | rdesc->type = REGULATOR_VOLTAGE; |
| 4908 | rdesc->ops = &qpnp_chg_otg_reg_ops; |
| 4909 | rdesc->name = init_data->constraints.name; |
| 4910 | |
| 4911 | init_data->constraints.valid_ops_mask |
| 4912 | |= REGULATOR_CHANGE_STATUS; |
| 4913 | |
| 4914 | chip->otg_vreg.rdev = regulator_register(rdesc, |
| 4915 | chip->dev, init_data, chip, |
| 4916 | spmi_resource->of_node); |
| 4917 | if (IS_ERR(chip->otg_vreg.rdev)) { |
| 4918 | rc = PTR_ERR(chip->otg_vreg.rdev); |
David Collins | cbb1213 | 2013-05-28 10:47:28 -0700 | [diff] [blame] | 4919 | chip->otg_vreg.rdev = NULL; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 4920 | if (rc != -EPROBE_DEFER) |
| 4921 | pr_err("OTG reg failed, rc=%d\n", rc); |
| 4922 | return rc; |
| 4923 | } |
| 4924 | } |
| 4925 | |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 4926 | rc = qpnp_chg_masked_write(chip, |
David Keitel | 5c3a770 | 2012-12-20 11:13:21 -0800 | [diff] [blame] | 4927 | chip->usb_chgpth_base + USB_OVP_CTL, |
| 4928 | USB_VALID_DEB_20MS, |
| 4929 | USB_VALID_DEB_20MS, 1); |
| 4930 | |
| 4931 | rc = qpnp_chg_masked_write(chip, |
David Keitel | d681cda | 2012-10-02 15:44:21 -0700 | [diff] [blame] | 4932 | chip->usb_chgpth_base + CHGR_USB_ENUM_T_STOP, |
| 4933 | ENUM_T_STOP_BIT, |
| 4934 | ENUM_T_STOP_BIT, 1); |
| 4935 | |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 4936 | rc = qpnp_chg_masked_write(chip, |
| 4937 | chip->usb_chgpth_base + SEC_ACCESS, |
| 4938 | 0xFF, |
| 4939 | 0xA5, 1); |
| 4940 | |
| 4941 | rc = qpnp_chg_masked_write(chip, |
| 4942 | chip->usb_chgpth_base + USB_CHG_GONE_REV_BST, |
| 4943 | 0xFF, |
| 4944 | 0x80, 1); |
| 4945 | |
David Keitel | 0b62bdd | 2013-07-10 17:30:51 -0700 | [diff] [blame] | 4946 | if ((subtype == SMBBP_USB_CHGPTH_SUBTYPE) || |
| 4947 | (subtype == SMBCL_USB_CHGPTH_SUBTYPE)) { |
| 4948 | rc = qpnp_chg_masked_write(chip, |
| 4949 | chip->usb_chgpth_base + USB_OCP_THR, |
| 4950 | OCP_THR_MASK, |
| 4951 | OCP_THR_900_MA, 1); |
| 4952 | if (rc) |
| 4953 | pr_err("Failed to configure OCP rc = %d\n", rc); |
| 4954 | } |
| 4955 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4956 | break; |
| 4957 | case SMBB_DC_CHGPTH_SUBTYPE: |
| 4958 | break; |
| 4959 | case SMBB_BOOST_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 4960 | case SMBBP_BOOST_SUBTYPE: |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 4961 | init_data = of_get_regulator_init_data(chip->dev, |
| 4962 | spmi_resource->of_node); |
| 4963 | if (!init_data) { |
| 4964 | pr_err("unable to allocate memory\n"); |
| 4965 | return -ENOMEM; |
| 4966 | } |
| 4967 | |
| 4968 | if (init_data->constraints.name) { |
| 4969 | if (of_get_property(chip->dev->of_node, |
| 4970 | "boost-parent-supply", NULL)) |
| 4971 | init_data->supply_regulator = "boost-parent"; |
| 4972 | |
| 4973 | rdesc = &(chip->boost_vreg.rdesc); |
| 4974 | rdesc->owner = THIS_MODULE; |
| 4975 | rdesc->type = REGULATOR_VOLTAGE; |
| 4976 | rdesc->ops = &qpnp_chg_boost_reg_ops; |
| 4977 | rdesc->name = init_data->constraints.name; |
| 4978 | |
| 4979 | init_data->constraints.valid_ops_mask |
| 4980 | |= REGULATOR_CHANGE_STATUS |
| 4981 | | REGULATOR_CHANGE_VOLTAGE; |
| 4982 | |
| 4983 | chip->boost_vreg.rdev = regulator_register(rdesc, |
| 4984 | chip->dev, init_data, chip, |
| 4985 | spmi_resource->of_node); |
| 4986 | if (IS_ERR(chip->boost_vreg.rdev)) { |
| 4987 | rc = PTR_ERR(chip->boost_vreg.rdev); |
David Collins | cbb1213 | 2013-05-28 10:47:28 -0700 | [diff] [blame] | 4988 | chip->boost_vreg.rdev = NULL; |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 4989 | if (rc != -EPROBE_DEFER) |
| 4990 | pr_err("boost reg failed, rc=%d\n", rc); |
| 4991 | return rc; |
| 4992 | } |
| 4993 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 4994 | break; |
| 4995 | case SMBB_MISC_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 4996 | case SMBBP_MISC_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 4997 | case SMBCL_MISC_SUBTYPE: |
David Keitel | 52d685e | 2013-05-02 11:59:05 -0700 | [diff] [blame] | 4998 | if (subtype == SMBB_MISC_SUBTYPE) |
| 4999 | chip->type = SMBB; |
| 5000 | else if (subtype == SMBBP_MISC_SUBTYPE) |
| 5001 | chip->type = SMBBP; |
| 5002 | else if (subtype == SMBCL_MISC_SUBTYPE) |
| 5003 | chip->type = SMBCL; |
| 5004 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5005 | pr_debug("Setting BOOT_DONE\n"); |
| 5006 | rc = qpnp_chg_masked_write(chip, |
| 5007 | chip->misc_base + CHGR_MISC_BOOT_DONE, |
| 5008 | CHGR_BOOT_DONE, CHGR_BOOT_DONE, 1); |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 5009 | rc = qpnp_chg_read(chip, ®, |
| 5010 | chip->misc_base + MISC_REVISION2, 1); |
| 5011 | if (rc) { |
| 5012 | pr_err("failed to read revision register rc=%d\n", rc); |
| 5013 | return rc; |
| 5014 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5015 | |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 5016 | chip->revision = reg; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5017 | break; |
| 5018 | default: |
| 5019 | pr_err("Invalid peripheral subtype\n"); |
| 5020 | } |
| 5021 | return rc; |
| 5022 | } |
| 5023 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5024 | #define OF_PROP_READ(chip, prop, qpnp_dt_property, retval, optional) \ |
| 5025 | do { \ |
| 5026 | if (retval) \ |
| 5027 | break; \ |
| 5028 | \ |
| 5029 | retval = of_property_read_u32(chip->spmi->dev.of_node, \ |
| 5030 | "qcom," qpnp_dt_property, \ |
| 5031 | &chip->prop); \ |
| 5032 | \ |
| 5033 | if ((retval == -EINVAL) && optional) \ |
| 5034 | retval = 0; \ |
| 5035 | else if (retval) \ |
| 5036 | pr_err("Error reading " #qpnp_dt_property \ |
| 5037 | " property rc = %d\n", rc); \ |
| 5038 | } while (0) |
| 5039 | |
| 5040 | static int |
| 5041 | qpnp_charger_read_dt_props(struct qpnp_chg_chip *chip) |
| 5042 | { |
| 5043 | int rc = 0; |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 5044 | const char *bpd; |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5045 | |
| 5046 | OF_PROP_READ(chip, max_voltage_mv, "vddmax-mv", rc, 0); |
| 5047 | OF_PROP_READ(chip, min_voltage_mv, "vinmin-mv", rc, 0); |
| 5048 | OF_PROP_READ(chip, safe_voltage_mv, "vddsafe-mv", rc, 0); |
| 5049 | OF_PROP_READ(chip, resume_delta_mv, "vbatdet-delta-mv", rc, 0); |
| 5050 | OF_PROP_READ(chip, safe_current, "ibatsafe-ma", rc, 0); |
| 5051 | OF_PROP_READ(chip, max_bat_chg_current, "ibatmax-ma", rc, 0); |
| 5052 | if (rc) |
| 5053 | pr_err("failed to read required dt parameters %d\n", rc); |
| 5054 | |
| 5055 | OF_PROP_READ(chip, term_current, "ibatterm-ma", rc, 1); |
| 5056 | OF_PROP_READ(chip, maxinput_dc_ma, "maxinput-dc-ma", rc, 1); |
| 5057 | OF_PROP_READ(chip, maxinput_usb_ma, "maxinput-usb-ma", rc, 1); |
| 5058 | OF_PROP_READ(chip, warm_bat_decidegc, "warm-bat-decidegc", rc, 1); |
| 5059 | OF_PROP_READ(chip, cool_bat_decidegc, "cool-bat-decidegc", rc, 1); |
David Keitel | a4b7b59 | 2013-04-11 18:34:35 -0700 | [diff] [blame] | 5060 | OF_PROP_READ(chip, tchg_mins, "tchg-mins", rc, 1); |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 5061 | OF_PROP_READ(chip, hot_batt_p, "batt-hot-percentage", rc, 1); |
| 5062 | OF_PROP_READ(chip, cold_batt_p, "batt-cold-percentage", rc, 1); |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 5063 | OF_PROP_READ(chip, soc_resume_limit, "resume-soc", rc, 1); |
Prasad Sodagudi | c367801 | 2013-09-23 12:01:19 +0530 | [diff] [blame] | 5064 | OF_PROP_READ(chip, batt_weak_voltage_mv, "vbatweak-mv", rc, 1); |
David Keitel | bdbae45 | 2014-01-14 17:19:50 -0800 | [diff] [blame] | 5065 | OF_PROP_READ(chip, vbatdet_max_err_mv, "vbatdet-maxerr-mv", rc, 1); |
Xiaozhe Shi | 7fc4e3c | 2013-06-06 13:52:29 -0700 | [diff] [blame] | 5066 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5067 | if (rc) |
| 5068 | return rc; |
| 5069 | |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 5070 | rc = of_property_read_string(chip->spmi->dev.of_node, |
| 5071 | "qcom,bpd-detection", &bpd); |
| 5072 | if (rc) { |
David Keitel | 0ef282b | 2013-05-29 16:04:57 -0700 | [diff] [blame] | 5073 | /* Select BAT_THM as default BPD scheme */ |
| 5074 | chip->bpd_detection = BPD_TYPE_BAT_THM; |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 5075 | rc = 0; |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 5076 | } else { |
| 5077 | chip->bpd_detection = get_bpd(bpd); |
| 5078 | if (chip->bpd_detection < 0) { |
| 5079 | pr_err("failed to determine bpd schema %d\n", rc); |
| 5080 | return rc; |
| 5081 | } |
| 5082 | } |
| 5083 | |
David Keitel | bdbae45 | 2014-01-14 17:19:50 -0800 | [diff] [blame] | 5084 | if (!chip->vbatdet_max_err_mv) |
| 5085 | chip->vbatdet_max_err_mv = VBATDET_MAX_ERR_MV; |
| 5086 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5087 | /* Look up JEITA compliance parameters if cool and warm temp provided */ |
Zhenhua Huang | 4c593ec | 2013-08-13 17:22:58 +0800 | [diff] [blame] | 5088 | if (chip->cool_bat_decidegc || chip->warm_bat_decidegc) { |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 5089 | chip->adc_tm_dev = qpnp_get_adc_tm(chip->dev, "chg"); |
| 5090 | if (IS_ERR(chip->adc_tm_dev)) { |
| 5091 | rc = PTR_ERR(chip->adc_tm_dev); |
| 5092 | if (rc != -EPROBE_DEFER) |
| 5093 | pr_err("adc-tm not ready, defer probe\n"); |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5094 | return rc; |
| 5095 | } |
| 5096 | |
| 5097 | OF_PROP_READ(chip, warm_bat_chg_ma, "ibatmax-warm-ma", rc, 1); |
| 5098 | OF_PROP_READ(chip, cool_bat_chg_ma, "ibatmax-cool-ma", rc, 1); |
| 5099 | OF_PROP_READ(chip, warm_bat_mv, "warm-bat-mv", rc, 1); |
| 5100 | OF_PROP_READ(chip, cool_bat_mv, "cool-bat-mv", rc, 1); |
| 5101 | if (rc) |
| 5102 | return rc; |
| 5103 | } |
| 5104 | |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 5105 | /* Get the use-external-rsense property */ |
| 5106 | chip->use_external_rsense = of_property_read_bool( |
| 5107 | chip->spmi->dev.of_node, |
| 5108 | "qcom,use-external-rsense"); |
| 5109 | |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 5110 | /* Get the btc-disabled property */ |
| 5111 | chip->btc_disabled = of_property_read_bool(chip->spmi->dev.of_node, |
| 5112 | "qcom,btc-disabled"); |
| 5113 | |
David Keitel | 2586f0a | 2013-08-20 10:02:26 -0700 | [diff] [blame] | 5114 | ext_ovp_present = of_property_read_bool(chip->spmi->dev.of_node, |
| 5115 | "qcom,ext-ovp-present"); |
| 5116 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 5117 | /* Check if external IOVP part is configured */ |
| 5118 | chip->ext_ovp_isns_gpio = of_get_named_gpio(chip->spmi->dev.of_node, |
| 5119 | "qcom,ext-ovp-isns-enable-gpio", 0); |
| 5120 | if (gpio_is_valid(chip->ext_ovp_isns_gpio)) { |
| 5121 | ext_ovp_isns_present = true; |
| 5122 | rc = of_property_read_u32(chip->spmi->dev.of_node, |
| 5123 | "qcom,ext-ovp-isns-r-ohm", &ext_ovp_isns_r); |
| 5124 | if (rc) |
| 5125 | return rc; |
| 5126 | } |
| 5127 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5128 | /* Get the charging-disabled property */ |
| 5129 | chip->charging_disabled = of_property_read_bool(chip->spmi->dev.of_node, |
| 5130 | "qcom,charging-disabled"); |
| 5131 | |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 5132 | chip->ovp_monitor_enable = of_property_read_bool(chip->spmi->dev.of_node, |
| 5133 | "qcom,ovp-monitor-en"); |
| 5134 | |
David Keitel | 8b68d2d | 2013-05-14 23:36:51 -0700 | [diff] [blame] | 5135 | /* Get the duty-cycle-100p property */ |
| 5136 | chip->duty_cycle_100p = of_property_read_bool( |
| 5137 | chip->spmi->dev.of_node, |
| 5138 | "qcom,duty-cycle-100p"); |
David Keitel | 8b68d2d | 2013-05-14 23:36:51 -0700 | [diff] [blame] | 5139 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5140 | /* Get the fake-batt-values property */ |
| 5141 | chip->use_default_batt_values = |
| 5142 | of_property_read_bool(chip->spmi->dev.of_node, |
| 5143 | "qcom,use-default-batt-values"); |
| 5144 | |
| 5145 | /* Disable charging when faking battery values */ |
| 5146 | if (chip->use_default_batt_values) |
| 5147 | chip->charging_disabled = true; |
| 5148 | |
Zhenhua Huang | dc33551 | 2013-10-30 11:45:15 +0800 | [diff] [blame] | 5149 | chip->power_stage_workaround_enable = |
| 5150 | of_property_read_bool(chip->spmi->dev.of_node, |
| 5151 | "qcom,power-stage-reduced"); |
| 5152 | |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 5153 | chip->ibat_calibration_enabled = |
| 5154 | of_property_read_bool(chip->spmi->dev.of_node, |
| 5155 | "qcom,ibat-calibration-enabled"); |
David Keitel | 975b535 | 2014-01-27 14:28:04 -0800 | [diff] [blame] | 5156 | chip->parallel_ovp_mode = |
| 5157 | of_property_read_bool(chip->spmi->dev.of_node, |
| 5158 | "qcom,parallel-ovp-mode"); |
Anirudh Ghayal | 42fc8a1 | 2013-10-21 17:42:54 -0700 | [diff] [blame] | 5159 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5160 | of_get_property(chip->spmi->dev.of_node, "qcom,thermal-mitigation", |
| 5161 | &(chip->thermal_levels)); |
| 5162 | |
| 5163 | if (chip->thermal_levels > sizeof(int)) { |
Prasad Sodagudi | db7bf15 | 2013-10-25 16:05:16 +0530 | [diff] [blame] | 5164 | chip->thermal_mitigation = devm_kzalloc(chip->dev, |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5165 | chip->thermal_levels, |
| 5166 | GFP_KERNEL); |
| 5167 | |
| 5168 | if (chip->thermal_mitigation == NULL) { |
| 5169 | pr_err("thermal mitigation kzalloc() failed.\n"); |
Prasad Sodagudi | db7bf15 | 2013-10-25 16:05:16 +0530 | [diff] [blame] | 5170 | return -ENOMEM; |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | chip->thermal_levels /= sizeof(int); |
| 5174 | rc = of_property_read_u32_array(chip->spmi->dev.of_node, |
| 5175 | "qcom,thermal-mitigation", |
| 5176 | chip->thermal_mitigation, chip->thermal_levels); |
| 5177 | if (rc) { |
| 5178 | pr_err("qcom,thermal-mitigation missing in dt\n"); |
| 5179 | return rc; |
| 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | return rc; |
| 5184 | } |
| 5185 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5186 | static int __devinit |
| 5187 | qpnp_charger_probe(struct spmi_device *spmi) |
| 5188 | { |
| 5189 | u8 subtype; |
| 5190 | struct qpnp_chg_chip *chip; |
| 5191 | struct resource *resource; |
| 5192 | struct spmi_resource *spmi_resource; |
| 5193 | int rc = 0; |
| 5194 | |
Prasad Sodagudi | db7bf15 | 2013-10-25 16:05:16 +0530 | [diff] [blame] | 5195 | chip = devm_kzalloc(&spmi->dev, |
| 5196 | sizeof(struct qpnp_chg_chip), GFP_KERNEL); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5197 | if (chip == NULL) { |
| 5198 | pr_err("kzalloc() failed.\n"); |
| 5199 | return -ENOMEM; |
| 5200 | } |
| 5201 | |
David Keitel | fd305ee | 2013-07-19 20:19:06 -0700 | [diff] [blame] | 5202 | chip->prev_usb_max_ma = -EINVAL; |
Xiaozhe Shi | 78797cf | 2013-11-12 13:18:42 -0800 | [diff] [blame] | 5203 | chip->fake_battery_soc = -EINVAL; |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5204 | chip->dev = &(spmi->dev); |
| 5205 | chip->spmi = spmi; |
| 5206 | |
| 5207 | chip->usb_psy = power_supply_get_by_name("usb"); |
| 5208 | if (!chip->usb_psy) { |
| 5209 | pr_err("usb supply not found deferring probe\n"); |
| 5210 | rc = -EPROBE_DEFER; |
| 5211 | goto fail_chg_enable; |
| 5212 | } |
| 5213 | |
David Keitel | ec3c0980 | 2013-06-13 13:05:30 -0700 | [diff] [blame] | 5214 | mutex_init(&chip->jeita_configure_lock); |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 5215 | spin_lock_init(&chip->usbin_health_monitor_lock); |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5216 | alarm_init(&chip->reduce_power_stage_alarm, ANDROID_ALARM_RTC_WAKEUP, |
| 5217 | qpnp_chg_reduce_power_stage_callback); |
| 5218 | INIT_WORK(&chip->reduce_power_stage_work, |
| 5219 | qpnp_chg_reduce_power_stage_work); |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 5220 | mutex_init(&chip->batfet_vreg_lock); |
David Keitel | 8b9bd31 | 2014-01-15 14:44:00 -0800 | [diff] [blame] | 5221 | INIT_WORK(&chip->ocp_clear_work, |
| 5222 | qpnp_chg_ocp_clear_work); |
David Keitel | 6e63bd5 | 2013-09-06 18:00:03 -0700 | [diff] [blame] | 5223 | INIT_WORK(&chip->batfet_lcl_work, |
| 5224 | qpnp_chg_batfet_lcl_work); |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 5225 | INIT_WORK(&chip->insertion_ocv_work, |
| 5226 | qpnp_chg_insertion_ocv_work); |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5227 | |
David Keitel | 112ba9c | 2013-04-12 18:40:43 -0700 | [diff] [blame] | 5228 | /* Get all device tree properties */ |
| 5229 | rc = qpnp_charger_read_dt_props(chip); |
| 5230 | if (rc) |
Prasad Sodagudi | db7bf15 | 2013-10-25 16:05:16 +0530 | [diff] [blame] | 5231 | return rc; |
David Keitel | 3dd5e0f | 2012-12-12 18:12:36 -0800 | [diff] [blame] | 5232 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 5233 | if (ext_ovp_isns_present) |
| 5234 | chip->ext_ovp_ic_gpio_enabled = 0; |
| 5235 | |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 5236 | /* |
| 5237 | * Check if bat_if is set in DT and make sure VADC is present |
| 5238 | * Also try loading the battery data profile if bat_if exists |
| 5239 | */ |
David Keitel | dd87a17 | 2013-05-29 17:05:43 -0700 | [diff] [blame] | 5240 | spmi_for_each_container_dev(spmi_resource, spmi) { |
| 5241 | if (!spmi_resource) { |
| 5242 | pr_err("qpnp_chg: spmi resource absent\n"); |
| 5243 | rc = -ENXIO; |
| 5244 | goto fail_chg_enable; |
| 5245 | } |
| 5246 | |
| 5247 | resource = spmi_get_resource(spmi, spmi_resource, |
| 5248 | IORESOURCE_MEM, 0); |
| 5249 | if (!(resource && resource->start)) { |
| 5250 | pr_err("node %s IO resource absent!\n", |
| 5251 | spmi->dev.of_node->full_name); |
| 5252 | rc = -ENXIO; |
| 5253 | goto fail_chg_enable; |
| 5254 | } |
| 5255 | |
| 5256 | rc = qpnp_chg_read(chip, &subtype, |
| 5257 | resource->start + REG_OFFSET_PERP_SUBTYPE, 1); |
| 5258 | if (rc) { |
| 5259 | pr_err("Peripheral subtype read failed rc=%d\n", rc); |
| 5260 | goto fail_chg_enable; |
| 5261 | } |
| 5262 | |
| 5263 | if (subtype == SMBB_BAT_IF_SUBTYPE || |
| 5264 | subtype == SMBBP_BAT_IF_SUBTYPE || |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 5265 | subtype == SMBCL_BAT_IF_SUBTYPE) { |
| 5266 | chip->vadc_dev = qpnp_get_vadc(chip->dev, "chg"); |
| 5267 | if (IS_ERR(chip->vadc_dev)) { |
| 5268 | rc = PTR_ERR(chip->vadc_dev); |
| 5269 | if (rc != -EPROBE_DEFER) |
| 5270 | pr_err("vadc property missing\n"); |
David Keitel | dd87a17 | 2013-05-29 17:05:43 -0700 | [diff] [blame] | 5271 | goto fail_chg_enable; |
David Keitel | 42648fd | 2013-08-16 17:45:20 -0700 | [diff] [blame] | 5272 | } |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 5273 | |
Abhijeet Dharmapurikar | 2d81b26 | 2013-11-14 19:15:05 -0800 | [diff] [blame] | 5274 | if (subtype == SMBB_BAT_IF_SUBTYPE) { |
| 5275 | chip->iadc_dev = qpnp_get_iadc(chip->dev, |
| 5276 | "chg"); |
| 5277 | if (IS_ERR(chip->iadc_dev)) { |
| 5278 | rc = PTR_ERR(chip->iadc_dev); |
| 5279 | if (rc != -EPROBE_DEFER) |
| 5280 | pr_err("iadc property missing\n"); |
| 5281 | goto fail_chg_enable; |
| 5282 | } |
| 5283 | } |
| 5284 | |
Xiaozhe Shi | ca289e0 | 2013-06-19 13:24:51 -0700 | [diff] [blame] | 5285 | rc = qpnp_chg_load_battery_data(chip); |
| 5286 | if (rc) |
| 5287 | goto fail_chg_enable; |
David Keitel | dd87a17 | 2013-05-29 17:05:43 -0700 | [diff] [blame] | 5288 | } |
| 5289 | } |
| 5290 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5291 | spmi_for_each_container_dev(spmi_resource, spmi) { |
| 5292 | if (!spmi_resource) { |
| 5293 | pr_err("qpnp_chg: spmi resource absent\n"); |
| 5294 | rc = -ENXIO; |
| 5295 | goto fail_chg_enable; |
| 5296 | } |
| 5297 | |
| 5298 | resource = spmi_get_resource(spmi, spmi_resource, |
| 5299 | IORESOURCE_MEM, 0); |
| 5300 | if (!(resource && resource->start)) { |
| 5301 | pr_err("node %s IO resource absent!\n", |
| 5302 | spmi->dev.of_node->full_name); |
| 5303 | rc = -ENXIO; |
| 5304 | goto fail_chg_enable; |
| 5305 | } |
| 5306 | |
| 5307 | rc = qpnp_chg_read(chip, &subtype, |
| 5308 | resource->start + REG_OFFSET_PERP_SUBTYPE, 1); |
| 5309 | if (rc) { |
| 5310 | pr_err("Peripheral subtype read failed rc=%d\n", rc); |
| 5311 | goto fail_chg_enable; |
| 5312 | } |
| 5313 | |
| 5314 | switch (subtype) { |
| 5315 | case SMBB_CHGR_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5316 | case SMBBP_CHGR_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 5317 | case SMBCL_CHGR_SUBTYPE: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5318 | chip->chgr_base = resource->start; |
| 5319 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5320 | if (rc) { |
| 5321 | pr_err("Failed to init subtype 0x%x rc=%d\n", |
| 5322 | subtype, rc); |
| 5323 | goto fail_chg_enable; |
| 5324 | } |
| 5325 | break; |
| 5326 | case SMBB_BUCK_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5327 | case SMBBP_BUCK_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 5328 | case SMBCL_BUCK_SUBTYPE: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5329 | chip->buck_base = resource->start; |
| 5330 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5331 | if (rc) { |
| 5332 | pr_err("Failed to init subtype 0x%x rc=%d\n", |
| 5333 | subtype, rc); |
| 5334 | goto fail_chg_enable; |
| 5335 | } |
David Keitel | 344c697 | 2013-04-09 19:28:21 -0700 | [diff] [blame] | 5336 | |
| 5337 | rc = qpnp_chg_masked_write(chip, |
| 5338 | chip->buck_base + SEC_ACCESS, |
| 5339 | 0xFF, |
| 5340 | 0xA5, 1); |
| 5341 | |
| 5342 | rc = qpnp_chg_masked_write(chip, |
| 5343 | chip->buck_base + BUCK_VCHG_OV, |
| 5344 | 0xff, |
| 5345 | 0x00, 1); |
| 5346 | |
David Keitel | dbc949b | 2013-07-11 10:56:03 -0700 | [diff] [blame] | 5347 | if (chip->duty_cycle_100p) { |
| 5348 | rc = qpnp_buck_set_100_duty_cycle_enable(chip, |
| 5349 | 1); |
| 5350 | if (rc) { |
| 5351 | pr_err("failed to set duty cycle %d\n", |
| 5352 | rc); |
| 5353 | goto fail_chg_enable; |
| 5354 | } |
| 5355 | } |
| 5356 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5357 | break; |
| 5358 | case SMBB_BAT_IF_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5359 | case SMBBP_BAT_IF_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 5360 | case SMBCL_BAT_IF_SUBTYPE: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5361 | chip->bat_if_base = resource->start; |
| 5362 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5363 | if (rc) { |
| 5364 | pr_err("Failed to init subtype 0x%x rc=%d\n", |
| 5365 | subtype, rc); |
| 5366 | goto fail_chg_enable; |
| 5367 | } |
| 5368 | break; |
| 5369 | case SMBB_USB_CHGPTH_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5370 | case SMBBP_USB_CHGPTH_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 5371 | case SMBCL_USB_CHGPTH_SUBTYPE: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5372 | chip->usb_chgpth_base = resource->start; |
| 5373 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5374 | if (rc) { |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 5375 | if (rc != -EPROBE_DEFER) |
| 5376 | pr_err("Failed to init subtype 0x%x rc=%d\n", |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5377 | subtype, rc); |
| 5378 | goto fail_chg_enable; |
| 5379 | } |
| 5380 | break; |
| 5381 | case SMBB_DC_CHGPTH_SUBTYPE: |
| 5382 | chip->dc_chgpth_base = resource->start; |
| 5383 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5384 | if (rc) { |
| 5385 | pr_err("Failed to init subtype 0x%x rc=%d\n", |
| 5386 | subtype, rc); |
| 5387 | goto fail_chg_enable; |
| 5388 | } |
| 5389 | break; |
| 5390 | case SMBB_BOOST_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5391 | case SMBBP_BOOST_SUBTYPE: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5392 | chip->boost_base = resource->start; |
| 5393 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5394 | if (rc) { |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 5395 | if (rc != -EPROBE_DEFER) |
| 5396 | pr_err("Failed to init subtype 0x%x rc=%d\n", |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5397 | subtype, rc); |
| 5398 | goto fail_chg_enable; |
| 5399 | } |
| 5400 | break; |
| 5401 | case SMBB_MISC_SUBTYPE: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5402 | case SMBBP_MISC_SUBTYPE: |
David Keitel | 46c9f7b | 2013-04-02 19:54:12 -0700 | [diff] [blame] | 5403 | case SMBCL_MISC_SUBTYPE: |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5404 | chip->misc_base = resource->start; |
| 5405 | rc = qpnp_chg_hwinit(chip, subtype, spmi_resource); |
| 5406 | if (rc) { |
| 5407 | pr_err("Failed to init subtype=0x%x rc=%d\n", |
| 5408 | subtype, rc); |
| 5409 | goto fail_chg_enable; |
| 5410 | } |
| 5411 | break; |
| 5412 | default: |
| 5413 | pr_err("Invalid peripheral subtype=0x%x\n", subtype); |
| 5414 | rc = -EINVAL; |
| 5415 | goto fail_chg_enable; |
| 5416 | } |
| 5417 | } |
| 5418 | dev_set_drvdata(&spmi->dev, chip); |
| 5419 | device_init_wakeup(&spmi->dev, 1); |
| 5420 | |
Xiaozhe Shi | bc4f9ee | 2013-08-29 17:03:44 -0700 | [diff] [blame] | 5421 | chip->insertion_ocv_uv = -EINVAL; |
| 5422 | chip->batt_present = qpnp_chg_is_batt_present(chip); |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5423 | if (chip->bat_if_base) { |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5424 | chip->batt_psy.name = "battery"; |
| 5425 | chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY; |
| 5426 | chip->batt_psy.properties = msm_batt_power_props; |
| 5427 | chip->batt_psy.num_properties = |
| 5428 | ARRAY_SIZE(msm_batt_power_props); |
| 5429 | chip->batt_psy.get_property = qpnp_batt_power_get_property; |
| 5430 | chip->batt_psy.set_property = qpnp_batt_power_set_property; |
| 5431 | chip->batt_psy.property_is_writeable = |
| 5432 | qpnp_batt_property_is_writeable; |
| 5433 | chip->batt_psy.external_power_changed = |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5434 | qpnp_batt_external_power_changed; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 5435 | chip->batt_psy.supplied_to = pm_batt_supplied_to; |
| 5436 | chip->batt_psy.num_supplicants = |
| 5437 | ARRAY_SIZE(pm_batt_supplied_to); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5438 | |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5439 | rc = power_supply_register(chip->dev, &chip->batt_psy); |
| 5440 | if (rc < 0) { |
| 5441 | pr_err("batt failed to register rc = %d\n", rc); |
| 5442 | goto fail_chg_enable; |
| 5443 | } |
David Keitel | 79f4c93 | 2013-04-03 16:08:39 -0700 | [diff] [blame] | 5444 | INIT_WORK(&chip->adc_measure_work, |
| 5445 | qpnp_bat_if_adc_measure_work); |
David Keitel | aa37556 | 2013-08-08 09:43:28 -0700 | [diff] [blame] | 5446 | INIT_WORK(&chip->adc_disable_work, |
David Keitel | ecab15a | 2013-08-06 15:41:39 -0700 | [diff] [blame] | 5447 | qpnp_bat_if_adc_disable_work); |
David Keitel | c7093b0 | 2013-02-14 12:50:04 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 5450 | INIT_DELAYED_WORK(&chip->eoc_work, qpnp_eoc_work); |
David Keitel | 5910eea | 2013-05-02 15:32:25 -0700 | [diff] [blame] | 5451 | INIT_DELAYED_WORK(&chip->arb_stop_work, qpnp_arb_stop_work); |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 5452 | INIT_DELAYED_WORK(&chip->usbin_health_check, |
| 5453 | qpnp_usbin_health_check_work); |
Xiaozhe Shi | 8fbbadc | 2013-07-31 10:59:09 -0700 | [diff] [blame] | 5454 | INIT_WORK(&chip->soc_check_work, qpnp_chg_soc_check_work); |
David Keitel | 7ac385e | 2013-08-19 18:02:40 -0700 | [diff] [blame] | 5455 | INIT_DELAYED_WORK(&chip->aicl_check_work, qpnp_aicl_check_work); |
David Keitel | 5910eea | 2013-05-02 15:32:25 -0700 | [diff] [blame] | 5456 | |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5457 | if (chip->dc_chgpth_base) { |
| 5458 | chip->dc_psy.name = "qpnp-dc"; |
| 5459 | chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS; |
| 5460 | chip->dc_psy.supplied_to = pm_power_supplied_to; |
| 5461 | chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to); |
| 5462 | chip->dc_psy.properties = pm_power_props_mains; |
| 5463 | chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props_mains); |
| 5464 | chip->dc_psy.get_property = qpnp_power_get_property_mains; |
David Keitel | 432e123 | 2013-06-05 16:10:18 -0700 | [diff] [blame] | 5465 | chip->dc_psy.set_property = qpnp_dc_power_set_property; |
| 5466 | chip->dc_psy.property_is_writeable = |
| 5467 | qpnp_dc_property_is_writeable; |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5468 | |
| 5469 | rc = power_supply_register(chip->dev, &chip->dc_psy); |
| 5470 | if (rc < 0) { |
| 5471 | pr_err("power_supply_register dc failed rc=%d\n", rc); |
| 5472 | goto unregister_batt; |
| 5473 | } |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5474 | } |
| 5475 | |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 5476 | /* Turn on appropriate workaround flags */ |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5477 | rc = qpnp_chg_setup_flags(chip); |
| 5478 | if (rc < 0) { |
| 5479 | pr_err("failed to setup flags rc=%d\n", rc); |
| 5480 | goto unregister_dc_psy; |
| 5481 | } |
Sridhar Parasuram | ae183bd | 2012-12-21 09:28:46 -0800 | [diff] [blame] | 5482 | |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5483 | if (chip->maxinput_dc_ma && chip->dc_chgpth_base) { |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 5484 | rc = qpnp_chg_idcmax_set(chip, chip->maxinput_dc_ma); |
| 5485 | if (rc) { |
| 5486 | pr_err("Error setting idcmax property %d\n", rc); |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5487 | goto unregister_dc_psy; |
David Keitel | 22ed223 | 2013-01-28 11:04:07 -0800 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | |
Zhenhua Huang | 4c593ec | 2013-08-13 17:22:58 +0800 | [diff] [blame] | 5491 | if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc) |
David Keitel | 0aea18a | 2013-06-14 18:57:57 -0700 | [diff] [blame] | 5492 | && chip->bat_if_base) { |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 5493 | chip->adc_param.low_temp = chip->cool_bat_decidegc; |
| 5494 | chip->adc_param.high_temp = chip->warm_bat_decidegc; |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 5495 | chip->adc_param.timer_interval = ADC_MEAS2_INTERVAL_1S; |
| 5496 | chip->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE; |
| 5497 | chip->adc_param.btm_ctx = chip; |
| 5498 | chip->adc_param.threshold_notification = |
| 5499 | qpnp_chg_adc_notification; |
Siddartha Mohanadoss | a3e3551 | 2013-02-22 17:06:07 -0800 | [diff] [blame] | 5500 | chip->adc_param.channel = LR_MUX1_BATT_THERM; |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 5501 | |
| 5502 | if (get_prop_batt_present(chip)) { |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 5503 | rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 5504 | &chip->adc_param); |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 5505 | if (rc) { |
| 5506 | pr_err("request ADC error %d\n", rc); |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5507 | goto unregister_dc_psy; |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 5508 | } |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 5509 | } |
| 5510 | } |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 5511 | rc = qpnp_chg_bat_if_configure_btc(chip); |
| 5512 | if (rc) { |
| 5513 | pr_err("failed to configure btc %d\n", rc); |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5514 | goto unregister_dc_psy; |
David Keitel | 89c1775 | 2013-06-28 14:52:48 -0700 | [diff] [blame] | 5515 | } |
David Keitel | 454ee84 | 2013-03-08 16:19:11 -0800 | [diff] [blame] | 5516 | |
David Keitel | cd2e102 | 2014-05-02 17:25:15 -0700 | [diff] [blame] | 5517 | chip->usb_trim_default = qpnp_chg_iusb_trim_get(chip); |
David Keitel | 03ee6b5 | 2012-10-22 12:25:19 -0700 | [diff] [blame] | 5518 | qpnp_chg_charge_en(chip, !chip->charging_disabled); |
David Keitel | b1ddb74 | 2012-11-06 19:05:51 -0800 | [diff] [blame] | 5519 | qpnp_chg_force_run_on_batt(chip, chip->charging_disabled); |
Xiaozhe Shi | 79a8c49 | 2013-05-24 13:09:23 -0700 | [diff] [blame] | 5520 | qpnp_chg_set_appropriate_vddmax(chip); |
David Keitel | bf35904 | 2012-10-19 16:54:58 -0700 | [diff] [blame] | 5521 | |
David Keitel | bf2edba | 2014-03-05 10:49:32 -0800 | [diff] [blame] | 5522 | if (chip->parallel_ovp_mode) { |
| 5523 | rc = override_dcin_ilimit(chip, 1); |
| 5524 | if (rc) { |
| 5525 | pr_err("Override DCIN LLIMIT %d\n", rc); |
| 5526 | goto unregister_dc_psy; |
| 5527 | } |
| 5528 | } |
| 5529 | |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 5530 | rc = qpnp_chg_request_irqs(chip); |
| 5531 | if (rc) { |
| 5532 | pr_err("failed to request interrupts %d\n", rc); |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5533 | goto unregister_dc_psy; |
David Keitel | 0f35be4 | 2013-04-16 11:10:40 -0700 | [diff] [blame] | 5534 | } |
| 5535 | |
David Keitel | 7e38f51 | 2013-11-19 11:15:11 -0800 | [diff] [blame] | 5536 | qpnp_chg_usb_chg_gone_irq_handler(chip->chg_gone.irq, chip); |
David Keitel | ad980fc | 2013-07-30 18:12:02 -0700 | [diff] [blame] | 5537 | qpnp_chg_usb_usbin_valid_irq_handler(chip->usbin_valid.irq, chip); |
| 5538 | qpnp_chg_dc_dcin_valid_irq_handler(chip->dcin_valid.irq, chip); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 5539 | power_supply_set_present(chip->usb_psy, |
| 5540 | qpnp_chg_is_usb_chg_plugged_in(chip)); |
| 5541 | |
David Keitel | 3c62b47 | 2013-05-06 15:38:11 -0700 | [diff] [blame] | 5542 | /* Set USB psy online to avoid userspace from shutting down if battery |
| 5543 | * capacity is at zero and no chargers online. */ |
| 5544 | if (qpnp_chg_is_usb_chg_plugged_in(chip)) |
| 5545 | power_supply_set_online(chip->usb_psy, 1); |
| 5546 | |
David Keitel | 7ac385e | 2013-08-19 18:02:40 -0700 | [diff] [blame] | 5547 | schedule_delayed_work(&chip->aicl_check_work, |
| 5548 | msecs_to_jiffies(EOC_CHECK_PERIOD_MS)); |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 5549 | pr_info("success chg_dis = %d, bpd = %d, usb = %d, dc = %d b_health = %d batt_present = %d\n", |
Abhijeet Dharmapurikar | 2d996b1 | 2013-01-03 17:48:02 -0800 | [diff] [blame] | 5550 | chip->charging_disabled, |
David Keitel | 796882d | 2013-05-14 18:01:11 -0700 | [diff] [blame] | 5551 | chip->bpd_detection, |
Abhijeet Dharmapurikar | 2d996b1 | 2013-01-03 17:48:02 -0800 | [diff] [blame] | 5552 | qpnp_chg_is_usb_chg_plugged_in(chip), |
| 5553 | qpnp_chg_is_dc_chg_plugged_in(chip), |
| 5554 | get_prop_batt_present(chip), |
| 5555 | get_prop_batt_health(chip)); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5556 | return 0; |
| 5557 | |
Abhijeet Dharmapurikar | e749700 | 2013-07-29 11:25:37 -0700 | [diff] [blame] | 5558 | unregister_dc_psy: |
| 5559 | if (chip->dc_chgpth_base) |
| 5560 | power_supply_unregister(&chip->dc_psy); |
David Keitel | c7093b0 | 2013-02-14 12:50:04 -0800 | [diff] [blame] | 5561 | unregister_batt: |
David Keitel | f2170cc | 2013-02-20 17:49:03 -0800 | [diff] [blame] | 5562 | if (chip->bat_if_base) |
| 5563 | power_supply_unregister(&chip->batt_psy); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5564 | fail_chg_enable: |
David Collins | cbb1213 | 2013-05-28 10:47:28 -0700 | [diff] [blame] | 5565 | regulator_unregister(chip->otg_vreg.rdev); |
| 5566 | regulator_unregister(chip->boost_vreg.rdev); |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5567 | return rc; |
| 5568 | } |
| 5569 | |
| 5570 | static int __devexit |
| 5571 | qpnp_charger_remove(struct spmi_device *spmi) |
| 5572 | { |
| 5573 | struct qpnp_chg_chip *chip = dev_get_drvdata(&spmi->dev); |
Zhenhua Huang | 4c593ec | 2013-08-13 17:22:58 +0800 | [diff] [blame] | 5574 | if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc) |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 5575 | && chip->batt_present) { |
Siddartha Mohanadoss | 88a3fde | 2013-06-24 16:18:52 -0700 | [diff] [blame] | 5576 | qpnp_adc_tm_disable_chan_meas(chip->adc_tm_dev, |
| 5577 | &chip->adc_param); |
David Keitel | 0c1a453 | 2013-03-21 16:39:06 -0700 | [diff] [blame] | 5578 | } |
Prasad Sodagudi | db7bf15 | 2013-10-25 16:05:16 +0530 | [diff] [blame] | 5579 | |
| 5580 | cancel_delayed_work_sync(&chip->aicl_check_work); |
| 5581 | power_supply_unregister(&chip->dc_psy); |
| 5582 | cancel_work_sync(&chip->soc_check_work); |
| 5583 | cancel_delayed_work_sync(&chip->usbin_health_check); |
| 5584 | cancel_delayed_work_sync(&chip->arb_stop_work); |
David Keitel | 9fd0738 | 2013-05-02 15:37:44 -0700 | [diff] [blame] | 5585 | cancel_delayed_work_sync(&chip->eoc_work); |
Prasad Sodagudi | db7bf15 | 2013-10-25 16:05:16 +0530 | [diff] [blame] | 5586 | cancel_work_sync(&chip->adc_disable_work); |
| 5587 | cancel_work_sync(&chip->adc_measure_work); |
| 5588 | power_supply_unregister(&chip->batt_psy); |
| 5589 | cancel_work_sync(&chip->batfet_lcl_work); |
| 5590 | cancel_work_sync(&chip->insertion_ocv_work); |
| 5591 | cancel_work_sync(&chip->reduce_power_stage_work); |
| 5592 | alarm_cancel(&chip->reduce_power_stage_alarm); |
| 5593 | |
| 5594 | mutex_destroy(&chip->batfet_vreg_lock); |
| 5595 | mutex_destroy(&chip->jeita_configure_lock); |
David Keitel | 79f4c93 | 2013-04-03 16:08:39 -0700 | [diff] [blame] | 5596 | |
David Collins | cbb1213 | 2013-05-28 10:47:28 -0700 | [diff] [blame] | 5597 | regulator_unregister(chip->otg_vreg.rdev); |
| 5598 | regulator_unregister(chip->boost_vreg.rdev); |
David Keitel | 6dc4ed4 | 2013-05-17 11:08:58 -0700 | [diff] [blame] | 5599 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5600 | return 0; |
| 5601 | } |
| 5602 | |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 5603 | static int qpnp_chg_resume(struct device *dev) |
| 5604 | { |
| 5605 | struct qpnp_chg_chip *chip = dev_get_drvdata(dev); |
| 5606 | int rc = 0; |
| 5607 | |
David Keitel | 3909357 | 2013-06-05 15:32:30 -0700 | [diff] [blame] | 5608 | if (chip->bat_if_base) { |
| 5609 | rc = qpnp_chg_masked_write(chip, |
| 5610 | chip->bat_if_base + BAT_IF_VREF_BAT_THM_CTRL, |
| 5611 | VREF_BATT_THERM_FORCE_ON, |
| 5612 | VREF_BATT_THERM_FORCE_ON, 1); |
| 5613 | if (rc) |
| 5614 | pr_debug("failed to force on VREF_BAT_THM rc=%d\n", rc); |
| 5615 | } |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 5616 | |
| 5617 | return rc; |
| 5618 | } |
| 5619 | |
| 5620 | static int qpnp_chg_suspend(struct device *dev) |
| 5621 | { |
| 5622 | struct qpnp_chg_chip *chip = dev_get_drvdata(dev); |
| 5623 | int rc = 0; |
| 5624 | |
David Keitel | 3909357 | 2013-06-05 15:32:30 -0700 | [diff] [blame] | 5625 | if (chip->bat_if_base) { |
| 5626 | rc = qpnp_chg_masked_write(chip, |
| 5627 | chip->bat_if_base + BAT_IF_VREF_BAT_THM_CTRL, |
| 5628 | VREF_BATT_THERM_FORCE_ON, |
| 5629 | VREF_BAT_THM_ENABLED_FSM, 1); |
| 5630 | if (rc) |
| 5631 | pr_debug("failed to set FSM VREF_BAT_THM rc=%d\n", rc); |
| 5632 | } |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 5633 | |
| 5634 | return rc; |
| 5635 | } |
| 5636 | |
David Keitel | 723d501 | 2013-05-03 13:17:27 -0700 | [diff] [blame] | 5637 | static const struct dev_pm_ops qpnp_chg_pm_ops = { |
David Keitel | 85ae434 | 2013-04-16 11:46:00 -0700 | [diff] [blame] | 5638 | .resume = qpnp_chg_resume, |
| 5639 | .suspend = qpnp_chg_suspend, |
| 5640 | }; |
| 5641 | |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5642 | static struct spmi_driver qpnp_charger_driver = { |
| 5643 | .probe = qpnp_charger_probe, |
| 5644 | .remove = __devexit_p(qpnp_charger_remove), |
| 5645 | .driver = { |
David Keitel | 723d501 | 2013-05-03 13:17:27 -0700 | [diff] [blame] | 5646 | .name = QPNP_CHARGER_DEV_NAME, |
| 5647 | .owner = THIS_MODULE, |
| 5648 | .of_match_table = qpnp_charger_match_table, |
| 5649 | .pm = &qpnp_chg_pm_ops, |
David Keitel | 8066895 | 2012-07-27 14:25:49 -0700 | [diff] [blame] | 5650 | }, |
| 5651 | }; |
| 5652 | |
| 5653 | /** |
| 5654 | * qpnp_chg_init() - register spmi driver for qpnp-chg |
| 5655 | */ |
| 5656 | int __init |
| 5657 | qpnp_chg_init(void) |
| 5658 | { |
| 5659 | return spmi_driver_register(&qpnp_charger_driver); |
| 5660 | } |
| 5661 | module_init(qpnp_chg_init); |
| 5662 | |
| 5663 | static void __exit |
| 5664 | qpnp_chg_exit(void) |
| 5665 | { |
| 5666 | spmi_driver_unregister(&qpnp_charger_driver); |
| 5667 | } |
| 5668 | module_exit(qpnp_chg_exit); |
| 5669 | |
| 5670 | |
| 5671 | MODULE_DESCRIPTION("QPNP charger driver"); |
| 5672 | MODULE_LICENSE("GPL v2"); |
| 5673 | MODULE_ALIAS("platform:" QPNP_CHARGER_DEV_NAME); |