MLK-16013-9 usb: typec: add interface to get port type and role
authorLi Jun <jun.li@nxp.com>
Wed, 26 Jul 2017 14:08:14 +0000 (22:08 +0800)
committerNitin Garg <nitin.garg@nxp.com>
Mon, 19 Mar 2018 20:36:18 +0000 (15:36 -0500)
Add interface to get typec port type and default power role from
dt. To validate a correct setting is specified, add TYPEC_PORT_TYPE_UNKNOWN
and TYPEC_ROLE_UNKNOWN for typec_port_type and typec_role enum.

Reviewed-by: Peter Chen <peter.chen@nxp.com>
Signed-off-by: Li Jun <jun.li@nxp.com>
drivers/usb/typec/typec.c
include/linux/usb/typec.h

index 89e540b..25b7451 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/of.h>
 #include <linux/usb/typec.h>
 
 struct typec_mode {
@@ -100,6 +101,12 @@ static const char * const typec_accessory_modes[] = {
        [TYPEC_ACCESSORY_DEBUG]         = "debug",
 };
 
+static const char *const typec_port_types[] = {
+       [TYPEC_PORT_DFP] = "dfp",
+       [TYPEC_PORT_UFP] = "ufp",
+       [TYPEC_PORT_DRP] = "drp",
+};
+
 static struct usb_pd_identity *get_pd_identity(struct device *dev)
 {
        if (is_typec_partner(dev)) {
@@ -1065,6 +1072,47 @@ static const struct device_type typec_port_dev_type = {
        .release = typec_release,
 };
 
+static enum typec_port_type typec_get_port_type_from_string(const char *str)
+{
+       int ret;
+
+       ret = match_string(typec_port_types, ARRAY_SIZE(typec_port_types), str);
+       return (ret < 0) ? TYPEC_PORT_TYPE_UNKNOWN : ret;
+}
+
+enum typec_port_type typec_get_port_type(struct device *dev)
+{
+       const char *port_type;
+       int err;
+
+       err = device_property_read_string(dev, "port-type", &port_type);
+       if (err < 0)
+               return TYPEC_PORT_TYPE_UNKNOWN;
+
+       return typec_get_port_type_from_string(port_type);
+}
+EXPORT_SYMBOL_GPL(typec_get_port_type);
+
+static enum typec_role typec_get_power_role_from_string(const char *str)
+{
+       int ret;
+
+       ret = match_string(typec_roles, ARRAY_SIZE(typec_roles), str);
+       return (ret < 0) ? TYPEC_ROLE_UNKNOWN : ret;
+}
+
+enum typec_role typec_get_power_role(struct device *dev)
+{
+       const char *power_role;
+       int err;
+
+       err = device_property_read_string(dev, "default-role", &power_role);
+       if (err < 0)
+               return TYPEC_ROLE_UNKNOWN;
+
+       return typec_get_power_role_from_string(power_role);
+}
+EXPORT_SYMBOL_GPL(typec_get_power_role);
 /* --------------------------------------- */
 /* Driver callbacks to report role updates */
 
index ec78204..e54728c 100644 (file)
@@ -24,6 +24,7 @@ enum typec_port_type {
        TYPEC_PORT_DFP,
        TYPEC_PORT_UFP,
        TYPEC_PORT_DRP,
+       TYPEC_PORT_TYPE_UNKNOWN,
 };
 
 enum typec_plug_type {
@@ -42,6 +43,7 @@ enum typec_data_role {
 enum typec_role {
        TYPEC_SINK,
        TYPEC_SOURCE,
+       TYPEC_ROLE_UNKNOWN,
 };
 
 enum typec_pwr_opmode {
@@ -239,5 +241,7 @@ void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
 void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
 void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
 void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
+enum typec_port_type typec_get_port_type(struct device *dev);
+enum typec_role typec_get_power_role(struct device *dev);
 
 #endif /* __LINUX_USB_TYPEC_H */