Skip to contentSkip to conversation

usb: typec: Add sysfs node to show cc orientation

PATCH Superseded Badhri Jagan Sridharan 2 participants 4 messages Feb 26, 2020

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Badhri Jagan Sridharan

author maintainer
#0 lore raw

PATCH v3 1/1 usb: typec: Add sysfs node to show cc orientation

Export Type-C orientation information when available.
- "normal": CC1 orientation
- "reverse": CC2 orientation
- "unknown": Orientation cannot be determined.

---
Version history:
V3:
- Heikki's suggestion to us .is_visible callback. unsigned int orientation_aware:1 has been introduced to make support of this attribute optional for drivers such as UCSI
- Guenter's suggestion to rename to "orientation".
- Heikki's suggestion to stick with string values instead of exposing it as integer values.

---
 Documentation/ABI/testing/sysfs-class-typec |  9 +++++++
 drivers/usb/typec/class.c                   | 27 +++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpm.c               |  1 +
 include/linux/usb/typec.h                   |  1 +
 4 files changed, 38 insertions(+)
Documentation/ABI/testing/sysfs-class-typec +9 -0
index 0c2eb26fdc06b..b834671522d6f 100644
@@ -108,6 +108,15 @@ Contact:	Heikki Krogerus <[email protected]>
 Description:
 		Revision number of the supported USB Type-C specification.
 
+What:		/sys/class/typec/<port>/orientation
+Date:		February 2020
+Contact:	Badhri Jagan Sridharan <[email protected]>
+Description:
+		Indicates the active orientation of the Type-C connector.
+		Valid values:
+		- "normal": CC1 orientation
+		- "reverse": CC2 orientation
+		- "unknown": Orientation cannot be determined.
 
 USB Type-C partner devices (eg. /sys/class/typec/port0-partner/)
 
drivers/usb/typec/class.c +27 -0
index 12be5bb6d32ca..2524f1571e425 100644
@@ -1244,6 +1244,26 @@ static ssize_t usb_power_delivery_revision_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(usb_power_delivery_revision);
 
+static ssize_t orientation_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct typec_port *p = to_typec_port(dev);
+	enum typec_orientation orientation = typec_get_orientation(p);
+
+	switch (orientation) {
+	case TYPEC_ORIENTATION_NONE:
+		return sprintf(buf, "%s\n", "unknown");
+	case TYPEC_ORIENTATION_NORMAL:
+		return sprintf(buf, "%s\n", "normal");
+	case TYPEC_ORIENTATION_REVERSE:
+		return sprintf(buf, "%s\n", "reverse");
+	default:
+		return sprintf(buf, "%s\n", "unknown");
+	}
+}
+static DEVICE_ATTR_RO(orientation);
+
 static struct attribute *typec_attrs[] = {
 	&dev_attr_data_role.attr,
 	&dev_attr_power_operation_mode.attr,
@@ -1254,6 +1274,7 @@ static struct attribute *typec_attrs[] = {
 	&dev_attr_usb_typec_revision.attr,
 	&dev_attr_vconn_source.attr,
 	&dev_attr_port_type.attr,
+	&dev_attr_orientation.attr,
 	NULL,
 };
 
@@ -1283,6 +1304,10 @@ static umode_t typec_attr_is_visible(struct kobject *kobj,
 			return 0;
 		if (port->cap->type != TYPEC_PORT_DRP)
 			return 0444;
+	} else if (attr == &dev_attr_orientation.attr) {
+		if (port->cap->orientation_aware)
+			return 0444;
+		return 0;
 	}
 
 	return attr->mode;
@@ -1493,6 +1518,8 @@ int typec_set_orientation(struct typec_port *port,
 	}
 
 	port->orientation = orientation;
+	sysfs_notify(&port->dev.kobj, NULL, "orientation");
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
 
 	return 0;
 }
