linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] usb: typec: tps6598x: Export some power supply properties
@ 2020-12-01 12:59 Guido Günther
  2020-12-01 12:59 ` [PATCH v4 1/2] usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C Guido Günther
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Guido Günther @ 2020-12-01 12:59 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, linux-usb, linux-kernel,
	Andy Shevchenko


This allows downstream supplies and userspace to detect whether external power
is supplied.

The Librem 5 has the tp65982 in front of bq25980 charge controller.  Since that
is capable of sinking and sourcing power the online property helps to decide
what to do. It also makes upower happy.

There will be follow up patches providing more properties but these need some
more time to cook and i wanted to check if this is the right way to go?

changes from v3
  - As per review comments from Andy Shevchenko
    https://lore.kernel.org/linux-usb/CAHp75VeLZtm85Y=3QMkPGb332wn05-zr-_mrrwXvnqLhazR1Gg@mail.gmail.com/
    - Use positive conditionals
  - Add reviewed by from Heikki Krogerus
    https://lore.kernel.org/linux-usb/20201130102720.GA2911464@kuha.fi.intel.com/T/#u
    https://lore.kernel.org/linux-usb/20201130102942.GB2911464@kuha.fi.intel.com/T/#u
  - Fix typc vs typec typo in commit message

changes from v2
  - As per kernel test robot
    https://lore.kernel.org/linux-usb/202011271005.zJVawX74-lkp@intel.com/
    - Flip USB_ROLE_SWITCH and REGMAP_I2C from 'depends on' to 'select'
      This matches tcpm and avoids a config symbol recursion which went
      unnoticed on my arm64 build but trips up x86_64.

changes from v1
  - As per review comments from Heikki Krogerus
    https://lore.kernel.org/linux-usb/20201126123552.GP1008337@kuha.fi.intel.com/
    - select POWER_SUPPLY
    - use POWER_SUPPLY_USB_TYPE_PD when a PD contract got negotiated

To: Heikki Krogerus <heikki.krogerus@linux.intel.com>,Greg Kroah-Hartman <gregkh@linuxfoundation.org>,linux-usb@vger.kernel.org,linux-kernel@vger.kernel.org,Andy Shevchenko <andy.shevchenko@gmail.com>

Guido Günther (2):
  usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C
  usb: typec: tps6598x: Export some power supply properties

 drivers/usb/typec/Kconfig    |   5 +-
 drivers/usb/typec/tps6598x.c | 105 +++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 2 deletions(-)

-- 
2.29.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v4 1/2] usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C
  2020-12-01 12:59 [PATCH v4 0/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
@ 2020-12-01 12:59 ` Guido Günther
  2020-12-01 12:59 ` [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
  2020-12-01 13:55 ` [PATCH v4 0/2] " Andy Shevchenko
  2 siblings, 0 replies; 6+ messages in thread
From: Guido Günther @ 2020-12-01 12:59 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, linux-usb, linux-kernel,
	Andy Shevchenko

This is more in line with what tcpm does and will be needed
to avoid recursive dependency like

 > drivers/power/supply/Kconfig:2:error: recursive dependency detected!
   drivers/power/supply/Kconfig:2: symbol POWER_SUPPLY is selected by TYPEC_TPS6598X
   drivers/usb/typec/Kconfig:64: symbol TYPEC_TPS6598X depends on REGMAP_I2C
   drivers/base/regmap/Kconfig:19: symbol REGMAP_I2C is selected by CHARGER_ADP5061
   drivers/power/supply/Kconfig:93: symbol CHARGER_ADP5061 depends on POWER_SUPPLY
   For a resolution refer to Documentation/kbuild/kconfig-language.rst
   subsection "Kconfig recursive dependency limitations"

when selecting POWER_SUPPLY.

Signed-off-by: Guido Günther <agx@sigxcpu.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/Kconfig | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 6c5908a37ee8..772b07e9f188 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -64,8 +64,8 @@ config TYPEC_HD3SS3220
 config TYPEC_TPS6598X
 	tristate "TI TPS6598x USB Power Delivery controller driver"
 	depends on I2C
