usb: typec: Add sysfs node to show cc orientation
authorBadhri Jagan Sridharan <badhri@google.com>
Wed, 26 Feb 2020 19:57:58 +0000 (11:57 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 4 Mar 2020 09:51:21 +0000 (10:51 +0100)
Export Type-C orientation information when available.
- "normal": CC1 orientation
- "reverse": CC2 orientation
- "unknown": Orientation cannot be determined.

Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Reviewed-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20200226195758.150477-1-badhri@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/ABI/testing/sysfs-class-typec
drivers/usb/typec/class.c
drivers/usb/typec/tcpm/tcpm.c
include/linux/usb/typec.h

index 0c2eb26..b834671 100644 (file)
@@ -108,6 +108,15 @@ Contact:   Heikki Krogerus <heikki.krogerus@linux.intel.com>
 Description:
                Revision number of the supported USB Type-C specification.
 
+What:          /sys/class/typec/<port>/orientation
+Date:          February 2020
+Contact:       Badhri Jagan Sridharan <badhri@google.com>
+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/)
 
index 12be5bb..bf97c31 100644 (file)
@@ -1244,6 +1244,25 @@ 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_NORMAL:
+               return sprintf(buf, "%s\n", "normal");
+       case TYPEC_ORIENTATION_REVERSE:
+               return sprintf(buf, "%s\n", "reverse");
+       case TYPEC_ORIENTATION_NONE:
+       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 +1273,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 +1303,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 +1517,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;
 }
index 78077c2..bc0032a 100644 (file)
@@ -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;
index 44d2838..b00a264 100644 (file)
@@ -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;