Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp565913pxu; Wed, 25 Nov 2020 09:58:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJwqCnwRux2hdWwieVku5E+M/SKDOByq2WFJEwsnJNddZ3usQKd3sWhYJ2JYLnkWIhB8Ted1 X-Received: by 2002:a17:906:a43:: with SMTP id x3mr4012111ejf.197.1606327135980; Wed, 25 Nov 2020 09:58:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606327135; cv=none; d=google.com; s=arc-20160816; b=Pveq/ferD9Y3zToa0R+AG7CJNz5ex8vGV1WvP0DX9Ya7SmSEJE/tboD1WwVlcF6AjG AvzpWsHYnG7T4L4tO+U4x4w7geEA4FIpqtU/SN0KL7hCi0YZmLPsNE8ZisxWQImHbyAo bcNnE3KN79UATKZYT3nNmLMJtyyjRY1QTURcl3EwoGDJ5u9intvvCZMBDPzaeOAxJAeD upazvHFj6h70XCXyOsH4Grj+YKbfGkJ1nptQKvVWFEYexUcn8HFKS0yUKMHBwYcFGxNk I1lD5sdgY7Sg4cgBXO1OsEDFVlW2gDSdNB39Lv0n/NBUV6UksyJZcCWwuv9FDC5SQ6fv AnFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from; bh=3VqQ4Om0cKh6HJ+P653s8NjLyyGdg0sp2YYrgYIwUyw=; b=sPnREN01wzGKWbKESKGoT6NnWrDoo1B2YQYndGR9F5ApwT0armHjx7JVTyWkq1yy4i suvPDpAGH8QoUwCKuX+qcSPU+AoYJ6C+iuICTEjKpf2OfJsOcnxgxPmJrc74ZlCvhEVn zEw3I1p574/53+ispLfqBD7rfGEzNpPnq5bmtOZRWhq1gQJNSOcYtwKT/dluad7MY0Ko TE5hjkOr5g0eU+uTo5tmahAYb4lp67o18woNRKsC9w5v9LzKUuWC7T3dzgsA4ZzBRL1t eUp1quGpWonAmtGXMaaeZ/KB0nu9o+7/MKlzz8yAkHR5VZk014cK3TKTP50G7w+LMYaW L3gQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ho9si1636248ejc.275.2020.11.25.09.58.33; Wed, 25 Nov 2020 09:58:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732583AbgKYRzy (ORCPT + 99 others); Wed, 25 Nov 2020 12:55:54 -0500 Received: from honk.sigxcpu.org ([24.134.29.49]:44896 "EHLO honk.sigxcpu.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731310AbgKYRzx (ORCPT ); Wed, 25 Nov 2020 12:55:53 -0500 Received: from localhost (localhost [127.0.0.1]) by honk.sigxcpu.org (Postfix) with ESMTP id 0BEF7FB02; Wed, 25 Nov 2020 18:55:51 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at honk.sigxcpu.org Received: from honk.sigxcpu.org ([127.0.0.1]) by localhost (honk.sigxcpu.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Zj3mMRwPQSRm; Wed, 25 Nov 2020 18:55:48 +0100 (CET) Received: by bogon.sigxcpu.org (Postfix, from userid 1000) id C83144026B; Wed, 25 Nov 2020 18:55:46 +0100 (CET) From: =?UTF-8?q?Guido=20G=C3=BCnther?= To: Heikki Krogerus , Greg Kroah-Hartman , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 1/1] usb: typec: tps6598x: Export some power supply properties Date: Wed, 25 Nov 2020 18:55:46 +0100 Message-Id: X-Mailer: git-send-email 2.29.2 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This allows downstream supplies and userspace to detect whether external power is supplied. Signed-off-by: Guido Günther --- drivers/usb/typec/tps6598x.c | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c index 3db33bb622c3..ea72957602d8 100644 --- a/drivers/usb/typec/tps6598x.c +++ b/drivers/usb/typec/tps6598x.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ enum { }; /* TPS_REG_POWER_STATUS bits */ +#define TPS_POWER_STATUS_CONNECTION BIT(0) #define TPS_POWER_STATUS_SOURCESINK BIT(1) #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2) @@ -96,8 +98,24 @@ struct tps6598x { struct typec_partner *partner; struct usb_pd_identity partner_identity; struct usb_role_switch *role_sw; + struct typec_capability typec_cap; + + struct power_supply *psy; + struct power_supply_desc psy_desc; + enum power_supply_usb_type usb_type; +}; + +static enum power_supply_property tps6598x_psy_props[] = { + POWER_SUPPLY_PROP_USB_TYPE, + POWER_SUPPLY_PROP_ONLINE, +}; + +static enum power_supply_usb_type tps6598x_psy_usb_types[] = { + POWER_SUPPLY_USB_TYPE_C, }; +static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-"; + /* * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2: * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf @@ -248,6 +266,8 @@ static int tps6598x_connect(struct tps6598x *tps, u32 status) if (desc.identity) typec_partner_set_identity(tps->partner); + power_supply_changed(tps->psy); + return 0; } @@ -260,6 +280,7 @@ static void tps6598x_disconnect(struct tps6598x *tps, u32 status) typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status)); typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status)); tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), false); + power_supply_changed(tps->psy); } static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd, @@ -467,6 +488,78 @@ static const struct regmap_config tps6598x_regmap_config = { .max_register = 0x7F, }; +static int tps6598x_psy_get_online(struct tps6598x *tps, + union power_supply_propval *val) +{ + int ret; + u16 pwr_status; + + ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status); + if (ret < 0) + return ret; + + if (!(pwr_status & TPS_POWER_STATUS_CONNECTION) || + !(pwr_status & TPS_POWER_STATUS_SOURCESINK)) { + val->intval = 0; + } else { + val->intval = 1; + } + return 0; +} + +static int tps6598x_psy_get_prop(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct tps6598x *tps = power_supply_get_drvdata(psy); + int ret = 0; + + switch (psp) { + case POWER_SUPPLY_PROP_USB_TYPE: + val->intval = POWER_SUPPLY_USB_TYPE_C; + break; + case POWER_SUPPLY_PROP_ONLINE: + ret = tps6598x_psy_get_online(tps, val); + break; + default: + ret = -EINVAL; + break; + } + + return ret; +} + +static int devm_tps6598_psy_register(struct tps6598x *tps) +{ + struct power_supply_config psy_cfg = {}; + const char *port_dev_name = dev_name(tps->dev); + size_t psy_name_len = strlen(tps6598x_psy_name_prefix) + + strlen(port_dev_name) + 1; + char *psy_name; + + psy_cfg.drv_data = tps; + psy_cfg.fwnode = dev_fwnode(tps->dev); + psy_name = devm_kzalloc(tps->dev, psy_name_len, GFP_KERNEL); + if (!psy_name) + return -ENOMEM; + + snprintf(psy_name, psy_name_len, "%s%s", tps6598x_psy_name_prefix, + port_dev_name); + tps->psy_desc.name = psy_name; + tps->psy_desc.type = POWER_SUPPLY_TYPE_USB; + tps->psy_desc.usb_types = tps6598x_psy_usb_types; + tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types); + tps->psy_desc.properties = tps6598x_psy_props; + tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props); + tps->psy_desc.get_property = tps6598x_psy_get_prop; + + tps->usb_type = POWER_SUPPLY_USB_TYPE_C; + + tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc, + &psy_cfg); + return PTR_ERR_OR_ZERO(tps->psy); +} + static int tps6598x_probe(struct i2c_client *client) { struct typec_capability typec_cap = { }; @@ -560,6 +653,10 @@ static int tps6598x_probe(struct i2c_client *client) goto err_role_put; } + ret = devm_tps6598_psy_register(tps); + if (ret) + return ret; + tps->port = typec_register_port(&client->dev, &typec_cap); if (IS_ERR(tps->port)) { ret = PTR_ERR(tps->port); -- 2.29.2