-	depends on REGMAP_I2C
-	depends on USB_ROLE_SWITCH || !USB_ROLE_SWITCH
+	select REGMAP_I2C
+	select USB_ROLE_SWITCH
 	help
 	  Say Y or M here if your system has TI TPS65982 or TPS65983 USB Power
 	  Delivery controller.
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties
  2020-12-01 12:59 [PATCH v4 0/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
  2020-12-01 12:59 ` [PATCH v4 1/2] usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C Guido Günther
@ 2020-12-01 12:59 ` Guido Günther
  2020-12-01 13:52   ` Andy Shevchenko
  2020-12-01 13:55 ` [PATCH v4 0/2] " Andy Shevchenko
  2 siblings, 1 reply; 6+ messages in thread
From: Guido Günther @ 2020-12-01 12:59 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, linux-usb, linux-kernel,
	Andy Shevchenko

This allows downstream supplies and userspace to detect
whether external power is supplied.

Signed-off-by: Guido Günther <agx@sigxcpu.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/Kconfig    |   1 +
 drivers/usb/typec/tps6598x.c | 105 +++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)

diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 772b07e9f188..365f905a8e49 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -64,6 +64,7 @@ config TYPEC_HD3SS3220
 config TYPEC_TPS6598X
 	tristate "TI TPS6598x USB Power Delivery controller driver"
 	depends on I2C
+	select POWER_SUPPLY
 	select REGMAP_I2C
 	select USB_ROLE_SWITCH
 	help
diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
index 3db33bb622c3..d7fc2813b4ee 100644
--- a/drivers/usb/typec/tps6598x.c
+++ b/drivers/usb/typec/tps6598x.c
@@ -9,6 +9,7 @@
 #include <linux/i2c.h>
 #include <linux/acpi.h>
 #include <linux/module.h>
+#include <linux/power_supply.h>
 #include <linux/regmap.h>
 #include <linux/interrupt.h>
 #include <linux/usb/typec.h>
@@ -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,25 @@ 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,
+	POWER_SUPPLY_USB_TYPE_PD,
+};
+
+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 +267,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 +281,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 +489,85 @@ 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 = 1;
+	} else {
+		val->intval = 0;
+	}
+	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);
+	u16 pwr_status;
+	int ret = 0;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_USB_TYPE:
+		ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
+		if (ret < 0)
+			return ret;
+		if (TPS_POWER_STATUS_PWROPMODE(pwr_status) == TYPEC_PWR_MODE_PD)
+			val->intval = POWER_SUPPLY_USB_TYPE_PD;
+		else
+			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 +661,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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties
  2020-12-01 12:59 ` [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
@ 2020-12-01 13:52   ` Andy Shevchenko
  2020-12-01 13:56     ` Guido Günther
  0 siblings, 1 reply; 6+ messages in thread
From: Andy Shevchenko @ 2020-12-01 13:52 UTC (permalink / raw)
  To: Guido Günther
  Cc: Heikki Krogerus, Greg Kroah-Hartman, USB, Linux Kernel Mailing List

On Tue, Dec 1, 2020 at 2:59 PM Guido Günther <agx@sigxcpu.org> wrote:
> This allows downstream supplies and userspace to detect
> whether external power is supplied.

...

> +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;

I'm so sorry by not noticing this one...

> +       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);

...followed by this.

Please, use devm_kasprintf() instead.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 0/2] usb: typec: tps6598x: Export some power supply properties
  2020-12-01 12:59 [PATCH v4 0/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
  2020-12-01 12:59 ` [PATCH v4 1/2] usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C Guido Günther
  2020-12-01 12:59 ` [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
@ 2020-12-01 13:55 ` Andy Shevchenko
  2 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2020-12-01 13:55 UTC (permalink / raw)
  To: Guido Günther
  Cc: Heikki Krogerus, Greg Kroah-Hartman, USB, Linux Kernel Mailing List

