MLK-16013-9 usb: typec: add interface to get port type and role
authorLi Jun <jun.li@nxp.com>
Wed, 9 Jan 2019 11:27:21 +0000 (16:57 +0530)
committerLeonard Crestez <leonard.crestez@nxp.com>
Wed, 17 Apr 2019 23:51:34 +0000 (02:51 +0300)
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>
During 4.14 rebase renamed to typec_port_types_dt to avoid conflict with
sysfs values.

Signed-off-by: Leonard Crestez <leonard.crestez@nxp.com>
Rebase it on v4.19
Signed-off-by: Vipul Kumar <vipul_kumar@mentor.com>
drivers/usb/typec/class.c
include/linux/usb/typec.h

index 00141e0..9c16055 100644 (file)
@@ -10,6 +10,7 @@
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/of.h>
 
 #include "bus.h"
 
@@ -930,6 +931,12 @@ static const char * const typec_port_data_roles[] = {
        [TYPEC_PORT_DRD] = "dual",
 };
 
+static const char *const typec_port_types_dt[] = {
+       [TYPEC_PORT_DFP] = "dfp",
+       [TYPEC_PORT_UFP] = "ufp",
+       [TYPEC_PORT_DRP] = "drp",
+};
+
 static const char * const typec_port_types_drp[] = {
        [TYPEC_PORT_SRC] = "dual [source] sink",
        [TYPEC_PORT_SNK] = "dual source [sink]",
@@ -1282,6 +1289,47 @@ 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_dt, ARRAY_SIZE(typec_port_types_dt), 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 7df4eca..353677f 100644 (file)
@@ -22,6 +22,7 @@ enum typec_port_type {
        TYPEC_PORT_SRC,
        TYPEC_PORT_SNK,
        TYPEC_PORT_DRP,
+       TYPEC_PORT_TYPE_UNKNOWN,
 };
 
 enum typec_port_data {
@@ -46,6 +47,7 @@ enum typec_data_role {
 enum typec_role {
        TYPEC_SINK,
        TYPEC_SOURCE,
+       TYPEC_ROLE_UNKNOWN,
 };
 
 enum typec_pwr_opmode {
@@ -235,6 +237,8 @@ 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);
 
 int typec_set_orientation(struct typec_port *port,
                          enum typec_orientation orientation);