Zhenhua Huang | cff7499 | 2014-02-11 10:19:17 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2014 The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "SMB358 %s: " fmt, __func__ |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/debugfs.h> |
| 16 | #include <linux/gpio.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/power_supply.h> |
| 23 | #include <linux/regulator/of_regulator.h> |
| 24 | #include <linux/regulator/machine.h> |
| 25 | #include <linux/regulator/consumer.h> |
| 26 | #include <linux/regulator/driver.h> |
| 27 | #include <linux/of.h> |
| 28 | #include <linux/of_gpio.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/qpnp/qpnp-adc.h> |
| 31 | |
| 32 | /* Config/Control registers */ |
| 33 | #define CHG_CURRENT_CTRL_REG 0x0 |
| 34 | #define CHG_OTH_CURRENT_CTRL_REG 0x1 |
| 35 | #define VARIOUS_FUNC_REG 0x2 |
| 36 | #define VFLOAT_REG 0x3 |
| 37 | #define CHG_CTRL_REG 0x4 |
| 38 | #define STAT_AND_TIMER_CTRL_REG 0x5 |
| 39 | #define CHG_PIN_EN_CTRL_REG 0x6 |
| 40 | #define THERM_A_CTRL_REG 0x7 |
| 41 | #define SYSOK_AND_USB3_REG 0x8 |
| 42 | #define FAULT_INT_REG 0xC |
| 43 | #define STATUS_INT_REG 0xD |
| 44 | |
| 45 | /* Command registers */ |
| 46 | #define CMD_A_REG 0x30 |
| 47 | #define CMD_B_REG 0x31 |
| 48 | |
| 49 | /* IRQ status registers */ |
| 50 | #define IRQ_A_REG 0x35 |
| 51 | #define IRQ_B_REG 0x36 |
| 52 | #define IRQ_C_REG 0x37 |
| 53 | #define IRQ_D_REG 0x38 |
| 54 | #define IRQ_E_REG 0x39 |
| 55 | #define IRQ_F_REG 0x3A |
| 56 | |
| 57 | /* Status registers */ |
| 58 | #define STATUS_C_REG 0x3D |
| 59 | #define STATUS_D_REG 0x3E |
| 60 | #define STATUS_E_REG 0x3F |
| 61 | |
| 62 | /* Config bits */ |
| 63 | #define CHG_INHI_EN_MASK BIT(1) |
| 64 | #define CHG_INHI_EN_BIT BIT(1) |
| 65 | #define CMD_A_CHG_ENABLE_BIT BIT(1) |
| 66 | #define CMD_A_VOLATILE_W_PERM_BIT BIT(7) |
| 67 | #define CMD_A_CHG_SUSP_EN_BIT BIT(2) |
| 68 | #define CMD_A_CHG_SUSP_EN_MASK BIT(2) |
| 69 | #define CMD_A_OTG_ENABLE_BIT BIT(4) |
| 70 | #define CMD_A_OTG_ENABLE_MASK BIT(4) |
| 71 | #define CMD_B_CHG_HC_ENABLE_BIT BIT(0) |
| 72 | #define USB3_ENABLE_BIT BIT(5) |
| 73 | #define USB3_ENABLE_MASK BIT(5) |
| 74 | #define CMD_B_CHG_USB_500_900_ENABLE_BIT BIT(1) |
| 75 | #define CHG_CTRL_AUTO_RECHARGE_ENABLE_BIT 0x0 |
| 76 | #define CHG_CTRL_CURR_TERM_END_CHG_BIT 0x0 |
| 77 | #define CHG_CTRL_BATT_MISSING_DET_THERM_IO (BIT(5) | BIT(4)) |
| 78 | #define CHG_CTRL_AUTO_RECHARGE_MASK BIT(7) |
| 79 | #define CHG_CTRL_CURR_TERM_END_MASK BIT(6) |
| 80 | #define CHG_CTRL_BATT_MISSING_DET_MASK (BIT(5) | BIT(4)) |
| 81 | #define CHG_CTRL_APSD_EN_BIT BIT(2) |
| 82 | #define CHG_CTRL_APSD_EN_MASK BIT(2) |
| 83 | #define CHG_ITERM_MASK 0x07 |
| 84 | #define CHG_PIN_CTRL_USBCS_REG_BIT 0x0 |
| 85 | /* This is to select if use external pin EN to control CHG */ |
| 86 | #define CHG_PIN_CTRL_CHG_EN_LOW_PIN_BIT (BIT(5) | BIT(6)) |
| 87 | #define CHG_PIN_CTRL_CHG_EN_LOW_REG_BIT 0x0 |
| 88 | #define CHG_PIN_CTRL_CHG_EN_MASK (BIT(5) | BIT(6)) |
| 89 | |
| 90 | #define CHG_PIN_CTRL_USBCS_REG_MASK BIT(4) |
| 91 | #define CHG_PIN_CTRL_APSD_IRQ_BIT BIT(1) |
| 92 | #define CHG_PIN_CTRL_APSD_IRQ_MASK BIT(1) |
| 93 | #define CHG_PIN_CTRL_CHG_ERR_IRQ_BIT BIT(2) |
| 94 | #define CHG_PIN_CTRL_CHG_ERR_IRQ_MASK BIT(2) |
| 95 | #define VARIOUS_FUNC_USB_SUSP_EN_REG_BIT BIT(6) |
| 96 | #define VARIOUS_FUNC_USB_SUSP_MASK BIT(6) |
| 97 | #define FAULT_INT_HOT_COLD_HARD_BIT BIT(7) |
| 98 | #define FAULT_INT_HOT_COLD_SOFT_BIT BIT(6) |
| 99 | #define FAULT_INT_INPUT_OV_BIT BIT(3) |
| 100 | #define FAULT_INT_INPUT_UV_BIT BIT(2) |
| 101 | #define FAULT_INT_AICL_COMPLETE_BIT BIT(1) |
| 102 | #define STATUS_INT_CHG_TIMEOUT_BIT BIT(7) |
| 103 | #define STATUS_INT_OTG_DETECT_BIT BIT(6) |
| 104 | #define STATUS_INT_BATT_OV_BIT BIT(5) |
| 105 | #define STATUS_INT_CHGING_BIT BIT(4) |
| 106 | #define STATUS_INT_CHG_INHI_BIT BIT(3) |
| 107 | #define STATUS_INT_INOK_BIT BIT(2) |
| 108 | #define STATUS_INT_MISSING_BATT_BIT BIT(1) |
| 109 | #define STATUS_INT_LOW_BATT_BIT BIT(0) |
| 110 | #define THERM_A_THERM_MONITOR_EN_BIT 0x0 |
| 111 | #define THERM_A_THERM_MONITOR_EN_MASK BIT(4) |
| 112 | #define VFLOAT_MASK 0x3F |
| 113 | |
| 114 | /* IRQ status bits */ |
| 115 | #define IRQ_A_HOT_HARD_BIT BIT(6) |
| 116 | #define IRQ_A_COLD_HARD_BIT BIT(4) |
| 117 | #define IRQ_A_HOT_SOFT_BIT BIT(2) |
| 118 | #define IRQ_A_COLD_SOFT_BIT BIT(0) |
| 119 | #define IRQ_B_BATT_MISSING_BIT BIT(4) |
| 120 | #define IRQ_B_BATT_LOW_BIT BIT(2) |
| 121 | #define IRQ_B_BATT_OV_BIT BIT(6) |
| 122 | #define IRQ_B_PRE_FAST_CHG_BIT BIT(0) |
| 123 | #define IRQ_C_TAPER_CHG_BIT BIT(2) |
| 124 | #define IRQ_C_TERM_BIT BIT(0) |
| 125 | #define IRQ_C_INT_OVER_TEMP_BIT BIT(6) |
| 126 | #define IRQ_D_CHG_TIMEOUT_BIT (BIT(0) | BIT(2)) |
| 127 | #define IRQ_D_AICL_DONE_BIT BIT(4) |
| 128 | #define IRQ_D_APSD_COMPLETE BIT(6) |
| 129 | #define IRQ_E_INPUT_UV_BIT BIT(0) |
| 130 | #define IRQ_E_INPUT_OV_BIT BIT(2) |
| 131 | #define IRQ_E_AFVC_ACTIVE BIT(4) |
| 132 | #define IRQ_F_OTG_VALID_BIT BIT(2) |
| 133 | #define IRQ_F_OTG_BATT_FAIL_BIT BIT(4) |
| 134 | #define IRQ_F_OTG_OC_BIT BIT(6) |
| 135 | #define IRQ_F_POWER_OK BIT(0) |
| 136 | |
| 137 | /* Status bits */ |
| 138 | #define STATUS_C_CHARGING_MASK (BIT(1) | BIT(2)) |
| 139 | #define STATUS_C_FAST_CHARGING BIT(2) |
| 140 | #define STATUS_C_PRE_CHARGING BIT(1) |
| 141 | #define STATUS_C_TAPER_CHARGING (BIT(2) | BIT(1)) |
| 142 | #define STATUS_C_CHG_ERR_STATUS_BIT BIT(6) |
| 143 | #define STATUS_C_CHG_ENABLE_STATUS_BIT BIT(0) |
| 144 | #define STATUS_C_CHG_HOLD_OFF_BIT BIT(3) |
| 145 | #define STATUS_D_PORT_OTHER BIT(0) |
| 146 | #define STATUS_D_PORT_SDP BIT(1) |
| 147 | #define STATUS_D_PORT_DCP BIT(2) |
| 148 | #define STATUS_D_PORT_CDP BIT(3) |
| 149 | #define STATUS_D_PORT_ACA_A BIT(4) |
| 150 | #define STATUS_D_PORT_ACA_B BIT(5) |
| 151 | #define STATUS_D_PORT_ACA_C BIT(6) |
| 152 | #define STATUS_D_PORT_ACA_DOCK BIT(7) |
| 153 | |
| 154 | /* constants */ |
| 155 | #define USB2_MIN_CURRENT_MA 100 |
| 156 | #define USB2_MAX_CURRENT_MA 500 |
| 157 | #define USB3_MIN_CURRENT_MA 150 |
| 158 | #define USB3_MAX_CURRENT_MA 900 |
| 159 | #define AC_CHG_CURRENT_MASK 0x70 |
| 160 | #define AC_CHG_CURRENT_SHIFT 4 |
| 161 | #define SMB358_IRQ_REG_COUNT 6 |
| 162 | #define SMB358_FAST_CHG_MIN_MA 200 |
| 163 | #define SMB358_FAST_CHG_MAX_MA 2000 |
| 164 | #define SMB358_FAST_CHG_SHIFT 5 |
| 165 | #define SMB_FAST_CHG_CURRENT_MASK 0xE0 |
| 166 | #define SMB358_DEFAULT_BATT_CAPACITY 50 |
| 167 | |
| 168 | enum { |
| 169 | USER = BIT(0), |
| 170 | THERMAL = BIT(1), |
| 171 | CURRENT = BIT(2), |
| 172 | }; |
| 173 | |
| 174 | struct smb358_regulator { |
| 175 | struct regulator_desc rdesc; |
| 176 | struct regulator_dev *rdev; |
| 177 | }; |
| 178 | |
| 179 | struct smb358_charger { |
| 180 | struct i2c_client *client; |
| 181 | struct device *dev; |
| 182 | |
| 183 | bool recharge_disabled; |
| 184 | int recharge_mv; |
| 185 | bool iterm_disabled; |
| 186 | int iterm_ma; |
| 187 | int vfloat_mv; |
| 188 | int chg_valid_gpio; |
| 189 | int chg_valid_act_low; |
| 190 | int chg_present; |
| 191 | int fake_battery_soc; |
| 192 | bool chg_autonomous_mode; |
| 193 | bool disable_apsd; |
| 194 | bool using_pmic_therm; |
| 195 | bool battery_missing; |
| 196 | const char *bms_psy_name; |
| 197 | bool resume_completed; |
| 198 | bool irq_waiting; |
| 199 | struct mutex read_write_lock; |
| 200 | struct mutex path_suspend_lock; |
| 201 | struct mutex irq_complete; |
| 202 | u8 irq_cfg_mask[2]; |
| 203 | int irq_gpio; |
| 204 | int charging_disabled; |
| 205 | int fastchg_current_max_ma; |
| 206 | |
| 207 | /* debugfs related */ |
| 208 | #if defined(CONFIG_DEBUG_FS) |
| 209 | struct dentry *debug_root; |
| 210 | u32 peek_poke_address; |
| 211 | #endif |
| 212 | /* status tracking */ |
| 213 | bool batt_full; |
| 214 | bool batt_hot; |
| 215 | bool batt_cold; |
| 216 | bool batt_warm; |
| 217 | bool batt_cool; |
| 218 | int charging_disabled_status; |
| 219 | int usb_suspended; |
| 220 | |
| 221 | /* power supply */ |
| 222 | struct power_supply *usb_psy; |
| 223 | struct power_supply *bms_psy; |
| 224 | struct power_supply batt_psy; |
| 225 | |
| 226 | /* otg 5V regulator */ |
| 227 | struct smb358_regulator otg_vreg; |
| 228 | |
| 229 | /* adc_tm paramters */ |
| 230 | struct qpnp_vadc_chip *vadc_dev; |
| 231 | struct qpnp_adc_tm_chip *adc_tm_dev; |
| 232 | struct qpnp_adc_tm_btm_param adc_param; |
| 233 | int cold_bat_decidegc; |
| 234 | int hot_bat_decidegc; |
| 235 | int bat_present_decidegc; |
| 236 | /* i2c pull up regulator */ |
| 237 | struct regulator *vcc_i2c; |
| 238 | }; |
| 239 | |
| 240 | struct smb_irq_info { |
| 241 | const char *name; |
| 242 | int (*smb_irq)(struct smb358_charger *chip, |
| 243 | u8 rt_stat); |
| 244 | int high; |
| 245 | int low; |
| 246 | }; |
| 247 | |
| 248 | struct irq_handler_info { |
| 249 | u8 stat_reg; |
| 250 | u8 val; |
| 251 | u8 prev_val; |
| 252 | struct smb_irq_info irq_info[4]; |
| 253 | }; |
| 254 | |
| 255 | static int chg_current[] = { |
| 256 | 300, 500, 700, 1000, 1200, 1500, 1800, 2000, |
| 257 | }; |
| 258 | |
| 259 | static int fast_chg_current[] = { |
| 260 | 200, 450, 600, 900, 1300, 1500, 1800, 2000, |
| 261 | }; |
| 262 | |
| 263 | /* add supplied to "bms" function */ |
| 264 | static char *pm_batt_supplied_to[] = { |
| 265 | "bms", |
| 266 | }; |
| 267 | |
| 268 | static int __smb358_read_reg(struct smb358_charger *chip, u8 reg, u8 *val) |
| 269 | { |
| 270 | s32 ret; |
| 271 | |
| 272 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
| 273 | if (ret < 0) { |
| 274 | dev_err(chip->dev, |
| 275 | "i2c read fail: can't read from %02x: %d\n", reg, ret); |
| 276 | return ret; |
| 277 | } else { |
| 278 | *val = ret; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static int __smb358_write_reg(struct smb358_charger *chip, int reg, u8 val) |
| 285 | { |
| 286 | s32 ret; |
| 287 | |
| 288 | ret = i2c_smbus_write_byte_data(chip->client, reg, val); |
| 289 | if (ret < 0) { |
| 290 | dev_err(chip->dev, |
| 291 | "i2c write fail: can't write %02x to %02x: %d\n", |
| 292 | val, reg, ret); |
| 293 | return ret; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int smb358_read_reg(struct smb358_charger *chip, int reg, |
| 299 | u8 *val) |
| 300 | { |
| 301 | int rc; |
| 302 | |
| 303 | mutex_lock(&chip->read_write_lock); |
| 304 | rc = __smb358_read_reg(chip, reg, val); |
| 305 | mutex_unlock(&chip->read_write_lock); |
| 306 | |
| 307 | return rc; |
| 308 | } |
| 309 | |
| 310 | static int smb358_write_reg(struct smb358_charger *chip, int reg, |
| 311 | u8 val) |
| 312 | { |
| 313 | int rc; |
| 314 | |
| 315 | mutex_lock(&chip->read_write_lock); |
| 316 | rc = __smb358_write_reg(chip, reg, val); |
| 317 | mutex_unlock(&chip->read_write_lock); |
| 318 | |
| 319 | return rc; |
| 320 | } |
| 321 | |
| 322 | static int smb358_masked_write(struct smb358_charger *chip, int reg, |
| 323 | u8 mask, u8 val) |
| 324 | { |
| 325 | s32 rc; |
| 326 | u8 temp; |
| 327 | |
| 328 | mutex_lock(&chip->read_write_lock); |
| 329 | rc = __smb358_read_reg(chip, reg, &temp); |
| 330 | if (rc) { |
| 331 | dev_err(chip->dev, |
| 332 | "smb358_read_reg Failed: reg=%03X, rc=%d\n", reg, rc); |
| 333 | goto out; |
| 334 | } |
| 335 | temp &= ~mask; |
| 336 | temp |= val & mask; |
| 337 | rc = __smb358_write_reg(chip, reg, temp); |
| 338 | if (rc) { |
| 339 | dev_err(chip->dev, |
| 340 | "smb358_write Failed: reg=%03X, rc=%d\n", reg, rc); |
| 341 | } |
| 342 | out: |
| 343 | mutex_unlock(&chip->read_write_lock); |
| 344 | return rc; |
| 345 | } |
| 346 | |
| 347 | static int smb358_enable_volatile_writes(struct smb358_charger *chip) |
| 348 | { |
| 349 | int rc; |
| 350 | |
| 351 | rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_VOLATILE_W_PERM_BIT, |
| 352 | CMD_A_VOLATILE_W_PERM_BIT); |
| 353 | if (rc) |
| 354 | dev_err(chip->dev, "Couldn't write VOLATILE_W_PERM_BIT rc=%d\n", |
| 355 | rc); |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | |
| 360 | static int smb358_fastchg_current_set(struct smb358_charger *chip) |
| 361 | { |
| 362 | int i; |
| 363 | |
| 364 | if ((chip->fastchg_current_max_ma < SMB358_FAST_CHG_MIN_MA) || |
| 365 | (chip->fastchg_current_max_ma > SMB358_FAST_CHG_MAX_MA)) { |
| 366 | dev_dbg(chip->dev, "bad fastchg current mA=%d asked to set\n", |
| 367 | chip->fastchg_current_max_ma); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
| 371 | for (i = ARRAY_SIZE(fast_chg_current) - 1; i >= 0; i--) { |
| 372 | if (fast_chg_current[i] <= chip->fastchg_current_max_ma) |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | if (i < 0) { |
| 377 | dev_err(chip->dev, "Invalid current setting %dmA\n", |
| 378 | chip->fastchg_current_max_ma); |
| 379 | i = 0; |
| 380 | } |
| 381 | |
| 382 | i = i << SMB358_FAST_CHG_SHIFT; |
| 383 | dev_dbg(chip->dev, "fastchg limit=%d setting %02x\n", |
| 384 | chip->fastchg_current_max_ma, i); |
| 385 | |
| 386 | return smb358_masked_write(chip, CHG_CURRENT_CTRL_REG, |
| 387 | SMB_FAST_CHG_CURRENT_MASK, i); |
| 388 | } |
| 389 | |
| 390 | #define MIN_FLOAT_MV 3500 |
| 391 | #define MAX_FLOAT_MV 4500 |
| 392 | #define VFLOAT_STEP_MV 20 |
| 393 | #define VFLOAT_4350MV 4350 |
| 394 | static int smb358_float_voltage_set(struct smb358_charger *chip, int vfloat_mv) |
| 395 | { |
| 396 | u8 temp; |
| 397 | |
| 398 | if ((vfloat_mv < MIN_FLOAT_MV) || (vfloat_mv > MAX_FLOAT_MV)) { |
| 399 | dev_err(chip->dev, "bad float voltage mv =%d asked to set\n", |
| 400 | vfloat_mv); |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | |
| 404 | if (VFLOAT_4350MV == vfloat_mv) |
| 405 | temp = 0x2B; |
| 406 | else if (vfloat_mv > VFLOAT_4350MV) |
| 407 | temp = (vfloat_mv - MIN_FLOAT_MV) / VFLOAT_STEP_MV - 1; |
| 408 | else |
| 409 | temp = (vfloat_mv - MIN_FLOAT_MV) / VFLOAT_STEP_MV; |
| 410 | |
| 411 | return smb358_masked_write(chip, VFLOAT_REG, VFLOAT_MASK, temp); |
| 412 | } |
| 413 | |
| 414 | #define CHG_ITERM_30MA 0x00 |
| 415 | #define CHG_ITERM_40MA 0x01 |
| 416 | #define CHG_ITERM_60MA 0x02 |
| 417 | #define CHG_ITERM_80MA 0x03 |
| 418 | #define CHG_ITERM_100MA 0x04 |
| 419 | #define CHG_ITERM_125MA 0x05 |
| 420 | #define CHG_ITERM_150MA 0x06 |
| 421 | #define CHG_ITERM_200MA 0x07 |
| 422 | static int smb358_term_current_set(struct smb358_charger *chip) |
| 423 | { |
| 424 | u8 reg = 0; |
| 425 | int rc; |
| 426 | |
| 427 | if (chip->iterm_ma != -EINVAL) { |
| 428 | if (chip->iterm_disabled) |
| 429 | dev_err(chip->dev, "Error: Both iterm_disabled and iterm_ma set\n"); |
| 430 | |
| 431 | if (chip->iterm_ma <= 30) |
| 432 | reg = CHG_ITERM_30MA; |
| 433 | else if (chip->iterm_ma <= 40) |
| 434 | reg = CHG_ITERM_40MA; |
| 435 | else if (chip->iterm_ma <= 60) |
| 436 | reg = CHG_ITERM_60MA; |
| 437 | else if (chip->iterm_ma <= 80) |
| 438 | reg = CHG_ITERM_80MA; |
| 439 | else if (chip->iterm_ma <= 100) |
| 440 | reg = CHG_ITERM_100MA; |
| 441 | else if (chip->iterm_ma <= 125) |
| 442 | reg = CHG_ITERM_125MA; |
| 443 | else if (chip->iterm_ma <= 150) |
| 444 | reg = CHG_ITERM_150MA; |
| 445 | else |
| 446 | reg = CHG_ITERM_200MA; |
| 447 | |
| 448 | rc = smb358_masked_write(chip, CHG_CURRENT_CTRL_REG, |
| 449 | CHG_ITERM_MASK, reg); |
| 450 | if (rc) { |
| 451 | dev_err(chip->dev, |
| 452 | "Couldn't set iterm rc = %d\n", rc); |
| 453 | return rc; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (chip->iterm_disabled) { |
| 458 | rc = smb358_masked_write(chip, CHG_CTRL_REG, |
| 459 | CHG_CTRL_CURR_TERM_END_MASK, |
| 460 | CHG_CTRL_CURR_TERM_END_MASK); |
| 461 | if (rc) { |
| 462 | dev_err(chip->dev, "Couldn't set iterm rc = %d\n", |
| 463 | rc); |
| 464 | return rc; |
| 465 | } |
| 466 | } else { |
| 467 | rc = smb358_masked_write(chip, CHG_CTRL_REG, |
| 468 | CHG_CTRL_CURR_TERM_END_MASK, 0); |
| 469 | if (rc) { |
| 470 | dev_err(chip->dev, |
| 471 | "Couldn't enable iterm rc = %d\n", rc); |
| 472 | return rc; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | #define VFLT_300MV 0x0C |
| 480 | #define VFLT_200MV 0x08 |
| 481 | #define VFLT_100MV 0x04 |
| 482 | #define VFLT_50MV 0x00 |
| 483 | #define VFLT_MASK 0x0C |
| 484 | static int smb358_recharge_set(struct smb358_charger *chip) |
| 485 | { |
| 486 | u8 reg = 0; |
| 487 | int rc; |
| 488 | |
| 489 | if (chip->recharge_disabled) |
| 490 | rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG, |
| 491 | CHG_INHI_EN_MASK, 0x0); |
| 492 | else |
| 493 | rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG, |
| 494 | CHG_INHI_EN_MASK, CHG_INHI_EN_BIT); |
| 495 | if (rc) { |
| 496 | dev_err(chip->dev, |
| 497 | "Couldn't set inhibit en reg rc = %d\n", rc); |
| 498 | return rc; |
| 499 | } |
| 500 | |
| 501 | if (chip->recharge_mv != -EINVAL) { |
| 502 | if (chip->recharge_mv <= 50) |
| 503 | reg = VFLT_50MV; |
| 504 | else if (chip->recharge_mv <= 100) |
| 505 | reg = VFLT_100MV; |
| 506 | else if (chip->recharge_mv <= 200) |
| 507 | reg = VFLT_200MV; |
| 508 | else |
| 509 | reg = VFLT_300MV; |
| 510 | |
| 511 | rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG, |
| 512 | VFLT_MASK, reg); |
| 513 | if (rc) { |
| 514 | dev_err(chip->dev, |
| 515 | "Couldn't set inhibit threshold rc = %d\n", rc); |
| 516 | return rc; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int smb358_chg_otg_regulator_enable(struct regulator_dev *rdev) |
| 524 | { |
| 525 | int rc = 0; |
| 526 | struct smb358_charger *chip = rdev_get_drvdata(rdev); |
| 527 | |
| 528 | rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_OTG_ENABLE_BIT, |
| 529 | CMD_A_OTG_ENABLE_BIT); |
| 530 | if (rc) |
| 531 | dev_err(chip->dev, "Couldn't enable OTG mode rc=%d, reg=%2x\n", |
| 532 | rc, CMD_A_REG); |
| 533 | return rc; |
| 534 | } |
| 535 | |
| 536 | static int smb358_chg_otg_regulator_disable(struct regulator_dev *rdev) |
| 537 | { |
| 538 | int rc = 0; |
| 539 | struct smb358_charger *chip = rdev_get_drvdata(rdev); |
| 540 | |
| 541 | rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_OTG_ENABLE_BIT, 0); |
| 542 | if (rc) |
| 543 | dev_err(chip->dev, "Couldn't disable OTG mode rc=%d, reg=%2x\n", |
| 544 | rc, CMD_A_REG); |
| 545 | return rc; |
| 546 | } |
| 547 | |
| 548 | static int smb358_chg_otg_regulator_is_enable(struct regulator_dev *rdev) |
| 549 | { |
| 550 | int rc = 0; |
| 551 | u8 reg = 0; |
| 552 | struct smb358_charger *chip = rdev_get_drvdata(rdev); |
| 553 | |
| 554 | rc = smb358_read_reg(chip, CMD_A_REG, ®); |
| 555 | if (rc) { |
| 556 | dev_err(chip->dev, |
| 557 | "Couldn't read OTG enable bit rc=%d, reg=%2x\n", |
| 558 | rc, CMD_A_REG); |
| 559 | return rc; |
| 560 | } |
| 561 | |
| 562 | return (reg & CMD_A_OTG_ENABLE_BIT) ? 1 : 0; |
| 563 | } |
| 564 | |
| 565 | struct regulator_ops smb358_chg_otg_reg_ops = { |
| 566 | .enable = smb358_chg_otg_regulator_enable, |
| 567 | .disable = smb358_chg_otg_regulator_disable, |
| 568 | .is_enabled = smb358_chg_otg_regulator_is_enable, |
| 569 | }; |
| 570 | |
| 571 | static int smb358_regulator_init(struct smb358_charger *chip) |
| 572 | { |
| 573 | int rc = 0; |
| 574 | struct regulator_init_data *init_data; |
| 575 | struct regulator_config cfg; |
| 576 | |
| 577 | init_data = of_get_regulator_init_data(chip->dev, chip->dev->of_node); |
| 578 | if (!init_data) { |
| 579 | dev_err(chip->dev, "Get regulator init data failed\n"); |
| 580 | return -EINVAL; |
| 581 | } |
| 582 | |
| 583 | /* Give the name, then will register */ |
| 584 | if (init_data->constraints.name) { |
| 585 | chip->otg_vreg.rdesc.owner = THIS_MODULE; |
| 586 | chip->otg_vreg.rdesc.type = REGULATOR_VOLTAGE; |
| 587 | chip->otg_vreg.rdesc.ops = &smb358_chg_otg_reg_ops; |
| 588 | chip->otg_vreg.rdesc.name = init_data->constraints.name; |
| 589 | |
| 590 | cfg.dev = chip->dev; |
| 591 | cfg.init_data = init_data; |
| 592 | cfg.driver_data = chip; |
| 593 | cfg.of_node = chip->dev->of_node; |
| 594 | |
| 595 | init_data->constraints.valid_ops_mask |
| 596 | |= REGULATOR_CHANGE_STATUS; |
| 597 | |
| 598 | chip->otg_vreg.rdev = regulator_register( |
| 599 | &chip->otg_vreg.rdesc, &cfg); |
| 600 | if (IS_ERR(chip->otg_vreg.rdev)) { |
| 601 | rc = PTR_ERR(chip->otg_vreg.rdev); |
| 602 | chip->otg_vreg.rdev = NULL; |
| 603 | if (rc != -EPROBE_DEFER) |
| 604 | dev_err(chip->dev, |
| 605 | "OTG reg failed, rc=%d\n", rc); |
| 606 | } |
| 607 | } |
| 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | static int __smb358_charging_disable(struct smb358_charger *chip, bool disable) |
| 612 | { |
| 613 | int rc; |
| 614 | |
| 615 | rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_CHG_ENABLE_BIT, |
| 616 | disable ? 0 : CMD_A_CHG_ENABLE_BIT); |
| 617 | if (rc < 0) |
| 618 | pr_err("Couldn't set CHG_ENABLE_BIT diable = %d, rc = %d\n", |
| 619 | disable, rc); |
| 620 | return rc; |
| 621 | } |
| 622 | |
| 623 | static int smb358_charging_disable(struct smb358_charger *chip, |
| 624 | int reason, int disable) |
| 625 | { |
| 626 | int rc = 0; |
| 627 | int disabled; |
| 628 | |
| 629 | disabled = chip->charging_disabled_status; |
| 630 | |
| 631 | pr_debug("reason = %d requested_disable = %d disabled_status = %d\n", |
| 632 | reason, disable, disabled); |
| 633 | |
| 634 | if (disable == true) |
| 635 | disabled |= reason; |
| 636 | else |
| 637 | disabled &= ~reason; |
| 638 | |
| 639 | if (!!disabled == !!chip->charging_disabled_status) |
| 640 | goto skip; |
| 641 | |
| 642 | rc = __smb358_charging_disable(chip, !!disabled); |
| 643 | if (rc) { |
| 644 | pr_err("Failed to disable charging rc = %d\n", rc); |
| 645 | return rc; |
| 646 | } else { |
| 647 | /* will not modify online status in this condition */ |
| 648 | power_supply_changed(&chip->batt_psy); |
| 649 | } |
| 650 | |
| 651 | skip: |
| 652 | chip->charging_disabled_status = disabled; |
| 653 | return rc; |
| 654 | } |
| 655 | |
| 656 | static int smb358_hw_init(struct smb358_charger *chip) |
| 657 | { |
| 658 | int rc; |
| 659 | u8 reg = 0, mask = 0; |
| 660 | |
| 661 | /* |
| 662 | * If the charger is pre-configured for autonomous operation, |
| 663 | * do not apply additonal settings |
| 664 | */ |
| 665 | if (chip->chg_autonomous_mode) { |
| 666 | dev_dbg(chip->dev, "Charger configured for autonomous mode\n"); |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | rc = smb358_enable_volatile_writes(chip); |
| 671 | if (rc) { |
| 672 | dev_err(chip->dev, "Couldn't configure volatile writes rc=%d\n", |
| 673 | rc); |
| 674 | return rc; |
| 675 | } |
| 676 | |
| 677 | /* setup defaults for CHG_CNTRL_REG */ |
| 678 | reg = CHG_CTRL_BATT_MISSING_DET_THERM_IO; |
| 679 | mask = CHG_CTRL_BATT_MISSING_DET_MASK; |
| 680 | rc = smb358_masked_write(chip, CHG_CTRL_REG, mask, reg); |
| 681 | if (rc) { |
| 682 | dev_err(chip->dev, "Couldn't set CHG_CTRL_REG rc=%d\n", rc); |
| 683 | return rc; |
| 684 | } |
| 685 | /* setup defaults for PIN_CTRL_REG */ |
| 686 | reg = CHG_PIN_CTRL_USBCS_REG_BIT | CHG_PIN_CTRL_CHG_EN_LOW_REG_BIT | |
| 687 | CHG_PIN_CTRL_APSD_IRQ_BIT | CHG_PIN_CTRL_CHG_ERR_IRQ_BIT; |
| 688 | mask = CHG_PIN_CTRL_CHG_EN_MASK | CHG_PIN_CTRL_USBCS_REG_MASK | |
| 689 | CHG_PIN_CTRL_APSD_IRQ_MASK | CHG_PIN_CTRL_CHG_ERR_IRQ_MASK; |
| 690 | rc = smb358_masked_write(chip, CHG_PIN_EN_CTRL_REG, mask, reg); |
| 691 | if (rc) { |
| 692 | dev_err(chip->dev, "Couldn't set CHG_PIN_EN_CTRL_REG rc=%d\n", |
| 693 | rc); |
| 694 | return rc; |
| 695 | } |
| 696 | |
| 697 | /* setup USB suspend and APSD */ |
| 698 | rc = smb358_masked_write(chip, VARIOUS_FUNC_REG, |
| 699 | VARIOUS_FUNC_USB_SUSP_MASK, VARIOUS_FUNC_USB_SUSP_EN_REG_BIT); |
| 700 | if (rc) { |
| 701 | dev_err(chip->dev, "Couldn't set VARIOUS_FUNC_REG rc=%d\n", |
| 702 | rc); |
| 703 | return rc; |
| 704 | } |
| 705 | |
| 706 | if (!chip->disable_apsd) |
| 707 | reg = CHG_CTRL_APSD_EN_BIT; |
| 708 | rc = smb358_masked_write(chip, CHG_CTRL_REG, |
| 709 | CHG_CTRL_APSD_EN_MASK, reg); |
| 710 | if (rc) { |
| 711 | dev_err(chip->dev, "Couldn't set CHG_CTRL_REG rc=%d\n", |
| 712 | rc); |
| 713 | return rc; |
| 714 | } |
| 715 | /* Fault and Status IRQ configuration */ |
| 716 | reg = FAULT_INT_HOT_COLD_HARD_BIT | FAULT_INT_HOT_COLD_SOFT_BIT |
| 717 | | FAULT_INT_INPUT_UV_BIT | FAULT_INT_AICL_COMPLETE_BIT |
| 718 | | FAULT_INT_INPUT_OV_BIT; |
| 719 | rc = smb358_write_reg(chip, FAULT_INT_REG, reg); |
| 720 | if (rc) { |
| 721 | dev_err(chip->dev, "Couldn't set FAULT_INT_REG rc=%d\n", rc); |
| 722 | return rc; |
| 723 | } |
| 724 | reg = STATUS_INT_CHG_TIMEOUT_BIT | STATUS_INT_OTG_DETECT_BIT | |
| 725 | STATUS_INT_BATT_OV_BIT | STATUS_INT_CHGING_BIT | |
| 726 | STATUS_INT_CHG_INHI_BIT | STATUS_INT_INOK_BIT | |
| 727 | STATUS_INT_LOW_BATT_BIT | STATUS_INT_MISSING_BATT_BIT; |
| 728 | rc = smb358_write_reg(chip, STATUS_INT_REG, reg); |
| 729 | if (rc) { |
| 730 | dev_err(chip->dev, "Couldn't set STATUS_INT_REG rc=%d\n", rc); |
| 731 | return rc; |
| 732 | } |
| 733 | /* setup THERM Monitor */ |
| 734 | rc = smb358_masked_write(chip, THERM_A_CTRL_REG, |
| 735 | THERM_A_THERM_MONITOR_EN_MASK, THERM_A_THERM_MONITOR_EN_BIT); |
| 736 | if (rc) { |
| 737 | dev_err(chip->dev, "Couldn't set THERM_A_CTRL_REG rc=%d\n", |
| 738 | rc); |
| 739 | return rc; |
| 740 | } |
| 741 | /* set the fast charge current limit */ |
| 742 | rc = smb358_fastchg_current_set(chip); |
| 743 | if (rc) { |
| 744 | dev_err(chip->dev, "Couldn't set fastchg current rc=%d\n", rc); |
| 745 | return rc; |
| 746 | } |
| 747 | |
| 748 | /* set the float voltage */ |
| 749 | rc = smb358_float_voltage_set(chip, chip->vfloat_mv); |
| 750 | if (rc < 0) { |
| 751 | dev_err(chip->dev, |
| 752 | "Couldn't set float voltage rc = %d\n", rc); |
| 753 | return rc; |
| 754 | } |
| 755 | |
| 756 | /* set iterm */ |
| 757 | rc = smb358_term_current_set(chip); |
| 758 | if (rc) |
| 759 | dev_err(chip->dev, "Couldn't set term current rc=%d\n", rc); |
| 760 | |
| 761 | /* set recharge */ |
| 762 | rc = smb358_recharge_set(chip); |
| 763 | if (rc) |
| 764 | dev_err(chip->dev, "Couldn't set recharge para rc=%d\n", rc); |
| 765 | |
| 766 | /* enable/disable charging */ |
| 767 | rc = smb358_charging_disable(chip, USER, !!chip->charging_disabled); |
| 768 | if (rc) |
| 769 | dev_err(chip->dev, "Couldn't '%s' charging rc = %d\n", |
| 770 | chip->charging_disabled ? "disable" : "enable", rc); |
| 771 | |
| 772 | return rc; |
| 773 | } |
| 774 | |
| 775 | static enum power_supply_property smb358_battery_properties[] = { |
| 776 | POWER_SUPPLY_PROP_STATUS, |
| 777 | POWER_SUPPLY_PROP_PRESENT, |
| 778 | POWER_SUPPLY_PROP_CHARGING_ENABLED, |
| 779 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 780 | POWER_SUPPLY_PROP_CAPACITY, |
| 781 | POWER_SUPPLY_PROP_HEALTH, |
| 782 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 783 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 784 | POWER_SUPPLY_PROP_TEMP, |
| 785 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 786 | }; |
| 787 | |
| 788 | static int smb358_get_prop_batt_status(struct smb358_charger *chip) |
| 789 | { |
| 790 | int rc; |
| 791 | u8 reg = 0; |
| 792 | |
| 793 | if (chip->batt_full) |
| 794 | return POWER_SUPPLY_STATUS_FULL; |
| 795 | |
| 796 | rc = smb358_read_reg(chip, STATUS_C_REG, ®); |
| 797 | if (rc) { |
| 798 | dev_err(chip->dev, "Couldn't read STAT_C rc = %d\n", rc); |
| 799 | return POWER_SUPPLY_STATUS_UNKNOWN; |
| 800 | } |
| 801 | |
| 802 | dev_dbg(chip->dev, "%s: STATUS_C_REG=%x\n", __func__, reg); |
| 803 | |
| 804 | if (reg & STATUS_C_CHG_HOLD_OFF_BIT) |
| 805 | return POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 806 | |
| 807 | if ((reg & STATUS_C_CHARGING_MASK) && |
| 808 | !(reg & STATUS_C_CHG_ERR_STATUS_BIT)) |
| 809 | return POWER_SUPPLY_STATUS_CHARGING; |
| 810 | |
| 811 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 812 | } |
| 813 | |
| 814 | static int smb358_get_prop_batt_present(struct smb358_charger *chip) |
| 815 | { |
| 816 | return !chip->battery_missing; |
| 817 | } |
| 818 | |
| 819 | static int smb358_get_prop_batt_capacity(struct smb358_charger *chip) |
| 820 | { |
| 821 | union power_supply_propval ret = {0, }; |
| 822 | |
| 823 | if (chip->fake_battery_soc >= 0) |
| 824 | return chip->fake_battery_soc; |
| 825 | |
| 826 | if (chip->bms_psy) { |
| 827 | chip->bms_psy->get_property(chip->bms_psy, |
| 828 | POWER_SUPPLY_PROP_CAPACITY, &ret); |
| 829 | return ret.intval; |
| 830 | } |
| 831 | |
| 832 | dev_dbg(chip->dev, |
| 833 | "Couldn't get bms_psy, return default capacity\n"); |
| 834 | return SMB358_DEFAULT_BATT_CAPACITY; |
| 835 | } |
| 836 | |
| 837 | static int smb358_get_prop_charge_type(struct smb358_charger *chip) |
| 838 | { |
| 839 | int rc; |
| 840 | u8 reg = 0; |
| 841 | |
| 842 | rc = smb358_read_reg(chip, STATUS_C_REG, ®); |
| 843 | if (rc) { |
| 844 | dev_err(chip->dev, "Couldn't read STAT_C rc = %d\n", rc); |
| 845 | return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; |
| 846 | } |
| 847 | |
| 848 | dev_dbg(chip->dev, "%s: STATUS_C_REG=%x\n", __func__, reg); |
| 849 | |
| 850 | reg &= STATUS_C_CHARGING_MASK; |
| 851 | |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 852 | if (reg == STATUS_C_FAST_CHARGING) |
Zhenhua Huang | cff7499 | 2014-02-11 10:19:17 +0800 | [diff] [blame] | 853 | return POWER_SUPPLY_CHARGE_TYPE_FAST; |
Zhenhua Huang | 95a05d3 | 2014-03-31 18:09:45 +0800 | [diff] [blame] | 854 | else if (reg == STATUS_C_TAPER_CHARGING) |
| 855 | return POWER_SUPPLY_CHARGE_TYPE_TAPER; |
Zhenhua Huang | cff7499 | 2014-02-11 10:19:17 +0800 | [diff] [blame] | 856 | else if (reg == STATUS_C_PRE_CHARGING) |
| 857 | return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 858 | else |
| 859 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 860 | } |
| 861 | |
| 862 | static int smb358_get_prop_batt_health(struct smb358_charger *chip) |
| 863 | { |
| 864 | union power_supply_propval ret = {0, }; |
| 865 | |
| 866 | if (chip->batt_hot) |
| 867 | ret.intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 868 | else if (chip->batt_cold) |
| 869 | ret.intval = POWER_SUPPLY_HEALTH_COLD; |
| 870 | else if (chip->batt_warm) |
| 871 | ret.intval = POWER_SUPPLY_HEALTH_WARM; |
| 872 | else if (chip->batt_cool) |
| 873 | ret.intval = POWER_SUPPLY_HEALTH_COOL; |
| 874 | else |
| 875 | ret.intval = POWER_SUPPLY_HEALTH_GOOD; |
| 876 | |
| 877 | return ret.intval; |
| 878 | } |
| 879 | |
| 880 | #define DEFAULT_TEMP 250 |
| 881 | static int smb358_get_prop_batt_temp(struct smb358_charger *chip) |
| 882 | { |
| 883 | int rc = 0; |
| 884 | struct qpnp_vadc_result results; |
| 885 | |
| 886 | if (!smb358_get_prop_batt_present(chip)) |
| 887 | return DEFAULT_TEMP; |
| 888 | |
| 889 | rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &results); |
| 890 | if (rc) { |
| 891 | pr_debug("Unable to read batt temperature rc=%d\n", rc); |
| 892 | return DEFAULT_TEMP; |
| 893 | } |
| 894 | pr_debug("get_bat_temp %d, %lld\n", |
| 895 | results.adc_code, results.physical); |
| 896 | |
| 897 | return (int)results.physical; |
| 898 | } |
| 899 | |
| 900 | static int |
| 901 | smb358_get_prop_battery_voltage_now(struct smb358_charger *chip) |
| 902 | { |
| 903 | int rc = 0; |
| 904 | struct qpnp_vadc_result results; |
| 905 | |
| 906 | rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &results); |
| 907 | if (rc) { |
| 908 | pr_err("Unable to read vbat rc=%d\n", rc); |
| 909 | return 0; |
| 910 | } |
| 911 | return results.physical; |
| 912 | } |
| 913 | |
| 914 | static int __smb358_path_suspend(struct smb358_charger *chip, bool suspend) |
| 915 | { |
| 916 | int rc; |
| 917 | |
| 918 | rc = smb358_masked_write(chip, CMD_A_REG, |
| 919 | CMD_A_CHG_SUSP_EN_MASK, |
| 920 | suspend ? CMD_A_CHG_SUSP_EN_BIT : 0); |
| 921 | if (rc < 0) |
| 922 | dev_err(chip->dev, "Couldn't set CMD_A reg, rc = %d\n", rc); |
| 923 | return rc; |
| 924 | } |
| 925 | |
| 926 | static int smb358_path_suspend(struct smb358_charger *chip, int reason, |
| 927 | bool suspend) |
| 928 | { |
| 929 | int rc = 0; |
| 930 | int suspended; |
| 931 | |
| 932 | mutex_lock(&chip->path_suspend_lock); |
| 933 | suspended = chip->usb_suspended; |
| 934 | |
| 935 | if (suspend == false) |
| 936 | suspended &= ~reason; |
| 937 | else |
| 938 | suspended |= reason; |
| 939 | |
| 940 | if (!chip->usb_suspended && suspended) { |
| 941 | rc = __smb358_path_suspend(chip, true); |
| 942 | chip->usb_suspended = suspended; |
| 943 | power_supply_set_online(chip->usb_psy, !chip->usb_suspended); |
| 944 | power_supply_changed(chip->usb_psy); |
| 945 | } else if (chip->usb_suspended && !suspended) { |
| 946 | rc = __smb358_path_suspend(chip, false); |
| 947 | chip->usb_suspended = suspended; |
| 948 | power_supply_set_online(chip->usb_psy, !chip->usb_suspended); |
| 949 | power_supply_changed(chip->usb_psy); |
| 950 | } |
| 951 | |
| 952 | if (rc) |
| 953 | dev_err(chip->dev, "Couldn't set/unset suspend rc = %d\n", rc); |
| 954 | |
| 955 | mutex_unlock(&chip->path_suspend_lock); |
| 956 | return rc; |
| 957 | } |
| 958 | |
| 959 | static int smb358_set_usb_chg_current(struct smb358_charger *chip, |
| 960 | int current_ma) |
| 961 | { |
| 962 | int i, rc = 0; |
| 963 | u8 reg1 = 0, reg2 = 0, mask = 0; |
| 964 | |
| 965 | dev_dbg(chip->dev, "%s: USB current_ma = %d\n", __func__, current_ma); |
| 966 | |
| 967 | if (chip->chg_autonomous_mode) { |
| 968 | dev_dbg(chip->dev, "%s: Charger in autonmous mode\n", __func__); |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | if (current_ma < USB3_MIN_CURRENT_MA && current_ma != 2) |
| 973 | current_ma = USB2_MIN_CURRENT_MA; |
| 974 | |
| 975 | if (current_ma == USB2_MIN_CURRENT_MA) { |
| 976 | /* USB 2.0 - 100mA */ |
| 977 | reg1 &= ~USB3_ENABLE_BIT; |
| 978 | reg2 &= ~CMD_B_CHG_USB_500_900_ENABLE_BIT; |
| 979 | } else if (current_ma == USB2_MAX_CURRENT_MA) { |
| 980 | /* USB 2.0 - 500mA */ |
| 981 | reg1 &= ~USB3_ENABLE_BIT; |
| 982 | reg2 |= CMD_B_CHG_USB_500_900_ENABLE_BIT; |
| 983 | } else if (current_ma == USB3_MAX_CURRENT_MA) { |
| 984 | /* USB 3.0 - 900mA */ |
| 985 | reg1 |= USB3_ENABLE_BIT; |
| 986 | reg2 |= CMD_B_CHG_USB_500_900_ENABLE_BIT; |
| 987 | } else if (current_ma > USB2_MAX_CURRENT_MA) { |
| 988 | /* HC mode - if none of the above */ |
| 989 | reg2 |= CMD_B_CHG_HC_ENABLE_BIT; |
| 990 | |
| 991 | for (i = ARRAY_SIZE(chg_current) - 1; i >= 0; i--) { |
| 992 | if (chg_current[i] <= current_ma) |
| 993 | break; |
| 994 | } |
| 995 | if (i < 0) { |
| 996 | dev_err(chip->dev, "Cannot find %dmA\n", current_ma); |
| 997 | i = 0; |
| 998 | } |
| 999 | |
| 1000 | i = i << AC_CHG_CURRENT_SHIFT; |
| 1001 | rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG, |
| 1002 | AC_CHG_CURRENT_MASK, i); |
| 1003 | if (rc) |
| 1004 | dev_err(chip->dev, "Couldn't set input mA rc=%d\n", rc); |
| 1005 | } |
| 1006 | |
| 1007 | mask = CMD_B_CHG_HC_ENABLE_BIT | CMD_B_CHG_USB_500_900_ENABLE_BIT; |
| 1008 | rc = smb358_masked_write(chip, CMD_B_REG, mask, reg2); |
| 1009 | if (rc < 0) |
| 1010 | dev_err(chip->dev, "Couldn't set charging mode rc = %d\n", rc); |
| 1011 | |
| 1012 | mask = USB3_ENABLE_MASK; |
| 1013 | rc = smb358_masked_write(chip, SYSOK_AND_USB3_REG, mask, reg1); |
| 1014 | if (rc < 0) |
| 1015 | dev_err(chip->dev, "Couldn't set USB3 mode rc = %d\n", rc); |
| 1016 | |
| 1017 | /* Only set suspend bit when chg present and current_ma = 2 */ |
| 1018 | if (current_ma == 2 && chip->chg_present) { |
| 1019 | rc = smb358_path_suspend(chip, CURRENT, true); |
| 1020 | if (rc < 0) |
| 1021 | dev_err(chip->dev, "Couldn't suspend rc = %d\n", rc); |
| 1022 | } else { |
| 1023 | rc = smb358_path_suspend(chip, CURRENT, false); |
| 1024 | if (rc < 0) |
| 1025 | dev_err(chip->dev, "Couldn't set susp rc = %d\n", rc); |
| 1026 | } |
| 1027 | |
| 1028 | return rc; |
| 1029 | } |
| 1030 | |
| 1031 | static int |
| 1032 | smb358_batt_property_is_writeable(struct power_supply *psy, |
| 1033 | enum power_supply_property psp) |
| 1034 | { |
| 1035 | switch (psp) { |
| 1036 | case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
| 1037 | case POWER_SUPPLY_PROP_CAPACITY: |
| 1038 | return 1; |
| 1039 | default: |
| 1040 | break; |
| 1041 | } |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static int bound_soc(int soc) |
| 1047 | { |
| 1048 | soc = max(0, soc); |
| 1049 | soc = min(soc, 100); |
| 1050 | return soc; |
| 1051 | } |
| 1052 | |
| 1053 | static int smb358_battery_set_property(struct power_supply *psy, |
| 1054 | enum power_supply_property prop, |
| 1055 | const union power_supply_propval *val) |
| 1056 | { |
| 1057 | struct smb358_charger *chip = container_of(psy, |
| 1058 | struct smb358_charger, batt_psy); |
| 1059 | |
| 1060 | switch (prop) { |
| 1061 | case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
| 1062 | smb358_charging_disable(chip, USER, !val->intval); |
| 1063 | smb358_path_suspend(chip, USER, !val->intval); |
| 1064 | break; |
| 1065 | case POWER_SUPPLY_PROP_CAPACITY: |
| 1066 | chip->fake_battery_soc = bound_soc(val->intval); |
| 1067 | power_supply_changed(&chip->batt_psy); |
| 1068 | break; |
| 1069 | default: |
| 1070 | return -EINVAL; |
| 1071 | } |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | static int smb358_battery_get_property(struct power_supply *psy, |
| 1077 | enum power_supply_property prop, |
| 1078 | union power_supply_propval *val) |
| 1079 | { |
| 1080 | struct smb358_charger *chip = container_of(psy, |
| 1081 | struct smb358_charger, batt_psy); |
| 1082 | |
| 1083 | switch (prop) { |
| 1084 | case POWER_SUPPLY_PROP_STATUS: |
| 1085 | val->intval = smb358_get_prop_batt_status(chip); |
| 1086 | break; |
| 1087 | case POWER_SUPPLY_PROP_PRESENT: |
| 1088 | val->intval = smb358_get_prop_batt_present(chip); |
| 1089 | break; |
| 1090 | case POWER_SUPPLY_PROP_CAPACITY: |
| 1091 | val->intval = smb358_get_prop_batt_capacity(chip); |
| 1092 | break; |
| 1093 | case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
| 1094 | val->intval = !(chip->charging_disabled_status & USER); |
| 1095 | break; |
| 1096 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 1097 | val->intval = smb358_get_prop_charge_type(chip); |
| 1098 | break; |
| 1099 | case POWER_SUPPLY_PROP_HEALTH: |
| 1100 | val->intval = smb358_get_prop_batt_health(chip); |
| 1101 | break; |
| 1102 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 1103 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 1104 | break; |
| 1105 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 1106 | val->strval = "SMB358"; |
| 1107 | break; |
| 1108 | case POWER_SUPPLY_PROP_TEMP: |
| 1109 | val->intval = smb358_get_prop_batt_temp(chip); |
| 1110 | break; |
| 1111 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 1112 | val->intval = smb358_get_prop_battery_voltage_now(chip); |
| 1113 | break; |
| 1114 | default: |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int apsd_complete(struct smb358_charger *chip, u8 status) |
| 1121 | { |
| 1122 | int rc; |
| 1123 | u8 reg = 0; |
| 1124 | enum power_supply_type type = POWER_SUPPLY_TYPE_UNKNOWN; |
| 1125 | |
| 1126 | /* |
| 1127 | * If apsd is disabled, charger detection is done by |
| 1128 | * DCIN UV irq. |
| 1129 | * status = ZERO - indicates charger removed, handled |
| 1130 | * by DCIN UV irq |
| 1131 | */ |
| 1132 | if (chip->disable_apsd || status == 0) { |
| 1133 | dev_dbg(chip->dev, "APSD %s, status = %d\n", |
| 1134 | chip->disable_apsd ? "disabled" : "enabled", !!status); |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | rc = smb358_read_reg(chip, STATUS_D_REG, ®); |
| 1139 | if (rc) { |
| 1140 | dev_err(chip->dev, "Couldn't read STATUS D rc = %d\n", rc); |
| 1141 | return rc; |
| 1142 | } |
| 1143 | |
| 1144 | dev_dbg(chip->dev, "%s: STATUS_D_REG=%x\n", __func__, reg); |
| 1145 | |
| 1146 | switch (reg) { |
| 1147 | case STATUS_D_PORT_ACA_DOCK: |
| 1148 | case STATUS_D_PORT_ACA_C: |
| 1149 | case STATUS_D_PORT_ACA_B: |
| 1150 | case STATUS_D_PORT_ACA_A: |
| 1151 | type = POWER_SUPPLY_TYPE_USB_ACA; |
| 1152 | break; |
| 1153 | case STATUS_D_PORT_CDP: |
| 1154 | type = POWER_SUPPLY_TYPE_USB_CDP; |
| 1155 | break; |
| 1156 | case STATUS_D_PORT_DCP: |
| 1157 | type = POWER_SUPPLY_TYPE_USB_DCP; |
| 1158 | break; |
| 1159 | case STATUS_D_PORT_SDP: |
| 1160 | type = POWER_SUPPLY_TYPE_USB; |
| 1161 | break; |
| 1162 | case STATUS_D_PORT_OTHER: |
| 1163 | type = POWER_SUPPLY_TYPE_USB_DCP; |
| 1164 | break; |
| 1165 | default: |
| 1166 | type = POWER_SUPPLY_TYPE_USB; |
| 1167 | break; |
| 1168 | } |
| 1169 | |
| 1170 | chip->chg_present = !!status; |
| 1171 | |
| 1172 | dev_dbg(chip->dev, "APSD complete. USB type detected=%d chg_present=%d", |
| 1173 | type, chip->chg_present); |
| 1174 | |
| 1175 | power_supply_set_charge_type(chip->usb_psy, type); |
| 1176 | |
| 1177 | /* SMB is now done sampling the D+/D- lines, indicate USB driver */ |
| 1178 | dev_dbg(chip->dev, "%s updating usb_psy present=%d", __func__, |
| 1179 | chip->chg_present); |
| 1180 | power_supply_set_present(chip->usb_psy, chip->chg_present); |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static int chg_uv(struct smb358_charger *chip, u8 status) |
| 1186 | { |
| 1187 | /* use this to detect USB insertion only if !apsd */ |
| 1188 | if (chip->disable_apsd && status == 0) { |
| 1189 | chip->chg_present = true; |
| 1190 | dev_dbg(chip->dev, "%s updating usb_psy present=%d", |
| 1191 | __func__, chip->chg_present); |
| 1192 | power_supply_set_supply_type(chip->usb_psy, |
| 1193 | POWER_SUPPLY_TYPE_USB); |
| 1194 | power_supply_set_present(chip->usb_psy, chip->chg_present); |
| 1195 | } |
| 1196 | |
| 1197 | if (status != 0) { |
| 1198 | chip->chg_present = false; |
| 1199 | dev_dbg(chip->dev, "%s updating usb_psy present=%d", |
| 1200 | __func__, chip->chg_present); |
| 1201 | /* we can't set usb_psy as UNKNOWN here, will lead USERSPACE issue */ |
| 1202 | power_supply_set_present(chip->usb_psy, chip->chg_present); |
| 1203 | } |
| 1204 | |
| 1205 | power_supply_changed(chip->usb_psy); |
| 1206 | dev_dbg(chip->dev, "chip->chg_present = %d\n", chip->chg_present); |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | static int chg_ov(struct smb358_charger *chip, u8 status) |
| 1212 | { |
| 1213 | u8 psy_health_sts; |
| 1214 | if (status) |
| 1215 | psy_health_sts = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 1216 | else |
| 1217 | psy_health_sts = POWER_SUPPLY_HEALTH_GOOD; |
| 1218 | |
| 1219 | power_supply_set_health_state( |
| 1220 | chip->usb_psy, psy_health_sts); |
| 1221 | power_supply_changed(chip->usb_psy); |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | static int fast_chg(struct smb358_charger *chip, u8 status) |
| 1227 | { |
| 1228 | dev_dbg(chip->dev, "%s\n", __func__); |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static int chg_term(struct smb358_charger *chip, u8 status) |
| 1233 | { |
| 1234 | dev_dbg(chip->dev, "%s\n", __func__); |
| 1235 | chip->batt_full = !!status; |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | static int taper_chg(struct smb358_charger *chip, u8 status) |
| 1240 | { |
| 1241 | dev_dbg(chip->dev, "%s\n", __func__); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static int chg_recharge(struct smb358_charger *chip, u8 status) |
| 1246 | { |
| 1247 | dev_dbg(chip->dev, "%s, status = %d\n", __func__, !!status); |
| 1248 | /* to check the status mean */ |
| 1249 | chip->batt_full = !status; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | #define HYSTERISIS_DECIDEGC 20 |
| 1254 | static void smb_chg_adc_notification(enum qpnp_tm_state state, void *ctx) |
| 1255 | { |
| 1256 | struct smb358_charger *chip = ctx; |
| 1257 | bool bat_hot = 0, bat_cold = 0, bat_present = 0; |
| 1258 | int temp; |
| 1259 | |
| 1260 | if (state >= ADC_TM_STATE_NUM) { |
| 1261 | pr_err("invallid state parameter %d\n", state); |
| 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | temp = smb358_get_prop_batt_temp(chip); |
| 1266 | |
| 1267 | pr_debug("temp = %d state = %s\n", temp, |
| 1268 | state == ADC_TM_WARM_STATE ? "hot" : "cold"); |
| 1269 | |
| 1270 | if (state == ADC_TM_WARM_STATE) { |
| 1271 | if (temp > chip->hot_bat_decidegc) { |
| 1272 | /* Normal to hot */ |
| 1273 | bat_hot = true; |
| 1274 | bat_cold = false; |
| 1275 | bat_present = true; |
| 1276 | |
| 1277 | chip->adc_param.low_temp = |
| 1278 | chip->hot_bat_decidegc - HYSTERISIS_DECIDEGC; |
| 1279 | /* shall we need add high_temp here? */ |
| 1280 | chip->adc_param.state_request = |
| 1281 | ADC_TM_COOL_THR_ENABLE; |
| 1282 | } else if (temp > |
| 1283 | chip->cold_bat_decidegc + HYSTERISIS_DECIDEGC) { |
| 1284 | /* Cool to normal */ |
| 1285 | bat_hot = false; |
| 1286 | bat_cold = false; |
| 1287 | bat_present = true; |
| 1288 | |
| 1289 | chip->adc_param.low_temp = chip->cold_bat_decidegc; |
| 1290 | chip->adc_param.high_temp = chip->hot_bat_decidegc; |
| 1291 | chip->adc_param.state_request = |
| 1292 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 1293 | } else if (temp > chip->bat_present_decidegc) { |
| 1294 | /* Present to cold */ |
| 1295 | bat_hot = false; |
| 1296 | bat_cold = true; |
| 1297 | bat_present = true; |
| 1298 | |
| 1299 | chip->adc_param.high_temp = chip->cold_bat_decidegc; |
| 1300 | chip->adc_param.low_temp = chip->bat_present_decidegc; |
| 1301 | chip->adc_param.state_request = |
| 1302 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 1303 | } |
| 1304 | } else { |
| 1305 | if (temp <= chip->bat_present_decidegc) { |
| 1306 | /* Cold to present */ |
| 1307 | bat_cold = true; |
| 1308 | bat_hot = false; |
| 1309 | bat_present = false; |
| 1310 | chip->adc_param.high_temp = |
| 1311 | chip->bat_present_decidegc; |
| 1312 | chip->adc_param.state_request = |
| 1313 | ADC_TM_WARM_THR_ENABLE; |
| 1314 | } else if (chip->bat_present_decidegc < temp && |
| 1315 | temp < chip->cold_bat_decidegc) { |
| 1316 | /* Normal to cold */ |
| 1317 | bat_hot = false; |
| 1318 | bat_cold = true; |
| 1319 | bat_present = true; |
| 1320 | chip->adc_param.high_temp = |
| 1321 | chip->cold_bat_decidegc + HYSTERISIS_DECIDEGC; |
| 1322 | /* add low_temp to enable batt present check */ |
| 1323 | chip->adc_param.low_temp = |
| 1324 | chip->bat_present_decidegc; |
| 1325 | chip->adc_param.state_request = |
| 1326 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 1327 | } else if (temp < |
| 1328 | chip->hot_bat_decidegc - HYSTERISIS_DECIDEGC) { |
| 1329 | /* Warm to normal */ |
| 1330 | bat_hot = false; |
| 1331 | bat_cold = false; |
| 1332 | bat_present = true; |
| 1333 | |
| 1334 | chip->adc_param.low_temp = chip->cold_bat_decidegc; |
| 1335 | chip->adc_param.high_temp = chip->hot_bat_decidegc; |
| 1336 | chip->adc_param.state_request = |
| 1337 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | if (bat_present) |
| 1342 | chip->battery_missing = false; |
| 1343 | else |
| 1344 | chip->battery_missing = true; |
| 1345 | |
| 1346 | if (bat_hot ^ chip->batt_hot || bat_cold ^ chip->batt_cold) { |
| 1347 | chip->batt_hot = bat_hot; |
| 1348 | chip->batt_cold = bat_cold; |
| 1349 | /* stop charging explicitly since we use PMIC thermal pin*/ |
| 1350 | if (bat_hot || bat_cold || chip->battery_missing) |
| 1351 | smb358_charging_disable(chip, THERMAL, 1); |
| 1352 | else |
| 1353 | smb358_charging_disable(chip, THERMAL, 0); |
| 1354 | } |
| 1355 | |
| 1356 | pr_debug("hot %d, cold %d, missing %d, low = %d deciDegC, high = %d deciDegC\n", |
| 1357 | chip->batt_hot, chip->batt_cold, chip->battery_missing, |
| 1358 | chip->adc_param.low_temp, chip->adc_param.high_temp); |
| 1359 | if (qpnp_adc_tm_channel_measure(chip->adc_tm_dev, &chip->adc_param)) |
| 1360 | pr_err("request ADC error\n"); |
| 1361 | } |
| 1362 | |
| 1363 | /* only for SMB thermal */ |
| 1364 | static int hot_hard_handler(struct smb358_charger *chip, u8 status) |
| 1365 | { |
| 1366 | pr_debug("status = 0x%02x\n", status); |
| 1367 | chip->batt_hot = !!status; |
| 1368 | return 0; |
| 1369 | } |
| 1370 | static int cold_hard_handler(struct smb358_charger *chip, u8 status) |
| 1371 | { |
| 1372 | pr_debug("status = 0x%02x\n", status); |
| 1373 | chip->batt_cold = !!status; |
| 1374 | return 0; |
| 1375 | } |
| 1376 | static int hot_soft_handler(struct smb358_charger *chip, u8 status) |
| 1377 | { |
| 1378 | pr_debug("status = 0x%02x\n", status); |
| 1379 | chip->batt_warm = !!status; |
| 1380 | return 0; |
| 1381 | } |
| 1382 | static int cold_soft_handler(struct smb358_charger *chip, u8 status) |
| 1383 | { |
| 1384 | pr_debug("status = 0x%02x\n", status); |
| 1385 | chip->batt_cool = !!status; |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | static int battery_missing(struct smb358_charger *chip, u8 status) |
| 1390 | { |
| 1391 | chip->battery_missing = !!status; |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | static struct irq_handler_info handlers[] = { |
| 1396 | [0] = { |
| 1397 | .stat_reg = IRQ_A_REG, |
| 1398 | .val = 0, |
| 1399 | .prev_val = 0, |
| 1400 | .irq_info = { |
| 1401 | { |
| 1402 | .name = "cold_soft", |
| 1403 | .smb_irq = cold_soft_handler, |
| 1404 | }, |
| 1405 | { |
| 1406 | .name = "hot_soft", |
| 1407 | .smb_irq = hot_soft_handler, |
| 1408 | }, |
| 1409 | { |
| 1410 | .name = "cold_hard", |
| 1411 | .smb_irq = cold_hard_handler, |
| 1412 | }, |
| 1413 | { |
| 1414 | .name = "hot_hard", |
| 1415 | .smb_irq = hot_hard_handler, |
| 1416 | }, |
| 1417 | }, |
| 1418 | }, |
| 1419 | [1] = { |
| 1420 | .stat_reg = IRQ_B_REG, |
| 1421 | .val = 0, |
| 1422 | .prev_val = 0, |
| 1423 | .irq_info = { |
| 1424 | { |
| 1425 | .name = "chg_hot", |
| 1426 | }, |
| 1427 | { |
| 1428 | .name = "vbat_low", |
| 1429 | }, |
| 1430 | { |
| 1431 | .name = "battery_missing", |
| 1432 | .smb_irq = battery_missing |
| 1433 | }, |
| 1434 | { |
| 1435 | .name = "battery_ov", |
| 1436 | }, |
| 1437 | }, |
| 1438 | }, |
| 1439 | [2] = { |
| 1440 | .stat_reg = IRQ_C_REG, |
| 1441 | .val = 0, |
| 1442 | .prev_val = 0, |
| 1443 | .irq_info = { |
| 1444 | { |
| 1445 | .name = "chg_term", |
| 1446 | .smb_irq = chg_term, |
| 1447 | }, |
| 1448 | { |
| 1449 | .name = "taper", |
| 1450 | .smb_irq = taper_chg, |
| 1451 | }, |
| 1452 | { |
| 1453 | .name = "recharge", |
| 1454 | .smb_irq = chg_recharge, |
| 1455 | }, |
| 1456 | { |
| 1457 | .name = "fast_chg", |
| 1458 | .smb_irq = fast_chg, |
| 1459 | }, |
| 1460 | }, |
| 1461 | }, |
| 1462 | [3] = { |
| 1463 | .stat_reg = IRQ_D_REG, |
| 1464 | .val = 0, |
| 1465 | .prev_val = 0, |
| 1466 | .irq_info = { |
| 1467 | { |
| 1468 | .name = "prechg_timeout", |
| 1469 | }, |
| 1470 | { |
| 1471 | .name = "safety_timeout", |
| 1472 | }, |
| 1473 | { |
| 1474 | .name = "aicl_complete", |
| 1475 | }, |
| 1476 | { |
| 1477 | .name = "src_detect", |
| 1478 | .smb_irq = apsd_complete, |
| 1479 | }, |
| 1480 | }, |
| 1481 | }, |
| 1482 | [4] = { |
| 1483 | .stat_reg = IRQ_E_REG, |
| 1484 | .val = 0, |
| 1485 | .prev_val = 0, |
| 1486 | .irq_info = { |
| 1487 | { |
| 1488 | .name = "usbin_uv", |
| 1489 | .smb_irq = chg_uv, |
| 1490 | }, |
| 1491 | { |
| 1492 | .name = "usbin_ov", |
| 1493 | .smb_irq = chg_ov, |
| 1494 | }, |
| 1495 | { |
| 1496 | .name = "unknown", |
| 1497 | }, |
| 1498 | { |
| 1499 | .name = "unknown", |
| 1500 | }, |
| 1501 | }, |
| 1502 | }, |
| 1503 | [5] = { |
| 1504 | .stat_reg = IRQ_F_REG, |
| 1505 | .val = 0, |
| 1506 | .prev_val = 0, |
| 1507 | .irq_info = { |
| 1508 | { |
| 1509 | .name = "power_ok", |
| 1510 | }, |
| 1511 | { |
| 1512 | .name = "otg_det", |
| 1513 | }, |
| 1514 | { |
| 1515 | .name = "otg_batt_uv", |
| 1516 | }, |
| 1517 | { |
| 1518 | .name = "otg_oc", |
| 1519 | }, |
| 1520 | }, |
| 1521 | }, |
| 1522 | }; |
| 1523 | |
| 1524 | #define IRQ_LATCHED_MASK 0x02 |
| 1525 | #define IRQ_STATUS_MASK 0x01 |
| 1526 | #define BITS_PER_IRQ 2 |
| 1527 | static irqreturn_t smb358_chg_stat_handler(int irq, void *dev_id) |
| 1528 | { |
| 1529 | struct smb358_charger *chip = dev_id; |
| 1530 | int i, j; |
| 1531 | u8 triggered; |
| 1532 | u8 changed; |
| 1533 | u8 rt_stat, prev_rt_stat; |
| 1534 | int rc; |
| 1535 | int handler_count = 0; |
| 1536 | |
| 1537 | mutex_lock(&chip->irq_complete); |
| 1538 | |
| 1539 | chip->irq_waiting = true; |
| 1540 | if (!chip->resume_completed) { |
| 1541 | dev_dbg(chip->dev, "IRQ triggered before device-resume\n"); |
| 1542 | disable_irq_nosync(irq); |
| 1543 | mutex_unlock(&chip->irq_complete); |
| 1544 | return IRQ_HANDLED; |
| 1545 | } |
| 1546 | chip->irq_waiting = false; |
| 1547 | |
| 1548 | for (i = 0; i < ARRAY_SIZE(handlers); i++) { |
| 1549 | rc = smb358_read_reg(chip, handlers[i].stat_reg, |
| 1550 | &handlers[i].val); |
| 1551 | if (rc < 0) { |
| 1552 | dev_err(chip->dev, "Couldn't read %d rc = %d\n", |
| 1553 | handlers[i].stat_reg, rc); |
| 1554 | continue; |
| 1555 | } |
| 1556 | |
| 1557 | for (j = 0; j < ARRAY_SIZE(handlers[i].irq_info); j++) { |
| 1558 | triggered = handlers[i].val |
| 1559 | & (IRQ_LATCHED_MASK << (j * BITS_PER_IRQ)); |
| 1560 | rt_stat = handlers[i].val |
| 1561 | & (IRQ_STATUS_MASK << (j * BITS_PER_IRQ)); |
| 1562 | prev_rt_stat = handlers[i].prev_val |
| 1563 | & (IRQ_STATUS_MASK << (j * BITS_PER_IRQ)); |
| 1564 | changed = prev_rt_stat ^ rt_stat; |
| 1565 | |
| 1566 | if (triggered || changed) |
| 1567 | rt_stat ? handlers[i].irq_info[j].high++ : |
| 1568 | handlers[i].irq_info[j].low++; |
| 1569 | |
| 1570 | if ((triggered || changed) |
| 1571 | && handlers[i].irq_info[j].smb_irq != NULL) { |
| 1572 | handler_count++; |
| 1573 | rc = handlers[i].irq_info[j].smb_irq(chip, |
| 1574 | rt_stat); |
| 1575 | if (rc < 0) |
| 1576 | dev_err(chip->dev, |
| 1577 | "Couldn't handle %d irq for reg 0x%02x rc = %d\n", |
| 1578 | j, handlers[i].stat_reg, rc); |
| 1579 | } |
| 1580 | } |
| 1581 | handlers[i].prev_val = handlers[i].val; |
| 1582 | } |
| 1583 | |
| 1584 | pr_debug("handler count = %d\n", handler_count); |
| 1585 | if (handler_count) { |
| 1586 | pr_debug("batt psy changed\n"); |
| 1587 | power_supply_changed(&chip->batt_psy); |
| 1588 | } |
| 1589 | |
| 1590 | mutex_unlock(&chip->irq_complete); |
| 1591 | |
| 1592 | return IRQ_HANDLED; |
| 1593 | } |
| 1594 | |
| 1595 | static irqreturn_t smb358_chg_valid_handler(int irq, void *dev_id) |
| 1596 | { |
| 1597 | struct smb358_charger *chip = dev_id; |
| 1598 | int present; |
| 1599 | |
| 1600 | present = gpio_get_value_cansleep(chip->chg_valid_gpio); |
| 1601 | if (present < 0) { |
| 1602 | dev_err(chip->dev, "Couldn't read chg_valid gpio=%d\n", |
| 1603 | chip->chg_valid_gpio); |
| 1604 | return IRQ_HANDLED; |
| 1605 | } |
| 1606 | present ^= chip->chg_valid_act_low; |
| 1607 | |
| 1608 | dev_dbg(chip->dev, "%s: chg_present = %d\n", __func__, present); |
| 1609 | |
| 1610 | if (present != chip->chg_present) { |
| 1611 | chip->chg_present = present; |
| 1612 | dev_dbg(chip->dev, "%s updating usb_psy present=%d", |
| 1613 | __func__, chip->chg_present); |
| 1614 | power_supply_set_present(chip->usb_psy, chip->chg_present); |
| 1615 | } |
| 1616 | |
| 1617 | return IRQ_HANDLED; |
| 1618 | } |
| 1619 | |
| 1620 | static void smb358_external_power_changed(struct power_supply *psy) |
| 1621 | { |
| 1622 | struct smb358_charger *chip = container_of(psy, |
| 1623 | struct smb358_charger, batt_psy); |
| 1624 | union power_supply_propval prop = {0,}; |
| 1625 | int rc, current_limit = 0; |
| 1626 | |
| 1627 | if (chip->bms_psy_name) |
| 1628 | chip->bms_psy = |
| 1629 | power_supply_get_by_name((char *)chip->bms_psy_name); |
| 1630 | |
| 1631 | rc = chip->usb_psy->get_property(chip->usb_psy, |
| 1632 | POWER_SUPPLY_PROP_CURRENT_MAX, &prop); |
| 1633 | if (rc) |
| 1634 | dev_err(chip->dev, |
| 1635 | "Couldn't read USB current_max property, rc=%d\n", rc); |
| 1636 | else |
| 1637 | current_limit = prop.intval / 1000; |
| 1638 | |
| 1639 | |
| 1640 | smb358_enable_volatile_writes(chip); |
| 1641 | smb358_set_usb_chg_current(chip, current_limit); |
| 1642 | |
| 1643 | dev_dbg(chip->dev, "current_limit = %d\n", current_limit); |
| 1644 | } |
| 1645 | |
| 1646 | #if defined(CONFIG_DEBUG_FS) |
| 1647 | #define LAST_CNFG_REG 0x13 |
| 1648 | static int show_cnfg_regs(struct seq_file *m, void *data) |
| 1649 | { |
| 1650 | struct smb358_charger *chip = m->private; |
| 1651 | int rc; |
| 1652 | u8 reg; |
| 1653 | u8 addr; |
| 1654 | |
| 1655 | for (addr = 0; addr <= LAST_CNFG_REG; addr++) { |
| 1656 | rc = smb358_read_reg(chip, addr, ®); |
| 1657 | if (!rc) |
| 1658 | seq_printf(m, "0x%02x = 0x%02x\n", addr, reg); |
| 1659 | } |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | static int cnfg_debugfs_open(struct inode *inode, struct file *file) |
| 1665 | { |
| 1666 | struct smb358_charger *chip = inode->i_private; |
| 1667 | |
| 1668 | return single_open(file, show_cnfg_regs, chip); |
| 1669 | } |
| 1670 | |
| 1671 | static const struct file_operations cnfg_debugfs_ops = { |
| 1672 | .owner = THIS_MODULE, |
| 1673 | .open = cnfg_debugfs_open, |
| 1674 | .read = seq_read, |
| 1675 | .llseek = seq_lseek, |
| 1676 | .release = single_release, |
| 1677 | }; |
| 1678 | |
| 1679 | #define FIRST_CMD_REG 0x30 |
| 1680 | #define LAST_CMD_REG 0x33 |
| 1681 | static int show_cmd_regs(struct seq_file *m, void *data) |
| 1682 | { |
| 1683 | struct smb358_charger *chip = m->private; |
| 1684 | int rc; |
| 1685 | u8 reg; |
| 1686 | u8 addr; |
| 1687 | |
| 1688 | for (addr = FIRST_CMD_REG; addr <= LAST_CMD_REG; addr++) { |
| 1689 | rc = smb358_read_reg(chip, addr, ®); |
| 1690 | if (!rc) |
| 1691 | seq_printf(m, "0x%02x = 0x%02x\n", addr, reg); |
| 1692 | } |
| 1693 | |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | static int cmd_debugfs_open(struct inode *inode, struct file *file) |
| 1698 | { |
| 1699 | struct smb358_charger *chip = inode->i_private; |
| 1700 | |
| 1701 | return single_open(file, show_cmd_regs, chip); |
| 1702 | } |
| 1703 | |
| 1704 | static const struct file_operations cmd_debugfs_ops = { |
| 1705 | .owner = THIS_MODULE, |
| 1706 | .open = cmd_debugfs_open, |
| 1707 | .read = seq_read, |
| 1708 | .llseek = seq_lseek, |
| 1709 | .release = single_release, |
| 1710 | }; |
| 1711 | |
| 1712 | #define FIRST_STATUS_REG 0x35 |
| 1713 | #define LAST_STATUS_REG 0x3F |
| 1714 | static int show_status_regs(struct seq_file *m, void *data) |
| 1715 | { |
| 1716 | struct smb358_charger *chip = m->private; |
| 1717 | int rc; |
| 1718 | u8 reg; |
| 1719 | u8 addr; |
| 1720 | |
| 1721 | for (addr = FIRST_STATUS_REG; addr <= LAST_STATUS_REG; addr++) { |
| 1722 | rc = smb358_read_reg(chip, addr, ®); |
| 1723 | if (!rc) |
| 1724 | seq_printf(m, "0x%02x = 0x%02x\n", addr, reg); |
| 1725 | } |
| 1726 | |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | static int status_debugfs_open(struct inode *inode, struct file *file) |
| 1731 | { |
| 1732 | struct smb358_charger *chip = inode->i_private; |
| 1733 | |
| 1734 | return single_open(file, show_status_regs, chip); |
| 1735 | } |
| 1736 | |
| 1737 | static const struct file_operations status_debugfs_ops = { |
| 1738 | .owner = THIS_MODULE, |
| 1739 | .open = status_debugfs_open, |
| 1740 | .read = seq_read, |
| 1741 | .llseek = seq_lseek, |
| 1742 | .release = single_release, |
| 1743 | }; |
| 1744 | |
| 1745 | static int show_irq_count(struct seq_file *m, void *data) |
| 1746 | { |
| 1747 | int i, j, total = 0; |
| 1748 | |
| 1749 | for (i = 0; i < ARRAY_SIZE(handlers); i++) |
| 1750 | for (j = 0; j < 4; j++) { |
| 1751 | seq_printf(m, "%s=%d\t(high=%d low=%d)\n", |
| 1752 | handlers[i].irq_info[j].name, |
| 1753 | handlers[i].irq_info[j].high |
| 1754 | + handlers[i].irq_info[j].low, |
| 1755 | handlers[i].irq_info[j].high, |
| 1756 | handlers[i].irq_info[j].low); |
| 1757 | total += (handlers[i].irq_info[j].high |
| 1758 | + handlers[i].irq_info[j].low); |
| 1759 | } |
| 1760 | |
| 1761 | seq_printf(m, "\n\tTotal = %d\n", total); |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | static int irq_count_debugfs_open(struct inode *inode, struct file *file) |
| 1767 | { |
| 1768 | struct smb358_charger *chip = inode->i_private; |
| 1769 | |
| 1770 | return single_open(file, show_irq_count, chip); |
| 1771 | } |
| 1772 | |
| 1773 | static const struct file_operations irq_count_debugfs_ops = { |
| 1774 | .owner = THIS_MODULE, |
| 1775 | .open = irq_count_debugfs_open, |
| 1776 | .read = seq_read, |
| 1777 | .llseek = seq_lseek, |
| 1778 | .release = single_release, |
| 1779 | }; |
| 1780 | |
| 1781 | static int get_reg(void *data, u64 *val) |
| 1782 | { |
| 1783 | struct smb358_charger *chip = data; |
| 1784 | int rc; |
| 1785 | u8 temp; |
| 1786 | |
| 1787 | rc = smb358_read_reg(chip, chip->peek_poke_address, &temp); |
| 1788 | if (rc < 0) { |
| 1789 | dev_err(chip->dev, |
| 1790 | "Couldn't read reg %x rc = %d\n", |
| 1791 | chip->peek_poke_address, rc); |
| 1792 | return -EAGAIN; |
| 1793 | } |
| 1794 | *val = temp; |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | static int set_reg(void *data, u64 val) |
| 1799 | { |
| 1800 | struct smb358_charger *chip = data; |
| 1801 | int rc; |
| 1802 | u8 temp; |
| 1803 | |
| 1804 | temp = (u8) val; |
| 1805 | rc = smb358_write_reg(chip, chip->peek_poke_address, temp); |
| 1806 | if (rc < 0) { |
| 1807 | dev_err(chip->dev, |
| 1808 | "Couldn't write 0x%02x to 0x%02x rc= %d\n", |
| 1809 | chip->peek_poke_address, temp, rc); |
| 1810 | return -EAGAIN; |
| 1811 | } |
| 1812 | return 0; |
| 1813 | } |
| 1814 | DEFINE_SIMPLE_ATTRIBUTE(poke_poke_debug_ops, get_reg, set_reg, "0x%02llx\n"); |
| 1815 | |
| 1816 | static int force_irq_set(void *data, u64 val) |
| 1817 | { |
| 1818 | struct smb358_charger *chip = data; |
| 1819 | |
| 1820 | smb358_chg_stat_handler(chip->client->irq, data); |
| 1821 | return 0; |
| 1822 | } |
| 1823 | DEFINE_SIMPLE_ATTRIBUTE(force_irq_ops, NULL, force_irq_set, "0x%02llx\n"); |
| 1824 | #endif |
| 1825 | |
| 1826 | #ifdef DEBUG |
| 1827 | static void dump_regs(struct smb358_charger *chip) |
| 1828 | { |
| 1829 | int rc; |
| 1830 | u8 reg; |
| 1831 | u8 addr; |
| 1832 | |
| 1833 | for (addr = 0; addr <= LAST_CNFG_REG; addr++) { |
| 1834 | rc = smb358_read_reg(chip, addr, ®); |
| 1835 | if (rc) |
| 1836 | dev_err(chip->dev, "Couldn't read 0x%02x rc = %d\n", |
| 1837 | addr, rc); |
| 1838 | else |
| 1839 | pr_debug("0x%02x = 0x%02x\n", addr, reg); |
| 1840 | } |
| 1841 | |
| 1842 | for (addr = FIRST_STATUS_REG; addr <= LAST_STATUS_REG; addr++) { |
| 1843 | rc = smb358_read_reg(chip, addr, ®); |
| 1844 | if (rc) |
| 1845 | dev_err(chip->dev, "Couldn't read 0x%02x rc = %d\n", |
| 1846 | addr, rc); |
| 1847 | else |
| 1848 | pr_debug("0x%02x = 0x%02x\n", addr, reg); |
| 1849 | } |
| 1850 | |
| 1851 | for (addr = FIRST_CMD_REG; addr <= LAST_CMD_REG; addr++) { |
| 1852 | rc = smb358_read_reg(chip, addr, ®); |
| 1853 | if (rc) |
| 1854 | dev_err(chip->dev, "Couldn't read 0x%02x rc = %d\n", |
| 1855 | addr, rc); |
| 1856 | else |
| 1857 | pr_debug("0x%02x = 0x%02x\n", addr, reg); |
| 1858 | } |
| 1859 | } |
| 1860 | #else |
| 1861 | static void dump_regs(struct smb358_charger *chip) |
| 1862 | { |
| 1863 | } |
| 1864 | #endif |
| 1865 | |
| 1866 | static int smb_parse_dt(struct smb358_charger *chip) |
| 1867 | { |
| 1868 | int rc; |
| 1869 | enum of_gpio_flags gpio_flags; |
| 1870 | struct device_node *node = chip->dev->of_node; |
| 1871 | int batt_present_degree_negative; |
| 1872 | |
| 1873 | if (!node) { |
| 1874 | dev_err(chip->dev, "device tree info. missing\n"); |
| 1875 | return -EINVAL; |
| 1876 | } |
| 1877 | |
| 1878 | chip->charging_disabled = of_property_read_bool(node, |
| 1879 | "qcom,charger-disabled"); |
| 1880 | |
| 1881 | chip->chg_autonomous_mode = of_property_read_bool(node, |
| 1882 | "qcom,chg-autonomous-mode"); |
| 1883 | |
| 1884 | chip->disable_apsd = of_property_read_bool(node, "qcom,disable-apsd"); |
| 1885 | |
| 1886 | chip->using_pmic_therm = of_property_read_bool(node, |
| 1887 | "qcom,using-pmic-therm"); |
| 1888 | |
| 1889 | rc = of_property_read_string(node, "qcom,bms-psy-name", |
| 1890 | &chip->bms_psy_name); |
| 1891 | if (rc) |
| 1892 | chip->bms_psy_name = NULL; |
| 1893 | |
| 1894 | chip->chg_valid_gpio = of_get_named_gpio_flags(node, |
| 1895 | "qcom,chg-valid-gpio", 0, &gpio_flags); |
| 1896 | if (!gpio_is_valid(chip->chg_valid_gpio)) |
| 1897 | dev_dbg(chip->dev, "Invalid chg-valid-gpio"); |
| 1898 | else |
| 1899 | chip->chg_valid_act_low = gpio_flags & OF_GPIO_ACTIVE_LOW; |
| 1900 | |
| 1901 | rc = of_property_read_u32(node, "qcom,fastchg-current-max-ma", |
| 1902 | &chip->fastchg_current_max_ma); |
| 1903 | if (rc) |
| 1904 | chip->fastchg_current_max_ma = SMB358_FAST_CHG_MAX_MA; |
| 1905 | |
| 1906 | chip->ieerm_disabled = of_property_read_bool(node, |
| 1907 | "qcom,iterm-disabled"); |
| 1908 | |
| 1909 | rc = of_property_read_u32(node, "qcom,iterm-ma", &chip->iterm_ma); |
| 1910 | if (rc < 0) |
| 1911 | chip->iterm_ma = -EINVAL; |
| 1912 | |
| 1913 | rc = of_property_read_u32(node, "qcom,float-voltage-mv", |
| 1914 | &chip->vfloat_mv); |
| 1915 | if (rc < 0) { |
| 1916 | chip->vfloat_mv = -EINVAL; |
| 1917 | pr_err("float-voltage-mv property missing, exit\n"); |
| 1918 | return -EINVAL; |
| 1919 | } |
| 1920 | |
| 1921 | rc = of_property_read_u32(node, "qcom,recharge-mv", |
| 1922 | &chip->recharge_mv); |
| 1923 | if (rc < 0) |
| 1924 | chip->recharge_mv = -EINVAL; |
| 1925 | |
| 1926 | chip->recharge_disabled = of_property_read_bool(node, |
| 1927 | "qcom,recharge-disabled"); |
| 1928 | |
| 1929 | rc = of_property_read_u32(node, "qcom,cold-bat-decidegc", |
| 1930 | &chip->cold_bat_decidegc); |
| 1931 | if (rc < 0) |
| 1932 | chip->cold_bat_decidegc = -EINVAL; |
| 1933 | |
| 1934 | rc = of_property_read_u32(node, "qcom,hot-bat-decidegc", |
| 1935 | &chip->hot_bat_decidegc); |
| 1936 | if (rc < 0) |
| 1937 | chip->hot_bat_decidegc = -EINVAL; |
| 1938 | |
| 1939 | rc = of_property_read_u32(node, "qcom,bat-present-decidegc", |
| 1940 | &batt_present_degree_negative); |
| 1941 | if (rc < 0) |
| 1942 | chip->bat_present_decidegc = -EINVAL; |
| 1943 | else |
| 1944 | chip->bat_present_decidegc = -batt_present_degree_negative; |
| 1945 | |
| 1946 | pr_debug("recharge-disabled = %d, recharge-mv = %d,", |
| 1947 | chip->recharge_disabled, chip->recharge_mv); |
| 1948 | pr_debug("vfloat-mv = %d, iterm-disabled = %d,", |
| 1949 | chip->vfloat_mv, chip->iterm_ma); |
| 1950 | pr_debug("fastchg-current = %d, charging-disabled = %d,", |
| 1951 | chip->fastchg_current_max_ma, |
| 1952 | chip->charging_disabled); |
| 1953 | pr_debug("disable-apsd = %d bms = %s cold-bat-degree = %d,", |
| 1954 | chip->disable_apsd, chip->bms_psy_name, |
| 1955 | chip->cold_bat_decidegc); |
| 1956 | pr_debug("hot-bat-degree = %d, bat-present-decidegc = %d\n", |
| 1957 | chip->hot_bat_decidegc, chip->bat_present_decidegc); |
| 1958 | return 0; |
| 1959 | } |
| 1960 | |
| 1961 | static int determine_initial_state(struct smb358_charger *chip) |
| 1962 | { |
| 1963 | int rc; |
| 1964 | u8 reg = 0; |
| 1965 | |
| 1966 | rc = smb358_read_reg(chip, IRQ_B_REG, ®); |
| 1967 | if (rc) { |
| 1968 | dev_err(chip->dev, "Couldn't read IRQ_B rc = %d\n", rc); |
| 1969 | goto fail_init_status; |
| 1970 | } |
| 1971 | |
| 1972 | rc = smb358_read_reg(chip, IRQ_C_REG, ®); |
| 1973 | if (rc) { |
| 1974 | dev_err(chip->dev, "Couldn't read IRQ_C rc = %d\n", rc); |
| 1975 | goto fail_init_status; |
| 1976 | } |
| 1977 | chip->batt_full = (reg & IRQ_C_TERM_BIT) ? true : false; |
| 1978 | |
| 1979 | rc = smb358_read_reg(chip, IRQ_A_REG, ®); |
| 1980 | if (rc < 0) { |
| 1981 | dev_err(chip->dev, "Couldn't read irq A rc = %d\n", rc); |
| 1982 | return rc; |
| 1983 | } |
| 1984 | |
| 1985 | /* For current design, can ignore this */ |
| 1986 | if (reg & IRQ_A_HOT_HARD_BIT) |
| 1987 | chip->batt_hot = true; |
| 1988 | if (reg & IRQ_A_COLD_HARD_BIT) |
| 1989 | chip->batt_cold = true; |
| 1990 | if (reg & IRQ_A_HOT_SOFT_BIT) |
| 1991 | chip->batt_warm = true; |
| 1992 | if (reg & IRQ_A_COLD_SOFT_BIT) |
| 1993 | chip->batt_cool = true; |
| 1994 | |
| 1995 | rc = smb358_read_reg(chip, IRQ_E_REG, ®); |
| 1996 | if (rc) { |
| 1997 | dev_err(chip->dev, "Couldn't read IRQ_E rc = %d\n", rc); |
| 1998 | goto fail_init_status; |
| 1999 | } |
| 2000 | |
| 2001 | if (reg & IRQ_E_INPUT_UV_BIT) { |
| 2002 | chg_uv(chip, 1); |
| 2003 | } else { |
| 2004 | chg_uv(chip, 0); |
| 2005 | apsd_complete(chip, 1); |
| 2006 | } |
| 2007 | |
| 2008 | return 0; |
| 2009 | |
| 2010 | fail_init_status: |
| 2011 | dev_err(chip->dev, "Couldn't determine initial status\n"); |
| 2012 | return rc; |
| 2013 | } |
| 2014 | |
| 2015 | #if defined(CONFIG_DEBUG_FS) |
| 2016 | static void smb358_debugfs_init(struct smb358_charger *chip) |
| 2017 | { |
| 2018 | int rc; |
| 2019 | chip->debug_root = debugfs_create_dir("smb358", NULL); |
| 2020 | if (!chip->debug_root) |
| 2021 | dev_err(chip->dev, "Couldn't create debug dir\n"); |
| 2022 | |
| 2023 | if (chip->debug_root) { |
| 2024 | struct dentry *ent; |
| 2025 | |
| 2026 | ent = debugfs_create_file("config_registers", S_IFREG | S_IRUGO, |
| 2027 | chip->debug_root, chip, |
| 2028 | &cnfg_debugfs_ops); |
| 2029 | if (!ent || IS_ERR(ent)) { |
| 2030 | rc = PTR_ERR(ent); |
| 2031 | dev_err(chip->dev, |
| 2032 | "Couldn't create cnfg debug file rc = %d\n", |
| 2033 | rc); |
| 2034 | } |
| 2035 | |
| 2036 | ent = debugfs_create_file("status_registers", S_IFREG | S_IRUGO, |
| 2037 | chip->debug_root, chip, |
| 2038 | &status_debugfs_ops); |
| 2039 | if (!ent || IS_ERR(ent)) { |
| 2040 | rc = PTR_ERR(ent); |
| 2041 | dev_err(chip->dev, |
| 2042 | "Couldn't create status debug file rc = %d\n", |
| 2043 | rc); |
| 2044 | } |
| 2045 | |
| 2046 | ent = debugfs_create_file("cmd_registers", S_IFREG | S_IRUGO, |
| 2047 | chip->debug_root, chip, |
| 2048 | &cmd_debugfs_ops); |
| 2049 | if (!ent || IS_ERR(ent)) { |
| 2050 | rc = PTR_ERR(ent); |
| 2051 | dev_err(chip->dev, |
| 2052 | "Couldn't create cmd debug file rc = %d\n", |
| 2053 | rc); |
| 2054 | } |
| 2055 | |
| 2056 | ent = debugfs_create_x32("address", S_IFREG | S_IWUSR | S_IRUGO, |
| 2057 | chip->debug_root, |
| 2058 | &(chip->peek_poke_address)); |
| 2059 | if (!ent || IS_ERR(ent)) { |
| 2060 | rc = PTR_ERR(ent); |
| 2061 | dev_err(chip->dev, |
| 2062 | "Couldn't create address debug file rc = %d\n", |
| 2063 | rc); |
| 2064 | } |
| 2065 | |
| 2066 | ent = debugfs_create_file("data", S_IFREG | S_IWUSR | S_IRUGO, |
| 2067 | chip->debug_root, chip, |
| 2068 | &poke_poke_debug_ops); |
| 2069 | if (!ent || IS_ERR(ent)) { |
| 2070 | rc = PTR_ERR(ent); |
| 2071 | dev_err(chip->dev, |
| 2072 | "Couldn't create data debug file rc = %d\n", |
| 2073 | rc); |
| 2074 | } |
| 2075 | |
| 2076 | ent = debugfs_create_file("force_irq", |
| 2077 | S_IFREG | S_IWUSR | S_IRUGO, |
| 2078 | chip->debug_root, chip, |
| 2079 | &force_irq_ops); |
| 2080 | if (!ent || IS_ERR(ent)) { |
| 2081 | rc = PTR_ERR(ent); |
| 2082 | dev_err(chip->dev, |
| 2083 | "Couldn't create force_irq debug file rc =%d\n", |
| 2084 | rc); |
| 2085 | } |
| 2086 | |
| 2087 | ent = debugfs_create_file("irq_count", S_IFREG | S_IRUGO, |
| 2088 | chip->debug_root, chip, |
| 2089 | &irq_count_debugfs_ops); |
| 2090 | if (!ent || IS_ERR(ent)) { |
| 2091 | rc = PTR_ERR(ent); |
| 2092 | dev_err(chip->dev, |
| 2093 | "Couldn't create cnfg irq_count file rc = %d\n", |
| 2094 | rc); |
| 2095 | } |
| 2096 | } |
| 2097 | } |
| 2098 | #else |
| 2099 | static void smb358_debugfs_init(struct smb358_charger *chip) |
| 2100 | { |
| 2101 | } |
| 2102 | #endif |
| 2103 | |
| 2104 | #define SMB_I2C_VTG_MIN_UV 1800000 |
| 2105 | #define SMB_I2C_VTG_MAX_UV 1800000 |
| 2106 | static int smb358_charger_probe(struct i2c_client *client, |
| 2107 | const struct i2c_device_id *id) |
| 2108 | { |
| 2109 | int rc, irq; |
| 2110 | struct smb358_charger *chip; |
| 2111 | struct power_supply *usb_psy; |
| 2112 | u8 reg = 0; |
| 2113 | |
| 2114 | usb_psy = power_supply_get_by_name("usb"); |
| 2115 | if (!usb_psy) { |
| 2116 | dev_dbg(&client->dev, "USB psy not found; deferring probe\n"); |
| 2117 | return -EPROBE_DEFER; |
| 2118 | } |
| 2119 | |
| 2120 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 2121 | if (!chip) { |
| 2122 | dev_err(&client->dev, "Couldn't allocate memory\n"); |
| 2123 | return -ENOMEM; |
| 2124 | } |
| 2125 | |
| 2126 | chip->client = client; |
| 2127 | chip->dev = &client->dev; |
| 2128 | chip->usb_psy = usb_psy; |
| 2129 | chip->fake_battery_soc = -EINVAL; |
| 2130 | |
| 2131 | /* early for VADC get, defer probe if needed */ |
| 2132 | chip->vadc_dev = qpnp_get_vadc(chip->dev, "chg"); |
| 2133 | if (IS_ERR(chip->vadc_dev)) { |
| 2134 | rc = PTR_ERR(chip->vadc_dev); |
| 2135 | if (rc != -EPROBE_DEFER) |
| 2136 | pr_err("vadc property missing\n"); |
| 2137 | return rc; |
| 2138 | } |
| 2139 | |
| 2140 | /* i2c pull up Regulator configuration */ |
| 2141 | chip->vcc_i2c = regulator_get(&client->dev, "vcc-i2c"); |
| 2142 | if (IS_ERR(chip->vcc_i2c)) { |
| 2143 | dev_err(&client->dev, |
| 2144 | "%s: Failed to get vcc_i2c regulator\n", |
| 2145 | __func__); |
| 2146 | rc = PTR_ERR(chip->vcc_i2c); |
| 2147 | goto err_get_vtg_i2c; |
| 2148 | } |
| 2149 | |
| 2150 | if (regulator_count_voltages(chip->vcc_i2c) > 0) { |
| 2151 | rc = regulator_set_voltage(chip->vcc_i2c, |
| 2152 | SMB_I2C_VTG_MIN_UV, SMB_I2C_VTG_MAX_UV); |
| 2153 | if (rc) { |
| 2154 | dev_err(&client->dev, |
| 2155 | "regulator vcc_i2c set failed, rc = %d\n", |
| 2156 | rc); |
| 2157 | goto err_set_vtg_i2c; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | rc = regulator_enable(chip->vcc_i2c); |
| 2162 | if (rc) { |
| 2163 | dev_err(&client->dev, |
| 2164 | "Regulator vcc_i2c enable failed " |
| 2165 | "rc=%d\n", rc); |
| 2166 | goto err_set_vtg_i2c; |
| 2167 | } |
| 2168 | |
| 2169 | mutex_init(&chip->irq_complete); |
| 2170 | mutex_init(&chip->read_write_lock); |
| 2171 | mutex_init(&chip->path_suspend_lock); |
| 2172 | |
| 2173 | /* probe the device to check if its actually connected */ |
| 2174 | rc = smb358_read_reg(chip, CHG_OTH_CURRENT_CTRL_REG, ®); |
| 2175 | if (rc) { |
| 2176 | pr_err("Failed to detect SMB358, device absent, rc = %d\n", rc); |
| 2177 | goto err_set_vtg_i2c; |
| 2178 | } |
| 2179 | |
| 2180 | rc = smb_parse_dt(chip); |
| 2181 | if (rc) { |
| 2182 | dev_err(&client->dev, "Couldn't parse DT nodes rc=%d\n", rc); |
| 2183 | goto err_set_vtg_i2c; |
| 2184 | } |
| 2185 | |
| 2186 | /* using adc_tm for implementing pmic therm */ |
| 2187 | if (chip->using_pmic_therm) { |
| 2188 | chip->adc_tm_dev = qpnp_get_adc_tm(chip->dev, "chg"); |
| 2189 | if (IS_ERR(chip->adc_tm_dev)) { |
| 2190 | rc = PTR_ERR(chip->adc_tm_dev); |
| 2191 | if (rc != -EPROBE_DEFER) |
| 2192 | pr_err("adc_tm property missing\n"); |
| 2193 | return rc; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | i2c_set_clientdata(client, chip); |
| 2198 | |
| 2199 | chip->batt_psy.name = "battery"; |
| 2200 | chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY; |
| 2201 | chip->batt_psy.get_property = smb358_battery_get_property; |
| 2202 | chip->batt_psy.set_property = smb358_battery_set_property; |
| 2203 | chip->batt_psy.property_is_writeable = |
| 2204 | smb358_batt_property_is_writeable; |
| 2205 | chip->batt_psy.properties = smb358_battery_properties; |
| 2206 | chip->batt_psy.num_properties = ARRAY_SIZE(smb358_battery_properties); |
| 2207 | chip->batt_psy.external_power_changed = smb358_external_power_changed; |
| 2208 | chip->batt_psy.supplied_to = pm_batt_supplied_to; |
| 2209 | chip->batt_psy.num_supplicants = ARRAY_SIZE(pm_batt_supplied_to); |
| 2210 | |
| 2211 | chip->resume_completed = true; |
| 2212 | |
| 2213 | rc = power_supply_register(chip->dev, &chip->batt_psy); |
| 2214 | if (rc < 0) { |
| 2215 | dev_err(&client->dev, "Couldn't register batt psy rc = %d\n", |
| 2216 | rc); |
| 2217 | goto err_set_vtg_i2c; |
| 2218 | } |
| 2219 | |
| 2220 | dump_regs(chip); |
| 2221 | |
| 2222 | rc = smb358_regulator_init(chip); |
| 2223 | if (rc) { |
| 2224 | dev_err(&client->dev, |
| 2225 | "Couldn't initialize smb358 ragulator rc=%d\n", rc); |
| 2226 | goto err_set_vtg_i2c; |
| 2227 | } |
| 2228 | |
| 2229 | rc = smb358_hw_init(chip); |
| 2230 | if (rc) { |
| 2231 | dev_err(&client->dev, |
| 2232 | "Couldn't intialize hardware rc=%d\n", rc); |
| 2233 | goto fail_smb358_hw_init; |
| 2234 | } |
| 2235 | |
| 2236 | rc = determine_initial_state(chip); |
| 2237 | if (rc) { |
| 2238 | dev_err(&client->dev, |
| 2239 | "Couldn't determine initial state rc=%d\n", rc); |
| 2240 | goto fail_smb358_hw_init; |
| 2241 | } |
| 2242 | |
| 2243 | /* We will not use it by default */ |
| 2244 | if (gpio_is_valid(chip->chg_valid_gpio)) { |
| 2245 | rc = gpio_request(chip->chg_valid_gpio, "smb358_chg_valid"); |
| 2246 | if (rc) { |
| 2247 | dev_err(&client->dev, |
| 2248 | "gpio_request for %d failed rc=%d\n", |
| 2249 | chip->chg_valid_gpio, rc); |
| 2250 | goto fail_chg_valid_irq; |
| 2251 | } |
| 2252 | irq = gpio_to_irq(chip->chg_valid_gpio); |
| 2253 | if (irq < 0) { |
| 2254 | dev_err(&client->dev, |
| 2255 | "Invalid chg_valid irq = %d\n", irq); |
| 2256 | goto fail_chg_valid_irq; |
| 2257 | } |
| 2258 | rc = devm_request_threaded_irq(&client->dev, irq, |
| 2259 | NULL, smb358_chg_valid_handler, |
| 2260 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 2261 | "smb358_chg_valid_irq", chip); |
| 2262 | if (rc) { |
| 2263 | dev_err(&client->dev, |
| 2264 | "Failed request_irq irq=%d, gpio=%d rc=%d\n", |
| 2265 | irq, chip->chg_valid_gpio, rc); |
| 2266 | goto fail_chg_valid_irq; |
| 2267 | } |
| 2268 | smb358_chg_valid_handler(irq, chip); |
| 2269 | enable_irq_wake(irq); |
| 2270 | } |
| 2271 | |
| 2272 | chip->irq_gpio = of_get_named_gpio_flags(chip->dev->of_node, |
| 2273 | "qcom,irq-gpio", 0, NULL); |
| 2274 | |
| 2275 | /* STAT irq configuration */ |
| 2276 | if (gpio_is_valid(chip->irq_gpio)) { |
| 2277 | rc = gpio_request(chip->irq_gpio, "smb358_irq"); |
| 2278 | if (rc) { |
| 2279 | dev_err(&client->dev, |
| 2280 | "irq gpio request failed, rc=%d", rc); |
| 2281 | goto fail_smb358_hw_init; |
| 2282 | } |
| 2283 | rc = gpio_direction_input(chip->irq_gpio); |
| 2284 | if (rc) { |
| 2285 | dev_err(&client->dev, |
| 2286 | "set_direction for irq gpio failed\n"); |
| 2287 | goto fail_irq_gpio; |
| 2288 | } |
| 2289 | |
| 2290 | irq = gpio_to_irq(chip->irq_gpio); |
| 2291 | if (irq < 0) { |
| 2292 | dev_err(&client->dev, |
| 2293 | "Invalid irq_gpio irq = %d\n", irq); |
| 2294 | goto fail_irq_gpio; |
| 2295 | } |
| 2296 | rc = devm_request_threaded_irq(&client->dev, irq, NULL, |
| 2297 | smb358_chg_stat_handler, |
| 2298 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 2299 | "smb358_chg_stat_irq", chip); |
| 2300 | if (rc) { |
| 2301 | dev_err(&client->dev, |
| 2302 | "Failed STAT irq=%d request rc = %d\n", |
| 2303 | irq, rc); |
| 2304 | goto fail_irq_gpio; |
| 2305 | } |
| 2306 | enable_irq_wake(irq); |
| 2307 | } else { |
| 2308 | goto fail_irq_gpio; |
| 2309 | } |
| 2310 | |
| 2311 | if (chip->using_pmic_therm) { |
| 2312 | /* add hot/cold temperature monitor */ |
| 2313 | chip->adc_param.low_temp = chip->cold_bat_decidegc; |
| 2314 | chip->adc_param.high_temp = chip->hot_bat_decidegc; |
| 2315 | chip->adc_param.timer_interval = ADC_MEAS2_INTERVAL_1S; |
| 2316 | chip->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2317 | chip->adc_param.btm_ctx = chip; |
| 2318 | chip->adc_param.threshold_notification = |
| 2319 | smb_chg_adc_notification; |
| 2320 | chip->adc_param.channel = LR_MUX1_BATT_THERM; |
| 2321 | |
| 2322 | /* update battery missing info in tm_channel_measure*/ |
| 2323 | rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev, |
| 2324 | &chip->adc_param); |
| 2325 | if (rc) |
| 2326 | pr_err("requesting ADC error %d\n", rc); |
| 2327 | } |
| 2328 | |
| 2329 | smb358_debugfs_init(chip); |
| 2330 | |
| 2331 | dump_regs(chip); |
| 2332 | |
| 2333 | dev_info(chip->dev, "SMB358 successfully probed. charger=%d, batt=%d\n", |
| 2334 | chip->chg_present, smb358_get_prop_batt_present(chip)); |
| 2335 | return 0; |
| 2336 | |
| 2337 | fail_chg_valid_irq: |
| 2338 | if (gpio_is_valid(chip->chg_valid_gpio)) |
| 2339 | gpio_free(chip->chg_valid_gpio); |
| 2340 | fail_irq_gpio: |
| 2341 | if (gpio_is_valid(chip->irq_gpio)) |
| 2342 | gpio_free(chip->irq_gpio); |
| 2343 | fail_smb358_hw_init: |
| 2344 | power_supply_unregister(&chip->batt_psy); |
| 2345 | regulator_unregister(chip->otg_vreg.rdev); |
| 2346 | err_set_vtg_i2c: |
| 2347 | if (regulator_count_voltages(chip->vcc_i2c) > 0) |
| 2348 | regulator_set_voltage(chip->vcc_i2c, 0, SMB_I2C_VTG_MAX_UV); |
| 2349 | err_get_vtg_i2c: |
| 2350 | regulator_put(chip->vcc_i2c); |
| 2351 | return rc; |
| 2352 | } |
| 2353 | |
| 2354 | static int smb358_charger_remove(struct i2c_client *client) |
| 2355 | { |
| 2356 | struct smb358_charger *chip = i2c_get_clientdata(client); |
| 2357 | |
| 2358 | power_supply_unregister(&chip->batt_psy); |
| 2359 | if (gpio_is_valid(chip->chg_valid_gpio)) |
| 2360 | gpio_free(chip->chg_valid_gpio); |
| 2361 | |
| 2362 | regulator_disable(chip->vcc_i2c); |
| 2363 | regulator_put(chip->vcc_i2c); |
| 2364 | mutex_destroy(&chip->irq_complete); |
| 2365 | debugfs_remove_recursive(chip->debug_root); |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
| 2369 | static int smb358_suspend(struct device *dev) |
| 2370 | { |
| 2371 | struct i2c_client *client = to_i2c_client(dev); |
| 2372 | struct smb358_charger *chip = i2c_get_clientdata(client); |
| 2373 | int rc; |
| 2374 | int i; |
| 2375 | |
| 2376 | for (i = 0; i < 2; i++) { |
| 2377 | rc = smb358_read_reg(chip, FAULT_INT_REG + i, |
| 2378 | &chip->irq_cfg_mask[i]); |
| 2379 | if (rc) |
| 2380 | dev_err(chip->dev, |
| 2381 | "Couldn't save irq cfg regs rc = %d\n", rc); |
| 2382 | } |
| 2383 | |
| 2384 | /* enable wake up IRQs */ |
| 2385 | rc = smb358_write_reg(chip, FAULT_INT_REG, |
| 2386 | FAULT_INT_HOT_COLD_HARD_BIT | FAULT_INT_INPUT_UV_BIT); |
| 2387 | if (rc < 0) |
| 2388 | dev_err(chip->dev, "Couldn't set fault_irq_cfg rc = %d\n", rc); |
| 2389 | |
| 2390 | rc = smb358_write_reg(chip, STATUS_INT_REG, |
| 2391 | STATUS_INT_LOW_BATT_BIT | STATUS_INT_MISSING_BATT_BIT | |
| 2392 | STATUS_INT_CHGING_BIT | STATUS_INT_INOK_BIT | |
| 2393 | STATUS_INT_OTG_DETECT_BIT | STATUS_INT_CHG_INHI_BIT); |
| 2394 | if (rc < 0) |
| 2395 | dev_err(chip->dev, |
| 2396 | "Couldn't set status_irq_cfg rc = %d\n", rc); |
| 2397 | |
| 2398 | mutex_lock(&chip->irq_complete); |
| 2399 | rc = regulator_disable(chip->vcc_i2c); |
| 2400 | if (rc) { |
| 2401 | dev_err(chip->dev, |
| 2402 | "Regulator vcc_i2c disable failed rc=%d\n", rc); |
| 2403 | mutex_unlock(&chip->irq_complete); |
| 2404 | return rc; |
| 2405 | } |
| 2406 | |
| 2407 | chip->resume_completed = false; |
| 2408 | mutex_unlock(&chip->irq_complete); |
| 2409 | return 0; |
| 2410 | } |
| 2411 | |
| 2412 | static int smb358_suspend_noirq(struct device *dev) |
| 2413 | { |
| 2414 | struct i2c_client *client = to_i2c_client(dev); |
| 2415 | struct smb358_charger *chip = i2c_get_clientdata(client); |
| 2416 | |
| 2417 | if (chip->irq_waiting) { |
| 2418 | pr_err_ratelimited("Aborting suspend, an interrupt was detected while suspending\n"); |
| 2419 | return -EBUSY; |
| 2420 | } |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | static int smb358_resume(struct device *dev) |
| 2425 | { |
| 2426 | struct i2c_client *client = to_i2c_client(dev); |
| 2427 | struct smb358_charger *chip = i2c_get_clientdata(client); |
| 2428 | int rc; |
| 2429 | int i; |
| 2430 | |
| 2431 | /* Restore IRQ config */ |
| 2432 | for (i = 0; i < 2; i++) { |
| 2433 | rc = smb358_write_reg(chip, FAULT_INT_REG + i, |
| 2434 | chip->irq_cfg_mask[i]); |
| 2435 | if (rc) |
| 2436 | dev_err(chip->dev, |
| 2437 | "Couldn't restore irq cfg regs rc=%d\n", rc); |
| 2438 | } |
| 2439 | |
| 2440 | mutex_lock(&chip->irq_complete); |
| 2441 | rc = regulator_enable(chip->vcc_i2c); |
| 2442 | if (rc) { |
| 2443 | dev_err(chip->dev, |
| 2444 | "Regulator vcc_i2c enable failed rc=%d\n", rc); |
| 2445 | mutex_unlock(&chip->irq_complete); |
| 2446 | return rc; |
| 2447 | } |
| 2448 | chip->resume_completed = true; |
| 2449 | |
| 2450 | mutex_unlock(&chip->irq_complete); |
| 2451 | if (chip->irq_waiting) { |
| 2452 | smb358_chg_stat_handler(client->irq, chip); |
| 2453 | enable_irq(client->irq); |
| 2454 | } |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | static const struct dev_pm_ops smb358_pm_ops = { |
| 2459 | .suspend = smb358_suspend, |
| 2460 | .suspend_noirq = smb358_suspend_noirq, |
| 2461 | .resume = smb358_resume, |
| 2462 | }; |
| 2463 | |
| 2464 | static struct of_device_id smb358_match_table[] = { |
| 2465 | { .compatible = "qcom,smb358-charger",}, |
| 2466 | { }, |
| 2467 | }; |
| 2468 | |
| 2469 | static const struct i2c_device_id smb358_charger_id[] = { |
| 2470 | {"smb358-charger", 0}, |
| 2471 | {}, |
| 2472 | }; |
| 2473 | MODULE_DEVICE_TABLE(i2c, smb358_charger_id); |
| 2474 | |
| 2475 | static struct i2c_driver smb358_charger_driver = { |
| 2476 | .driver = { |
| 2477 | .name = "smb358-charger", |
| 2478 | .owner = THIS_MODULE, |
| 2479 | .of_match_table = smb358_match_table, |
| 2480 | .pm = &smb358_pm_ops, |
| 2481 | }, |
| 2482 | .probe = smb358_charger_probe, |
| 2483 | .remove = smb358_charger_remove, |
| 2484 | .id_table = smb358_charger_id, |
| 2485 | }; |
| 2486 | |
| 2487 | module_i2c_driver(smb358_charger_driver); |
| 2488 | |
| 2489 | MODULE_DESCRIPTION("SMB358 Charger"); |
| 2490 | MODULE_LICENSE("GPL v2"); |
| 2491 | MODULE_ALIAS("i2c:smb358-charger"); |