Chrome platform driver development
 help / color / mirror / Atom feed
* [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs
@ 2025-02-10 13:04 Andrei Kuchynski
  2025-02-18 14:26 ` Tzung-Bi Shih
  2025-02-21  1:45 ` Tzung-Bi Shih
  0 siblings, 2 replies; 5+ messages in thread
From: Andrei Kuchynski @ 2025-02-10 13:04 UTC (permalink / raw)
  To: Prashant Malani, Benson Leung, Abhishek Pandit-Subedi,
	chrome-platform, Jameson Thies, Łukasz Bartosik
  Cc: linux-kernel, Andrei Kuchynski

This patch implements USB mode setting via a sysfs interface in
cros_ec_typec driver. User-space applications can now change the current
USB mode by writing to "usb_mode" sysfs entry, replacing the previous
ioctl-based method.
The embedded controller (EC) currently supports only entering USB4 mode
and exiting all modes (including altmodes). Both of these operations
trigger Data Reset Message, even if no USB Mode is active.
Additionally, the patch exposes the USB modes supported by the port via
"usb_capability" sysfs attribute.

Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
 drivers/platform/chrome/cros_ec_typec.c | 28 +++++++++++++++++++++++++
 drivers/platform/chrome/cros_ec_typec.h |  1 +
 2 files changed, 29 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c
index 6ee182101bc9..d2228720991f 100644
--- a/drivers/platform/chrome/cros_ec_typec.c
+++ b/drivers/platform/chrome/cros_ec_typec.c
@@ -42,6 +42,24 @@ static void cros_typec_role_switch_quirk(struct fwnode_handle *fwnode)
 #endif
 }
 
+static int cros_typec_enter_usb_mode(struct typec_port *tc_port, enum usb_mode mode)
+{
+	struct cros_typec_port *port = typec_get_drvdata(tc_port);
+	struct ec_params_typec_control req = {
+		.port = port->port_num,
+		.command = (mode == USB_MODE_USB4) ?
+			TYPEC_CONTROL_COMMAND_ENTER_MODE : TYPEC_CONTROL_COMMAND_EXIT_MODES,
+		.mode_to_enter = CROS_EC_ALTMODE_USB4
+	};
+
+	return cros_ec_cmd(port->typec_data->ec, 0, EC_CMD_TYPEC_CONTROL,
+			  &req, sizeof(req), NULL, 0);
+}
+
+static const struct typec_operations cros_typec_usb_mode_ops = {
+	.enter_usb_mode = cros_typec_enter_usb_mode
+};
+
 static int cros_typec_parse_port_props(struct typec_capability *cap,
 				       struct fwnode_handle *fwnode,
 				       struct device *dev)
@@ -84,6 +102,13 @@ static int cros_typec_parse_port_props(struct typec_capability *cap,
 		cap->prefer_role = ret;
 	}
 
+	if (fwnode_property_present(fwnode, "usb2-port"))
+		cap->usb_capability |= USB_CAPABILITY_USB2;
+	if (fwnode_property_present(fwnode, "usb3-port"))
+		cap->usb_capability |= USB_CAPABILITY_USB3;
+	if (fwnode_property_present(fwnode, "usb4-port"))
+		cap->usb_capability |= USB_CAPABILITY_USB4;
+
 	cros_typec_role_switch_quirk(fwnode);
 
 	cap->fwnode = fwnode;
@@ -379,6 +404,9 @@ static int cros_typec_init_ports(struct cros_typec_data *typec)
 		if (ret < 0)
 			goto unregister_ports;
 
