--- a/drivers/power/supply/pm8916_lbc.c +++ b/drivers/power/supply/pm8916_lbc.c @@ -44,7 +44,28 @@ #define PM8916_LBC_CHGR_CHG_EN BIT(7) #define PM8916_LBC_CHGR_PSTG_EN BIT(5) +#define PM8916_LBC_CHGR_CHG_FAILED 0x4a +#define PM8916_LBC_CHGR_CHG_FAILED_CLEAR BIT(7) +#define PM8916_LBC_CHGR_USB_ENUM_T_STOP 0x4e +#define PM8916_LBC_CHGR_CHG_WDOG_EN 0x65 +#define PM8916_LBC_SEC_ACCESS 0xd0 +#define PM8916_LBC_SEC_MAGIC 0xa5 +#define PM8916_LBC_CHGR_CHG_COMP_OVR1 0xee +#define PM8916_LBC_CHGR_VDD_DET_OVR_0 0x02 + +#define PM8916_LBC_BAT_IF_BPD_CTRL 0x48 +#define PM8916_LBC_BAT_IF_BATT_THM_EN 0x02 +#define PM8916_LBC_BAT_IF_BTC_CTRL 0x4a +#define PM8916_LBC_BAT_IF_BTC_COMP_EN BIT(7) + +#define PM8916_LBC_USB_USB_SUSP 0x47 +#define PM8916_LBC_USB_ENUM_T_STOP 0x4e + +#define PM8916_LBC_MISC_BOOT_DONE 0x42 +#define PM8916_LBC_MISC_BOOT_DONE_BIT BIT(7) + #define PM8916_LBC_CHGR_MIN_CURRENT 90000 +#define PM8916_LBC_CHGR_DEFAULT_SAFE_CURRENT 540000 #define PM8916_LBC_CHGR_MAX_CURRENT 1440000 #define PM8916_LBC_CHGR_MIN_VOLTAGE 4000000 @@ -85,6 +106,60 @@ int ret = 0; unsigned int tmp; + /* Signal BOOT_DONE to LBC_MISC so post-boot charging current is allowed */ + ret = regmap_write(chg->regmap, + chg->reg[LBC_MISC] + PM8916_LBC_MISC_BOOT_DONE, + PM8916_LBC_MISC_BOOT_DONE_BIT); + if (ret) + goto error; + + /* Stop USB enumeration timers and ensure USB path is not suspended */ + (void)regmap_write(chg->regmap, + chg->reg[LBC_USB] + PM8916_LBC_USB_ENUM_T_STOP, 0x00); + (void)regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_USB_ENUM_T_STOP, 0x00); + (void)regmap_write(chg->regmap, + chg->reg[LBC_USB] + PM8916_LBC_USB_USB_SUSP, 0x00); + + /* Ensure battery thermistor detection is enabled and watchdog is off */ + (void)regmap_update_bits(chg->regmap, + chg->reg[LBC_BAT_IF] + PM8916_LBC_BAT_IF_BPD_CTRL, + 0x03, PM8916_LBC_BAT_IF_BATT_THM_EN); + (void)regmap_update_bits(chg->regmap, + chg->reg[LBC_BAT_IF] + PM8916_LBC_BAT_IF_BTC_CTRL, + PM8916_LBC_BAT_IF_BTC_COMP_EN, + PM8916_LBC_BAT_IF_BTC_COMP_EN); + (void)regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_WDOG_EN, 0x00); + (void)regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_TCHG_MAX_EN, 0x00); + + /* + * Override VBAT_DET comparator (CHG_COMP_OVR1 = 0x02) as required by + * Qualcomm qpnp-linear-charger so charging starts regardless of VBAT_DET. + * Note: SEC_ACCESS must be followed immediately by a direct regmap_write + * because any intervening SPMI read relocks SEC_ACCESS. + */ + ret = regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_SEC_ACCESS, + PM8916_LBC_SEC_MAGIC); + if (ret) + goto error; + ret = regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_COMP_OVR1, + PM8916_LBC_CHGR_VDD_DET_OVR_0); + if (ret) + goto error; + + /* Clear CHG_FAILED latch if set */ + if (regmap_read(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_FAILED, + &tmp) == 0 && tmp) { + (void)regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_FAILED, + PM8916_LBC_CHGR_CHG_FAILED_CLEAR); + } + chg->charge_voltage_max = clamp_t(u32, chg->charge_voltage_max, PM8916_LBC_CHGR_MIN_VOLTAGE, chg->charge_voltage_safe); @@ -187,6 +262,8 @@ return IRQ_HANDLED; chg->online = !!(tmp & PM8916_LBC_USB_USBIN_VALID); + if (chg->online) + pm8916_lbc_charger_configure(chg); extcon_set_state_sync(chg->edev, EXTCON_USB, chg->online); power_supply_changed(chg->charger); @@ -223,6 +300,8 @@ chg->charge_current_safe = clamp_t(u32, chg->charge_current_safe, PM8916_LBC_CHGR_MIN_CURRENT, PM8916_LBC_CHGR_MAX_CURRENT); + if (chg->charge_current_safe < PM8916_LBC_CHGR_DEFAULT_SAFE_CURRENT) + chg->charge_current_safe = PM8916_LBC_CHGR_DEFAULT_SAFE_CURRENT; chg->charge_current_max = chg->charge_current_safe; @@ -347,6 +426,18 @@ chg->online = !!(tmp & PM8916_LBC_USB_USBIN_VALID); extcon_set_state_sync(chg->edev, EXTCON_USB, chg->online); + if (chg->info->voltage_max_design_uv > chg->charge_voltage_safe) { + chg->charge_voltage_safe = clamp_t(u32, + chg->info->voltage_max_design_uv, + PM8916_LBC_CHGR_MIN_VOLTAGE, + PM8916_LBC_CHGR_MAX_VOLTAGE); + tmp = (chg->charge_voltage_safe - PM8916_LBC_CHGR_MIN_VOLTAGE) / + PM8916_LBC_CHGR_VOLTAGE_STEP; + (void)regmap_write(chg->regmap, + chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_VDD_SAFE, + tmp); + } + chg->charge_voltage_max = chg->info->voltage_max_design_uv; ret = pm8916_lbc_charger_configure(chg); if (ret) --- a/drivers/power/supply/pm8916_bms_vm.c +++ b/drivers/power/supply/pm8916_bms_vm.c @@ -12,12 +12,18 @@ #include #include #include +#include #include #include #define PM8916_PERPH_TYPE 0x04 #define PM8916_BMS_VM_TYPE 0x020D +#define PM8916_LBC_CHGR_INT_RT_STS 0x1010 +#define PM8916_LBC_CHGR_FAST_CHG_ON BIT(5) +#define PM8916_LBC_CHGR_TRKL_CHG_ON BIT(6) +#define PM8916_LBC_CHGR_CHG_DONE BIT(7) + #define PM8916_SEC_ACCESS 0xD0 #define PM8916_SEC_MAGIC 0xA5 @@ -56,13 +62,23 @@ unsigned int fake_ocv; }; +static unsigned int pm8916_bms_vm_raw_to_uv(u16 raw) +{ + /* + * Qualcomm VM-BMS VADC resolution is 97.65625 uV * 3 = 292.96875 uV/LSB + * (see qpnp-vm-bms.c convert_vbatt_raw_to_uv). + */ + return (unsigned int)div_u64((u64)raw * 292969ULL, 1000ULL); +} + static int pm8916_bms_vm_battery_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) { struct pm8916_bms_vm_battery *bat = power_supply_get_drvdata(psy); struct power_supply_battery_info *info = bat->info; - int supplied; + unsigned int chgr_sts = 0; + int supplied, cap; switch (psp) { case POWER_SUPPLY_PROP_STATUS: @@ -70,10 +86,23 @@ if (supplied < 0 && supplied != -ENODEV) return supplied; - else if (supplied && supplied != -ENODEV) - val->intval = POWER_SUPPLY_STATUS_CHARGING; - else + else if (supplied && supplied != -ENODEV) { + cap = power_supply_batinfo_ocv2cap(info, bat->fake_ocv, 20); + if (regmap_read(bat->regmap, PM8916_LBC_CHGR_INT_RT_STS, + &chgr_sts) == 0) { + if ((chgr_sts & PM8916_LBC_CHGR_CHG_DONE) || + (!(chgr_sts & (PM8916_LBC_CHGR_FAST_CHG_ON | + PM8916_LBC_CHGR_TRKL_CHG_ON)) && + cap >= 99)) { + val->intval = POWER_SUPPLY_STATUS_FULL; + return 0; + } + } + val->intval = (cap >= 100) ? POWER_SUPPLY_STATUS_FULL + : POWER_SUPPLY_STATUS_CHARGING; + } else { val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + } return 0; case POWER_SUPPLY_PROP_HEALTH: @@ -122,8 +151,9 @@ struct pm8916_bms_vm_battery *bat = data; struct power_supply_battery_info *info = bat->info; u16 vbat_data[PM8916_BMS_VM_FIFO_COUNT]; - int i, ret, delta = 0, loc_delta; - unsigned int tmp = 0; + u64 sum_uv = 0; + int i, ret, valid_cnt = 0; + unsigned int tmp = 0, chgr_sts = 0, target_ocv, chg_comp = 0, step; int supplied; ret = regmap_bulk_read(bat->regmap, bat->reg + PM8916_BMS_VM_BMS_FIFO_REG_0_LSB, @@ -139,49 +169,59 @@ return IRQ_HANDLED; for (i = 0; i < PM8916_BMS_VM_FIFO_COUNT; i++) { - tmp = vbat_data[i] * 300 - 100000; - - loc_delta = tmp - bat->vbat_now; - delta += loc_delta; - bat->vbat_now = tmp; + if (vbat_data[i] > 8000) { + sum_uv += pm8916_bms_vm_raw_to_uv(vbat_data[i]); + valid_cnt++; + } } + if (valid_cnt == 0) + return IRQ_HANDLED; - /* - * The VM-BMS hardware only collects voltage data and the software - * has to process it to calculate the OCV and SoC. Hardware provides - * up to 8 averaged measurements for software to take in account. - * - * NOTE: Since VM-BMS is mostly implemented in software, OCV needs to be estimated. - * This driver makes some assumptions to estimate OCV from averaged VBAT measurements - * and initial OCV measurements taken on boot or while in suspend: - * - * - When charger is online, ocv can only increase. - * - When charger is offline, ocv can only decrease and ocv > vbat. - * - ocv can't change more than 0.025v between the measurements. - * - When charger is in CV mode (vbat = const vbat-max), ocv increases by - * 0.004v every measurement until it reaches vbat. - * - * Those assumptions give somewhat realistic estimation of ocv and capacity, though - * in some worst case scenarios it will perform poorly. - * Ideally proper BMS algorithm should be implemented in userspace. - */ + bat->vbat_now = (unsigned int)div_u64(sum_uv, valid_cnt); - if ((supplied && delta > 0) || (!supplied && delta < 0)) - if (abs(delta) < 25000) /* 0.025v */ - bat->fake_ocv += delta; + if (supplied) { + (void)regmap_read(bat->regmap, PM8916_LBC_CHGR_INT_RT_STS, &chgr_sts); + if (chgr_sts & PM8916_LBC_CHGR_CHG_DONE) { + target_ocv = max_t(unsigned int, bat->vbat_now, + info->voltage_max_design_uv); + } else if (chgr_sts & (PM8916_LBC_CHGR_FAST_CHG_ON | + PM8916_LBC_CHGR_TRKL_CHG_ON)) { + /* + * Compensate for CC charging IR rise (~60 mV below 4.15 V, + * tapering linearly to 0 mV as CV approaches 4.35 V). + */ + if (bat->vbat_now >= 4350000) + chg_comp = 0; + else if (bat->vbat_now <= 4150000) + chg_comp = 60000; + else + chg_comp = (4350000 - bat->vbat_now) * 60 / 200; + target_ocv = bat->vbat_now - chg_comp; + } else { + target_ocv = bat->vbat_now; + } + } else { + /* Compensate ~12 mV for baseline idle discharge load */ + target_ocv = bat->vbat_now + 12000; + } - if (!supplied && bat->fake_ocv < bat->vbat_now) - bat->fake_ocv = bat->vbat_now; + if (bat->fake_ocv == 0 || + abs((int)target_ocv - (int)bat->fake_ocv) > 150000) { + bat->fake_ocv = target_ocv; + } else if (target_ocv > bat->fake_ocv) { + step = min_t(unsigned int, 15000, + max_t(unsigned int, 1000, (target_ocv - bat->fake_ocv) / 6)); + bat->fake_ocv = min(target_ocv, bat->fake_ocv + step); + } else if (target_ocv < bat->fake_ocv) { + step = min_t(unsigned int, 15000, + max_t(unsigned int, 1000, (bat->fake_ocv - target_ocv) / 8)); + bat->fake_ocv = max(target_ocv, bat->fake_ocv - step); + } regmap_write(bat->regmap, bat->reg + PM8916_BMS_VM_STATUS1, 0); regmap_read(bat->regmap, bat->reg + PM8916_BMS_VM_STATUS1, &tmp); - if (PM8916_BMS_VM_FSM_STATE(tmp) == PM8916_BMS_VM_FSM_STATE_S2 && - bat->fake_ocv < bat->vbat_now - 10000 /* 0.01v */) { - bat->fake_ocv += 4000; /* 0.004v */ - } - - if (supplied && bat->fake_ocv > info->voltage_max_design_uv) + if (bat->fake_ocv > info->voltage_max_design_uv) bat->fake_ocv = info->voltage_max_design_uv; power_supply_changed(bat->battery); @@ -258,10 +298,23 @@ goto comm_error; bat->last_ocv_time = ktime_get_seconds(); - bat->last_ocv = tmp * 300; + bat->last_ocv = pm8916_bms_vm_raw_to_uv((u16)(tmp & 0xffff)); bat->vbat_now = bat->last_ocv; bat->fake_ocv = bat->last_ocv; + { + u16 fifo_init[PM8916_BMS_VM_FIFO_COUNT] = {}; + + if (regmap_bulk_read(bat->regmap, + bat->reg + PM8916_BMS_VM_BMS_FIFO_REG_0_LSB, + &fifo_init, + PM8916_BMS_VM_FIFO_COUNT * 2) == 0 && + fifo_init[0] > 8000) { + bat->vbat_now = pm8916_bms_vm_raw_to_uv(fifo_init[0]); + bat->fake_ocv = bat->vbat_now + 12000; + } + } + psy_cfg.drv_data = bat; psy_cfg.of_node = dev->of_node; @@ -318,7 +371,7 @@ bat->reg + PM8916_BMS_VM_S3_S7_OCV_DATA0, &tmp, 2); bat->last_ocv_time = ktime_get_seconds(); - bat->last_ocv = tmp * 300; + bat->last_ocv = pm8916_bms_vm_raw_to_uv((u16)(tmp & 0xffff)); bat->fake_ocv = bat->last_ocv; ret = regmap_write(bat->regmap, --- a/drivers/net/wireless/ath/wcn36xx/pmc.c +++ b/drivers/net/wireless/ath/wcn36xx/pmc.c @@ -25,6 +25,10 @@ { int ret = 0; struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif); + + if (!vif->cfg.assoc || !vif->bss_conf.sync_tsf) + return -EAGAIN; + /* TODO: Make sure the TX chain clean */ ret = wcn36xx_smd_enter_bmps(wcn, vif); if (!ret) { @@ -38,12 +42,7 @@ * driver is trying to enter bmps before first beacon was * received just after auth complete */ - wcn36xx_err("Can not enter BMPS!\n"); - - if (vif_priv->bmps_fail_ct++ == WCN36XX_BMPS_FAIL_THREHOLD) { - ieee80211_connection_loss(vif); - vif_priv->bmps_fail_ct = 0; - } + wcn36xx_dbg(WCN36XX_DBG_PMC, "Can not enter BMPS yet\n"); } return ret; } --- a/drivers/net/wireless/ath/wcn36xx/main.c +++ b/drivers/net/wireless/ath/wcn36xx/main.c @@ -1454,6 +1454,8 @@ #endif wcn->hw->max_listen_interval = 200; + wcn->hw->conf.dynamic_ps_timeout = 200; + wcn->hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT; wcn->hw->queues = 4;