On Tue, Dec 1, 2020 at 2:59 PM Guido Günther <agx@sigxcpu.org> wrote:
> This allows downstream supplies and userspace to detect whether external power
> is supplied.
>
> The Librem 5 has the tp65982 in front of bq25980 charge controller.  Since that
> is capable of sinking and sourcing power the online property helps to decide
> what to do. It also makes upower happy.
>
> There will be follow up patches providing more properties but these need some
> more time to cook and i wanted to check if this is the right way to go?

From my perspective the patches are okay (after addressing one more
comment), FWIW
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> changes from v3
>   - As per review comments from Andy Shevchenko
>     https://lore.kernel.org/linux-usb/CAHp75VeLZtm85Y=3QMkPGb332wn05-zr-_mrrwXvnqLhazR1Gg@mail.gmail.com/
>     - Use positive conditionals
>   - Add reviewed by from Heikki Krogerus
>     https://lore.kernel.org/linux-usb/20201130102720.GA2911464@kuha.fi.intel.com/T/#u
>     https://lore.kernel.org/linux-usb/20201130102942.GB2911464@kuha.fi.intel.com/T/#u
>   - Fix typc vs typec typo in commit message
>
> changes from v2
>   - As per kernel test robot
>     https://lore.kernel.org/linux-usb/202011271005.zJVawX74-lkp@intel.com/
>     - Flip USB_ROLE_SWITCH and REGMAP_I2C from 'depends on' to 'select'
>       This matches tcpm and avoids a config symbol recursion which went
>       unnoticed on my arm64 build but trips up x86_64.
>
> changes from v1
>   - As per review comments from Heikki Krogerus
>     https://lore.kernel.org/linux-usb/20201126123552.GP1008337@kuha.fi.intel.com/
>     - select POWER_SUPPLY
>     - use POWER_SUPPLY_USB_TYPE_PD when a PD contract got negotiated
>
> To: Heikki Krogerus <heikki.krogerus@linux.intel.com>,Greg Kroah-Hartman <gregkh@linuxfoundation.org>,linux-usb@vger.kernel.org,linux-kernel@vger.kernel.org,Andy Shevchenko <andy.shevchenko@gmail.com>
>
> Guido Günther (2):
>   usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C
>   usb: typec: tps6598x: Export some power supply properties
>
>  drivers/usb/typec/Kconfig    |   5 +-
>  drivers/usb/typec/tps6598x.c | 105 +++++++++++++++++++++++++++++++++++
>  2 files changed, 108 insertions(+), 2 deletions(-)
>
> --
> 2.29.2
>


-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties
  2020-12-01 13:52   ` Andy Shevchenko
@ 2020-12-01 13:56     ` Guido Günther
  0 siblings, 0 replies; 6+ messages in thread
From: Guido Günther @ 2020-12-01 13:56 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Heikki Krogerus, Greg Kroah-Hartman, USB, Linux Kernel Mailing List

Hi Andy,
On Tue, Dec 01, 2020 at 03:52:40PM +0200, Andy Shevchenko wrote:
> On Tue, Dec 1, 2020 at 2:59 PM Guido Günther <agx@sigxcpu.org> wrote:
> > This allows downstream supplies and userspace to detect
> > whether external power is supplied.
> 
> ...
> 
> > +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;
> 
> I'm so sorry by not noticing this one...
> 
> > +       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);
> 
> ...followed by this.
> 
> Please, use devm_kasprintf() instead.

Will do. I'll let the series sit for a couple of days before sending a
v5.
Cheers,
 -- Guido

> 
> -- 
> With Best Regards,
> Andy Shevchenko
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-12-01 13:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-01 12:59 [PATCH v4 0/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
2020-12-01 12:59 ` [PATCH v4 1/2] usb: typec: tps6598x: Select USB_ROLE_SWITCH and REGMAP_I2C Guido Günther
2020-12-01 12:59 ` [PATCH v4 2/2] usb: typec: tps6598x: Export some power supply properties Guido Günther
2020-12-01 13:52   ` Andy Shevchenko
2020-12-01 13:56     ` Guido Günther
2020-12-01 13:55 ` [PATCH v4 0/2] " Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).