+		cap->driver_data = cros_port;
+		cap->ops = &cros_typec_usb_mode_ops;
+
 		cros_port->port = typec_register_port(dev, cap);
 		if (IS_ERR(cros_port->port)) {
 			ret = PTR_ERR(cros_port->port);
diff --git a/drivers/platform/chrome/cros_ec_typec.h b/drivers/platform/chrome/cros_ec_typec.h
index 9fd5342bb0ad..f9c31f04c102 100644
--- a/drivers/platform/chrome/cros_ec_typec.h
+++ b/drivers/platform/chrome/cros_ec_typec.h
@@ -18,6 +18,7 @@
 enum {
 	CROS_EC_ALTMODE_DP = 0,
 	CROS_EC_ALTMODE_TBT,
+	CROS_EC_ALTMODE_USB4,
 	CROS_EC_ALTMODE_MAX,
 };
 
-- 
2.48.1.502.g6dc24dfdaf-goog


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

* Re: [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs
  2025-02-10 13:04 [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs Andrei Kuchynski
@ 2025-02-18 14:26 ` Tzung-Bi Shih
  2025-02-20 13:39   ` Andrei Kuchynski
  2025-02-21  1:45 ` Tzung-Bi Shih
  1 sibling, 1 reply; 5+ messages in thread
From: Tzung-Bi Shih @ 2025-02-18 14:26 UTC (permalink / raw)
  To: Andrei Kuchynski
  Cc: Prashant Malani, Benson Leung, Abhishek Pandit-Subedi,
	chrome-platform, Jameson Thies, Łukasz Bartosik,
	linux-kernel

On Mon, Feb 10, 2025 at 01:04:19PM +0000, Andrei Kuchynski wrote:
> +static int cros_typec_enter_usb_mode(struct typec_port *tc_port, enum usb_mode mode)
> +{
> +	struct cros_typec_port *port = typec_get_drvdata(tc_port);
> +	struct ec_params_typec_control req = {
> +		.port = port->port_num,
> +		.command = (mode == USB_MODE_USB4) ?
> +			TYPEC_CONTROL_COMMAND_ENTER_MODE : TYPEC_CONTROL_COMMAND_EXIT_MODES,
> +		.mode_to_enter = CROS_EC_ALTMODE_USB4

The symbol `CROS_EC_ALTMODE_USB4` doesn't exist.  On a related note, wouldn't
it always enter CROS_EC_ALTMODE_USB4 if the value is hard-coded here?

> @@ -84,6 +102,13 @@ static int cros_typec_parse_port_props(struct typec_capability *cap,
>  		cap->prefer_role = ret;
>  	}
>  
> +	if (fwnode_property_present(fwnode, "usb2-port"))
> +		cap->usb_capability |= USB_CAPABILITY_USB2;
> +	if (fwnode_property_present(fwnode, "usb3-port"))
> +		cap->usb_capability |= USB_CAPABILITY_USB3;
> +	if (fwnode_property_present(fwnode, "usb4-port"))
> +		cap->usb_capability |= USB_CAPABILITY_USB4;

Are these defined somewhere?  E.g. the bindings?

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

* Re: [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs
  2025-02-18 14:26 ` Tzung-Bi Shih
@ 2025-02-20 13:39   ` Andrei Kuchynski
  2025-02-21  1:41     ` Tzung-Bi Shih
  0 siblings, 1 reply; 5+ messages in thread
From: Andrei Kuchynski @ 2025-02-20 13:39 UTC (permalink / raw)
  To: Tzung-Bi Shih
  Cc: Prashant Malani, Benson Leung, Abhishek Pandit-Subedi,
	chrome-platform, Jameson Thies, Łukasz Bartosik,
	linux-kernel

On Tue, Feb 18, 2025 at 2:26 PM Tzung-Bi Shih <tzungbi@kernel.org> wrote:
>
> On Mon, Feb 10, 2025 at 01:04:19PM +0000, Andrei Kuchynski wrote:
> > +static int cros_typec_enter_usb_mode(struct typec_port *tc_port, enum usb_mode mode)
> > +{
> > +     struct cros_typec_port *port = typec_get_drvdata(tc_port);
> > +     struct ec_params_typec_control req = {
> > +             .port = port->port_num,
> > +             .command = (mode == USB_MODE_USB4) ?
> > +                     TYPEC_CONTROL_COMMAND_ENTER_MODE : TYPEC_CONTROL_COMMAND_EXIT_MODES,
> > +             .mode_to_enter = CROS_EC_ALTMODE_USB4
>
> The symbol `CROS_EC_ALTMODE_USB4` doesn't exist.  On a related note, wouldn't
> it always enter CROS_EC_ALTMODE_USB4 if the value is hard-coded here?
>

CROS_EC_ALTMODE_USB4 is defined in drivers/platform/chrome/cros_ec_typec.h.
TYPEC_CONTROL_COMMAND_ENTER_MODE command will be sent only if
mode == USB_MODE_USB4 because EC currently only supports entering USB4 mode.
Otherwise, TYPEC_CONTROL_COMMAND_EXIT_MODES is sent, and the mode_to_enter
field is irrelevant for this command.

> > @@ -84,6 +102,13 @@ static int cros_typec_parse_port_props(struct typec_capability *cap,
> >               cap->prefer_role = ret;
> >       }
> >
> > +     if (fwnode_property_present(fwnode, "usb2-port"))
> > +             cap->usb_capability |= USB_CAPABILITY_USB2;
> > +     if (fwnode_property_present(fwnode, "usb3-port"))
> > +             cap->usb_capability |= USB_CAPABILITY_USB3;
> > +     if (fwnode_property_present(fwnode, "usb4-port"))
> > +             cap->usb_capability |= USB_CAPABILITY_USB4;
>
> Are these defined somewhere?  E.g. the bindings?

Unfortunately, property names are set in the ACPI firmware in the same way.

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

* Re: [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs
  2025-02-20 13:39   ` Andrei Kuchynski
@ 2025-02-21  1:41     ` Tzung-Bi Shih
  0 siblings, 0 replies; 5+ messages in thread
From: Tzung-Bi Shih @ 2025-02-21  1:41 UTC (permalink / raw)
  To: Andrei Kuchynski
  Cc: Prashant Malani, Benson Leung, Abhishek Pandit-Subedi,
	chrome-platform, Jameson Thies, Łukasz Bartosik,
	linux-kernel

On Thu, Feb 20, 2025 at 01:39:57PM +0000, Andrei Kuchynski wrote:
> On Tue, Feb 18, 2025 at 2:26 PM Tzung-Bi Shih <tzungbi@kernel.org> wrote:
> >
> > On Mon, Feb 10, 2025 at 01:04:19PM +0000, Andrei Kuchynski wrote:
> > > +static int cros_typec_enter_usb_mode(struct typec_port *tc_port, enum usb_mode mode)
> > > +{
> > > +     struct cros_typec_port *port = typec_get_drvdata(tc_port);
> > > +     struct ec_params_typec_control req = {
> > > +             .port = port->port_num,
> > > +             .command = (mode == USB_MODE_USB4) ?
> > > +                     TYPEC_CONTROL_COMMAND_ENTER_MODE : TYPEC_CONTROL_COMMAND_EXIT_MODES,
> > > +             .mode_to_enter = CROS_EC_ALTMODE_USB4
> >
> > The symbol `CROS_EC_ALTMODE_USB4` doesn't exist.  On a related note, wouldn't
> > it always enter CROS_EC_ALTMODE_USB4 if the value is hard-coded here?
> >
> 
> CROS_EC_ALTMODE_USB4 is defined in drivers/platform/chrome/cros_ec_typec.h.

I see.  Overlooked that.

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

* Re: [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs
  2025-02-10 13:04 [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs Andrei Kuchynski
  2025-02-18 14:26 ` Tzung-Bi Shih
@ 2025-02-21  1:45 ` Tzung-Bi Shih
  1 sibling, 0 replies; 5+ messages in thread
From: Tzung-Bi Shih @ 2025-02-21  1:45 UTC (permalink / raw)
  To: Andrei Kuchynski
  Cc: Prashant Malani, Benson Leung, Abhishek Pandit-Subedi,
	chrome-platform, Jameson Thies, Łukasz Bartosik,
	linux-kernel

On Mon, Feb 10, 2025 at 01:04:19PM +0000, Andrei Kuchynski wrote:
> This patch implements USB mode setting via a sysfs interface in
> cros_ec_typec driver. User-space applications can now change the current
> USB mode by writing to "usb_mode" sysfs entry, replacing the previous
> ioctl-based method.
> The embedded controller (EC) currently supports only entering USB4 mode
> and exiting all modes (including altmodes). Both of these operations
> trigger Data Reset Message, even if no USB Mode is active.
> Additionally, the patch exposes the USB modes supported by the port via
> "usb_capability" sysfs attribute.
> 
> [...]

Applied to

    https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git for-next

[1/1] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs
      commit: 9fc83373f0ffb8834da48b1446a5c2fef9525bb1

Thanks!

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

end of thread, other threads:[~2025-02-21  1:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-10 13:04 [PATCH] platform/chrome: cros_ec_typec: Add support for setting USB mode via sysfs Andrei Kuchynski
2025-02-18 14:26 ` Tzung-Bi Shih
2025-02-20 13:39   ` Andrei Kuchynski
2025-02-21  1:41     ` Tzung-Bi Shih
2025-02-21  1:45 ` Tzung-Bi Shih

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox