blob: b3f66d53ba5e61553787cc7bf32120852d57fdee [file] [log] [blame]
/* 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 "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_stat = {
.name = "input current limit status",
.reg = AICL_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_pmi855_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_stat = {
.name = "input current limit 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;
bool auto_recharge_soc;
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 __weak_chg_icl_ua = 500000;
module_param_named(
weak_chg_icl_ua, __weak_chg_icl_ua, int, 0600
);
#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_pmi855_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";
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 MICRO_1P5A 1500000
#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 = 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 = of_property_read_bool(node,
"qcom,auto-recharge-soc");
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;
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_ALLOWED,
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_SCOPE,
};
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_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_ALLOWED:
rc = smblib_get_prop_pd_allowed(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;
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;
mutex_lock(&chg->lock);
if (!chg->typec_present) {
rc = -EINVAL;
goto unlock;
}
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;
}
unlock:
mutex_unlock(&chg->lock);
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;
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:
chg->flash_active = val->intval;
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_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,
};
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_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;
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);
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_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:
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);
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:
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;
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:
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;
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;
}
/* 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;
/* configure micro USB mode */
rc = smblib_masked_write(chg, TYPEC_U_USB_CFG_REG,
EN_MICRO_USB_MODE_BIT, EN_MICRO_USB_MODE_BIT);
if (rc < 0) {
dev_err(chg->dev, "Couldn't enable micro USB mode rc=%d\n", rc);
return 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_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 */
/* TODO: auth can be enabled through vote based on APSD flow */
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);
}
chg->connector_type = type ? POWER_SUPPLY_CONNECTOR_MICRO_USB
: POWER_SUPPLY_CONNECTOR_TYPEC;
pr_debug("Connector type=%s\n", type ? "Micro USB" : "TypeC");
/*
* PMI632 based hw init:
* - Initialize flash module for PMI632
*/
if (chg->smb_version == PMI632_SUBTYPE)
schgm_flash_init(chg);
smblib_rerun_apsd_if_required(chg);
/* 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);
vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER,
true, 0);
vote(chg->pd_disallowed_votable_indirect, APSD_VOTER,
true, 0);
vote(chg->pd_disallowed_votable_indirect, MICRO_USB_VOTER,
chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB, 0);
/* 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:
* start from min and AICL ADC disable
*/
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 configure 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;
}
if (chg->connector_type == POWER_SUPPLY_CONNECTOR_MICRO_USB)
rc = smb5_configure_micro_usb(chg);
else
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;
}
/* 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;
}
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;
}
/* enable WD BARK and enable it on plugin */
rc = smblib_masked_write(chg, WD_CFG_REG,
WATCHDOG_TRIGGER_AFP_EN_BIT |
WDOG_TIMER_EN_ON_PLUGIN_BIT |
BARK_WDOG_INT_EN_BIT,
WDOG_TIMER_EN_ON_PLUGIN_BIT |
BARK_WDOG_INT_EN_BIT);
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,
SOC_BASED_RECHG_BIT,
chip->dt.auto_recharge_soc ? SOC_BASED_RECHG_BIT : 0);
if (rc < 0) {
dev_err(chg->dev, "Couldn't configure FG_UPDATE_CFG2_SEL_REG 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;
}
}
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;
if (chg->bms_psy)
smblib_suspend_on_debug_battery(chg);
usb_plugin_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,
},
[STEP_CHG_STATE_CHANGE_IRQ] = {
.name = "step-chg-state-change",
.handler = default_irq_handler,
},
[STEP_CHG_SOC_UPDATE_FAIL_IRQ] = {
.name = "step-chg-soc-update-fail",
.handler = default_irq_handler,
},
[STEP_CHG_SOC_UPDATE_REQ_IRQ] = {
.name = "step-chg-soc-update-req",
.handler = default_irq_handler,
},
[FG_FVCAL_QUALIFIED_IRQ] = {
.name = "fg-fvcal-qualified",
.handler = default_irq_handler,
},
[VPH_ALARM_IRQ] = {
.name = "vph-alarm",
.handler = default_irq_handler,
},
[VPH_DROP_PRECHG_IRQ] = {
.name = "vph-drop-prechg",
.handler = default_irq_handler,
},
/* DCDC IRQs */
[OTG_FAIL_IRQ] = {
.name = "otg-fail",
.handler = default_irq_handler,
},
[OTG_OC_DISABLE_SW_IRQ] = {
.name = "otg-oc-disable-sw",
.handler = default_irq_handler,
},
[OTG_OC_HICCUP_IRQ] = {
.name = "otg-oc-hiccup",
.handler = default_irq_handler,
},
[BSM_ACTIVE_IRQ] = {
.name = "bsm-active",
.handler = default_irq_handler,
},
[HIGH_DUTY_CYCLE_IRQ] = {
.name = "high-duty-cycle",
.handler = high_duty_cycle_irq_handler,
},
[INPUT_CURRENT_LIMITING_IRQ] = {
.name = "input-current-limiting",
.handler = default_irq_handler,
},
[CONCURRENT_MODE_DISABLE_IRQ] = {
.name = "concurrent-mode-disable",
.handler = default_irq_handler,
},
[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,
},
[ALL_CHNL_CONV_DONE_IRQ] = {
.name = "all-chnl-conv-done",
.handler = default_irq_handler,
},
[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",
.handler = default_irq_handler,
},
[VPH_OV_IRQ] = {
.name = "vph-ov",
.handler = default_irq_handler,
},
/* 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,
},
[USBIN_REVI_CHANGE_IRQ] = {
.name = "usbin-revi-change",
.handler = default_irq_handler,
},
[USBIN_SRC_CHANGE_IRQ] = {
.name = "usbin-src-change",
.handler = usb_source_change_irq_handler,
},
[USBIN_ICL_CHANGE_IRQ] = {
.name = "usbin-icl-change",
.handler = icl_change_irq_handler,
},
/* DC INPUT IRQs */
[DCIN_VASHDN_IRQ] = {
.name = "dcin-vashdn",
.handler = default_irq_handler,
},
[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",
.handler = default_irq_handler,
},
[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,
},
[TYPEC_VPD_DETECT_IRQ] = {
.name = "typec-vpd-detect",
.handler = default_irq_handler,
},
[TYPEC_CC_STATE_CHANGE_IRQ] = {
.name = "typec-cc-state-change",
.handler = typec_state_change_irq_handler,
},
[TYPEC_VCONN_OC_IRQ] = {
.name = "typec-vconn-oc",
.handler = default_irq_handler,
},
[TYPEC_VBUS_CHANGE_IRQ] = {
.name = "typec-vbus-change",
.handler = default_irq_handler,
},
[TYPEC_ATTACH_DETACH_IRQ] = {
.name = "typec-attach-detach",
.handler = default_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",
.handler = default_irq_handler,
},
/* MISCELLANEOUS IRQs */
[WDOG_SNARL_IRQ] = {
.name = "wdog-snarl",
.handler = NULL,
},
[WDOG_BARK_IRQ] = {
.name = "wdog-bark",
.handler = wdog_bark_irq_handler,
},
[AICL_FAIL_IRQ] = {
.name = "aicl-fail",
.handler = default_irq_handler,
},
[AICL_DONE_IRQ] = {
.name = "aicl-done",
.handler = default_irq_handler,
},
[SMB_EN_IRQ] = {
.name = "smb-en",
.handler = default_irq_handler,
},
[IMP_TRIGGER_IRQ] = {
.name = "imp-trigger",
.handler = default_irq_handler,
},
[TEMP_CHANGE_IRQ] = {
.name = "temp-change",
.handler = default_irq_handler,
},
[TEMP_CHANGE_SMB_IRQ] = {
.name = "temp-change-smb",
.handler = default_irq_handler,
},
/* FLASH */
[VREG_OK_IRQ] = {
.name = "vreg-ok",
.handler = schgm_flash_default_irq_handler,
},
[ILIM_S2_IRQ] = {
.name = "ilim2-s2",
.handler = schgm_flash_ilim2_irq_handler,
},
[ILIM_S1_IRQ] = {
.name = "ilim1-s1",
.handler = schgm_flash_default_irq_handler,
},
[VOUT_DOWN_IRQ] = {
.name = "vout-down",
.handler = schgm_flash_default_irq_handler,
},
[VOUT_UP_IRQ] = {
.name = "vout-up",
.handler = schgm_flash_default_irq_handler,
},
[FLASH_STATE_CHANGE_IRQ] = {
.name = "flash-state-change",
.handler = schgm_flash_state_change_irq_handler,
},
[TORCH_REQ_IRQ] = {
.name = "torch-req",
.handler = schgm_flash_default_irq_handler,
},
[FLASH_EN_IRQ] = {
.name = "flash-en",
.handler = schgm_flash_default_irq_handler,
},
};
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->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_parse_dt(chip);
if (rc < 0) {
pr_err("Couldn't parse device tree rc=%d\n", rc);
return rc;
}
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 = 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;
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");