| /* Copyright (c) 2018 The Linux Foundation. All rights reserved. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 and |
| * only version 2 as published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| */ |
| |
| #include <linux/debugfs.h> |
| #include <linux/delay.h> |
| #include <linux/device.h> |
| #include <linux/module.h> |
| #include <linux/platform_device.h> |
| #include <linux/regmap.h> |
| #include <linux/power_supply.h> |
| #include <linux/of.h> |
| #include <linux/of_irq.h> |
| #include <linux/log2.h> |
| #include <linux/qpnp/qpnp-revid.h> |
| #include <linux/regulator/driver.h> |
| #include <linux/regulator/of_regulator.h> |
| #include <linux/regulator/machine.h> |
| #include <linux/pmic-voter.h> |
| #include <linux/qpnp/qpnp-adc.h> |
| #include "smb5-reg.h" |
| #include "smb5-lib.h" |
| #include "schgm-flash.h" |
| |
| static struct smb_params smb5_pmi632_params = { |
| .fcc = { |
| .name = "fast charge current", |
| .reg = CHGR_FAST_CHARGE_CURRENT_CFG_REG, |
| .min_u = 0, |
| .max_u = 3000000, |
| .step_u = 50000, |
| }, |
| .fv = { |
| .name = "float voltage", |
| .reg = CHGR_FLOAT_VOLTAGE_CFG_REG, |
| .min_u = 3600000, |
| .max_u = 4800000, |
| .step_u = 10000, |
| }, |
| .usb_icl = { |
| .name = "usb input current limit", |
| .reg = USBIN_CURRENT_LIMIT_CFG_REG, |
| .min_u = 0, |
| .max_u = 3000000, |
| .step_u = 50000, |
| }, |
| .icl_max_stat = { |
| .name = "dcdc icl max status", |
| .reg = ICL_MAX_STATUS_REG, |
| .min_u = 0, |
| .max_u = 3000000, |
| .step_u = 50000, |
| }, |
| .icl_stat = { |
| .name = "input current limit status", |
| .reg = ICL_STATUS_REG, |
| .min_u = 0, |
| .max_u = 3000000, |
| .step_u = 50000, |
| }, |
| .otg_cl = { |
| .name = "usb otg current limit", |
| .reg = DCDC_OTG_CURRENT_LIMIT_CFG_REG, |
| .min_u = 500000, |
| .max_u = 1000000, |
| .step_u = 250000, |
| }, |
| .jeita_cc_comp_hot = { |
| .name = "jeita fcc reduction", |
| .reg = JEITA_CCCOMP_CFG_HOT_REG, |
| .min_u = 0, |
| .max_u = 1575000, |
| .step_u = 25000, |
| }, |
| .jeita_cc_comp_cold = { |
| .name = "jeita fcc reduction", |
| .reg = JEITA_CCCOMP_CFG_COLD_REG, |
| .min_u = 0, |
| .max_u = 1575000, |
| .step_u = 25000, |
| }, |
| .freq_switcher = { |
| .name = "switching frequency", |
| .reg = DCDC_FSW_SEL_REG, |
| .min_u = 600, |
| .max_u = 1200, |
| .step_u = 400, |
| .set_proc = smblib_set_chg_freq, |
| }, |
| }; |
| |
| static struct smb_params smb5_pm855b_params = { |
| .fcc = { |
| .name = "fast charge current", |
| .reg = CHGR_FAST_CHARGE_CURRENT_CFG_REG, |
| .min_u = 0, |
| .max_u = 8000000, |
| .step_u = 25000, |
| }, |
| .fv = { |
| .name = "float voltage", |
| .reg = CHGR_FLOAT_VOLTAGE_CFG_REG, |
| .min_u = 3600000, |
| .max_u = 4790000, |
| .step_u = 10000, |
| }, |
| .usb_icl = { |
| .name = "usb input current limit", |
| .reg = USBIN_CURRENT_LIMIT_CFG_REG, |
| .min_u = 0, |
| .max_u = 5000000, |
| .step_u = 50000, |
| }, |
| .icl_max_stat = { |
| .name = "dcdc icl max status", |
| .reg = ICL_MAX_STATUS_REG, |
| .min_u = 0, |
| .max_u = 5000000, |
| .step_u = 50000, |
| }, |
| .icl_stat = { |
| .name = "aicl icl status", |
| .reg = AICL_ICL_STATUS_REG, |
| .min_u = 0, |
| .max_u = 5000000, |
| .step_u = 50000, |
| }, |
| .otg_cl = { |
| .name = "usb otg current limit", |
| .reg = DCDC_OTG_CURRENT_LIMIT_CFG_REG, |
| .min_u = 500000, |
| .max_u = 3000000, |
| .step_u = 500000, |
| }, |
| .jeita_cc_comp_hot = { |
| .name = "jeita fcc reduction", |
| .reg = JEITA_CCCOMP_CFG_HOT_REG, |
| .min_u = 0, |
| .max_u = 8000000, |
| .step_u = 25000, |
| .set_proc = NULL, |
| }, |
| .jeita_cc_comp_cold = { |
| .name = "jeita fcc reduction", |
| .reg = JEITA_CCCOMP_CFG_COLD_REG, |
| .min_u = 0, |
| .max_u = 8000000, |
| .step_u = 25000, |
| .set_proc = NULL, |
| }, |
| .freq_switcher = { |
| .name = "switching frequency", |
| .reg = DCDC_FSW_SEL_REG, |
| .min_u = 1200, |
| .max_u = 2400, |
| .step_u = 400, |
| .set_proc = NULL, |
| }, |
| }; |
| |
| struct smb_dt_props { |
| int usb_icl_ua; |
| struct device_node *revid_dev_node; |
| enum float_options float_option; |
| int chg_inhibit_thr_mv; |
| bool no_battery; |
| bool hvdcp_disable; |
| int auto_recharge_soc; |
| int auto_recharge_vbat_mv; |
| int wd_bark_time; |
| int batt_profile_fcc_ua; |
| int batt_profile_fv_uv; |
| }; |
| |
| struct smb5 { |
| struct smb_charger chg; |
| struct dentry *dfs_root; |
| struct smb_dt_props dt; |
| }; |
| |
| static int __debug_mask; |
| module_param_named( |
| debug_mask, __debug_mask, int, 0600 |
| ); |
| |
| static int __pd_disabled; |
| module_param_named( |
| pd_disabled, __pd_disabled, int, 0600 |
| ); |
| |
| static int __weak_chg_icl_ua = 500000; |
| module_param_named( |
| weak_chg_icl_ua, __weak_chg_icl_ua, int, 0600 |
| ); |
| |
| enum { |
| USBIN_CURRENT, |
| USBIN_VOLTAGE, |
| }; |
| |
| enum { |
| BAT_THERM = 0, |
| MISC_THERM, |
| CONN_THERM, |
| SMB_THERM, |
| }; |
| |
| #define PMI632_MAX_ICL_UA 3000000 |
| static int smb5_chg_config_init(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| struct pmic_revid_data *pmic_rev_id; |
| struct device_node *revid_dev_node; |
| int rc = 0; |
| |
| revid_dev_node = of_parse_phandle(chip->chg.dev->of_node, |
| "qcom,pmic-revid", 0); |
| if (!revid_dev_node) { |
| pr_err("Missing qcom,pmic-revid property\n"); |
| return -EINVAL; |
| } |
| |
| pmic_rev_id = get_revid_data(revid_dev_node); |
| if (IS_ERR_OR_NULL(pmic_rev_id)) { |
| /* |
| * the revid peripheral must be registered, any failure |
| * here only indicates that the rev-id module has not |
| * probed yet. |
| */ |
| rc = -EPROBE_DEFER; |
| goto out; |
| } |
| |
| switch (pmic_rev_id->pmic_subtype) { |
| case PM855B_SUBTYPE: |
| chip->chg.smb_version = PM855B_SUBTYPE; |
| chg->param = smb5_pm855b_params; |
| chg->name = "pm855b_charger"; |
| break; |
| case PMI632_SUBTYPE: |
| chip->chg.smb_version = PMI632_SUBTYPE; |
| chg->param = smb5_pmi632_params; |
| chg->use_extcon = true; |
| chg->name = "pmi632_charger"; |
| /* PMI632 does not support PD */ |
| chg->pd_not_supported = true; |
| chg->hw_max_icl_ua = |
| (chip->dt.usb_icl_ua > 0) ? chip->dt.usb_icl_ua |
| : PMI632_MAX_ICL_UA; |
| chg->chg_freq.freq_5V = 600; |
| chg->chg_freq.freq_6V_8V = 800; |
| chg->chg_freq.freq_9V = 1050; |
| chg->chg_freq.freq_removal = 1050; |
| chg->chg_freq.freq_below_otg_threshold = 800; |
| chg->chg_freq.freq_above_otg_threshold = 800; |
| break; |
| default: |
| pr_err("PMIC subtype %d not supported\n", |
| pmic_rev_id->pmic_subtype); |
| rc = -EINVAL; |
| } |
| |
| out: |
| of_node_put(revid_dev_node); |
| return rc; |
| } |
| |
| #define PULL_NO_PULL 0 |
| #define PULL_30K 30 |
| #define PULL_100K 100 |
| #define PULL_400K 400 |
| static int get_valid_pullup(int pull_up) |
| { |
| int pull; |
| |
| /* pull up can only be 0/30K/100K/400K) */ |
| switch (pull_up) { |
| case PULL_NO_PULL: |
| pull = INTERNAL_PULL_NO_PULL; |
| break; |
| case PULL_30K: |
| pull = INTERNAL_PULL_30K_PULL; |
| break; |
| case PULL_100K: |
| pull = INTERNAL_PULL_100K_PULL; |
| break; |
| case PULL_400K: |
| pull = INTERNAL_PULL_400K_PULL; |
| break; |
| default: |
| pull = INTERNAL_PULL_100K_PULL; |
| } |
| |
| return pull; |
| } |
| |
| #define INTERNAL_PULL_UP_MASK 0x3 |
| static int smb5_configure_internal_pull(struct smb_charger *chg, int type, |
| int pull) |
| { |
| int rc; |
| int shift = type * 2; |
| u8 mask = INTERNAL_PULL_UP_MASK << shift; |
| u8 val = pull << shift; |
| |
| rc = smblib_masked_write(chg, BATIF_ADC_INTERNAL_PULL_UP_REG, |
| mask, val); |
| if (rc < 0) |
| dev_err(chg->dev, |
| "Couldn't configure ADC pull-up reg rc=%d\n", rc); |
| |
| return rc; |
| } |
| |
| #define MICRO_1P5A 1500000 |
| #define MICRO_1PA 1000000 |
| #define MICRO_P1A 100000 |
| #define OTG_DEFAULT_DEGLITCH_TIME_MS 50 |
| #define MIN_WD_BARK_TIME 16 |
| #define DEFAULT_WD_BARK_TIME 64 |
| #define BITE_WDOG_TIMEOUT_8S 0x3 |
| #define BARK_WDOG_TIMEOUT_MASK GENMASK(3, 2) |
| #define BARK_WDOG_TIMEOUT_SHIFT 2 |
| static int smb5_parse_dt(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| struct device_node *node = chg->dev->of_node; |
| int rc, byte_len; |
| |
| if (!node) { |
| pr_err("device tree node missing\n"); |
| return -EINVAL; |
| } |
| |
| chg->step_chg_enabled = of_property_read_bool(node, |
| "qcom,step-charging-enable"); |
| |
| chg->sw_jeita_enabled = of_property_read_bool(node, |
| "qcom,sw-jeita-enable"); |
| |
| rc = of_property_read_u32(node, "qcom,wd-bark-time-secs", |
| &chip->dt.wd_bark_time); |
| if (rc < 0 || chip->dt.wd_bark_time < MIN_WD_BARK_TIME) |
| chip->dt.wd_bark_time = DEFAULT_WD_BARK_TIME; |
| |
| chip->dt.no_battery = of_property_read_bool(node, |
| "qcom,batteryless-platform"); |
| |
| rc = of_property_read_u32(node, |
| "qcom,fcc-max-ua", &chip->dt.batt_profile_fcc_ua); |
| if (rc < 0) |
| chip->dt.batt_profile_fcc_ua = -EINVAL; |
| |
| rc = of_property_read_u32(node, |
| "qcom,fv-max-uv", &chip->dt.batt_profile_fv_uv); |
| if (rc < 0) |
| chip->dt.batt_profile_fv_uv = -EINVAL; |
| |
| rc = of_property_read_u32(node, |
| "qcom,usb-icl-ua", &chip->dt.usb_icl_ua); |
| if (rc < 0) |
| chip->dt.usb_icl_ua = -EINVAL; |
| |
| rc = of_property_read_u32(node, |
| "qcom,otg-cl-ua", &chg->otg_cl_ua); |
| if (rc < 0) |
| chg->otg_cl_ua = (chip->chg.smb_version == PMI632_SUBTYPE) ? |
| MICRO_1PA : MICRO_1P5A; |
| |
| if (of_find_property(node, "qcom,thermal-mitigation", &byte_len)) { |
| chg->thermal_mitigation = devm_kzalloc(chg->dev, byte_len, |
| GFP_KERNEL); |
| |
| if (chg->thermal_mitigation == NULL) |
| return -ENOMEM; |
| |
| chg->thermal_levels = byte_len / sizeof(u32); |
| rc = of_property_read_u32_array(node, |
| "qcom,thermal-mitigation", |
| chg->thermal_mitigation, |
| chg->thermal_levels); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't read threm limits rc = %d\n", rc); |
| return rc; |
| } |
| } |
| |
| rc = of_property_read_u32(node, "qcom,float-option", |
| &chip->dt.float_option); |
| if (!rc && (chip->dt.float_option < 0 || chip->dt.float_option > 4)) { |
| pr_err("qcom,float-option is out of range [0, 4]\n"); |
| return -EINVAL; |
| } |
| |
| chip->dt.hvdcp_disable = of_property_read_bool(node, |
| "qcom,hvdcp-disable"); |
| |
| |
| rc = of_property_read_u32(node, "qcom,chg-inhibit-threshold-mv", |
| &chip->dt.chg_inhibit_thr_mv); |
| if (!rc && (chip->dt.chg_inhibit_thr_mv < 0 || |
| chip->dt.chg_inhibit_thr_mv > 300)) { |
| pr_err("qcom,chg-inhibit-threshold-mv is incorrect\n"); |
| return -EINVAL; |
| } |
| |
| chip->dt.auto_recharge_soc = -EINVAL; |
| rc = of_property_read_u32(node, "qcom,auto-recharge-soc", |
| &chip->dt.auto_recharge_soc); |
| if (!rc && (chip->dt.auto_recharge_soc < 0 || |
| chip->dt.auto_recharge_soc > 100)) { |
| pr_err("qcom,auto-recharge-soc is incorrect\n"); |
| return -EINVAL; |
| } |
| chg->auto_recharge_soc = chip->dt.auto_recharge_soc; |
| |
| chip->dt.auto_recharge_vbat_mv = -EINVAL; |
| rc = of_property_read_u32(node, "qcom,auto-recharge-vbat-mv", |
| &chip->dt.auto_recharge_vbat_mv); |
| if (!rc && (chip->dt.auto_recharge_vbat_mv < 0)) { |
| pr_err("qcom,auto-recharge-vbat-mv is incorrect\n"); |
| return -EINVAL; |
| } |
| |
| chg->dcp_icl_ua = chip->dt.usb_icl_ua; |
| |
| chg->suspend_input_on_debug_batt = of_property_read_bool(node, |
| "qcom,suspend-input-on-debug-batt"); |
| |
| rc = of_property_read_u32(node, "qcom,otg-deglitch-time-ms", |
| &chg->otg_delay_ms); |
| if (rc < 0) |
| chg->otg_delay_ms = OTG_DEFAULT_DEGLITCH_TIME_MS; |
| |
| chg->hw_die_temp_mitigation = of_property_read_bool(node, |
| "qcom,hw-die-temp-mitigation"); |
| |
| chg->hw_connector_mitigation = of_property_read_bool(node, |
| "qcom,hw-connector-mitigation"); |
| |
| chg->connector_pull_up = -EINVAL; |
| of_property_read_u32(node, "qcom,connector-internal-pull-kohm", |
| &chg->connector_pull_up); |
| |
| return 0; |
| } |
| |
| static int smb5_get_adc_data(struct smb_charger *chg, int channel, |
| union power_supply_propval *val) |
| { |
| int rc; |
| struct qpnp_vadc_result result; |
| |
| if (!chg->vadc_dev) { |
| if (of_find_property(chg->dev->of_node, "qcom,chg-vadc", |
| NULL)) { |
| chg->vadc_dev = qpnp_get_vadc(chg->dev, "chg"); |
| if (IS_ERR(chg->vadc_dev)) { |
| rc = PTR_ERR(chg->vadc_dev); |
| if (rc != -EPROBE_DEFER) |
| pr_debug("Failed to find VADC node, rc=%d\n", |
| rc); |
| else |
| chg->vadc_dev = NULL; |
| |
| return rc; |
| } |
| } else { |
| return -ENODATA; |
| } |
| } |
| |
| if (IS_ERR(chg->vadc_dev)) |
| return PTR_ERR(chg->vadc_dev); |
| |
| switch (channel) { |
| case USBIN_VOLTAGE: |
| rc = qpnp_vadc_read(chg->vadc_dev, VADC_USB_IN_V_DIV_16_PM5, |
| &result); |
| if (rc < 0) { |
| pr_err("Failed to read USBIN_V over vadc, rc=%d\n", rc); |
| return rc; |
| } |
| val->intval = result.physical; |
| break; |
| case USBIN_CURRENT: |
| rc = qpnp_vadc_read(chg->vadc_dev, VADC_USB_IN_I_PM5, &result); |
| if (rc < 0) { |
| pr_err("Failed to read USBIN_I over vadc, rc=%d\n", rc); |
| return rc; |
| } |
| val->intval = result.physical; |
| break; |
| default: |
| pr_debug("Invalid channel\n"); |
| return -EINVAL; |
| } |
| |
| return 0; |
| } |
| |
| |
| /************************ |
| * USB PSY REGISTRATION * |
| ************************/ |
| static enum power_supply_property smb5_usb_props[] = { |
| POWER_SUPPLY_PROP_PRESENT, |
| POWER_SUPPLY_PROP_ONLINE, |
| POWER_SUPPLY_PROP_PD_CURRENT_MAX, |
| POWER_SUPPLY_PROP_CURRENT_MAX, |
| POWER_SUPPLY_PROP_TYPE, |
| POWER_SUPPLY_PROP_TYPEC_MODE, |
| POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, |
| POWER_SUPPLY_PROP_TYPEC_CC_ORIENTATION, |
| POWER_SUPPLY_PROP_PD_ACTIVE, |
| POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED, |
| POWER_SUPPLY_PROP_INPUT_CURRENT_NOW, |
| POWER_SUPPLY_PROP_BOOST_CURRENT, |
| POWER_SUPPLY_PROP_PE_START, |
| POWER_SUPPLY_PROP_CTM_CURRENT_MAX, |
| POWER_SUPPLY_PROP_HW_CURRENT_MAX, |
| POWER_SUPPLY_PROP_REAL_TYPE, |
| POWER_SUPPLY_PROP_PR_SWAP, |
| POWER_SUPPLY_PROP_PD_VOLTAGE_MAX, |
| POWER_SUPPLY_PROP_PD_VOLTAGE_MIN, |
| POWER_SUPPLY_PROP_SDP_CURRENT_MAX, |
| POWER_SUPPLY_PROP_CONNECTOR_TYPE, |
| POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| POWER_SUPPLY_PROP_SCOPE, |
| POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| POWER_SUPPLY_PROP_HVDCP_OPTI_ALLOWED, |
| POWER_SUPPLY_PROP_QC_OPTI_DISABLE, |
| }; |
| |
| static int smb5_usb_get_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| union power_supply_propval pval; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_PRESENT: |
| rc = smblib_get_prop_usb_present(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_ONLINE: |
| rc = smblib_get_prop_usb_online(chg, val); |
| if (!val->intval) |
| break; |
| |
| if (((chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) || |
| (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB)) |
| && (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)) |
| val->intval = 0; |
| else |
| val->intval = 1; |
| |
| if (chg->real_charger_type == POWER_SUPPLY_TYPE_UNKNOWN) |
| val->intval = 0; |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| rc = smblib_get_prop_usb_voltage_max(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| rc = smblib_get_prop_usb_voltage_max_design(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_CURRENT_MAX: |
| val->intval = get_client_vote(chg->usb_icl_votable, PD_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_CURRENT_MAX: |
| rc = smblib_get_prop_input_current_settled(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_TYPE: |
| val->intval = POWER_SUPPLY_TYPE_USB_PD; |
| break; |
| case POWER_SUPPLY_PROP_REAL_TYPE: |
| val->intval = chg->real_charger_type; |
| break; |
| case POWER_SUPPLY_PROP_TYPEC_MODE: |
| if (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB) |
| val->intval = POWER_SUPPLY_TYPEC_NONE; |
| else |
| val->intval = chg->typec_mode; |
| break; |
| case POWER_SUPPLY_PROP_TYPEC_POWER_ROLE: |
| if (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB) |
| val->intval = POWER_SUPPLY_TYPEC_PR_NONE; |
| else |
| rc = smblib_get_prop_typec_power_role(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_TYPEC_CC_ORIENTATION: |
| if (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB) |
| val->intval = 0; |
| else |
| rc = smblib_get_prop_typec_cc_orientation(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_ACTIVE: |
| val->intval = chg->pd_active; |
| break; |
| case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED: |
| rc = smblib_get_prop_input_current_settled(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_BOOST_CURRENT: |
| val->intval = chg->boost_current_ua; |
| break; |
| case POWER_SUPPLY_PROP_PD_IN_HARD_RESET: |
| rc = smblib_get_prop_pd_in_hard_reset(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_USB_SUSPEND_SUPPORTED: |
| val->intval = chg->system_suspend_supported; |
| break; |
| case POWER_SUPPLY_PROP_PE_START: |
| rc = smblib_get_pe_start(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CTM_CURRENT_MAX: |
| val->intval = get_client_vote(chg->usb_icl_votable, CTM_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_HW_CURRENT_MAX: |
| rc = smblib_get_charge_current(chg, &val->intval); |
| break; |
| case POWER_SUPPLY_PROP_PR_SWAP: |
| rc = smblib_get_prop_pr_swap_in_progress(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_VOLTAGE_MAX: |
| val->intval = chg->voltage_max_uv; |
| break; |
| case POWER_SUPPLY_PROP_PD_VOLTAGE_MIN: |
| val->intval = chg->voltage_min_uv; |
| break; |
| case POWER_SUPPLY_PROP_SDP_CURRENT_MAX: |
| val->intval = get_client_vote(chg->usb_icl_votable, |
| USB_PSY_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_CONNECTOR_TYPE: |
| val->intval = chg->connector_type; |
| break; |
| case POWER_SUPPLY_PROP_SCOPE: |
| val->intval = POWER_SUPPLY_SCOPE_UNKNOWN; |
| rc = smblib_get_prop_usb_present(chg, &pval); |
| if (rc < 0) |
| break; |
| val->intval = pval.intval ? POWER_SUPPLY_SCOPE_DEVICE |
| : chg->otg_present ? POWER_SUPPLY_SCOPE_SYSTEM |
| : POWER_SUPPLY_SCOPE_UNKNOWN; |
| break; |
| case POWER_SUPPLY_PROP_INPUT_CURRENT_NOW: |
| rc = smblib_get_prop_usb_present(chg, &pval); |
| if (rc < 0 || !pval.intval) { |
| val->intval = 0; |
| return rc; |
| } |
| if (chg->smb_version == PMI632_SUBTYPE) |
| rc = smb5_get_adc_data(chg, USBIN_CURRENT, val); |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| if (chg->smb_version == PMI632_SUBTYPE) |
| rc = smb5_get_adc_data(chg, USBIN_VOLTAGE, val); |
| break; |
| case POWER_SUPPLY_PROP_HVDCP_OPTI_ALLOWED: |
| val->intval = !chg->flash_active; |
| break; |
| case POWER_SUPPLY_PROP_QC_OPTI_DISABLE: |
| if (chg->hw_die_temp_mitigation) |
| val->intval = POWER_SUPPLY_QC_THERMAL_BALANCE_DISABLE |
| | POWER_SUPPLY_QC_INOV_THERMAL_DISABLE; |
| if (chg->hw_connector_mitigation) |
| val->intval |= POWER_SUPPLY_QC_CTM_DISABLE; |
| break; |
| default: |
| pr_err("get prop %d is not supported in usb\n", psp); |
| rc = -EINVAL; |
| break; |
| } |
| |
| if (rc < 0) { |
| pr_debug("Couldn't get prop %d rc = %d\n", psp, rc); |
| return -ENODATA; |
| } |
| |
| return 0; |
| } |
| |
| static int smb5_usb_set_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| const union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_PD_CURRENT_MAX: |
| rc = smblib_set_prop_pd_current_max(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_TYPEC_POWER_ROLE: |
| rc = smblib_set_prop_typec_power_role(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_ACTIVE: |
| rc = smblib_set_prop_pd_active(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_IN_HARD_RESET: |
| rc = smblib_set_prop_pd_in_hard_reset(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_USB_SUSPEND_SUPPORTED: |
| chg->system_suspend_supported = val->intval; |
| break; |
| case POWER_SUPPLY_PROP_BOOST_CURRENT: |
| rc = smblib_set_prop_boost_current(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CTM_CURRENT_MAX: |
| rc = vote(chg->usb_icl_votable, CTM_VOTER, |
| val->intval >= 0, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_PR_SWAP: |
| rc = smblib_set_prop_pr_swap_in_progress(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_VOLTAGE_MAX: |
| rc = smblib_set_prop_pd_voltage_max(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PD_VOLTAGE_MIN: |
| rc = smblib_set_prop_pd_voltage_min(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_SDP_CURRENT_MAX: |
| rc = smblib_set_prop_sdp_current_max(chg, val); |
| break; |
| default: |
| pr_err("set prop %d is not supported\n", psp); |
| rc = -EINVAL; |
| break; |
| } |
| |
| return rc; |
| } |
| |
| static int smb5_usb_prop_is_writeable(struct power_supply *psy, |
| enum power_supply_property psp) |
| { |
| switch (psp) { |
| case POWER_SUPPLY_PROP_CTM_CURRENT_MAX: |
| return 1; |
| default: |
| break; |
| } |
| |
| return 0; |
| } |
| |
| static const struct power_supply_desc usb_psy_desc = { |
| .name = "usb", |
| .type = POWER_SUPPLY_TYPE_USB_PD, |
| .properties = smb5_usb_props, |
| .num_properties = ARRAY_SIZE(smb5_usb_props), |
| .get_property = smb5_usb_get_prop, |
| .set_property = smb5_usb_set_prop, |
| .property_is_writeable = smb5_usb_prop_is_writeable, |
| }; |
| |
| static int smb5_init_usb_psy(struct smb5 *chip) |
| { |
| struct power_supply_config usb_cfg = {}; |
| struct smb_charger *chg = &chip->chg; |
| |
| usb_cfg.drv_data = chip; |
| usb_cfg.of_node = chg->dev->of_node; |
| chg->usb_psy = devm_power_supply_register(chg->dev, |
| &usb_psy_desc, |
| &usb_cfg); |
| if (IS_ERR(chg->usb_psy)) { |
| pr_err("Couldn't register USB power supply\n"); |
| return PTR_ERR(chg->usb_psy); |
| } |
| |
| return 0; |
| } |
| |
| /******************************** |
| * USB PC_PORT PSY REGISTRATION * |
| ********************************/ |
| static enum power_supply_property smb5_usb_port_props[] = { |
| POWER_SUPPLY_PROP_TYPE, |
| POWER_SUPPLY_PROP_ONLINE, |
| POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| POWER_SUPPLY_PROP_CURRENT_MAX, |
| }; |
| |
| static int smb5_usb_port_get_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_TYPE: |
| val->intval = POWER_SUPPLY_TYPE_USB; |
| break; |
| case POWER_SUPPLY_PROP_ONLINE: |
| rc = smblib_get_prop_usb_online(chg, val); |
| if (!val->intval) |
| break; |
| |
| if (((chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) || |
| (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB)) |
| && (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)) |
| val->intval = 1; |
| else |
| val->intval = 0; |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| val->intval = 5000000; |
| break; |
| case POWER_SUPPLY_PROP_CURRENT_MAX: |
| rc = smblib_get_prop_input_current_settled(chg, val); |
| break; |
| default: |
| pr_err_ratelimited("Get prop %d is not supported in pc_port\n", |
| psp); |
| return -EINVAL; |
| } |
| |
| if (rc < 0) { |
| pr_debug("Couldn't get prop %d rc = %d\n", psp, rc); |
| return -ENODATA; |
| } |
| |
| return 0; |
| } |
| |
| static int smb5_usb_port_set_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| const union power_supply_propval *val) |
| { |
| int rc = 0; |
| |
| switch (psp) { |
| default: |
| pr_err_ratelimited("Set prop %d is not supported in pc_port\n", |
| psp); |
| rc = -EINVAL; |
| break; |
| } |
| |
| return rc; |
| } |
| |
| static const struct power_supply_desc usb_port_psy_desc = { |
| .name = "pc_port", |
| .type = POWER_SUPPLY_TYPE_USB, |
| .properties = smb5_usb_port_props, |
| .num_properties = ARRAY_SIZE(smb5_usb_port_props), |
| .get_property = smb5_usb_port_get_prop, |
| .set_property = smb5_usb_port_set_prop, |
| }; |
| |
| static int smb5_init_usb_port_psy(struct smb5 *chip) |
| { |
| struct power_supply_config usb_port_cfg = {}; |
| struct smb_charger *chg = &chip->chg; |
| |
| usb_port_cfg.drv_data = chip; |
| usb_port_cfg.of_node = chg->dev->of_node; |
| chg->usb_port_psy = devm_power_supply_register(chg->dev, |
| &usb_port_psy_desc, |
| &usb_port_cfg); |
| if (IS_ERR(chg->usb_port_psy)) { |
| pr_err("Couldn't register USB pc_port power supply\n"); |
| return PTR_ERR(chg->usb_port_psy); |
| } |
| |
| return 0; |
| } |
| |
| /***************************** |
| * USB MAIN PSY REGISTRATION * |
| *****************************/ |
| |
| static enum power_supply_property smb5_usb_main_props[] = { |
| POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
| POWER_SUPPLY_PROP_TYPE, |
| POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED, |
| POWER_SUPPLY_PROP_INPUT_VOLTAGE_SETTLED, |
| POWER_SUPPLY_PROP_FCC_DELTA, |
| POWER_SUPPLY_PROP_CURRENT_MAX, |
| POWER_SUPPLY_PROP_FLASH_ACTIVE, |
| POWER_SUPPLY_PROP_FLASH_TRIGGER, |
| }; |
| |
| static int smb5_usb_main_get_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| rc = smblib_get_charge_param(chg, &chg->param.fv, &val->intval); |
| break; |
| case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| rc = smblib_get_charge_param(chg, &chg->param.fcc, |
| &val->intval); |
| break; |
| case POWER_SUPPLY_PROP_TYPE: |
| val->intval = POWER_SUPPLY_TYPE_MAIN; |
| break; |
| case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED: |
| rc = smblib_get_prop_input_current_settled(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_INPUT_VOLTAGE_SETTLED: |
| rc = smblib_get_prop_input_voltage_settled(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_FCC_DELTA: |
| rc = smblib_get_prop_fcc_delta(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CURRENT_MAX: |
| rc = smblib_get_icl_current(chg, &val->intval); |
| break; |
| case POWER_SUPPLY_PROP_FLASH_ACTIVE: |
| val->intval = chg->flash_active; |
| break; |
| case POWER_SUPPLY_PROP_FLASH_TRIGGER: |
| rc = schgm_flash_get_vreg_ok(chg, &val->intval); |
| break; |
| default: |
| pr_debug("get prop %d is not supported in usb-main\n", psp); |
| rc = -EINVAL; |
| break; |
| } |
| if (rc < 0) { |
| pr_debug("Couldn't get prop %d rc = %d\n", psp, rc); |
| return -ENODATA; |
| } |
| |
| return 0; |
| } |
| |
| static int smb5_usb_main_set_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| const union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| union power_supply_propval pval = {0, }; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| rc = smblib_set_charge_param(chg, &chg->param.fv, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| rc = smblib_set_charge_param(chg, &chg->param.fcc, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_CURRENT_MAX: |
| rc = smblib_set_icl_current(chg, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_FLASH_ACTIVE: |
| if ((chg->smb_version == PMI632_SUBTYPE) |
| && (chg->flash_active != val->intval)) { |
| chg->flash_active = val->intval; |
| |
| rc = smblib_get_prop_usb_present(chg, &pval); |
| if (rc < 0) |
| pr_err("Failed to get USB preset status rc=%d\n", |
| rc); |
| if (pval.intval) { |
| rc = smblib_force_vbus_voltage(chg, |
| chg->flash_active ? FORCE_5V_BIT |
| : IDLE_BIT); |
| if (rc < 0) |
| pr_err("Failed to force 5V\n"); |
| else |
| chg->pulse_cnt = 0; |
| } |
| |
| pr_debug("flash active VBUS 5V restriction %s\n", |
| chg->flash_active ? "applied" : "removed"); |
| |
| /* Update userspace */ |
| if (chg->batt_psy) |
| power_supply_changed(chg->batt_psy); |
| } |
| break; |
| default: |
| pr_err("set prop %d is not supported\n", psp); |
| rc = -EINVAL; |
| break; |
| } |
| |
| return rc; |
| } |
| |
| static const struct power_supply_desc usb_main_psy_desc = { |
| .name = "main", |
| .type = POWER_SUPPLY_TYPE_MAIN, |
| .properties = smb5_usb_main_props, |
| .num_properties = ARRAY_SIZE(smb5_usb_main_props), |
| .get_property = smb5_usb_main_get_prop, |
| .set_property = smb5_usb_main_set_prop, |
| }; |
| |
| static int smb5_init_usb_main_psy(struct smb5 *chip) |
| { |
| struct power_supply_config usb_main_cfg = {}; |
| struct smb_charger *chg = &chip->chg; |
| |
| usb_main_cfg.drv_data = chip; |
| usb_main_cfg.of_node = chg->dev->of_node; |
| chg->usb_main_psy = devm_power_supply_register(chg->dev, |
| &usb_main_psy_desc, |
| &usb_main_cfg); |
| if (IS_ERR(chg->usb_main_psy)) { |
| pr_err("Couldn't register USB main power supply\n"); |
| return PTR_ERR(chg->usb_main_psy); |
| } |
| |
| return 0; |
| } |
| |
| /************************* |
| * DC PSY REGISTRATION * |
| *************************/ |
| |
| static enum power_supply_property smb5_dc_props[] = { |
| POWER_SUPPLY_PROP_INPUT_SUSPEND, |
| POWER_SUPPLY_PROP_PRESENT, |
| POWER_SUPPLY_PROP_ONLINE, |
| POWER_SUPPLY_PROP_REAL_TYPE, |
| }; |
| |
| static int smb5_dc_get_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_INPUT_SUSPEND: |
| val->intval = get_effective_result(chg->dc_suspend_votable); |
| break; |
| case POWER_SUPPLY_PROP_PRESENT: |
| rc = smblib_get_prop_dc_present(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_ONLINE: |
| rc = smblib_get_prop_dc_online(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_REAL_TYPE: |
| val->intval = POWER_SUPPLY_TYPE_WIPOWER; |
| break; |
| default: |
| return -EINVAL; |
| } |
| if (rc < 0) { |
| pr_debug("Couldn't get prop %d rc = %d\n", psp, rc); |
| return -ENODATA; |
| } |
| return 0; |
| } |
| |
| static int smb5_dc_set_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| const union power_supply_propval *val) |
| { |
| struct smb5 *chip = power_supply_get_drvdata(psy); |
| struct smb_charger *chg = &chip->chg; |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_INPUT_SUSPEND: |
| rc = vote(chg->dc_suspend_votable, WBC_VOTER, |
| (bool)val->intval, 0); |
| break; |
| default: |
| return -EINVAL; |
| } |
| |
| return rc; |
| } |
| |
| static int smb5_dc_prop_is_writeable(struct power_supply *psy, |
| enum power_supply_property psp) |
| { |
| int rc; |
| |
| switch (psp) { |
| default: |
| rc = 0; |
| break; |
| } |
| |
| return rc; |
| } |
| |
| static const struct power_supply_desc dc_psy_desc = { |
| .name = "dc", |
| .type = POWER_SUPPLY_TYPE_WIRELESS, |
| .properties = smb5_dc_props, |
| .num_properties = ARRAY_SIZE(smb5_dc_props), |
| .get_property = smb5_dc_get_prop, |
| .set_property = smb5_dc_set_prop, |
| .property_is_writeable = smb5_dc_prop_is_writeable, |
| }; |
| |
| static int smb5_init_dc_psy(struct smb5 *chip) |
| { |
| struct power_supply_config dc_cfg = {}; |
| struct smb_charger *chg = &chip->chg; |
| |
| dc_cfg.drv_data = chip; |
| dc_cfg.of_node = chg->dev->of_node; |
| chg->dc_psy = devm_power_supply_register(chg->dev, |
| &dc_psy_desc, |
| &dc_cfg); |
| if (IS_ERR(chg->dc_psy)) { |
| pr_err("Couldn't register USB power supply\n"); |
| return PTR_ERR(chg->dc_psy); |
| } |
| |
| return 0; |
| } |
| |
| /************************* |
| * BATT PSY REGISTRATION * |
| *************************/ |
| static enum power_supply_property smb5_batt_props[] = { |
| POWER_SUPPLY_PROP_INPUT_SUSPEND, |
| POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED, |
| POWER_SUPPLY_PROP_STATUS, |
| POWER_SUPPLY_PROP_HEALTH, |
| POWER_SUPPLY_PROP_PRESENT, |
| POWER_SUPPLY_PROP_CHARGE_TYPE, |
| POWER_SUPPLY_PROP_CAPACITY, |
| POWER_SUPPLY_PROP_INPUT_CURRENT_LIMITED, |
| POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| POWER_SUPPLY_PROP_CURRENT_NOW, |
| POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
| POWER_SUPPLY_PROP_TEMP, |
| POWER_SUPPLY_PROP_TECHNOLOGY, |
| POWER_SUPPLY_PROP_STEP_CHARGING_ENABLED, |
| POWER_SUPPLY_PROP_SW_JEITA_ENABLED, |
| POWER_SUPPLY_PROP_CHARGE_DONE, |
| POWER_SUPPLY_PROP_PARALLEL_DISABLE, |
| POWER_SUPPLY_PROP_SET_SHIP_MODE, |
| POWER_SUPPLY_PROP_DIE_HEALTH, |
| POWER_SUPPLY_PROP_RERUN_AICL, |
| POWER_SUPPLY_PROP_DP_DM, |
| POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, |
| POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, |
| POWER_SUPPLY_PROP_CHARGE_COUNTER, |
| POWER_SUPPLY_PROP_CYCLE_COUNT, |
| POWER_SUPPLY_PROP_RECHARGE_SOC, |
| }; |
| |
| static int smb5_batt_get_prop(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct smb_charger *chg = power_supply_get_drvdata(psy); |
| int rc = 0; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_STATUS: |
| rc = smblib_get_prop_batt_status(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_HEALTH: |
| rc = smblib_get_prop_batt_health(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PRESENT: |
| rc = smblib_get_prop_batt_present(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_INPUT_SUSPEND: |
| rc = smblib_get_prop_input_suspend(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED: |
| val->intval = !get_client_vote(chg->chg_disable_votable, |
| USER_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| rc = smblib_get_prop_batt_charge_type(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CAPACITY: |
| rc = smblib_get_prop_batt_capacity(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT: |
| rc = smblib_get_prop_system_temp_level(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX: |
| rc = smblib_get_prop_system_temp_level_max(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMITED: |
| rc = smblib_get_prop_input_current_limited(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_STEP_CHARGING_ENABLED: |
| val->intval = chg->step_chg_enabled; |
| break; |
| case POWER_SUPPLY_PROP_SW_JEITA_ENABLED: |
| val->intval = chg->sw_jeita_enabled; |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| rc = smblib_get_prop_batt_voltage_now(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| val->intval = get_client_vote(chg->fv_votable, |
| BATT_PROFILE_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_CURRENT_NOW: |
| rc = smblib_get_prop_batt_current_now(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| val->intval = get_client_vote(chg->fcc_votable, |
| BATT_PROFILE_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_TEMP: |
| rc = smblib_get_prop_batt_temp(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_TECHNOLOGY: |
| val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_DONE: |
| rc = smblib_get_prop_batt_charge_done(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PARALLEL_DISABLE: |
| val->intval = get_client_vote(chg->pl_disable_votable, |
| USER_VOTER); |
| break; |
| case POWER_SUPPLY_PROP_SET_SHIP_MODE: |
| /* Not in ship mode as long as device is active */ |
| val->intval = 0; |
| break; |
| case POWER_SUPPLY_PROP_DIE_HEALTH: |
| if (chg->die_health == -EINVAL) |
| rc = smblib_get_prop_die_health(chg, val); |
| else |
| val->intval = chg->die_health; |
| break; |
| case POWER_SUPPLY_PROP_DP_DM: |
| val->intval = chg->pulse_cnt; |
| break; |
| case POWER_SUPPLY_PROP_RERUN_AICL: |
| val->intval = 0; |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
| rc = smblib_get_prop_batt_charge_counter(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| rc = smblib_get_prop_batt_cycle_count(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_RECHARGE_SOC: |
| val->intval = chg->auto_recharge_soc; |
| break; |
| default: |
| pr_err("batt power supply prop %d not supported\n", psp); |
| return -EINVAL; |
| } |
| |
| if (rc < 0) { |
| pr_debug("Couldn't get prop %d rc = %d\n", psp, rc); |
| return -ENODATA; |
| } |
| |
| return 0; |
| } |
| |
| static int smb5_batt_set_prop(struct power_supply *psy, |
| enum power_supply_property prop, |
| const union power_supply_propval *val) |
| { |
| int rc = 0; |
| struct smb_charger *chg = power_supply_get_drvdata(psy); |
| bool enable; |
| |
| switch (prop) { |
| case POWER_SUPPLY_PROP_STATUS: |
| rc = smblib_set_prop_batt_status(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_INPUT_SUSPEND: |
| rc = smblib_set_prop_input_suspend(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED: |
| vote(chg->chg_disable_votable, USER_VOTER, !val->intval, 0); |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT: |
| rc = smblib_set_prop_system_temp_level(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_CAPACITY: |
| rc = smblib_set_prop_batt_capacity(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_PARALLEL_DISABLE: |
| vote(chg->pl_disable_votable, USER_VOTER, (bool)val->intval, 0); |
| break; |
| case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| chg->batt_profile_fv_uv = val->intval; |
| vote(chg->fv_votable, BATT_PROFILE_VOTER, true, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_STEP_CHARGING_ENABLED: |
| enable = !!val->intval || chg->sw_jeita_enabled; |
| rc = smblib_configure_wdog(chg, enable); |
| if (rc == 0) |
| chg->step_chg_enabled = !!val->intval; |
| break; |
| case POWER_SUPPLY_PROP_SW_JEITA_ENABLED: |
| if (chg->sw_jeita_enabled != (!!val->intval)) { |
| rc = smblib_disable_hw_jeita(chg, !!val->intval); |
| enable = !!val->intval || chg->step_chg_enabled; |
| rc |= smblib_configure_wdog(chg, enable); |
| if (rc == 0) |
| chg->sw_jeita_enabled = !!val->intval; |
| } |
| break; |
| case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| chg->batt_profile_fcc_ua = val->intval; |
| vote(chg->fcc_votable, BATT_PROFILE_VOTER, true, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_SET_SHIP_MODE: |
| /* Not in ship mode as long as the device is active */ |
| if (!val->intval) |
| break; |
| if (chg->pl.psy) |
| power_supply_set_property(chg->pl.psy, |
| POWER_SUPPLY_PROP_SET_SHIP_MODE, val); |
| rc = smblib_set_prop_ship_mode(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_RERUN_AICL: |
| rc = smblib_rerun_aicl(chg); |
| break; |
| case POWER_SUPPLY_PROP_DP_DM: |
| if (!chg->flash_active) |
| rc = smblib_dp_dm(chg, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMITED: |
| rc = smblib_set_prop_input_current_limited(chg, val); |
| break; |
| case POWER_SUPPLY_PROP_DIE_HEALTH: |
| chg->die_health = val->intval; |
| power_supply_changed(chg->batt_psy); |
| break; |
| case POWER_SUPPLY_PROP_RECHARGE_SOC: |
| if (chg->smb_version == PMI632_SUBTYPE) { |
| /* toggle charging to force recharge */ |
| vote(chg->chg_disable_votable, FORCE_RECHARGE_VOTER, |
| true, 0); |
| /* charge disable delay */ |
| msleep(50); |
| vote(chg->chg_disable_votable, FORCE_RECHARGE_VOTER, |
| false, 0); |
| } |
| break; |
| default: |
| rc = -EINVAL; |
| } |
| |
| return rc; |
| } |
| |
| static int smb5_batt_prop_is_writeable(struct power_supply *psy, |
| enum power_supply_property psp) |
| { |
| switch (psp) { |
| case POWER_SUPPLY_PROP_STATUS: |
| case POWER_SUPPLY_PROP_INPUT_SUSPEND: |
| case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL: |
| case POWER_SUPPLY_PROP_CAPACITY: |
| case POWER_SUPPLY_PROP_PARALLEL_DISABLE: |
| case POWER_SUPPLY_PROP_DP_DM: |
| case POWER_SUPPLY_PROP_RERUN_AICL: |
| case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMITED: |
| case POWER_SUPPLY_PROP_STEP_CHARGING_ENABLED: |
| case POWER_SUPPLY_PROP_SW_JEITA_ENABLED: |
| case POWER_SUPPLY_PROP_DIE_HEALTH: |
| case POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED: |
| return 1; |
| default: |
| break; |
| } |
| |
| return 0; |
| } |
| |
| static const struct power_supply_desc batt_psy_desc = { |
| .name = "battery", |
| .type = POWER_SUPPLY_TYPE_BATTERY, |
| .properties = smb5_batt_props, |
| .num_properties = ARRAY_SIZE(smb5_batt_props), |
| .get_property = smb5_batt_get_prop, |
| .set_property = smb5_batt_set_prop, |
| .property_is_writeable = smb5_batt_prop_is_writeable, |
| }; |
| |
| static int smb5_init_batt_psy(struct smb5 *chip) |
| { |
| struct power_supply_config batt_cfg = {}; |
| struct smb_charger *chg = &chip->chg; |
| int rc = 0; |
| |
| batt_cfg.drv_data = chg; |
| batt_cfg.of_node = chg->dev->of_node; |
| chg->batt_psy = devm_power_supply_register(chg->dev, |
| &batt_psy_desc, |
| &batt_cfg); |
| if (IS_ERR(chg->batt_psy)) { |
| pr_err("Couldn't register battery power supply\n"); |
| return PTR_ERR(chg->batt_psy); |
| } |
| |
| return rc; |
| } |
| |
| /****************************** |
| * VBUS REGULATOR REGISTRATION * |
| ******************************/ |
| |
| static struct regulator_ops smb5_vbus_reg_ops = { |
| .enable = smblib_vbus_regulator_enable, |
| .disable = smblib_vbus_regulator_disable, |
| .is_enabled = smblib_vbus_regulator_is_enabled, |
| }; |
| |
| static int smb5_init_vbus_regulator(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| struct regulator_config cfg = {}; |
| int rc = 0; |
| |
| chg->vbus_vreg = devm_kzalloc(chg->dev, sizeof(*chg->vbus_vreg), |
| GFP_KERNEL); |
| if (!chg->vbus_vreg) |
| return -ENOMEM; |
| |
| cfg.dev = chg->dev; |
| cfg.driver_data = chip; |
| |
| chg->vbus_vreg->rdesc.owner = THIS_MODULE; |
| chg->vbus_vreg->rdesc.type = REGULATOR_VOLTAGE; |
| chg->vbus_vreg->rdesc.ops = &smb5_vbus_reg_ops; |
| chg->vbus_vreg->rdesc.of_match = "qcom,smb5-vbus"; |
| chg->vbus_vreg->rdesc.name = "qcom,smb5-vbus"; |
| |
| chg->vbus_vreg->rdev = devm_regulator_register(chg->dev, |
| &chg->vbus_vreg->rdesc, &cfg); |
| if (IS_ERR(chg->vbus_vreg->rdev)) { |
| rc = PTR_ERR(chg->vbus_vreg->rdev); |
| chg->vbus_vreg->rdev = NULL; |
| if (rc != -EPROBE_DEFER) |
| pr_err("Couldn't register VBUS regulator rc=%d\n", rc); |
| } |
| |
| return rc; |
| } |
| |
| /****************************** |
| * VCONN REGULATOR REGISTRATION * |
| ******************************/ |
| |
| static struct regulator_ops smb5_vconn_reg_ops = { |
| .enable = smblib_vconn_regulator_enable, |
| .disable = smblib_vconn_regulator_disable, |
| .is_enabled = smblib_vconn_regulator_is_enabled, |
| }; |
| |
| static int smb5_init_vconn_regulator(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| struct regulator_config cfg = {}; |
| int rc = 0; |
| |
| if (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB) |
| return 0; |
| |
| chg->vconn_vreg = devm_kzalloc(chg->dev, sizeof(*chg->vconn_vreg), |
| GFP_KERNEL); |
| if (!chg->vconn_vreg) |
| return -ENOMEM; |
| |
| cfg.dev = chg->dev; |
| cfg.driver_data = chip; |
| |
| chg->vconn_vreg->rdesc.owner = THIS_MODULE; |
| chg->vconn_vreg->rdesc.type = REGULATOR_VOLTAGE; |
| chg->vconn_vreg->rdesc.ops = &smb5_vconn_reg_ops; |
| chg->vconn_vreg->rdesc.of_match = "qcom,smb5-vconn"; |
| chg->vconn_vreg->rdesc.name = "qcom,smb5-vconn"; |
| |
| chg->vconn_vreg->rdev = devm_regulator_register(chg->dev, |
| &chg->vconn_vreg->rdesc, &cfg); |
| if (IS_ERR(chg->vconn_vreg->rdev)) { |
| rc = PTR_ERR(chg->vconn_vreg->rdev); |
| chg->vconn_vreg->rdev = NULL; |
| if (rc != -EPROBE_DEFER) |
| pr_err("Couldn't register VCONN regulator rc=%d\n", rc); |
| } |
| |
| return rc; |
| } |
| |
| /*************************** |
| * HARDWARE INITIALIZATION * |
| ***************************/ |
| static int smb5_configure_typec(struct smb_charger *chg) |
| { |
| int rc; |
| u8 val = 0; |
| |
| rc = smblib_read(chg, LEGACY_CABLE_STATUS_REG, &val); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't read Legacy status rc=%d\n", rc); |
| return rc; |
| } |
| /* |
| * If Legacy cable is detected re-trigger Legacy detection |
| * by disabling/enabling typeC mode. |
| */ |
| if (val & TYPEC_LEGACY_CABLE_STATUS_BIT) { |
| rc = smblib_masked_write(chg, TYPE_C_MODE_CFG_REG, |
| TYPEC_DISABLE_CMD_BIT, TYPEC_DISABLE_CMD_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't disable TYPEC rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* delay before enabling typeC */ |
| msleep(500); |
| |
| rc = smblib_masked_write(chg, TYPE_C_MODE_CFG_REG, |
| TYPEC_DISABLE_CMD_BIT, 0); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't enable TYPEC rc=%d\n", rc); |
| return rc; |
| } |
| } |
| |
| /* disable apsd */ |
| rc = smblib_configure_hvdcp_apsd(chg, false); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't disable APSD rc=%d\n", rc); |
| return rc; |
| } |
| |
| rc = smblib_write(chg, TYPE_C_INTERRUPT_EN_CFG_1_REG, |
| TYPEC_CCOUT_DETACH_INT_EN_BIT | |
| TYPEC_CCOUT_ATTACH_INT_EN_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure Type-C interrupts rc=%d\n", rc); |
| return rc; |
| } |
| |
| rc = smblib_write(chg, TYPE_C_INTERRUPT_EN_CFG_2_REG, |
| TYPEC_WATER_DETECTION_INT_EN_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure Type-C interrupts rc=%d\n", rc); |
| return rc; |
| } |
| |
| rc = smblib_masked_write(chg, TYPE_C_MODE_CFG_REG, |
| EN_TRY_SNK_BIT, EN_TRY_SNK_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't enable try.snk rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* Keep VCONN in h/w controlled mode for PMI632 */ |
| if (chg->smb_version != PMI632_SUBTYPE) { |
| /* configure VCONN for software control */ |
| rc = smblib_masked_write(chg, TYPE_C_VCONN_CONTROL_REG, |
| VCONN_EN_SRC_BIT | VCONN_EN_VALUE_BIT, |
| VCONN_EN_SRC_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure VCONN for SW control rc=%d\n", |
| rc); |
| return rc; |
| } |
| } |
| |
| return rc; |
| } |
| |
| static int smb5_configure_micro_usb(struct smb_charger *chg) |
| { |
| int rc; |
| |
| rc = smblib_masked_write(chg, TYPE_C_INTERRUPT_EN_CFG_2_REG, |
| MICRO_USB_STATE_CHANGE_INT_EN_BIT, |
| MICRO_USB_STATE_CHANGE_INT_EN_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure Type-C interrupts rc=%d\n", rc); |
| return rc; |
| } |
| |
| return rc; |
| } |
| |
| static int smb5_configure_mitigation(struct smb_charger *chg) |
| { |
| int rc; |
| u8 chan = 0; |
| |
| if (!chg->hw_die_temp_mitigation && !chg->hw_connector_mitigation) |
| return 0; |
| |
| if (chg->hw_die_temp_mitigation) { |
| rc = smblib_write(chg, MISC_THERMREG_SRC_CFG_REG, |
| THERMREG_CONNECTOR_ADC_SRC_EN_BIT |
| | THERMREG_DIE_ADC_SRC_EN_BIT |
| | THERMREG_DIE_CMP_SRC_EN_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure THERM_SRC reg rc=%d\n", rc); |
| return rc; |
| }; |
| |
| chan = DIE_TEMP_CHANNEL_EN_BIT; |
| } |
| |
| if (chg->hw_connector_mitigation) |
| chan |= CONN_THM_CHANNEL_EN_BIT; |
| |
| rc = smblib_masked_write(chg, BATIF_ADC_CHANNEL_EN_REG, |
| CONN_THM_CHANNEL_EN_BIT | DIE_TEMP_CHANNEL_EN_BIT, |
| chan); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't enable ADC channelrc=%d\n", rc); |
| return rc; |
| } |
| |
| return 0; |
| } |
| |
| static int smb5_init_hw(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| int rc, type = 0; |
| u8 val = 0; |
| |
| if (chip->dt.no_battery) |
| chg->fake_capacity = 50; |
| |
| if (chip->dt.batt_profile_fcc_ua < 0) |
| smblib_get_charge_param(chg, &chg->param.fcc, |
| &chg->batt_profile_fcc_ua); |
| |
| if (chip->dt.batt_profile_fv_uv < 0) |
| smblib_get_charge_param(chg, &chg->param.fv, |
| &chg->batt_profile_fv_uv); |
| |
| smblib_get_charge_param(chg, &chg->param.usb_icl, |
| &chg->default_icl_ua); |
| |
| /* Use SW based VBUS control, disable HW autonomous mode */ |
| rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| HVDCP_AUTH_ALG_EN_CFG_BIT | HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT, |
| HVDCP_AUTH_ALG_EN_CFG_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure HVDCP rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* |
| * PMI632 can have the connector type defined by a dedicated register |
| * TYPEC_MICRO_USB_MODE_REG or by a common TYPEC_U_USB_CFG_REG. |
| */ |
| if (chg->smb_version == PMI632_SUBTYPE) { |
| rc = smblib_read(chg, TYPEC_MICRO_USB_MODE_REG, &val); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't read USB mode rc=%d\n", rc); |
| return rc; |
| } |
| type = !!(val & MICRO_USB_MODE_ONLY_BIT); |
| } |
| |
| /* |
| * If TYPEC_MICRO_USB_MODE_REG is not set and for all non-PMI632 |
| * check the connector type using TYPEC_U_USB_CFG_REG. |
| */ |
| if (!type) { |
| rc = smblib_read(chg, TYPEC_U_USB_CFG_REG, &val); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't read U_USB config rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| type = !!(val & EN_MICRO_USB_MODE_BIT); |
| } |
| |
| pr_debug("Connector type=%s\n", type ? "Micro USB" : "TypeC"); |
| |
| if (type) { |
| chg->connector_type = POWER_SUPPLY_CONNECTOR_MICRO_USB; |
| rc = smb5_configure_micro_usb(chg); |
| } else { |
| chg->connector_type = POWER_SUPPLY_CONNECTOR_TYPEC; |
| rc = smb5_configure_typec(chg); |
| } |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure TypeC/micro-USB mode rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* |
| * PMI632 based hw init: |
| * - Enable STAT pin function on SMB_EN |
| * - Rerun APSD to ensure proper charger detection if device |
| * boots with charger connected. |
| * - Initialize flash module for PMI632 |
| */ |
| if (chg->smb_version == PMI632_SUBTYPE) { |
| rc = smblib_masked_write(chg, MISC_SMB_EN_CMD_REG, |
| EN_STAT_CMD_BIT, EN_STAT_CMD_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure SMB_EN rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| schgm_flash_init(chg); |
| smblib_rerun_apsd_if_required(chg); |
| } |
| |
| /* clear the ICL override if it is set */ |
| rc = smblib_icl_override(chg, false); |
| if (rc < 0) { |
| pr_err("Couldn't disable ICL override rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* set OTG current limit */ |
| rc = smblib_set_charge_param(chg, &chg->param.otg_cl, chg->otg_cl_ua); |
| if (rc < 0) { |
| pr_err("Couldn't set otg current limit rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* configure temperature mitigation */ |
| rc = smb5_configure_mitigation(chg); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure mitigation rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* vote 0mA on usb_icl for non battery platforms */ |
| vote(chg->usb_icl_votable, |
| DEFAULT_VOTER, chip->dt.no_battery, 0); |
| vote(chg->dc_suspend_votable, |
| DEFAULT_VOTER, chip->dt.no_battery, 0); |
| vote(chg->fcc_votable, HW_LIMIT_VOTER, |
| chip->dt.batt_profile_fcc_ua > 0, chip->dt.batt_profile_fcc_ua); |
| vote(chg->fv_votable, HW_LIMIT_VOTER, |
| chip->dt.batt_profile_fv_uv > 0, chip->dt.batt_profile_fv_uv); |
| vote(chg->fcc_votable, |
| BATT_PROFILE_VOTER, chg->batt_profile_fcc_ua > 0, |
| chg->batt_profile_fcc_ua); |
| vote(chg->fv_votable, |
| BATT_PROFILE_VOTER, chg->batt_profile_fv_uv > 0, |
| chg->batt_profile_fv_uv); |
| |
| /* Some h/w limit maximum supported ICL */ |
| vote(chg->usb_icl_votable, HW_LIMIT_VOTER, |
| chg->hw_max_icl_ua > 0, chg->hw_max_icl_ua); |
| |
| /* |
| * AICL configuration: |
| * AICL ADC disable |
| */ |
| if (chg->smb_version != PMI632_SUBTYPE) { |
| rc = smblib_masked_write(chg, USBIN_AICL_OPTIONS_CFG_REG, |
| USBIN_AICL_ADC_EN_BIT, 0); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't config AICL rc=%d\n", rc); |
| return rc; |
| } |
| } |
| |
| /* enable the charging path */ |
| rc = vote(chg->chg_disable_votable, DEFAULT_VOTER, false, 0); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't enable charging rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* configure VBUS for software control */ |
| rc = smblib_masked_write(chg, DCDC_OTG_CFG_REG, OTG_EN_SRC_CFG_BIT, 0); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure VBUS for SW control rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* |
| * configure the one time watchdong periodic interval and |
| * disable "watchdog bite disable charging". |
| */ |
| val = (ilog2(chip->dt.wd_bark_time / 16) << BARK_WDOG_TIMEOUT_SHIFT) |
| & BARK_WDOG_TIMEOUT_MASK; |
| val |= BITE_WDOG_TIMEOUT_8S; |
| rc = smblib_masked_write(chg, SNARL_BARK_BITE_WD_CFG_REG, |
| BITE_WDOG_DISABLE_CHARGING_CFG_BIT | |
| BARK_WDOG_TIMEOUT_MASK | BITE_WDOG_TIMEOUT_MASK, |
| val); |
| if (rc < 0) { |
| pr_err("Couldn't configue WD config rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* configure float charger options */ |
| switch (chip->dt.float_option) { |
| case FLOAT_DCP: |
| rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG, |
| FLOAT_OPTIONS_MASK, 0); |
| break; |
| case FLOAT_SDP: |
| rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG, |
| FLOAT_OPTIONS_MASK, FORCE_FLOAT_SDP_CFG_BIT); |
| break; |
| case DISABLE_CHARGING: |
| rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG, |
| FLOAT_OPTIONS_MASK, FLOAT_DIS_CHGING_CFG_BIT); |
| break; |
| case SUSPEND_INPUT: |
| rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG, |
| FLOAT_OPTIONS_MASK, SUSPEND_FLOAT_CFG_BIT); |
| break; |
| default: |
| rc = 0; |
| break; |
| } |
| |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure float charger options rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| rc = smblib_read(chg, USBIN_OPTIONS_2_CFG_REG, &chg->float_cfg); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't read float charger options rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| switch (chip->dt.chg_inhibit_thr_mv) { |
| case 50: |
| rc = smblib_masked_write(chg, CHARGE_INHIBIT_THRESHOLD_CFG_REG, |
| CHARGE_INHIBIT_THRESHOLD_MASK, |
| INHIBIT_ANALOG_VFLT_MINUS_50MV); |
| break; |
| case 100: |
| rc = smblib_masked_write(chg, CHARGE_INHIBIT_THRESHOLD_CFG_REG, |
| CHARGE_INHIBIT_THRESHOLD_MASK, |
| INHIBIT_ANALOG_VFLT_MINUS_100MV); |
| break; |
| case 200: |
| rc = smblib_masked_write(chg, CHARGE_INHIBIT_THRESHOLD_CFG_REG, |
| CHARGE_INHIBIT_THRESHOLD_MASK, |
| INHIBIT_ANALOG_VFLT_MINUS_200MV); |
| break; |
| case 300: |
| rc = smblib_masked_write(chg, CHARGE_INHIBIT_THRESHOLD_CFG_REG, |
| CHARGE_INHIBIT_THRESHOLD_MASK, |
| INHIBIT_ANALOG_VFLT_MINUS_300MV); |
| break; |
| case 0: |
| rc = smblib_masked_write(chg, CHGR_CFG2_REG, |
| CHARGER_INHIBIT_BIT, 0); |
| default: |
| break; |
| } |
| |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure charge inhibit threshold rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| rc = smblib_masked_write(chg, CHGR_CFG2_REG, RECHG_MASK, |
| (chip->dt.auto_recharge_vbat_mv != -EINVAL) ? |
| VBAT_BASED_RECHG_BIT : 0); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure VBAT-rechg CHG_CFG2_REG rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| /* program the auto-recharge VBAT threshold */ |
| if (chip->dt.auto_recharge_vbat_mv != -EINVAL) { |
| u32 temp = VBAT_TO_VRAW_ADC(chip->dt.auto_recharge_vbat_mv); |
| |
| temp = ((temp & 0xFF00) >> 8) | ((temp & 0xFF) << 8); |
| rc = smblib_batch_write(chg, |
| CHGR_ADC_RECHARGE_THRESHOLD_MSB_REG, (u8 *)&temp, 2); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure ADC_RECHARGE_THRESHOLD REG rc=%d\n", |
| rc); |
| return rc; |
| } |
| /* Program the sample count for VBAT based recharge to 3 */ |
| rc = smblib_masked_write(chg, CHGR_NO_SAMPLE_TERM_RCHG_CFG_REG, |
| NO_OF_SAMPLE_FOR_RCHG, |
| 2 << NO_OF_SAMPLE_FOR_RCHG_SHIFT); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure CHGR_NO_SAMPLE_FOR_TERM_RCHG_CFG rc=%d\n", |
| rc); |
| return rc; |
| } |
| } |
| |
| rc = smblib_masked_write(chg, CHGR_CFG2_REG, RECHG_MASK, |
| (chip->dt.auto_recharge_soc != -EINVAL) ? |
| SOC_BASED_RECHG_BIT : VBAT_BASED_RECHG_BIT); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure SOC-rechg CHG_CFG2_REG rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| /* program the auto-recharge threshold */ |
| if (chip->dt.auto_recharge_soc != -EINVAL) { |
| rc = smblib_write(chg, CHARGE_RCHG_SOC_THRESHOLD_CFG_REG, |
| (chip->dt.auto_recharge_soc * 255) / 100); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure CHG_RCHG_SOC_REG rc=%d\n", |
| rc); |
| return rc; |
| } |
| /* Program the sample count for SOC based recharge to 1 */ |
| rc = smblib_masked_write(chg, CHGR_NO_SAMPLE_TERM_RCHG_CFG_REG, |
| NO_OF_SAMPLE_FOR_RCHG, 0); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure CHGR_NO_SAMPLE_FOR_TERM_RCHG_CFG rc=%d\n", |
| rc); |
| return rc; |
| } |
| } |
| |
| if (chg->sw_jeita_enabled) { |
| rc = smblib_disable_hw_jeita(chg, true); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't set hw jeita rc=%d\n", rc); |
| return rc; |
| } |
| } |
| |
| rc = smblib_configure_wdog(chg, |
| chg->step_chg_enabled || chg->sw_jeita_enabled); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure watchdog rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| if (chg->connector_pull_up != -EINVAL) { |
| rc = smb5_configure_internal_pull(chg, CONN_THERM, |
| get_valid_pullup(chg->connector_pull_up)); |
| if (rc < 0) { |
| dev_err(chg->dev, |
| "Couldn't configure CONN_THERM pull-up rc=%d\n", |
| rc); |
| return rc; |
| } |
| } |
| |
| return rc; |
| } |
| |
| static int smb5_post_init(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| union power_supply_propval pval; |
| int rc; |
| |
| /* |
| * In case the usb path is suspended, we would have missed disabling |
| * the icl change interrupt because the interrupt could have been |
| * not requested |
| */ |
| rerun_election(chg->usb_icl_votable); |
| |
| /* configure power role for dual-role */ |
| pval.intval = POWER_SUPPLY_TYPEC_PR_DUAL; |
| rc = smblib_set_prop_typec_power_role(chg, &pval); |
| if (rc < 0) { |
| dev_err(chg->dev, "Couldn't configure DRP role rc=%d\n", |
| rc); |
| return rc; |
| } |
| |
| rerun_election(chg->usb_irq_enable_votable); |
| |
| return 0; |
| } |
| |
| /**************************** |
| * DETERMINE INITIAL STATUS * |
| ****************************/ |
| |
| static int smb5_determine_initial_status(struct smb5 *chip) |
| { |
| struct smb_irq_data irq_data = {chip, "determine-initial-status"}; |
| struct smb_charger *chg = &chip->chg; |
| union power_supply_propval val; |
| int rc; |
| |
| rc = smblib_get_prop_usb_present(chg, &val); |
| if (rc < 0) { |
| pr_err("Couldn't get usb present rc=%d\n", rc); |
| return rc; |
| } |
| chg->early_usb_attach = val.intval; |
| |
| if (chg->bms_psy) |
| smblib_suspend_on_debug_battery(chg); |
| |
| usb_plugin_irq_handler(0, &irq_data); |
| typec_attach_detach_irq_handler(0, &irq_data); |
| typec_state_change_irq_handler(0, &irq_data); |
| usb_source_change_irq_handler(0, &irq_data); |
| chg_state_change_irq_handler(0, &irq_data); |
| icl_change_irq_handler(0, &irq_data); |
| batt_temp_changed_irq_handler(0, &irq_data); |
| wdog_bark_irq_handler(0, &irq_data); |
| typec_or_rid_detection_change_irq_handler(0, &irq_data); |
| |
| return 0; |
| } |
| |
| /************************** |
| * INTERRUPT REGISTRATION * |
| **************************/ |
| |
| static struct smb_irq_info smb5_irqs[] = { |
| /* CHARGER IRQs */ |
| [CHGR_ERROR_IRQ] = { |
| .name = "chgr-error", |
| .handler = default_irq_handler, |
| }, |
| [CHG_STATE_CHANGE_IRQ] = { |
| .name = "chg-state-change", |
| .handler = chg_state_change_irq_handler, |
| .wake = true, |
| }, |
| [STEP_CHG_STATE_CHANGE_IRQ] = { |
| .name = "step-chg-state-change", |
| }, |
| [STEP_CHG_SOC_UPDATE_FAIL_IRQ] = { |
| .name = "step-chg-soc-update-fail", |
| }, |
| [STEP_CHG_SOC_UPDATE_REQ_IRQ] = { |
| .name = "step-chg-soc-update-req", |
| }, |
| [FG_FVCAL_QUALIFIED_IRQ] = { |
| .name = "fg-fvcal-qualified", |
| }, |
| [VPH_ALARM_IRQ] = { |
| .name = "vph-alarm", |
| }, |
| [VPH_DROP_PRECHG_IRQ] = { |
| .name = "vph-drop-prechg", |
| }, |
| /* DCDC IRQs */ |
| [OTG_FAIL_IRQ] = { |
| .name = "otg-fail", |
| .handler = default_irq_handler, |
| }, |
| [OTG_OC_DISABLE_SW_IRQ] = { |
| .name = "otg-oc-disable-sw", |
| }, |
| [OTG_OC_HICCUP_IRQ] = { |
| .name = "otg-oc-hiccup", |
| }, |
| [BSM_ACTIVE_IRQ] = { |
| .name = "bsm-active", |
| }, |
| [HIGH_DUTY_CYCLE_IRQ] = { |
| .name = "high-duty-cycle", |
| .handler = high_duty_cycle_irq_handler, |
| .wake = true, |
| }, |
| [INPUT_CURRENT_LIMITING_IRQ] = { |
| .name = "input-current-limiting", |
| .handler = default_irq_handler, |
| }, |
| [CONCURRENT_MODE_DISABLE_IRQ] = { |
| .name = "concurrent-mode-disable", |
| }, |
| [SWITCHER_POWER_OK_IRQ] = { |
| .name = "switcher-power-ok", |
| .handler = switcher_power_ok_irq_handler, |
| }, |
| /* BATTERY IRQs */ |
| [BAT_TEMP_IRQ] = { |
| .name = "bat-temp", |
| .handler = batt_temp_changed_irq_handler, |
| .wake = true, |
| }, |
| [ALL_CHNL_CONV_DONE_IRQ] = { |
| .name = "all-chnl-conv-done", |
| }, |
| [BAT_OV_IRQ] = { |
| .name = "bat-ov", |
| .handler = batt_psy_changed_irq_handler, |
| }, |
| [BAT_LOW_IRQ] = { |
| .name = "bat-low", |
| .handler = batt_psy_changed_irq_handler, |
| }, |
| [BAT_THERM_OR_ID_MISSING_IRQ] = { |
| .name = "bat-therm-or-id-missing", |
| .handler = batt_psy_changed_irq_handler, |
| }, |
| [BAT_TERMINAL_MISSING_IRQ] = { |
| .name = "bat-terminal-missing", |
| .handler = batt_psy_changed_irq_handler, |
| }, |
| [BUCK_OC_IRQ] = { |
| .name = "buck-oc", |
| }, |
| [VPH_OV_IRQ] = { |
| .name = "vph-ov", |
| }, |
| /* USB INPUT IRQs */ |
| [USBIN_COLLAPSE_IRQ] = { |
| .name = "usbin-collapse", |
| .handler = default_irq_handler, |
| }, |
| [USBIN_VASHDN_IRQ] = { |
| .name = "usbin-vashdn", |
| .handler = default_irq_handler, |
| }, |
| [USBIN_UV_IRQ] = { |
| .name = "usbin-uv", |
| .handler = usbin_uv_irq_handler, |
| }, |
| [USBIN_OV_IRQ] = { |
| .name = "usbin-ov", |
| .handler = default_irq_handler, |
| }, |
| [USBIN_PLUGIN_IRQ] = { |
| .name = "usbin-plugin", |
| .handler = usb_plugin_irq_handler, |
| .wake = true, |
| }, |
| [USBIN_REVI_CHANGE_IRQ] = { |
| .name = "usbin-revi-change", |
| }, |
| [USBIN_SRC_CHANGE_IRQ] = { |
| .name = "usbin-src-change", |
| .handler = usb_source_change_irq_handler, |
| .wake = true, |
| }, |
| [USBIN_ICL_CHANGE_IRQ] = { |
| .name = "usbin-icl-change", |
| .handler = icl_change_irq_handler, |
| .wake = true, |
| }, |
| /* DC INPUT IRQs */ |
| [DCIN_VASHDN_IRQ] = { |
| .name = "dcin-vashdn", |
| }, |
| [DCIN_UV_IRQ] = { |
| .name = "dcin-uv", |
| .handler = default_irq_handler, |
| }, |
| [DCIN_OV_IRQ] = { |
| .name = "dcin-ov", |
| .handler = default_irq_handler, |
| }, |
| [DCIN_PLUGIN_IRQ] = { |
| .name = "dcin-plugin", |
| .handler = dc_plugin_irq_handler, |
| .wake = true, |
| }, |
| [DCIN_REVI_IRQ] = { |
| .name = "dcin-revi", |
| }, |
| [DCIN_PON_IRQ] = { |
| .name = "dcin-pon", |
| .handler = default_irq_handler, |
| }, |
| [DCIN_EN_IRQ] = { |
| .name = "dcin-en", |
| .handler = default_irq_handler, |
| }, |
| /* TYPEC IRQs */ |
| [TYPEC_OR_RID_DETECTION_CHANGE_IRQ] = { |
| .name = "typec-or-rid-detect-change", |
| .handler = typec_or_rid_detection_change_irq_handler, |
| .wake = true, |
| }, |
| [TYPEC_VPD_DETECT_IRQ] = { |
| .name = "typec-vpd-detect", |
| }, |
| [TYPEC_CC_STATE_CHANGE_IRQ] = { |
| .name = "typec-cc-state-change", |
| .handler = typec_state_change_irq_handler, |
| .wake = true, |
| }, |
| [TYPEC_VCONN_OC_IRQ] = { |
| .name = "typec-vconn-oc", |
| .handler = default_irq_handler, |
| }, |
| [TYPEC_VBUS_CHANGE_IRQ] = { |
| .name = "typec-vbus-change", |
| }, |
| [TYPEC_ATTACH_DETACH_IRQ] = { |
| .name = "typec-attach-detach", |
| .handler = typec_attach_detach_irq_handler, |
| }, |
| [TYPEC_LEGACY_CABLE_DETECT_IRQ] = { |
| .name = "typec-legacy-cable-detect", |
| .handler = default_irq_handler, |
| }, |
| [TYPEC_TRY_SNK_SRC_DETECT_IRQ] = { |
| .name = "typec-try-snk-src-detect", |
| }, |
| /* MISCELLANEOUS IRQs */ |
| [WDOG_SNARL_IRQ] = { |
| .name = "wdog-snarl", |
| }, |
| [WDOG_BARK_IRQ] = { |
| .name = "wdog-bark", |
| .handler = wdog_bark_irq_handler, |
| .wake = true, |
| }, |
| [AICL_FAIL_IRQ] = { |
| .name = "aicl-fail", |
| }, |
| [AICL_DONE_IRQ] = { |
| .name = "aicl-done", |
| .handler = default_irq_handler, |
| }, |
| [SMB_EN_IRQ] = { |
| .name = "smb-en", |
| }, |
| [IMP_TRIGGER_IRQ] = { |
| .name = "imp-trigger", |
| }, |
| [TEMP_CHANGE_IRQ] = { |
| .name = "temp-change", |
| }, |
| [TEMP_CHANGE_SMB_IRQ] = { |
| .name = "temp-change-smb", |
| }, |
| /* FLASH */ |
| [VREG_OK_IRQ] = { |
| .name = "vreg-ok", |
| }, |
| [ILIM_S2_IRQ] = { |
| .name = "ilim2-s2", |
| .handler = schgm_flash_ilim2_irq_handler, |
| }, |
| [ILIM_S1_IRQ] = { |
| .name = "ilim1-s1", |
| }, |
| [VOUT_DOWN_IRQ] = { |
| .name = "vout-down", |
| }, |
| [VOUT_UP_IRQ] = { |
| .name = "vout-up", |
| }, |
| [FLASH_STATE_CHANGE_IRQ] = { |
| .name = "flash-state-change", |
| .handler = schgm_flash_state_change_irq_handler, |
| }, |
| [TORCH_REQ_IRQ] = { |
| .name = "torch-req", |
| }, |
| [FLASH_EN_IRQ] = { |
| .name = "flash-en", |
| }, |
| }; |
| |
| static int smb5_get_irq_index_byname(const char *irq_name) |
| { |
| int i; |
| |
| for (i = 0; i < ARRAY_SIZE(smb5_irqs); i++) { |
| if (strcmp(smb5_irqs[i].name, irq_name) == 0) |
| return i; |
| } |
| |
| return -ENOENT; |
| } |
| |
| static int smb5_request_interrupt(struct smb5 *chip, |
| struct device_node *node, const char *irq_name) |
| { |
| struct smb_charger *chg = &chip->chg; |
| int rc, irq, irq_index; |
| struct smb_irq_data *irq_data; |
| |
| irq = of_irq_get_byname(node, irq_name); |
| if (irq < 0) { |
| pr_err("Couldn't get irq %s byname\n", irq_name); |
| return irq; |
| } |
| |
| irq_index = smb5_get_irq_index_byname(irq_name); |
| if (irq_index < 0) { |
| pr_err("%s is not a defined irq\n", irq_name); |
| return irq_index; |
| } |
| |
| if (!smb5_irqs[irq_index].handler) |
| return 0; |
| |
| irq_data = devm_kzalloc(chg->dev, sizeof(*irq_data), GFP_KERNEL); |
| if (!irq_data) |
| return -ENOMEM; |
| |
| irq_data->parent_data = chip; |
| irq_data->name = irq_name; |
| irq_data->storm_data = smb5_irqs[irq_index].storm_data; |
| mutex_init(&irq_data->storm_data.storm_lock); |
| |
| rc = devm_request_threaded_irq(chg->dev, irq, NULL, |
| smb5_irqs[irq_index].handler, |
| IRQF_ONESHOT, irq_name, irq_data); |
| if (rc < 0) { |
| pr_err("Couldn't request irq %d\n", irq); |
| return rc; |
| } |
| |
| smb5_irqs[irq_index].irq = irq; |
| smb5_irqs[irq_index].irq_data = irq_data; |
| if (smb5_irqs[irq_index].wake) |
| enable_irq_wake(irq); |
| |
| return rc; |
| } |
| |
| static int smb5_request_interrupts(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| struct device_node *node = chg->dev->of_node; |
| struct device_node *child; |
| int rc = 0; |
| const char *name; |
| struct property *prop; |
| |
| for_each_available_child_of_node(node, child) { |
| of_property_for_each_string(child, "interrupt-names", |
| prop, name) { |
| rc = smb5_request_interrupt(chip, child, name); |
| if (rc < 0) |
| return rc; |
| } |
| } |
| if (chg->irq_info[USBIN_ICL_CHANGE_IRQ].irq) |
| chg->usb_icl_change_irq_enabled = true; |
| |
| return rc; |
| } |
| |
| static void smb5_free_interrupts(struct smb_charger *chg) |
| { |
| int i; |
| |
| for (i = 0; i < ARRAY_SIZE(smb5_irqs); i++) { |
| if (smb5_irqs[i].irq > 0) { |
| if (smb5_irqs[i].wake) |
| disable_irq_wake(smb5_irqs[i].irq); |
| |
| devm_free_irq(chg->dev, smb5_irqs[i].irq, |
| smb5_irqs[i].irq_data); |
| } |
| } |
| } |
| |
| static void smb5_disable_interrupts(struct smb_charger *chg) |
| { |
| int i; |
| |
| for (i = 0; i < ARRAY_SIZE(smb5_irqs); i++) { |
| if (smb5_irqs[i].irq > 0) |
| disable_irq(smb5_irqs[i].irq); |
| } |
| } |
| |
| #if defined(CONFIG_DEBUG_FS) |
| |
| static int force_batt_psy_update_write(void *data, u64 val) |
| { |
| struct smb_charger *chg = data; |
| |
| power_supply_changed(chg->batt_psy); |
| return 0; |
| } |
| DEFINE_SIMPLE_ATTRIBUTE(force_batt_psy_update_ops, NULL, |
| force_batt_psy_update_write, "0x%02llx\n"); |
| |
| static int force_usb_psy_update_write(void *data, u64 val) |
| { |
| struct smb_charger *chg = data; |
| |
| power_supply_changed(chg->usb_psy); |
| return 0; |
| } |
| DEFINE_SIMPLE_ATTRIBUTE(force_usb_psy_update_ops, NULL, |
| force_usb_psy_update_write, "0x%02llx\n"); |
| |
| static int force_dc_psy_update_write(void *data, u64 val) |
| { |
| struct smb_charger *chg = data; |
| |
| power_supply_changed(chg->dc_psy); |
| return 0; |
| } |
| DEFINE_SIMPLE_ATTRIBUTE(force_dc_psy_update_ops, NULL, |
| force_dc_psy_update_write, "0x%02llx\n"); |
| |
| static void smb5_create_debugfs(struct smb5 *chip) |
| { |
| struct dentry *file; |
| |
| chip->dfs_root = debugfs_create_dir("charger", NULL); |
| if (IS_ERR_OR_NULL(chip->dfs_root)) { |
| pr_err("Couldn't create charger debugfs rc=%ld\n", |
| (long)chip->dfs_root); |
| return; |
| } |
| |
| file = debugfs_create_file("force_batt_psy_update", 0600, |
| chip->dfs_root, chip, &force_batt_psy_update_ops); |
| if (IS_ERR_OR_NULL(file)) |
| pr_err("Couldn't create force_batt_psy_update file rc=%ld\n", |
| (long)file); |
| |
| file = debugfs_create_file("force_usb_psy_update", 0600, |
| chip->dfs_root, chip, &force_usb_psy_update_ops); |
| if (IS_ERR_OR_NULL(file)) |
| pr_err("Couldn't create force_usb_psy_update file rc=%ld\n", |
| (long)file); |
| |
| file = debugfs_create_file("force_dc_psy_update", 0600, |
| chip->dfs_root, chip, &force_dc_psy_update_ops); |
| if (IS_ERR_OR_NULL(file)) |
| pr_err("Couldn't create force_dc_psy_update file rc=%ld\n", |
| (long)file); |
| } |
| |
| #else |
| |
| static void smb5_create_debugfs(struct smb5 *chip) |
| {} |
| |
| #endif |
| |
| static int smb5_show_charger_status(struct smb5 *chip) |
| { |
| struct smb_charger *chg = &chip->chg; |
| union power_supply_propval val; |
| int usb_present, batt_present, batt_health, batt_charge_type; |
| int rc; |
| |
| rc = smblib_get_prop_usb_present(chg, &val); |
| if (rc < 0) { |
| pr_err("Couldn't get usb present rc=%d\n", rc); |
| return rc; |
| } |
| usb_present = val.intval; |
| |
| rc = smblib_get_prop_batt_present(chg, &val); |
| if (rc < 0) { |
| pr_err("Couldn't get batt present rc=%d\n", rc); |
| return rc; |
| } |
| batt_present = val.intval; |
| |
| rc = smblib_get_prop_batt_health(chg, &val); |
| if (rc < 0) { |
| pr_err("Couldn't get batt health rc=%d\n", rc); |
| val.intval = POWER_SUPPLY_HEALTH_UNKNOWN; |
| } |
| batt_health = val.intval; |
| |
| rc = smblib_get_prop_batt_charge_type(chg, &val); |
| if (rc < 0) { |
| pr_err("Couldn't get batt charge type rc=%d\n", rc); |
| return rc; |
| } |
| batt_charge_type = val.intval; |
| |
| pr_info("SMB5 status - usb:present=%d type=%d batt:present = %d health = %d charge = %d\n", |
| usb_present, chg->real_charger_type, |
| batt_present, batt_health, batt_charge_type); |
| return rc; |
| } |
| |
| static int smb5_probe(struct platform_device *pdev) |
| { |
| struct smb5 *chip; |
| struct smb_charger *chg; |
| int rc = 0; |
| |
| chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
| if (!chip) |
| return -ENOMEM; |
| |
| chg = &chip->chg; |
| chg->dev = &pdev->dev; |
| chg->debug_mask = &__debug_mask; |
| chg->pd_disabled = &__pd_disabled; |
| chg->weak_chg_icl_ua = &__weak_chg_icl_ua; |
| chg->mode = PARALLEL_MASTER; |
| chg->irq_info = smb5_irqs; |
| chg->die_health = -EINVAL; |
| chg->otg_present = false; |
| |
| chg->regmap = dev_get_regmap(chg->dev->parent, NULL); |
| if (!chg->regmap) { |
| pr_err("parent regmap is missing\n"); |
| return -EINVAL; |
| } |
| |
| rc = smb5_chg_config_init(chip); |
| if (rc < 0) { |
| if (rc != -EPROBE_DEFER) |
| pr_err("Couldn't setup chg_config rc=%d\n", rc); |
| return rc; |
| } |
| |
| rc = smb5_parse_dt(chip); |
| if (rc < 0) { |
| pr_err("Couldn't parse device tree rc=%d\n", rc); |
| return rc; |
| } |
| |
| rc = smblib_init(chg); |
| if (rc < 0) { |
| pr_err("Smblib_init failed rc=%d\n", rc); |
| return rc; |
| } |
| |
| /* set driver data before resources request it */ |
| platform_set_drvdata(pdev, chip); |
| |
| rc = smb5_init_vbus_regulator(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize vbus regulator rc=%d\n", |
| rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_init_vconn_regulator(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize vconn regulator rc=%d\n", |
| rc); |
| goto cleanup; |
| } |
| |
| /* extcon registration */ |
| chg->extcon = devm_extcon_dev_allocate(chg->dev, smblib_extcon_cable); |
| if (IS_ERR(chg->extcon)) { |
| rc = PTR_ERR(chg->extcon); |
| dev_err(chg->dev, "failed to allocate extcon device rc=%d\n", |
| rc); |
| goto cleanup; |
| } |
| |
| rc = devm_extcon_dev_register(chg->dev, chg->extcon); |
| if (rc < 0) { |
| dev_err(chg->dev, "failed to register extcon device rc=%d\n", |
| rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_init_hw(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize hardware rc=%d\n", rc); |
| goto cleanup; |
| } |
| |
| if (chg->smb_version == PM855B_SUBTYPE) { |
| rc = smb5_init_dc_psy(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize dc psy rc=%d\n", rc); |
| goto cleanup; |
| } |
| } |
| |
| rc = smb5_init_usb_psy(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize usb psy rc=%d\n", rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_init_usb_main_psy(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize usb main psy rc=%d\n", rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_init_usb_port_psy(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize usb pc_port psy rc=%d\n", rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_init_batt_psy(chip); |
| if (rc < 0) { |
| pr_err("Couldn't initialize batt psy rc=%d\n", rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_determine_initial_status(chip); |
| if (rc < 0) { |
| pr_err("Couldn't determine initial status rc=%d\n", |
| rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_request_interrupts(chip); |
| if (rc < 0) { |
| pr_err("Couldn't request interrupts rc=%d\n", rc); |
| goto cleanup; |
| } |
| |
| rc = smb5_post_init(chip); |
| if (rc < 0) { |
| pr_err("Failed in post init rc=%d\n", rc); |
| goto free_irq; |
| } |
| |
| smb5_create_debugfs(chip); |
| |
| rc = smb5_show_charger_status(chip); |
| if (rc < 0) { |
| pr_err("Failed in getting charger status rc=%d\n", rc); |
| goto free_irq; |
| } |
| |
| device_init_wakeup(chg->dev, true); |
| |
| pr_info("QPNP SMB5 probed successfully\n"); |
| |
| return rc; |
| |
| free_irq: |
| smb5_free_interrupts(chg); |
| cleanup: |
| smblib_deinit(chg); |
| platform_set_drvdata(pdev, NULL); |
| |
| return rc; |
| } |
| |
| static int smb5_remove(struct platform_device *pdev) |
| { |
| struct smb5 *chip = platform_get_drvdata(pdev); |
| struct smb_charger *chg = &chip->chg; |
| |
| /* force enable APSD */ |
| smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| BC1P2_SRC_DETECT_BIT, BC1P2_SRC_DETECT_BIT); |
| |
| smb5_free_interrupts(chg); |
| smblib_deinit(chg); |
| platform_set_drvdata(pdev, NULL); |
| return 0; |
| } |
| |
| static void smb5_shutdown(struct platform_device *pdev) |
| { |
| struct smb5 *chip = platform_get_drvdata(pdev); |
| struct smb_charger *chg = &chip->chg; |
| |
| /* disable all interrupts */ |
| smb5_disable_interrupts(chg); |
| |
| /* configure power role for UFP */ |
| if (chg->connector_type == POWER_SUPPLY_CONNECTOR_TYPEC) |
| smblib_masked_write(chg, TYPE_C_MODE_CFG_REG, |
| TYPEC_POWER_ROLE_CMD_MASK, EN_SNK_ONLY_BIT); |
| |
| /* force HVDCP to 5V */ |
| smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT, 0); |
| smblib_write(chg, CMD_HVDCP_2_REG, FORCE_5V_BIT); |
| |
| /* force enable APSD */ |
| smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| BC1P2_SRC_DETECT_BIT, BC1P2_SRC_DETECT_BIT); |
| } |
| |
| static const struct of_device_id match_table[] = { |
| { .compatible = "qcom,qpnp-smb5", }, |
| { }, |
| }; |
| |
| static struct platform_driver smb5_driver = { |
| .driver = { |
| .name = "qcom,qpnp-smb5", |
| .owner = THIS_MODULE, |
| .of_match_table = match_table, |
| }, |
| .probe = smb5_probe, |
| .remove = smb5_remove, |
| .shutdown = smb5_shutdown, |
| }; |
| module_platform_driver(smb5_driver); |
| |
| MODULE_DESCRIPTION("QPNP SMB5 Charger Driver"); |
| MODULE_LICENSE("GPL v2"); |