drivers/usb/typec/tcpm/tcpm.c +1 -0
index 78077c234ef27..bc0032a6b9a14 100644
@@ -4742,6 +4742,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 	port->typec_caps.pd_revision = 0x0300;	/* USB-PD spec release 3.0 */
 	port->typec_caps.driver_data = port;
 	port->typec_caps.ops = &tcpm_ops;
+	port->typec_caps.orientation_aware = 1;
 
 	port->partner_desc.identity = &port->partner_ident;
 	port->port_type = port->typec_caps.type;
include/linux/usb/typec.h +1 -0
index 44d28387ced48..b00a2642a9cd6 100644
@@ -211,6 +211,7 @@ struct typec_capability {
 	u16			pd_revision; /* 0300H = "3.0" */
 	int			prefer_role;
 	enum typec_accessory	accessory[TYPEC_MAX_ACCESSORY];
+	unsigned int		orientation_aware:1;
 
 	struct fwnode_handle	*fwnode;
 	void			*driver_data;
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
author maintainer
#1 lore raw

Hi all,

Following up with the initial patch from Puma(https://lkml.org/lkml/2019/10/15/1317) after addressing the comments from the previous patches.

Thanks & Regards,
Badhri

Quoted 130 lines · Badhri Jagan Sridharan

Export Type-C orientation information when available.
- "normal": CC1 orientation
- "reverse": CC2 orientation
- "unknown": Orientation cannot be determined.

---
Version history:
V3:
- Heikki's suggestion to us .is_visible callback. unsigned int orientation_aware:1 has been introduced to make support of this attribute optional for drivers such as UCSI
- Guenter's suggestion to rename to "orientation".
- Heikki's suggestion to stick with string values instead of exposing it as integer values.

---
 Documentation/ABI/testing/sysfs-class-typec |  9 +++++++
 drivers/usb/typec/class.c                   | 27 +++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpm.c               |  1 +
 include/linux/usb/typec.h                   |  1 +
 4 files changed, 38 insertions(+)
Documentation/ABI/testing/sysfs-class-typec +9 -0
index 0c2eb26fdc06b..b834671522d6f 100644
@@ -108,6 +108,15 @@ Contact:   Heikki Krogerus <[email protected]>
 Description:
                Revision number of the supported USB Type-C specification.
 
+What:          /sys/class/typec/<port>/orientation
+Date:          February 2020
+Contact:       Badhri Jagan Sridharan <[email protected]>
+Description:
+               Indicates the active orientation of the Type-C connector.
+               Valid values:
+               - "normal": CC1 orientation
+               - "reverse": CC2 orientation
+               - "unknown": Orientation cannot be determined.
 
 USB Type-C partner devices (eg. /sys/class/typec/port0-partner/)
 
drivers/usb/typec/class.c +27 -0
index 12be5bb6d32ca..2524f1571e425 100644
@@ -1244,6 +1244,26 @@ static ssize_t usb_power_delivery_revision_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(usb_power_delivery_revision);
 
+static ssize_t orientation_show(struct device *dev,
+                                  struct device_attribute *attr,
+                                  char *buf)
+{
+       struct typec_port *p = to_typec_port(dev);
+       enum typec_orientation orientation = typec_get_orientation(p);
+
+       switch (orientation) {
+       case TYPEC_ORIENTATION_NONE:
+               return sprintf(buf, "%s\n", "unknown");
+       case TYPEC_ORIENTATION_NORMAL:
+               return sprintf(buf, "%s\n", "normal");
+       case TYPEC_ORIENTATION_REVERSE:
+               return sprintf(buf, "%s\n", "reverse");
+       default:
+               return sprintf(buf, "%s\n", "unknown");
+       }
+}
+static DEVICE_ATTR_RO(orientation);
+
 static struct attribute *typec_attrs[] = {
        &dev_attr_data_role.attr,
        &dev_attr_power_operation_mode.attr,
@@ -1254,6 +1274,7 @@ static struct attribute *typec_attrs[] = {
        &dev_attr_usb_typec_revision.attr,
        &dev_attr_vconn_source.attr,
        &dev_attr_port_type.attr,
+       &dev_attr_orientation.attr,
        NULL,
 };
 
@@ -1283,6 +1304,10 @@ static umode_t typec_attr_is_visible(struct kobject *kobj,
                        return 0;
                if (port->cap->type != TYPEC_PORT_DRP)
                        return 0444;
+       } else if (attr == &dev_attr_orientation.attr) {
+               if (port->cap->orientation_aware)
+                       return 0444;
+               return 0;
        }
 
        return attr->mode;
@@ -1493,6 +1518,8 @@ int typec_set_orientation(struct typec_port *port,
        }
 
        port->orientation = orientation;
+       sysfs_notify(&port->dev.kobj, NULL, "orientation");
+       kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
 
        return 0;
 }
drivers/usb/typec/tcpm/tcpm.c +1 -0
index 78077c234ef27..bc0032a6b9a14 100644
@@ -4742,6 +4742,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
        port->typec_caps.pd_revision = 0x0300;  /* USB-PD spec release 3.0 */
        port->typec_caps.driver_data = port;
        port->typec_caps.ops = &tcpm_ops;
+       port->typec_caps.orientation_aware = 1;
 
        port->partner_desc.identity = &port->partner_ident;
        port->port_type = port->typec_caps.type;
include/linux/usb/typec.h +1 -1
index 44d28387ced48..b00a2642a9cd6 100644
@@ -211,6 +211,7 @@ struct typec_capability {
        u16                     pd_revision; /* 0300H = "3.0" */
        int                     prefer_role;
        enum typec_accessory    accessory[TYPEC_MAX_ACCESSORY];
+       unsigned int            orientation_aware:1;
 
        struct fwnode_handle    *fwnode;
        void                    *driver_data;
--

2.25.0.265.gbab2e86ba0-goog

#2 lore raw
In reply to Badhri Jagan Sridharan
Quoted 57 lines · Badhri Jagan Sridharan

Export Type-C orientation information when available.
- "normal": CC1 orientation
- "reverse": CC2 orientation
- "unknown": Orientation cannot be determined.

---
Version history:
V3:
- Heikki's suggestion to us .is_visible callback. unsigned int orientation_aware:1 has been introduced to make support of this attribute optional for drivers such as UCSI
- Guenter's suggestion to rename to "orientation".
- Heikki's suggestion to stick with string values instead of exposing it as integer values.

---
 Documentation/ABI/testing/sysfs-class-typec |  9 +++++++
 drivers/usb/typec/class.c                   | 27 +++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpm.c               |  1 +
 include/linux/usb/typec.h                   |  1 +
 4 files changed, 38 insertions(+)
Documentation/ABI/testing/sysfs-class-typec +9 -0
index 0c2eb26fdc06b..b834671522d6f 100644
@@ -108,6 +108,15 @@ Contact:	Heikki Krogerus <[email protected]>
 Description:
 		Revision number of the supported USB Type-C specification.
 
+What:		/sys/class/typec/<port>/orientation
+Date:		February 2020
+Contact:	Badhri Jagan Sridharan <[email protected]>
+Description:
+		Indicates the active orientation of the Type-C connector.
+		Valid values:
+		- "normal": CC1 orientation
+		- "reverse": CC2 orientation
+		- "unknown": Orientation cannot be determined.
 
 USB Type-C partner devices (eg. /sys/class/typec/port0-partner/)
 
drivers/usb/typec/class.c +6 -0
index 12be5bb6d32ca..2524f1571e425 100644
@@ -1244,6 +1244,26 @@ static ssize_t usb_power_delivery_revision_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(usb_power_delivery_revision);
 
+static ssize_t orientation_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct typec_port *p = to_typec_port(dev);
+	enum typec_orientation orientation = typec_get_orientation(p);

drivers/usb/typec/class.c static ssize_t usb_power_delivery_revision_show(struct device *dev, · line 1262

Quoted 10 lines
drivers/usb/typec/class.c +10 -0
+
+	switch (orientation) {
+	case TYPEC_ORIENTATION_NONE:
+		return sprintf(buf, "%s\n", "unknown");
+	case TYPEC_ORIENTATION_NORMAL:
+		return sprintf(buf, "%s\n", "normal");
+	case TYPEC_ORIENTATION_REVERSE:
+		return sprintf(buf, "%s\n", "reverse");
+	default:
+		return sprintf(buf, "%s\n", "unknown");

TYPEC_ORIENTATION_NONE and default can be handled in a single case statement.

Quoted 62 lines
drivers/usb/typec/class.c +11 -0
+	}
+}
+static DEVICE_ATTR_RO(orientation);
+
 static struct attribute *typec_attrs[] = {
 	&dev_attr_data_role.attr,
 	&dev_attr_power_operation_mode.attr,
@@ -1254,6 +1274,7 @@ static struct attribute *typec_attrs[] = {
 	&dev_attr_usb_typec_revision.attr,
 	&dev_attr_vconn_source.attr,
 	&dev_attr_port_type.attr,
+	&dev_attr_orientation.attr,
 	NULL,
 };
 
@@ -1283,6 +1304,10 @@ static umode_t typec_attr_is_visible(struct kobject *kobj,
 			return 0;
 		if (port->cap->type != TYPEC_PORT_DRP)
 			return 0444;
+	} else if (attr == &dev_attr_orientation.attr) {
+		if (port->cap->orientation_aware)
+			return 0444;
+		return 0;
 	}
 
 	return attr->mode;
@@ -1493,6 +1518,8 @@ int typec_set_orientation(struct typec_port *port,
 	}
 
 	port->orientation = orientation;
+	sysfs_notify(&port->dev.kobj, NULL, "orientation");
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
 
 	return 0;
 }
drivers/usb/typec/tcpm/tcpm.c +1 -0
index 78077c234ef27..bc0032a6b9a14 100644
@@ -4742,6 +4742,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 	port->typec_caps.pd_revision = 0x0300;	/* USB-PD spec release 3.0 */
 	port->typec_caps.driver_data = port;
 	port->typec_caps.ops = &tcpm_ops;
+	port->typec_caps.orientation_aware = 1;
 
 	port->partner_desc.identity = &port->partner_ident;
 	port->port_type = port->typec_caps.type;
include/linux/usb/typec.h +1 -0
index 44d28387ced48..b00a2642a9cd6 100644
@@ -211,6 +211,7 @@ struct typec_capability {
 	u16			pd_revision; /* 0300H = "3.0" */
 	int			prefer_role;
 	enum typec_accessory	accessory[TYPEC_MAX_ACCESSORY];
+	unsigned int		orientation_aware:1;
 
 	struct fwnode_handle	*fwnode;
 	void			*driver_data;
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
author maintainer
#3 lore raw

Thanks Geunter ! Sure ! Updated the patch.

Quoted 134 lines · Guenter Roeck
Quoted 57 lines · Badhri Jagan Sridharan

Export Type-C orientation information when available.
- "normal": CC1 orientation
- "reverse": CC2 orientation
- "unknown": Orientation cannot be determined.

---
Version history:
V3:
- Heikki's suggestion to us .is_visible callback. unsigned int orientation_aware:1 has been introduced to make support of this attribute optional for drivers such as UCSI
- Guenter's suggestion to rename to "orientation".
- Heikki's suggestion to stick with string values instead of exposing it as integer values.

---
 Documentation/ABI/testing/sysfs-class-typec |  9 +++++++
 drivers/usb/typec/class.c                   | 27 +++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpm.c               |  1 +
 include/linux/usb/typec.h                   |  1 +
 4 files changed, 38 insertions(+)
Documentation/ABI/testing/sysfs-class-typec +9 -0
index 0c2eb26fdc06b..b834671522d6f 100644
@@ -108,6 +108,15 @@ Contact: Heikki Krogerus <[email protected]>
 Description:
              Revision number of the supported USB Type-C specification.
 
+What:                /sys/class/typec/<port>/orientation
+Date:                February 2020
+Contact:     Badhri Jagan Sridharan <[email protected]>
+Description:
+             Indicates the active orientation of the Type-C connector.
+             Valid values:
+             - "normal": CC1 orientation
+             - "reverse": CC2 orientation
+             - "unknown": Orientation cannot be determined.
 
 USB Type-C partner devices (eg. /sys/class/typec/port0-partner/)
 
drivers/usb/typec/class.c +6 -0
index 12be5bb6d32ca..2524f1571e425 100644
@@ -1244,6 +1244,26 @@ static ssize_t usb_power_delivery_revision_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(usb_power_delivery_revision);
 
+static ssize_t orientation_show(struct device *dev,
+                                struct device_attribute *attr,
+                                char *buf)
+{
+     struct typec_port *p = to_typec_port(dev);
+     enum typec_orientation orientation = typec_get_orientation(p);

drivers/usb/typec/class.c static ssize_t usb_power_delivery_revision_show(struct device *dev, · line 1262

Quoted 10 lines
drivers/usb/typec/class.c +10 -0
+
+     switch (orientation) {
+     case TYPEC_ORIENTATION_NONE:
+             return sprintf(buf, "%s\n", "unknown");
+     case TYPEC_ORIENTATION_NORMAL:
+             return sprintf(buf, "%s\n", "normal");
+     case TYPEC_ORIENTATION_REVERSE:
+             return sprintf(buf, "%s\n", "reverse");
+     default:
+             return sprintf(buf, "%s\n", "unknown");

TYPEC_ORIENTATION_NONE and default can be handled in a single case statement.

Quoted 62 lines
drivers/usb/typec/class.c +11 -0
+     }
+}
+static DEVICE_ATTR_RO(orientation);
+
 static struct attribute *typec_attrs[] = {
      &dev_attr_data_role.attr,
      &dev_attr_power_operation_mode.attr,
@@ -1254,6 +1274,7 @@ static struct attribute *typec_attrs[] = {
      &dev_attr_usb_typec_revision.attr,
      &dev_attr_vconn_source.attr,
      &dev_attr_port_type.attr,
+     &dev_attr_orientation.attr,
      NULL,
 };
 
@@ -1283,6 +1304,10 @@ static umode_t typec_attr_is_visible(struct kobject *kobj,
                      return 0;
              if (port->cap->type != TYPEC_PORT_DRP)
                      return 0444;
+     } else if (attr == &dev_attr_orientation.attr) {
+             if (port->cap->orientation_aware)
+                     return 0444;
+             return 0;
      }
 
      return attr->mode;
@@ -1493,6 +1518,8 @@ int typec_set_orientation(struct typec_port *port,
      }
 
      port->orientation = orientation;
+     sysfs_notify(&port->dev.kobj, NULL, "orientation");
+     kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
 
      return 0;
 }
drivers/usb/typec/tcpm/tcpm.c +1 -0
index 78077c234ef27..bc0032a6b9a14 100644
@@ -4742,6 +4742,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
      port->typec_caps.pd_revision = 0x0300;  /* USB-PD spec release 3.0 */
      port->typec_caps.driver_data = port;
      port->typec_caps.ops = &tcpm_ops;
+     port->typec_caps.orientation_aware = 1;
 
      port->partner_desc.identity = &port->partner_ident;
      port->port_type = port->typec_caps.type;
include/linux/usb/typec.h +1 -1
index 44d28387ced48..b00a2642a9cd6 100644
@@ -211,6 +211,7 @@ struct typec_capability {
      u16                     pd_revision; /* 0300H = "3.0" */
      int                     prefer_role;
      enum typec_accessory    accessory[TYPEC_MAX_ACCESSORY];
+     unsigned int            orientation_aware:1;
 
      struct fwnode_handle    *fwnode;
      void                    *driver_data;
--

2.25.0.265.gbab2e86ba0-goog