usb: typec: ucsi: psy: Add support for the charge type property
authorHeikki Krogerus <heikki.krogerus@linux.intel.com>
Wed, 19 Jun 2024 14:08:06 +0000 (17:08 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 20 Jun 2024 17:31:31 +0000 (19:31 +0200)
Adding support for the charge type Linux power supply class
property (POWER_SUPPLY_PROP_CHARGE_TYPE) to the UCSI driver.
That will make the charge type visible in the charge_type
power supply class sysfs attribute file.

UCSI has the charge type specified in the Battery Charging
Status field of the response to the GET_CONNECTOR_STATUS
command.

Reviewed-by: Sebastian Reichel <sebastian.reichel@collabora.com>
Reviewed-by: Mario Limonciello <mario.limonciello@amd.com>
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Link: https://lore.kernel.org/r/20240619140806.3502590-1-heikki.krogerus@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/typec/ucsi/psy.c
drivers/usb/typec/ucsi/ucsi.c

index b35c6e07911e90cc477de02a55280c0ba421ef9e..e623d80e177c07415e0475fae21a5573165a6612 100644 (file)
@@ -20,6 +20,7 @@ enum ucsi_psy_online_states {
 };
 
 static enum power_supply_property ucsi_psy_props[] = {
+       POWER_SUPPLY_PROP_CHARGE_TYPE,
        POWER_SUPPLY_PROP_USB_TYPE,
        POWER_SUPPLY_PROP_ONLINE,
        POWER_SUPPLY_PROP_VOLTAGE_MIN,
@@ -194,6 +195,35 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
        return 0;
 }
 
+static int ucsi_psy_get_charge_type(struct ucsi_connector *con, union power_supply_propval *val)
+{
+       if (!(con->status.flags & UCSI_CONSTAT_CONNECTED)) {
+               val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+               return 0;
+       }
+
+       /* The Battery Charging Cabability Status field is only valid in sink role. */
+       if ((con->status.flags & UCSI_CONSTAT_PWR_DIR) != TYPEC_SINK) {
+               val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
+               return 0;
+       }
+
+       switch (UCSI_CONSTAT_BC_STATUS(con->status.pwr_status)) {
+       case UCSI_CONSTAT_BC_NOMINAL_CHARGING:
+               val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+               break;
+       case UCSI_CONSTAT_BC_SLOW_CHARGING:
+       case UCSI_CONSTAT_BC_TRICKLE_CHARGING:
+               val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+               break;
+       default:
+               val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+               break;
+       }
+
+       return 0;
+}
+
 static int ucsi_psy_get_prop(struct power_supply *psy,
                             enum power_supply_property psp,
                             union power_supply_propval *val)
@@ -201,6 +231,8 @@ static int ucsi_psy_get_prop(struct power_supply *psy,
        struct ucsi_connector *con = power_supply_get_drvdata(psy);
 
        switch (psp) {
+       case POWER_SUPPLY_PROP_CHARGE_TYPE:
+               return ucsi_psy_get_charge_type(con, val);
        case POWER_SUPPLY_PROP_USB_TYPE:
                return ucsi_psy_get_usb_type(con, val);
        case POWER_SUPPLY_PROP_ONLINE:
index b39e8dcf0dba3b7aa7301c1ecec5fff1857c7742..76c48d873b2ac55af134f8fae97b4eda006448e6 100644 (file)
@@ -1266,6 +1266,9 @@ static void ucsi_handle_connector_change(struct work_struct *work)
        if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
                ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
 
+       if (con->status.change & UCSI_CONSTAT_BC_CHANGE)
+               ucsi_port_psy_changed(con);
+
 out_unlock:
        mutex_unlock(&con->lock);
 }