|
@@ -12,6 +12,7 @@
|
|
|
#include <linux/kernel.h>
|
|
|
#include <linux/module.h>
|
|
|
#include <linux/mutex.h>
|
|
|
+#include <linux/power_supply.h>
|
|
|
#include <linux/proc_fs.h>
|
|
|
#include <linux/sched/clock.h>
|
|
|
#include <linux/seq_file.h>
|
|
@@ -276,6 +277,11 @@ struct tcpm_port {
|
|
|
u32 current_limit;
|
|
|
u32 supply_voltage;
|
|
|
|
|
|
+ /* Used to export TA voltage and current */
|
|
|
+ struct power_supply *psy;
|
|
|
+ struct power_supply_desc psy_desc;
|
|
|
+ enum power_supply_usb_type usb_type;
|
|
|
+
|
|
|
u32 bist_request;
|
|
|
|
|
|
/* PD state for Vendor Defined Messages */
|
|
@@ -1895,6 +1901,7 @@ static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
port->pps_data.supported = false;
|
|
|
+ port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
|
|
|
|
|
|
/*
|
|
|
* Select the source PDO providing the most power which has a
|
|
@@ -1915,8 +1922,11 @@ static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
|
|
|
min_src_mv = pdo_min_voltage(pdo);
|
|
|
break;
|
|
|
case PDO_TYPE_APDO:
|
|
|
- if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
|
|
|
+ if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
|
|
|
port->pps_data.supported = true;
|
|
|
+ port->usb_type =
|
|
|
+ POWER_SUPPLY_USB_TYPE_PD_PPS;
|
|
|
+ }
|
|
|
continue;
|
|
|
default:
|
|
|
tcpm_log(port, "Invalid source PDO type, ignoring");
|
|
@@ -2472,6 +2482,9 @@ static void tcpm_reset_port(struct tcpm_port *port)
|
|
|
port->try_snk_count = 0;
|
|
|
port->supply_voltage = 0;
|
|
|
port->current_limit = 0;
|
|
|
+ port->usb_type = POWER_SUPPLY_USB_TYPE_C;
|
|
|
+
|
|
|
+ power_supply_changed(port->psy);
|
|
|
}
|
|
|
|
|
|
static void tcpm_detach(struct tcpm_port *port)
|
|
@@ -2999,6 +3012,8 @@ static void run_state_machine(struct tcpm_port *port)
|
|
|
tcpm_check_send_discover(port);
|
|
|
tcpm_pps_complete(port, port->pps_status);
|
|
|
|
|
|
+ power_supply_changed(port->psy);
|
|
|
+
|
|
|
break;
|
|
|
|
|
|
/* Accessory states */
|
|
@@ -3849,7 +3864,7 @@ static int tcpm_try_role(const struct typec_capability *cap, int role)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-static int __maybe_unused tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr)
|
|
|
+static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr)
|
|
|
{
|
|
|
unsigned int target_mw;
|
|
|
int ret;
|
|
@@ -3901,7 +3916,7 @@ swap_unlock:
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-static int __maybe_unused tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt)
|
|
|
+static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt)
|
|
|
{
|
|
|
unsigned int target_mw;
|
|
|
int ret;
|
|
@@ -3954,7 +3969,7 @@ swap_unlock:
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-static int __maybe_unused tcpm_pps_activate(struct tcpm_port *port, bool activate)
|
|
|
+static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
|
|
|
{
|
|
|
int ret = 0;
|
|
|
|
|
@@ -4159,6 +4174,230 @@ int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
|
|
|
|
|
|
+/* Power Supply access to expose source power information */
|
|
|
+enum tcpm_psy_online_states {
|
|
|
+ TCPM_PSY_OFFLINE = 0,
|
|
|
+ TCPM_PSY_FIXED_ONLINE,
|
|
|
+ TCPM_PSY_PROG_ONLINE,
|
|
|
+};
|
|
|
+
|
|
|
+static enum power_supply_property tcpm_psy_props[] = {
|
|
|
+ POWER_SUPPLY_PROP_USB_TYPE,
|
|
|
+ POWER_SUPPLY_PROP_ONLINE,
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_MIN,
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_MAX,
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
|
+ POWER_SUPPLY_PROP_CURRENT_MAX,
|
|
|
+ POWER_SUPPLY_PROP_CURRENT_NOW,
|
|
|
+};
|
|
|
+
|
|
|
+static int tcpm_psy_get_online(struct tcpm_port *port,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ if (port->vbus_charge) {
|
|
|
+ if (port->pps_data.active)
|
|
|
+ val->intval = TCPM_PSY_PROG_ONLINE;
|
|
|
+ else
|
|
|
+ val->intval = TCPM_PSY_FIXED_ONLINE;
|
|
|
+ } else {
|
|
|
+ val->intval = TCPM_PSY_OFFLINE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ if (port->pps_data.active)
|
|
|
+ val->intval = port->pps_data.min_volt * 1000;
|
|
|
+ else
|
|
|
+ val->intval = port->supply_voltage * 1000;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ if (port->pps_data.active)
|
|
|
+ val->intval = port->pps_data.max_volt * 1000;
|
|
|
+ else
|
|
|
+ val->intval = port->supply_voltage * 1000;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ val->intval = port->supply_voltage * 1000;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_get_current_max(struct tcpm_port *port,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ if (port->pps_data.active)
|
|
|
+ val->intval = port->pps_data.max_curr * 1000;
|
|
|
+ else
|
|
|
+ val->intval = port->current_limit * 1000;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_get_current_now(struct tcpm_port *port,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ val->intval = port->current_limit * 1000;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_get_prop(struct power_supply *psy,
|
|
|
+ enum power_supply_property psp,
|
|
|
+ union power_supply_propval *val)
|
|
|
+{
|
|
|
+ struct tcpm_port *port = power_supply_get_drvdata(psy);
|
|
|
+ int ret = 0;
|
|
|
+
|
|
|
+ switch (psp) {
|
|
|
+ case POWER_SUPPLY_PROP_USB_TYPE:
|
|
|
+ val->intval = port->usb_type;
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_ONLINE:
|
|
|
+ ret = tcpm_psy_get_online(port, val);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_MIN:
|
|
|
+ ret = tcpm_psy_get_voltage_min(port, val);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_MAX:
|
|
|
+ ret = tcpm_psy_get_voltage_max(port, val);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
+ ret = tcpm_psy_get_voltage_now(port, val);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_CURRENT_MAX:
|
|
|
+ ret = tcpm_psy_get_current_max(port, val);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
|
|
|
+ ret = tcpm_psy_get_current_now(port, val);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ ret = -EINVAL;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_set_online(struct tcpm_port *port,
|
|
|
+ const union power_supply_propval *val)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
+
|
|
|
+ switch (val->intval) {
|
|
|
+ case TCPM_PSY_FIXED_ONLINE:
|
|
|
+ ret = tcpm_pps_activate(port, false);
|
|
|
+ break;
|
|
|
+ case TCPM_PSY_PROG_ONLINE:
|
|
|
+ ret = tcpm_pps_activate(port, true);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ ret = -EINVAL;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_set_prop(struct power_supply *psy,
|
|
|
+ enum power_supply_property psp,
|
|
|
+ const union power_supply_propval *val)
|
|
|
+{
|
|
|
+ struct tcpm_port *port = power_supply_get_drvdata(psy);
|
|
|
+ int ret;
|
|
|
+
|
|
|
+ switch (psp) {
|
|
|
+ case POWER_SUPPLY_PROP_ONLINE:
|
|
|
+ ret = tcpm_psy_set_online(port, val);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
+ if (val->intval < port->pps_data.min_volt * 1000 ||
|
|
|
+ val->intval > port->pps_data.max_volt * 1000)
|
|
|
+ ret = -EINVAL;
|
|
|
+ else
|
|
|
+ ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
|
|
|
+ break;
|
|
|
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
|
|
|
+ if (val->intval > port->pps_data.max_curr * 1000)
|
|
|
+ ret = -EINVAL;
|
|
|
+ else
|
|
|
+ ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ ret = -EINVAL;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+static int tcpm_psy_prop_writeable(struct power_supply *psy,
|
|
|
+ enum power_supply_property psp)
|
|
|
+{
|
|
|
+ switch (psp) {
|
|
|
+ case POWER_SUPPLY_PROP_ONLINE:
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
|
|
|
+ return 1;
|
|
|
+ default:
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static enum power_supply_usb_type tcpm_psy_usb_types[] = {
|
|
|
+ POWER_SUPPLY_USB_TYPE_C,
|
|
|
+ POWER_SUPPLY_USB_TYPE_PD,
|
|
|
+ POWER_SUPPLY_USB_TYPE_PD_PPS,
|
|
|
+};
|
|
|
+
|
|
|
+static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
|
|
|
+
|
|
|
+static int devm_tcpm_psy_register(struct tcpm_port *port)
|
|
|
+{
|
|
|
+ struct power_supply_config psy_cfg = {};
|
|
|
+ const char *port_dev_name = dev_name(port->dev);
|
|
|
+ size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
|
|
|
+ strlen(port_dev_name) + 1;
|
|
|
+ char *psy_name;
|
|
|
+
|
|
|
+ psy_cfg.drv_data = port;
|
|
|
+ psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
|
|
|
+ if (!psy_name)
|
|
|
+ return -ENOMEM;
|
|
|
+
|
|
|
+ snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
|
|
|
+ port_dev_name);
|
|
|
+ port->psy_desc.name = psy_name;
|
|
|
+ port->psy_desc.type = POWER_SUPPLY_TYPE_USB,
|
|
|
+ port->psy_desc.usb_types = tcpm_psy_usb_types;
|
|
|
+ port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
|
|
|
+ port->psy_desc.properties = tcpm_psy_props,
|
|
|
+ port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props),
|
|
|
+ port->psy_desc.get_property = tcpm_psy_get_prop,
|
|
|
+ port->psy_desc.set_property = tcpm_psy_set_prop,
|
|
|
+ port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable,
|
|
|
+
|
|
|
+ port->usb_type = POWER_SUPPLY_USB_TYPE_C;
|
|
|
+
|
|
|
+ port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
|
|
|
+ &psy_cfg);
|
|
|
+
|
|
|
+ return PTR_ERR_OR_ZERO(port->psy);
|
|
|
+}
|
|
|
+
|
|
|
struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
|
|
|
{
|
|
|
struct tcpm_port *port;
|
|
@@ -4234,6 +4473,10 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
|
|
|
goto out_destroy_wq;
|
|
|
}
|
|
|
|
|
|
+ err = devm_tcpm_psy_register(port);
|
|
|
+ if (err)
|
|
|
+ goto out_destroy_wq;
|
|
|
+
|
|
|
port->typec_port = typec_register_port(port->dev, &port->typec_caps);
|
|
|
if (IS_ERR(port->typec_port)) {
|
|
|
err = PTR_ERR(port->typec_port);
|