[PATCH v8 14/14] staging: typec: tcpci: move tcpci drivers out of staging

2018-06-26 Thread Li Jun
Move TCPCI(Typec port controller interface) driver and rt1711h
driver out of staging.

Signed-off-by: Li Jun 
---
 drivers/staging/Kconfig|  2 --
 drivers/staging/Makefile   |  1 -
 drivers/staging/typec/Kconfig  | 22 --
 drivers/staging/typec/Makefile |  2 --
 drivers/staging/typec/TODO |  5 -
 drivers/usb/typec/Kconfig  | 15 +++
 drivers/usb/typec/Makefile |  2 ++
 drivers/{staging => usb}/typec/tcpci.c |  0
 drivers/{staging => usb}/typec/tcpci.h |  0
 drivers/{staging => usb}/typec/tcpci_rt1711h.c |  0
 10 files changed, 17 insertions(+), 32 deletions(-)

diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index 75a4804..af9a82f 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -106,8 +106,6 @@ source "drivers/staging/greybus/Kconfig"
 
 source "drivers/staging/vc04_services/Kconfig"
 
-source "drivers/staging/typec/Kconfig"
-
 source "drivers/staging/vboxvideo/Kconfig"
 
 source "drivers/staging/pi433/Kconfig"
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index e84959a..8479d47 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -2,7 +2,6 @@
 # Makefile for staging directory
 
 obj-y  += media/
-obj-y  += typec/
 obj-$(CONFIG_PRISM2_USB)   += wlan-ng/
 obj-$(CONFIG_COMEDI)   += comedi/
 obj-$(CONFIG_FB_OLPC_DCON) += olpc_dcon/
diff --git a/drivers/staging/typec/Kconfig b/drivers/staging/typec/Kconfig
deleted file mode 100644
index 3aa981f..000
--- a/drivers/staging/typec/Kconfig
+++ /dev/null
@@ -1,22 +0,0 @@
-menu "USB Power Delivery and Type-C drivers"
-
-if TYPEC_TCPM
-
-config TYPEC_TCPCI
-   tristate "Type-C Port Controller Interface driver"
-   depends on I2C
-   select REGMAP_I2C
-   help
- Type-C Port Controller driver for TCPCI-compliant controller.
-
-config TYPEC_RT1711H
-   tristate "Richtek RT1711H Type-C chip driver"
-   select TYPEC_TCPCI
-   help
- Richtek RT1711H Type-C chip driver that works with
- Type-C Port Controller Manager to provide USB PD and USB
- Type-C functionalities.
-
-endif
-
-endmenu
diff --git a/drivers/staging/typec/Makefile b/drivers/staging/typec/Makefile
deleted file mode 100644
index 7803d48..000
--- a/drivers/staging/typec/Makefile
+++ /dev/null
@@ -1,2 +0,0 @@
-obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
-obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
diff --git a/drivers/staging/typec/TODO b/drivers/staging/typec/TODO
deleted file mode 100644
index 53fe2f7..000
--- a/drivers/staging/typec/TODO
+++ /dev/null
@@ -1,5 +0,0 @@
-tcpci:
-- Test with real hardware
-
-Please send patches to Guenter Roeck  and copy
-Heikki Krogerus .
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 2c8eab1..972fd19 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -56,6 +56,21 @@ config TYPEC_TCPM
 
 if TYPEC_TCPM
 
+config TYPEC_TCPCI
+   tristate "Type-C Port Controller Interface driver"
+   depends on I2C
+   select REGMAP_I2C
+   help
+ Type-C Port Controller driver for TCPCI-compliant controller.
+
+config TYPEC_RT1711H
+   tristate "Richtek RT1711H Type-C chip driver"
+   select TYPEC_TCPCI
+   help
+ Richtek RT1711H Type-C chip driver that works with
+ Type-C Port Controller Manager to provide USB PD and USB
+ Type-C functionalities.
+
 source "drivers/usb/typec/fusb302/Kconfig"
 
 config TYPEC_WCOVE
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index 1f599a6..46f86ee 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -7,3 +7,5 @@ obj-$(CONFIG_TYPEC_WCOVE)   += typec_wcove.o
 obj-$(CONFIG_TYPEC_UCSI)   += ucsi/
 obj-$(CONFIG_TYPEC_TPS6598X)   += tps6598x.o
 obj-$(CONFIG_TYPEC)+= mux/
+obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
+obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
diff --git a/drivers/staging/typec/tcpci.c b/drivers/usb/typec/tcpci.c
similarity index 100%
rename from drivers/staging/typec/tcpci.c
rename to drivers/usb/typec/tcpci.c
diff --git a/drivers/staging/typec/tcpci.h b/drivers/usb/typec/tcpci.h
similarity index 100%
rename from drivers/staging/typec/tcpci.h
rename to drivers/usb/typec/tcpci.h
diff --git a/drivers/staging/typec/tcpci_rt1711h.c 
b/drivers/usb/typec/tcpci_rt1711h.c
similarity index 100%
rename from drivers/staging/typec/tcpci_rt1711h.c
rename to drivers/usb/typec/tcpci_rt1711h.c
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 12/14] staging: typec: tcpci: keep the disconnected cc line open

2018-06-26 Thread Li Jun
While set polarity, we should keep the disconnected cc line to be
open.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 4d3b0ae..11c2d37 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -185,15 +185,25 @@ static int tcpci_set_polarity(struct tcpc_dev *tcpc,
  enum typec_cc_polarity polarity)
 {
struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+   unsigned int reg;
int ret;
 
-   ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
-  (polarity == TYPEC_POLARITY_CC2) ?
-  TCPC_TCPC_CTRL_ORIENTATION : 0);
+   /* Keep the disconnect cc line open */
+   ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, );
if (ret < 0)
return ret;
 
-   return 0;
+   if (polarity == TYPEC_POLARITY_CC2)
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT;
+   else
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT;
+   ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+   if (ret < 0)
+   return ret;
+
+   return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+  (polarity == TYPEC_POLARITY_CC2) ?
+  TCPC_TCPC_CTRL_ORIENTATION : 0);
 }
 
 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 13/14] staging: typec: tcpci: Only touch target bit when enable vconn

2018-06-26 Thread Li Jun
We need regmap_update_bits to avoid touch any other bits when
enable or disable vconn.

Reviewed-by: Guenter Roeck 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 11c2d37..ac6b418 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -218,12 +218,9 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool 
enable)
return ret;
}
 
-   ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
-  enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
-   if (ret < 0)
-   return ret;
-
-   return 0;
+   return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
+   TCPC_POWER_CTRL_VCONN_ENABLE,
+   enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 10/14] staging: typec: tcpci: enable vbus detection

2018-06-26 Thread Li Jun
TCPCI implementation may need SW to enable VBUS detection to generate
power status events.

Reviewed-by: Guenter Roeck 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 3b35fce..4d3b0ae 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -373,6 +373,12 @@ static int tcpci_init(struct tcpc_dev *tcpc)
if (ret < 0)
return ret;
 
+   /* Enable Vbus detection */
+   ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+  TCPC_CMD_ENABLE_VBUS_DETECT);
+   if (ret < 0)
+   return ret;
+
reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 11/14] typec: tcpm: add starting value for drp toggling

2018-06-26 Thread Li Jun
As DRP port autonomously toggles the Rp/Rd need a start value to
begin with, so add one parameter for it in tcpm_start_drp_toggling.

Reviewed-by: Guenter Roeck 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/usb/typec/tcpm.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 9dcab81..f5e9558 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2431,15 +2431,15 @@ static int tcpm_set_charge(struct tcpm_port *port, bool 
charge)
return 0;
 }
 
-static bool tcpm_start_drp_toggling(struct tcpm_port *port)
+static bool tcpm_start_drp_toggling(struct tcpm_port *port,
+   enum typec_cc_status cc)
 {
int ret;
 
if (port->tcpc->start_drp_toggling &&
port->port_type == TYPEC_PORT_DRP) {
tcpm_log_force(port, "Start DRP toggling");
-   ret = port->tcpc->start_drp_toggling(port->tcpc,
-tcpm_rp_cc(port));
+   ret = port->tcpc->start_drp_toggling(port->tcpc, cc);
if (!ret)
return true;
}
@@ -2747,7 +2747,7 @@ static void run_state_machine(struct tcpm_port *port)
if (!port->non_pd_role_swap)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_src_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, tcpm_rp_cc(port))) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
@@ -2922,7 +2922,7 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_pps_complete(port, -ENOTCONN);
tcpm_snk_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, TYPEC_CC_RD)) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 08/14] staging: typec: tcpci: use IS_ERR() instead of PTR_ERR_OR_ZERO()

2018-06-26 Thread Li Jun
As tcpm_register_port() and tcpci_register_port() never return
NULL and NULL is not a success in this case, use IS_ERR() to check
the return value of both.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076498a..b63f147 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -509,7 +509,7 @@ struct tcpci *tcpci_register_port(struct device *dev, 
struct tcpci_data *data)
return ERR_PTR(err);
 
tcpci->port = tcpm_register_port(tcpci->dev, >tcpc);
-   if (PTR_ERR_OR_ZERO(tcpci->port))
+   if (IS_ERR(tcpci->port))
return ERR_CAST(tcpci->port);
 
return tcpci;
@@ -551,7 +551,7 @@ static int tcpci_probe(struct i2c_client *client,
return err;
 
chip->tcpci = tcpci_register_port(>dev, >data);
-   if (PTR_ERR_OR_ZERO(chip->tcpci))
+   if (IS_ERR(chip->tcpci))
return PTR_ERR(chip->tcpci);
 
i2c_set_clientdata(client, chip);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 06/14] usb: typec: tcpm: support get typec and pd config from device properties

2018-06-26 Thread Li Jun
This patch adds support of get typec and power delivery config from
firmware description.

Reviewed-by: Heikki Krogerus 
Reviewed-by: Guenter Roeck 
Signed-off-by: Li Jun 
---
 drivers/usb/typec/tcpm.c | 131 +++
 1 file changed, 109 insertions(+), 22 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index d22b37b..9dcab81 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4236,6 +4236,81 @@ static int tcpm_copy_vdos(u32 *dest_vdo, const u32 
*src_vdo,
return nr_vdo;
 }
 
+static int tcpm_fw_get_caps(struct tcpm_port *port,
+   struct fwnode_handle *fwnode)
+{
+   const char *cap_str;
+   int ret;
+   u32 mw;
+
+   if (!fwnode)
+   return -EINVAL;
+
+   /* USB data support is optional */
+   ret = fwnode_property_read_string(fwnode, "data-role", _str);
+   if (ret == 0) {
+   port->typec_caps.data = typec_find_port_data_role(cap_str);
+   if (port->typec_caps.data < 0)
+   return -EINVAL;
+   }
+
+   ret = fwnode_property_read_string(fwnode, "power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.type = typec_find_port_power_role(cap_str);
+   if (port->typec_caps.type < 0)
+   return -EINVAL;
+   port->port_type = port->typec_caps.type;
+
+   if (port->port_type == TYPEC_PORT_SNK)
+   goto sink;
+
+   /* Get source pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+port->src_pdo, port->nr_src_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
+   port->nr_src_pdo))
+   return -EINVAL;
+
+   if (port->port_type == TYPEC_PORT_SRC)
+   return 0;
+
+   /* Get the preferred power role for DRP */
+   ret = fwnode_property_read_string(fwnode, "try-power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.prefer_role = typec_find_power_role(cap_str);
+   if (port->typec_caps.prefer_role < 0)
+   return -EINVAL;
+sink:
+   /* Get sink pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+port->snk_pdo, port->nr_snk_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
+   port->nr_snk_pdo))
+   return -EINVAL;
+
+   if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", ) < 0)
+   return -EINVAL;
+   port->operating_snk_mw = mw / 1000;
+
+   return 0;
+}
+
 int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
unsigned int nr_pdo)
 {
@@ -4521,12 +4596,36 @@ static int devm_tcpm_psy_register(struct tcpm_port 
*port)
return PTR_ERR_OR_ZERO(port->psy);
 }
 
+static int tcpm_copy_caps(struct tcpm_port *port,
+ const struct tcpc_config *tcfg)
+{
+   if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
+   tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
+   return -EINVAL;
+
+   port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
+ tcfg->nr_src_pdo);
+   port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
+ tcfg->nr_snk_pdo);
+
+   port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
+ tcfg->nr_snk_vdo);
+
+   port->operating_snk_mw = tcfg->operating_snk_mw;
+
+   port->typec_caps.prefer_role = tcfg->default_role;
+   port->typec_caps.type = tcfg->type;
+   port->typec_caps.data = tcfg->data;
+
+   return 0;
+}
+
 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 {
struct tcpm_port *port;
int i, err;
 
-   if (!dev || !tcpc || !tcpc->config ||
+   if (!dev || !tcpc ||
!tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
!tcpc->set_po

[PATCH v8 09/14] staging: typec: tcpci: register port before request irq

2018-06-26 Thread Li Jun
From: Peter Chen 

With that we can clear any pending events and the port is registered
so driver can be ready to handle typec events once we request irq.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Peter Chen 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 15 +--
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index b63f147..3b35fce 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -537,24 +537,27 @@ static int tcpci_probe(struct i2c_client *client,
if (IS_ERR(chip->data.regmap))
return PTR_ERR(chip->data.regmap);
 
+   i2c_set_clientdata(client, chip);
+
/* Disable chip interrupts before requesting irq */
err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, ,
   sizeof(u16));
if (err < 0)
return err;
 
+   chip->tcpci = tcpci_register_port(>dev, >data);
+   if (IS_ERR(chip->tcpci))
+   return PTR_ERR(chip->tcpci);
+
err = devm_request_threaded_irq(>dev, client->irq, NULL,
_tcpci_irq,
IRQF_ONESHOT | IRQF_TRIGGER_LOW,
dev_name(>dev), chip);
-   if (err < 0)
+   if (err < 0) {
+   tcpci_unregister_port(chip->tcpci);
return err;
+   }
 
-   chip->tcpci = tcpci_register_port(>dev, >data);
-   if (IS_ERR(chip->tcpci))
-   return PTR_ERR(chip->tcpci);
-
-   i2c_set_clientdata(client, chip);
return 0;
 }
 
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 07/14] staging: typec: tcpci: remove unused tcpci_tcpc_config

2018-06-26 Thread Li Jun
Since we will use config settings via device properties, so
remove the hard code tcpci_tcpc_config.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 7 ---
 1 file changed, 7 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index e59547a..076498a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -464,17 +464,10 @@ static const struct regmap_config tcpci_regmap_config = {
.max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
 };
 
-static const struct tcpc_config tcpci_tcpc_config = {
-   .type = TYPEC_PORT_DFP,
-   .default_role = TYPEC_SINK,
-};
-
 static int tcpci_parse_config(struct tcpci *tcpci)
 {
tcpci->controls_vbus = true; /* XXX */
 
-   /* TODO: Populate struct tcpc_config from ACPI/device-tree */
-   tcpci->tcpc.config = _tcpc_config;
tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
 "connector");
if (!tcpci->tcpc.fwnode) {
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 03/14] staging: typec: tcpci: add compatible string for nxp ptn5110

2018-06-26 Thread Li Jun
Add nxp ptn5110 typec controller compatible string: "nxp,ptn5110",
which is a standard tcpci chip with power delivery support. Meanwhile
remove "usb,tcpci" because it doesn't follow the binding format rule
and has not been used yet.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076d97e..dd29288 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -575,7 +575,7 @@ MODULE_DEVICE_TABLE(i2c, tcpci_id);
 
 #ifdef CONFIG_OF
 static const struct of_device_id tcpci_of_match[] = {
-   { .compatible = "usb,tcpci", },
+   { .compatible = "nxp,ptn5110", },
{},
 };
 MODULE_DEVICE_TABLE(of, tcpci_of_match);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 05/14] usb: typec: add API to get typec basic port power and data config

2018-06-26 Thread Li Jun
This patch adds 3 APIs to get the typec port power and data type,
and preferred power role by its name string.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/usb/typec/class.c | 58 +++
 include/linux/usb/typec.h |  3 +++
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 53df10d..6331059 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -796,12 +796,18 @@ static const char * const typec_data_roles[] = {
[TYPEC_HOST]= "host",
 };
 
-static const char * const typec_port_types[] = {
+static const char * const typec_port_power_roles[] = {
[TYPEC_PORT_SRC] = "source",
[TYPEC_PORT_SNK] = "sink",
[TYPEC_PORT_DRP] = "dual",
 };
 
+static const char * const typec_port_data_roles[] = {
+   [TYPEC_PORT_DFP] = "host",
+   [TYPEC_PORT_UFP] = "device",
+   [TYPEC_PORT_DRD] = "dual",
+};
+
 static const char * const typec_port_types_drp[] = {
[TYPEC_PORT_SRC] = "dual [source] sink",
[TYPEC_PORT_SNK] = "dual source [sink]",
@@ -932,7 +938,7 @@ static ssize_t power_role_store(struct device *dev,
mutex_lock(>port_type_lock);
if (port->port_type != TYPEC_PORT_DRP) {
dev_dbg(dev, "port type fixed at \"%s\"",
-typec_port_types[port->port_type]);
+typec_port_power_roles[port->port_type]);
ret = -EOPNOTSUPP;
goto unlock_and_ret;
}
@@ -973,7 +979,7 @@ port_type_store(struct device *dev, struct device_attribute 
*attr,
return -EOPNOTSUPP;
}
 
-   ret = sysfs_match_string(typec_port_types, buf);
+   ret = sysfs_match_string(typec_port_power_roles, buf);
if (ret < 0)
return ret;
 
@@ -1007,7 +1013,7 @@ port_type_show(struct device *dev, struct 
device_attribute *attr,
return sprintf(buf, "%s\n",
   typec_port_types_drp[port->port_type]);
 
-   return sprintf(buf, "[%s]\n", typec_port_types[port->cap->type]);
+   return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
 }
 static DEVICE_ATTR_RW(port_type);
 
@@ -1252,6 +1258,50 @@ void typec_set_pwr_opmode(struct typec_port *port,
 }
 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
 
+/**
+ * typec_find_port_power_role - Get the typec port power capability
+ * @name: port power capability string
+ *
+ * This routine is used to find the typec_port_type by its string name.
+ *
+ * Returns typec_port_type if success, otherwise negative error code.
+ */
+int typec_find_port_power_role(const char *name)
+{
+   return match_string(typec_port_power_roles,
+   ARRAY_SIZE(typec_port_power_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_port_power_role);
+
+/**
+ * typec_find_power_role - Find the typec one specific power role
+ * @name: power role string
+ *
+ * This routine is used to find the typec_role by its string name.
+ *
+ * Returns typec_role if success, otherwise negative error code.
+ */
+int typec_find_power_role(const char *name)
+{
+   return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_power_role);
+
+/**
+ * typec_find_port_data_role - Get the typec port data capability
+ * @name: port data capability string
+ *
+ * This routine is used to find the typec_port_data by its string name.
+ *
+ * Returns typec_port_data if success, otherwise negative error code.
+ */
+int typec_find_port_data_role(const char *name)
+{
+   return match_string(typec_port_data_roles,
+   ARRAY_SIZE(typec_port_data_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_port_data_role);
+
 /* -- */
 /* API for Multiplexer/DeMultiplexer Switches */
 
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 672b39b..15f8d9a 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -267,4 +267,7 @@ int typec_set_orientation(struct typec_port *port,
  enum typec_orientation orientation);
 int typec_set_mode(struct typec_port *port, int mode);
 
+int typec_find_port_power_role(const char *name);
+int typec_find_power_role(const char *name);
+int typec_find_port_data_role(const char *name);
 #endif /* __LINUX_USB_TYPEC_H */
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 04/14] usb: typec: add fwnode to tcpc

2018-06-26 Thread Li Jun
Add fwnode handle to get the fwnode so we can get typec configs
it contains.

Suggested-by: Heikki Krogerus 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 7 +++
 drivers/usb/typec/tcpm.c  | 1 +
 include/linux/usb/tcpm.h  | 2 ++
 3 files changed, 10 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index dd29288..e59547a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -474,6 +475,12 @@ static int tcpci_parse_config(struct tcpci *tcpci)
 
/* TODO: Populate struct tcpc_config from ACPI/device-tree */
tcpci->tcpc.config = _tcpc_config;
+   tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
+"connector");
+   if (!tcpci->tcpc.fwnode) {
+   dev_err(tcpci->dev, "Can't find connector node.\n");
+   return -EINVAL;
+   }
 
return 0;
 }
diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 8a201dd..d22b37b 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4576,6 +4576,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
else
port->try_role = TYPEC_NO_PREFERRED_ROLE;
 
+   port->typec_caps.fwnode = tcpc->fwnode;
port->typec_caps.prefer_role = tcpc->config->default_role;
port->typec_caps.type = tcpc->config->type;
port->typec_caps.data = tcpc->config->data;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index b231b93..193920a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -110,6 +110,7 @@ enum tcpc_mux_mode {
 /**
  * struct tcpc_dev - Port configuration and callback functions
  * @config:Pointer to port configuration
+ * @fwnode:Pointer to port fwnode
  * @get_vbus:  Called to read current VBUS state
  * @get_current_limit:
  * Optional; called by the tcpm core when configured as a snk
@@ -138,6 +139,7 @@ enum tcpc_mux_mode {
  */
 struct tcpc_dev {
const struct tcpc_config *config;
+   struct fwnode_handle *fwnode;
 
int (*init)(struct tcpc_dev *dev);
int (*get_vbus)(struct tcpc_dev *dev);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 00/14] staging: typec: tcpci: move out of staging

2018-06-26 Thread Li Jun
This patch set attempts to move the tcpci drivers out of staging by fix
some tcpci driver issues and define typec and power delivery device
properties, the changes are verified on NXP PTN5110, which is a standard
tcpci typec port controller device with power delivery support, tested
power source and sink with drp config.

Changes for v8:
- Favouring the generic FW based capabilities(tcpc_dev->fwnode) over values
  defined in a Type C port controller driver(tcpc_dev->config)
- Add Heikki's Reviewed-by for patch:
  usb: typec: add API to get typec basic port power and data config

Changes for v7:
- Fix export symbols mismatch in patch:
  usb: typec: add API to get typec basic port power and data config
  rename typec_port_types to be typec_port_power_roles.
- Drop patch:
  usb: typec: tcpm: set cc for drp toggling attach
  It's arguable as it's only to address specific HW problem, and which
  can be resolved by add try.src/snk property.

Changes for v6:
- Change function name to be typec_find_port_power/data_role for find
  capability, and typec_find_power_role for find one specific power role.
- Fix rt1711h driver move missing.
- Add one patch to improve the error checking in tcpci driver, use IS_ERR()
  instead of PTR_ERR_OR_ZERO().
- Add Rob's Reviewed-by for patch [2/15].
- Misc typos fix.
 
Changes for v5:
- Use "power-role" and "data-role" for typec port power and data capability
  property name.  
- Use macro definition instead of cryptic numbers for readability of
  power delivery properties(PDO).
- Add one tcpci driver binding string to be "nxp,ptn5110" to follow binding
  name rule ",".
- Change operating power property name to be op-sink-microwatt.
- Add one patch(remove unused tcpci_tcpc_config) to resolve bisectablitity
  issue of patch usb: typec: add fwnode to tcpc.
- A minor error handling change to keep the error path and success path
  separate in patch: register port before request irq
- Rebase to latest Greg's tree with max_snk_* removed.

Changes for v4:
- Remove max-sink-* properties as we will purge max_snk_* in tcpm,
  see patch set[4].
- Get typec power and data type value via name string(patch 5).
- Move finding typec and pd properties code from tcpci to tcpm(patch 6)
- Add a compatible string for nxp ptn5110 typec controller in tcpci driver.
  (patch 3)
- Add set cc for drp toggling without try.src/snk in tcpm(patch 10), then
  patch 11 can only update CCx bits for keep disconnect cc line open.
- Update op-sink-microwatt-hours example value to be the right value in
  micorwatts, and accordingly divide 1000 to get its miliwatts value
  in patch 6.
- Add Guenter's Reviewed-by for patch(8/9/12)

[4] https://www.spinics.net/lists/linux-usb/msg167261.html

Changes for v3:
- Use 2 properties to separate power and data capability of typec port:
  "power-type" and "data-type", this is based on Heikki's typec class code
  change[2]. use "try-power-role" to present if the typec port can support
  Try.SNK or Try.SRC.
- 4 sink properties(max_sink_mv/ma/mw and op_sink_mw) are kept because the
  counterpart code is back, see revert patch[3], meanwhile I post a patch
  to fix the reported problem of current source pdo select machinism(which
  completely ignored those 4 sink settings), to see if we can keep current
  code, once it was discussed and have conclusion I can update this
  accordingly.
- Use fwnode to get the connector node for dt setting parse.

Main changes for v2:
- Typec properties are based on general usb connector bindings[1] proposed
  by Andrzej Hajda, use the standard unit suffixes as defined in
  property-units.txt.
- Add 2 infra APIs to get power sink and source config from dt.
- Don't change the set_cc api, to keep the uncontacted cc line open,
  set cc1/cc2 to be open in tcpci driver when set polarity.
- Directly enable vbus detect in tcpci driver rather than add a API.
- Details added in each patch.

[1] https://patchwork.kernel.org/patch/10231447/
[2] https://patchwork.kernel.org/patch/10276483/
[3] https://www.spinics.net/lists/linux-usb/msg166366.html

Li Jun (13):
  dt-bindings: connector: add properties for typec
  dt-bindings: usb: add documentation for typec port controller(TCPCI)
  staging: typec: tcpci: add compatible string for nxp ptn5110
  usb: typec: add fwnode to tcpc
  usb: typec: add API to get typec basic port power and data config
  usb: typec: tcpm: support get typec and pd config from device
properties
  staging: typec: tcpci: remove unused tcpci_tcpc_config
  staging: typec: tcpci: use IS_ERR() instead of PTR_ERR_OR_ZERO()
  staging: typec: tcpci: enable vbus detection
  typec: tcpm: add starting value for drp toggling
  staging: typec: tcpci: keep the disconnected cc line open
  staging: typec: tcpci: Only touch target bit when enable vconn
  staging: typec: tcpci: move tcpci drivers out of staging

Peter Chen (1):
  staging: typec: tcpci: register port b

[PATCH v8 01/14] dt-bindings: connector: add properties for typec

2018-06-26 Thread Li Jun
Add bindings supported by current typec driver, so user can pass
all those properties via dt.

Reviewed-by: Rob Herring 
Signed-off-by: Li Jun 
---
 .../bindings/connector/usb-connector.txt   | 44 +++
 include/dt-bindings/usb/pd.h   | 62 ++
 2 files changed, 106 insertions(+)

diff --git a/Documentation/devicetree/bindings/connector/usb-connector.txt 
b/Documentation/devicetree/bindings/connector/usb-connector.txt
index e1463f1..8855bfc 100644
--- a/Documentation/devicetree/bindings/connector/usb-connector.txt
+++ b/Documentation/devicetree/bindings/connector/usb-connector.txt
@@ -15,6 +15,33 @@ Optional properties:
 - type: size of the connector, should be specified in case of USB-A, USB-B
   non-fullsize connectors: "mini", "micro".
 
+Optional properties for usb-c-connector:
+- power-role: should be one of "source", "sink" or "dual"(DRP) if typec
+  connector has power support.
+- try-power-role: preferred power role if "dual"(DRP) can support Try.SNK
+  or Try.SRC, should be "sink" for Try.SNK or "source" for Try.SRC.
+- data-role: should be one of "host", "device", "dual"(DRD) if typec
+  connector supports USB data.
+
+Required properties for usb-c-connector with power delivery support:
+- source-pdos: An array of u32 with each entry providing supported power
+  source data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.2
+  Source_Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power source and power dual role.
+  User can specify the source PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- sink-pdos: An array of u32 with each entry providing supported power
+  sink data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.3
+  Sink Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power sink and power dual role.
+  User can specify the sink PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- op-sink-microwatt: Sink required operating power in microwatt, if source
+  can't offer the power, Capability Mismatch is set. Required for power
+  sink and power dual role.
+
 Required nodes:
 - any data bus to the connector should be modeled using the OF graph bindings
   specified in bindings/graph.txt, unless the bus is between parent node and
@@ -73,3 +100,20 @@ ccic: s2mm005@33 {
};
};
 };
+
+3. USB-C connector attached to a typec port controller(ptn5110), which has
+power delivery support and enables drp.
+
+typec: ptn5110@50 {
+   ...
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = ;
+   sink-pdos = ;
+   op-sink-microwatt = <1000>;
+   };
+};
diff --git a/include/dt-bindings/usb/pd.h b/include/dt-bindings/usb/pd.h
new file mode 100644
index 000..7b7a92f
--- /dev/null
+++ b/include/dt-bindings/usb/pd.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __DT_POWER_DELIVERY_H
+#define __DT_POWER_DELIVERY_H
+
+/* Power delivery Power Data Object definitions */
+#define PDO_TYPE_FIXED 0
+#define PDO_TYPE_BATT  1
+#define PDO_TYPE_VAR   2
+#define PDO_TYPE_APDO  3
+
+#define PDO_TYPE_SHIFT 30
+#define PDO_TYPE_MASK  0x3
+
+#define PDO_TYPE(t)((t) << PDO_TYPE_SHIFT)
+
+#define PDO_VOLT_MASK  0x3ff
+#define PDO_CURR_MASK  0x3ff
+#define PDO_PWR_MASK   0x3ff
+
+#define PDO_FIXED_DUAL_ROLE(1 << 29) /* Power role swap supported */
+#define PDO_FIXED_SUSPEND  (1 << 28) /* USB Suspend supported (Source) */
+#define PDO_FIXED_HIGHER_CAP   (1 << 28) /* Requires more than vSafe5V (Sink) 
*/
+#define PDO_FIXED_EXTPOWER (1 << 27) /* Externally powered */
+#define PDO_FIXED_USB_COMM (1 << 26) /* USB communications capable */
+#define PDO_FIXED_DATA_SWAP(1 << 25) /* Data role swap supported */
+#define PDO_FIXED_VOLT_SHIFT   10  /* 50mV units */
+#define PDO_FIXED_CURR_SHIFT   0   /* 10mA units */
+
+#define PDO_FIXED_VOLT(mv) mv) / 50) & PDO_VOLT_MASK) << 
PDO_FIXED_VOLT_SHIFT)
+#define PDO_FIXED_CURR(ma) ma) / 10) & PDO_CURR_MASK) << 
PDO_FIXED_CURR_SHIFT)
+
+#define PDO_FIXED(mv, ma, flags)   \
+   (PDO_TYPE(PDO_TYPE_FIXED) | (flags) |   \
+PDO_FIXED_VOLT(mv) | PDO_FIXED_CURR(ma))
+

[PATCH v8 02/14] dt-bindings: usb: add documentation for typec port controller(TCPCI)

2018-06-26 Thread Li Jun
TCPCI stands for typec port controller interface, its implementation
has full typec port control with power delivery support, it's a
standard i2c slave with GPIO input as irq interface, detail see spec
"Universal Serial Bus Type-C Port Controller Interface Specification
Revision 1.0, Version 1.1"

Reviewed-by: Rob Herring 
Signed-off-by: Li Jun 
---
 .../devicetree/bindings/usb/typec-tcpci.txt| 49 ++
 1 file changed, 49 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/typec-tcpci.txt 
b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
new file mode 100644
index 000..0dd1469
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
@@ -0,0 +1,49 @@
+TCPCI(Typec port cotroller interface) binding
+-
+
+Required properties:
+- compatible:   should be set one of following:
+   - "nxp,ptn5110" for NXP USB PD TCPC PHY IC ptn5110.
+
+- reg:  the i2c slave address of typec port controller device.
+- interrupt-parent: the phandle to the interrupt controller which provides
+the interrupt.
+- interrupts:   interrupt specification for tcpci alert.
+
+Required sub-node:
+- connector: The "usb-c-connector" attached to the tcpci chip, the bindings
+  of connector node are specified in
+  Documentation/devicetree/bindings/connector/usb-connector.txt
+
+Example:
+
+ptn5110@50 {
+   compatible = "nxp,ptn5110";
+   reg = <0x50>;
+   interrupt-parent = <>;
+   interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   data-role = "dual";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = ;
+   sink-pdos = ;
+   op-sink-microwatt = <1000>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@1 {
+   reg = <1>;
+   usb_con_ss: endpoint {
+   remote-endpoint = <_data_ss>;
+   };
+   };
+   };
+   };
+};
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 13/14] staging: typec: tcpci: Only touch target bit when enable vconn

2018-06-25 Thread Li Jun
We need regmap_update_bits to avoid touch any other bits when
enable or disable vconn.

Reviewed-by: Guenter Roeck 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 11c2d37..ac6b418 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -218,12 +218,9 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool 
enable)
return ret;
}
 
-   ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
-  enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
-   if (ret < 0)
-   return ret;
-
-   return 0;
+   return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
+   TCPC_POWER_CTRL_VCONN_ENABLE,
+   enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 14/14] staging: typec: tcpci: move tcpci drivers out of staging

2018-06-25 Thread Li Jun
Move TCPCI(Typec port controller interface) driver and rt1711h
driver out of staging.

Signed-off-by: Li Jun 
---
 drivers/staging/Kconfig|  2 --
 drivers/staging/Makefile   |  1 -
 drivers/staging/typec/Kconfig  | 22 --
 drivers/staging/typec/Makefile |  2 --
 drivers/staging/typec/TODO |  5 -
 drivers/usb/typec/Kconfig  | 15 +++
 drivers/usb/typec/Makefile |  2 ++
 drivers/{staging => usb}/typec/tcpci.c |  0
 drivers/{staging => usb}/typec/tcpci.h |  0
 drivers/{staging => usb}/typec/tcpci_rt1711h.c |  0
 10 files changed, 17 insertions(+), 32 deletions(-)

diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index 75a4804..af9a82f 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -106,8 +106,6 @@ source "drivers/staging/greybus/Kconfig"
 
 source "drivers/staging/vc04_services/Kconfig"
 
-source "drivers/staging/typec/Kconfig"
-
 source "drivers/staging/vboxvideo/Kconfig"
 
 source "drivers/staging/pi433/Kconfig"
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index e84959a..8479d47 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -2,7 +2,6 @@
 # Makefile for staging directory
 
 obj-y  += media/
-obj-y  += typec/
 obj-$(CONFIG_PRISM2_USB)   += wlan-ng/
 obj-$(CONFIG_COMEDI)   += comedi/
 obj-$(CONFIG_FB_OLPC_DCON) += olpc_dcon/
diff --git a/drivers/staging/typec/Kconfig b/drivers/staging/typec/Kconfig
deleted file mode 100644
index 3aa981f..000
--- a/drivers/staging/typec/Kconfig
+++ /dev/null
@@ -1,22 +0,0 @@
-menu "USB Power Delivery and Type-C drivers"
-
-if TYPEC_TCPM
-
-config TYPEC_TCPCI
-   tristate "Type-C Port Controller Interface driver"
-   depends on I2C
-   select REGMAP_I2C
-   help
- Type-C Port Controller driver for TCPCI-compliant controller.
-
-config TYPEC_RT1711H
-   tristate "Richtek RT1711H Type-C chip driver"
-   select TYPEC_TCPCI
-   help
- Richtek RT1711H Type-C chip driver that works with
- Type-C Port Controller Manager to provide USB PD and USB
- Type-C functionalities.
-
-endif
-
-endmenu
diff --git a/drivers/staging/typec/Makefile b/drivers/staging/typec/Makefile
deleted file mode 100644
index 7803d48..000
--- a/drivers/staging/typec/Makefile
+++ /dev/null
@@ -1,2 +0,0 @@
-obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
-obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
diff --git a/drivers/staging/typec/TODO b/drivers/staging/typec/TODO
deleted file mode 100644
index 53fe2f7..000
--- a/drivers/staging/typec/TODO
+++ /dev/null
@@ -1,5 +0,0 @@
-tcpci:
-- Test with real hardware
-
-Please send patches to Guenter Roeck  and copy
-Heikki Krogerus .
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 2c8eab1..972fd19 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -56,6 +56,21 @@ config TYPEC_TCPM
 
 if TYPEC_TCPM
 
+config TYPEC_TCPCI
+   tristate "Type-C Port Controller Interface driver"
+   depends on I2C
+   select REGMAP_I2C
+   help
+ Type-C Port Controller driver for TCPCI-compliant controller.
+
+config TYPEC_RT1711H
+   tristate "Richtek RT1711H Type-C chip driver"
+   select TYPEC_TCPCI
+   help
+ Richtek RT1711H Type-C chip driver that works with
+ Type-C Port Controller Manager to provide USB PD and USB
+ Type-C functionalities.
+
 source "drivers/usb/typec/fusb302/Kconfig"
 
 config TYPEC_WCOVE
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index 1f599a6..46f86ee 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -7,3 +7,5 @@ obj-$(CONFIG_TYPEC_WCOVE)   += typec_wcove.o
 obj-$(CONFIG_TYPEC_UCSI)   += ucsi/
 obj-$(CONFIG_TYPEC_TPS6598X)   += tps6598x.o
 obj-$(CONFIG_TYPEC)+= mux/
+obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
+obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
diff --git a/drivers/staging/typec/tcpci.c b/drivers/usb/typec/tcpci.c
similarity index 100%
rename from drivers/staging/typec/tcpci.c
rename to drivers/usb/typec/tcpci.c
diff --git a/drivers/staging/typec/tcpci.h b/drivers/usb/typec/tcpci.h
similarity index 100%
rename from drivers/staging/typec/tcpci.h
rename to drivers/usb/typec/tcpci.h
diff --git a/drivers/staging/typec/tcpci_rt1711h.c 
b/drivers/usb/typec/tcpci_rt1711h.c
similarity index 100%
rename from drivers/staging/typec/tcpci_rt1711h.c
rename to drivers/usb/typec/tcpci_rt1711h.c
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 08/14] staging: typec: tcpci: use IS_ERR() instead of PTR_ERR_OR_ZERO()

2018-06-25 Thread Li Jun
As tcpm_register_port() and tcpci_register_port() never return
NULL and NULL is not a success in this case, use IS_ERR() to check
the return value of both.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076498a..b63f147 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -509,7 +509,7 @@ struct tcpci *tcpci_register_port(struct device *dev, 
struct tcpci_data *data)
return ERR_PTR(err);
 
tcpci->port = tcpm_register_port(tcpci->dev, >tcpc);
-   if (PTR_ERR_OR_ZERO(tcpci->port))
+   if (IS_ERR(tcpci->port))
return ERR_CAST(tcpci->port);
 
return tcpci;
@@ -551,7 +551,7 @@ static int tcpci_probe(struct i2c_client *client,
return err;
 
chip->tcpci = tcpci_register_port(>dev, >data);
-   if (PTR_ERR_OR_ZERO(chip->tcpci))
+   if (IS_ERR(chip->tcpci))
return PTR_ERR(chip->tcpci);
 
i2c_set_clientdata(client, chip);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 10/14] staging: typec: tcpci: enable vbus detection

2018-06-25 Thread Li Jun
TCPCI implementation may need SW to enable VBUS detection to generate
power status events.

Reviewed-by: Guenter Roeck 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 3b35fce..4d3b0ae 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -373,6 +373,12 @@ static int tcpci_init(struct tcpc_dev *tcpc)
if (ret < 0)
return ret;
 
+   /* Enable Vbus detection */
+   ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+  TCPC_CMD_ENABLE_VBUS_DETECT);
+   if (ret < 0)
+   return ret;
+
reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 09/14] staging: typec: tcpci: register port before request irq

2018-06-25 Thread Li Jun
From: Peter Chen 

With that we can clear any pending events and the port is registered
so driver can be ready to handle typec events once we request irq.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Peter Chen 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 15 +--
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index b63f147..3b35fce 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -537,24 +537,27 @@ static int tcpci_probe(struct i2c_client *client,
if (IS_ERR(chip->data.regmap))
return PTR_ERR(chip->data.regmap);
 
+   i2c_set_clientdata(client, chip);
+
/* Disable chip interrupts before requesting irq */
err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, ,
   sizeof(u16));
if (err < 0)
return err;
 
+   chip->tcpci = tcpci_register_port(>dev, >data);
+   if (IS_ERR(chip->tcpci))
+   return PTR_ERR(chip->tcpci);
+
err = devm_request_threaded_irq(>dev, client->irq, NULL,
_tcpci_irq,
IRQF_ONESHOT | IRQF_TRIGGER_LOW,
dev_name(>dev), chip);
-   if (err < 0)
+   if (err < 0) {
+   tcpci_unregister_port(chip->tcpci);
return err;
+   }
 
-   chip->tcpci = tcpci_register_port(>dev, >data);
-   if (IS_ERR(chip->tcpci))
-   return PTR_ERR(chip->tcpci);
-
-   i2c_set_clientdata(client, chip);
return 0;
 }
 
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 06/14] usb: typec: tcpm: support get typec and pd config from device properties

2018-06-25 Thread Li Jun
This patch adds support of get typec and power delivery config from
firmware description.

Reviewed-by: Heikki Krogerus 
Reviewed-by: Guenter Roeck 
Signed-off-by: Li Jun 
---
 drivers/usb/typec/tcpm.c | 132 +++
 1 file changed, 110 insertions(+), 22 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index d22b37b..0454654 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4236,6 +4236,81 @@ static int tcpm_copy_vdos(u32 *dest_vdo, const u32 
*src_vdo,
return nr_vdo;
 }
 
+static int tcpm_fw_get_caps(struct tcpm_port *port,
+   struct fwnode_handle *fwnode)
+{
+   const char *cap_str;
+   int ret;
+   u32 mw;
+
+   if (!fwnode)
+   return -EINVAL;
+
+   /* USB data support is optional */
+   ret = fwnode_property_read_string(fwnode, "data-role", _str);
+   if (ret == 0) {
+   port->typec_caps.data = typec_find_port_data_role(cap_str);
+   if (port->typec_caps.data < 0)
+   return -EINVAL;
+   }
+
+   ret = fwnode_property_read_string(fwnode, "power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.type = typec_find_port_power_role(cap_str);
+   if (port->typec_caps.type < 0)
+   return -EINVAL;
+   port->port_type = port->typec_caps.type;
+
+   if (port->port_type == TYPEC_PORT_SNK)
+   goto sink;
+
+   /* Get source pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+port->src_pdo, port->nr_src_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
+   port->nr_src_pdo))
+   return -EINVAL;
+
+   if (port->port_type == TYPEC_PORT_SRC)
+   return 0;
+
+   /* Get the preferred power role for DRP */
+   ret = fwnode_property_read_string(fwnode, "try-power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.prefer_role = typec_find_power_role(cap_str);
+   if (port->typec_caps.prefer_role < 0)
+   return -EINVAL;
+sink:
+   /* Get sink pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+port->snk_pdo, port->nr_snk_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
+   port->nr_snk_pdo))
+   return -EINVAL;
+
+   if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", ) < 0)
+   return -EINVAL;
+   port->operating_snk_mw = mw / 1000;
+
+   return 0;
+}
+
 int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
unsigned int nr_pdo)
 {
@@ -4521,12 +4596,36 @@ static int devm_tcpm_psy_register(struct tcpm_port 
*port)
return PTR_ERR_OR_ZERO(port->psy);
 }
 
+static int tcpm_copy_caps(struct tcpm_port *port,
+ const struct tcpc_config *tcfg)
+{
+   if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
+   tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
+   return -EINVAL;
+
+   port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
+ tcfg->nr_src_pdo);
+   port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
+ tcfg->nr_snk_pdo);
+
+   port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
+ tcfg->nr_snk_vdo);
+
+   port->operating_snk_mw = tcfg->operating_snk_mw;
+
+   port->typec_caps.prefer_role = tcfg->default_role;
+   port->typec_caps.type = tcfg->type;
+   port->typec_caps.data = tcfg->data;
+
+   return 0;
+}
+
 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 {
struct tcpm_port *port;
int i, err;
 
-   if (!dev || !tcpc || !tcpc->config ||
+   if (!dev || !tcpc ||
!tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
!tcpc->set_po

[PATCH v7 12/14] staging: typec: tcpci: keep the disconnected cc line open

2018-06-25 Thread Li Jun
While set polarity, we should keep the disconnected cc line to be
open.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 4d3b0ae..11c2d37 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -185,15 +185,25 @@ static int tcpci_set_polarity(struct tcpc_dev *tcpc,
  enum typec_cc_polarity polarity)
 {
struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+   unsigned int reg;
int ret;
 
-   ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
-  (polarity == TYPEC_POLARITY_CC2) ?
-  TCPC_TCPC_CTRL_ORIENTATION : 0);
+   /* Keep the disconnect cc line open */
+   ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, );
if (ret < 0)
return ret;
 
-   return 0;
+   if (polarity == TYPEC_POLARITY_CC2)
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT;
+   else
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT;
+   ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+   if (ret < 0)
+   return ret;
+
+   return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+  (polarity == TYPEC_POLARITY_CC2) ?
+  TCPC_TCPC_CTRL_ORIENTATION : 0);
 }
 
 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 05/14] usb: typec: add API to get typec basic port power and data config

2018-06-25 Thread Li Jun
This patch adds 3 APIs to get the typec port power and data type,
and preferred power role by its name string.

Signed-off-by: Li Jun 
---
 drivers/usb/typec/class.c | 58 +++
 include/linux/usb/typec.h |  3 +++
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 53df10d..6331059 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -796,12 +796,18 @@ static const char * const typec_data_roles[] = {
[TYPEC_HOST]= "host",
 };
 
-static const char * const typec_port_types[] = {
+static const char * const typec_port_power_roles[] = {
[TYPEC_PORT_SRC] = "source",
[TYPEC_PORT_SNK] = "sink",
[TYPEC_PORT_DRP] = "dual",
 };
 
+static const char * const typec_port_data_roles[] = {
+   [TYPEC_PORT_DFP] = "host",
+   [TYPEC_PORT_UFP] = "device",
+   [TYPEC_PORT_DRD] = "dual",
+};
+
 static const char * const typec_port_types_drp[] = {
[TYPEC_PORT_SRC] = "dual [source] sink",
[TYPEC_PORT_SNK] = "dual source [sink]",
@@ -932,7 +938,7 @@ static ssize_t power_role_store(struct device *dev,
mutex_lock(>port_type_lock);
if (port->port_type != TYPEC_PORT_DRP) {
dev_dbg(dev, "port type fixed at \"%s\"",
-typec_port_types[port->port_type]);
+typec_port_power_roles[port->port_type]);
ret = -EOPNOTSUPP;
goto unlock_and_ret;
}
@@ -973,7 +979,7 @@ port_type_store(struct device *dev, struct device_attribute 
*attr,
return -EOPNOTSUPP;
}
 
-   ret = sysfs_match_string(typec_port_types, buf);
+   ret = sysfs_match_string(typec_port_power_roles, buf);
if (ret < 0)
return ret;
 
@@ -1007,7 +1013,7 @@ port_type_show(struct device *dev, struct 
device_attribute *attr,
return sprintf(buf, "%s\n",
   typec_port_types_drp[port->port_type]);
 
-   return sprintf(buf, "[%s]\n", typec_port_types[port->cap->type]);
+   return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
 }
 static DEVICE_ATTR_RW(port_type);
 
@@ -1252,6 +1258,50 @@ void typec_set_pwr_opmode(struct typec_port *port,
 }
 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
 
+/**
+ * typec_find_port_power_role - Get the typec port power capability
+ * @name: port power capability string
+ *
+ * This routine is used to find the typec_port_type by its string name.
+ *
+ * Returns typec_port_type if success, otherwise negative error code.
+ */
+int typec_find_port_power_role(const char *name)
+{
+   return match_string(typec_port_power_roles,
+   ARRAY_SIZE(typec_port_power_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_port_power_role);
+
+/**
+ * typec_find_power_role - Find the typec one specific power role
+ * @name: power role string
+ *
+ * This routine is used to find the typec_role by its string name.
+ *
+ * Returns typec_role if success, otherwise negative error code.
+ */
+int typec_find_power_role(const char *name)
+{
+   return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_power_role);
+
+/**
+ * typec_find_port_data_role - Get the typec port data capability
+ * @name: port data capability string
+ *
+ * This routine is used to find the typec_port_data by its string name.
+ *
+ * Returns typec_port_data if success, otherwise negative error code.
+ */
+int typec_find_port_data_role(const char *name)
+{
+   return match_string(typec_port_data_roles,
+   ARRAY_SIZE(typec_port_data_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_port_data_role);
+
 /* -- */
 /* API for Multiplexer/DeMultiplexer Switches */
 
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 672b39b..15f8d9a 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -267,4 +267,7 @@ int typec_set_orientation(struct typec_port *port,
  enum typec_orientation orientation);
 int typec_set_mode(struct typec_port *port, int mode);
 
+int typec_find_port_power_role(const char *name);
+int typec_find_power_role(const char *name);
+int typec_find_port_data_role(const char *name);
 #endif /* __LINUX_USB_TYPEC_H */
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 11/14] typec: tcpm: add starting value for drp toggling

2018-06-25 Thread Li Jun
As DRP port autonomously toggles the Rp/Rd need a start value to
begin with, so add one parameter for it in tcpm_start_drp_toggling.

Reviewed-by: Guenter Roeck 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/usb/typec/tcpm.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 0454654..c80f1a4 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2431,15 +2431,15 @@ static int tcpm_set_charge(struct tcpm_port *port, bool 
charge)
return 0;
 }
 
-static bool tcpm_start_drp_toggling(struct tcpm_port *port)
+static bool tcpm_start_drp_toggling(struct tcpm_port *port,
+   enum typec_cc_status cc)
 {
int ret;
 
if (port->tcpc->start_drp_toggling &&
port->port_type == TYPEC_PORT_DRP) {
tcpm_log_force(port, "Start DRP toggling");
-   ret = port->tcpc->start_drp_toggling(port->tcpc,
-tcpm_rp_cc(port));
+   ret = port->tcpc->start_drp_toggling(port->tcpc, cc);
if (!ret)
return true;
}
@@ -2747,7 +2747,7 @@ static void run_state_machine(struct tcpm_port *port)
if (!port->non_pd_role_swap)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_src_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, tcpm_rp_cc(port))) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
@@ -2922,7 +2922,7 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_pps_complete(port, -ENOTCONN);
tcpm_snk_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, TYPEC_CC_RD)) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 07/14] staging: typec: tcpci: remove unused tcpci_tcpc_config

2018-06-25 Thread Li Jun
Since we will use config settings via device properties, so
remove the hard code tcpci_tcpc_config.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 7 ---
 1 file changed, 7 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index e59547a..076498a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -464,17 +464,10 @@ static const struct regmap_config tcpci_regmap_config = {
.max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
 };
 
-static const struct tcpc_config tcpci_tcpc_config = {
-   .type = TYPEC_PORT_DFP,
-   .default_role = TYPEC_SINK,
-};
-
 static int tcpci_parse_config(struct tcpci *tcpci)
 {
tcpci->controls_vbus = true; /* XXX */
 
-   /* TODO: Populate struct tcpc_config from ACPI/device-tree */
-   tcpci->tcpc.config = _tcpc_config;
tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
 "connector");
if (!tcpci->tcpc.fwnode) {
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 04/14] usb: typec: add fwnode to tcpc

2018-06-25 Thread Li Jun
Add fwnode handle to get the fwnode so we can get typec configs
it contains.

Suggested-by: Heikki Krogerus 
Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 7 +++
 drivers/usb/typec/tcpm.c  | 1 +
 include/linux/usb/tcpm.h  | 2 ++
 3 files changed, 10 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index dd29288..e59547a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -474,6 +475,12 @@ static int tcpci_parse_config(struct tcpci *tcpci)
 
/* TODO: Populate struct tcpc_config from ACPI/device-tree */
tcpci->tcpc.config = _tcpc_config;
+   tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
+"connector");
+   if (!tcpci->tcpc.fwnode) {
+   dev_err(tcpci->dev, "Can't find connector node.\n");
+   return -EINVAL;
+   }
 
return 0;
 }
diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 8a201dd..d22b37b 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4576,6 +4576,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
else
port->try_role = TYPEC_NO_PREFERRED_ROLE;
 
+   port->typec_caps.fwnode = tcpc->fwnode;
port->typec_caps.prefer_role = tcpc->config->default_role;
port->typec_caps.type = tcpc->config->type;
port->typec_caps.data = tcpc->config->data;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index b231b93..193920a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -110,6 +110,7 @@ enum tcpc_mux_mode {
 /**
  * struct tcpc_dev - Port configuration and callback functions
  * @config:Pointer to port configuration
+ * @fwnode:Pointer to port fwnode
  * @get_vbus:  Called to read current VBUS state
  * @get_current_limit:
  * Optional; called by the tcpm core when configured as a snk
@@ -138,6 +139,7 @@ enum tcpc_mux_mode {
  */
 struct tcpc_dev {
const struct tcpc_config *config;
+   struct fwnode_handle *fwnode;
 
int (*init)(struct tcpc_dev *dev);
int (*get_vbus)(struct tcpc_dev *dev);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 03/14] staging: typec: tcpci: add compatible string for nxp ptn5110

2018-06-25 Thread Li Jun
Add nxp ptn5110 typec controller compatible string: "nxp,ptn5110",
which is a standard tcpci chip with power delivery support. Meanwhile
remove "usb,tcpci" because it doesn't follow the binding format rule
and has not been used yet.

Reviewed-by: Heikki Krogerus 
Signed-off-by: Li Jun 
---
 drivers/staging/typec/tcpci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076d97e..dd29288 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -575,7 +575,7 @@ MODULE_DEVICE_TABLE(i2c, tcpci_id);
 
 #ifdef CONFIG_OF
 static const struct of_device_id tcpci_of_match[] = {
-   { .compatible = "usb,tcpci", },
+   { .compatible = "nxp,ptn5110", },
{},
 };
 MODULE_DEVICE_TABLE(of, tcpci_of_match);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 02/14] dt-bindings: usb: add documentation for typec port controller(TCPCI)

2018-06-25 Thread Li Jun
TCPCI stands for typec port controller interface, its implementation
has full typec port control with power delivery support, it's a
standard i2c slave with GPIO input as irq interface, detail see spec
"Universal Serial Bus Type-C Port Controller Interface Specification
Revision 1.0, Version 1.1"

Reviewed-by: Rob Herring 
Signed-off-by: Li Jun 
---
 .../devicetree/bindings/usb/typec-tcpci.txt| 49 ++
 1 file changed, 49 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/typec-tcpci.txt 
b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
new file mode 100644
index 000..0dd1469
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
@@ -0,0 +1,49 @@
+TCPCI(Typec port cotroller interface) binding
+-
+
+Required properties:
+- compatible:   should be set one of following:
+   - "nxp,ptn5110" for NXP USB PD TCPC PHY IC ptn5110.
+
+- reg:  the i2c slave address of typec port controller device.
+- interrupt-parent: the phandle to the interrupt controller which provides
+the interrupt.
+- interrupts:   interrupt specification for tcpci alert.
+
+Required sub-node:
+- connector: The "usb-c-connector" attached to the tcpci chip, the bindings
+  of connector node are specified in
+  Documentation/devicetree/bindings/connector/usb-connector.txt
+
+Example:
+
+ptn5110@50 {
+   compatible = "nxp,ptn5110";
+   reg = <0x50>;
+   interrupt-parent = <>;
+   interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   data-role = "dual";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = ;
+   sink-pdos = ;
+   op-sink-microwatt = <1000>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@1 {
+   reg = <1>;
+   usb_con_ss: endpoint {
+   remote-endpoint = <_data_ss>;
+   };
+   };
+   };
+   };
+};
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 01/14] dt-bindings: connector: add properties for typec

2018-06-25 Thread Li Jun
Add bindings supported by current typec driver, so user can pass
all those properties via dt.

Reviewed-by: Rob Herring 
Signed-off-by: Li Jun 
---
 .../bindings/connector/usb-connector.txt   | 44 +++
 include/dt-bindings/usb/pd.h   | 62 ++
 2 files changed, 106 insertions(+)

diff --git a/Documentation/devicetree/bindings/connector/usb-connector.txt 
b/Documentation/devicetree/bindings/connector/usb-connector.txt
index e1463f1..8855bfc 100644
--- a/Documentation/devicetree/bindings/connector/usb-connector.txt
+++ b/Documentation/devicetree/bindings/connector/usb-connector.txt
@@ -15,6 +15,33 @@ Optional properties:
 - type: size of the connector, should be specified in case of USB-A, USB-B
   non-fullsize connectors: "mini", "micro".
 
+Optional properties for usb-c-connector:
+- power-role: should be one of "source", "sink" or "dual"(DRP) if typec
+  connector has power support.
+- try-power-role: preferred power role if "dual"(DRP) can support Try.SNK
+  or Try.SRC, should be "sink" for Try.SNK or "source" for Try.SRC.
+- data-role: should be one of "host", "device", "dual"(DRD) if typec
+  connector supports USB data.
+
+Required properties for usb-c-connector with power delivery support:
+- source-pdos: An array of u32 with each entry providing supported power
+  source data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.2
+  Source_Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power source and power dual role.
+  User can specify the source PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- sink-pdos: An array of u32 with each entry providing supported power
+  sink data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.3
+  Sink Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power sink and power dual role.
+  User can specify the sink PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- op-sink-microwatt: Sink required operating power in microwatt, if source
+  can't offer the power, Capability Mismatch is set. Required for power
+  sink and power dual role.
+
 Required nodes:
 - any data bus to the connector should be modeled using the OF graph bindings
   specified in bindings/graph.txt, unless the bus is between parent node and
@@ -73,3 +100,20 @@ ccic: s2mm005@33 {
};
};
 };
+
+3. USB-C connector attached to a typec port controller(ptn5110), which has
+power delivery support and enables drp.
+
+typec: ptn5110@50 {
+   ...
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = ;
+   sink-pdos = ;
+   op-sink-microwatt = <1000>;
+   };
+};
diff --git a/include/dt-bindings/usb/pd.h b/include/dt-bindings/usb/pd.h
new file mode 100644
index 000..7b7a92f
--- /dev/null
+++ b/include/dt-bindings/usb/pd.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __DT_POWER_DELIVERY_H
+#define __DT_POWER_DELIVERY_H
+
+/* Power delivery Power Data Object definitions */
+#define PDO_TYPE_FIXED 0
+#define PDO_TYPE_BATT  1
+#define PDO_TYPE_VAR   2
+#define PDO_TYPE_APDO  3
+
+#define PDO_TYPE_SHIFT 30
+#define PDO_TYPE_MASK  0x3
+
+#define PDO_TYPE(t)((t) << PDO_TYPE_SHIFT)
+
+#define PDO_VOLT_MASK  0x3ff
+#define PDO_CURR_MASK  0x3ff
+#define PDO_PWR_MASK   0x3ff
+
+#define PDO_FIXED_DUAL_ROLE(1 << 29) /* Power role swap supported */
+#define PDO_FIXED_SUSPEND  (1 << 28) /* USB Suspend supported (Source) */
+#define PDO_FIXED_HIGHER_CAP   (1 << 28) /* Requires more than vSafe5V (Sink) 
*/
+#define PDO_FIXED_EXTPOWER (1 << 27) /* Externally powered */
+#define PDO_FIXED_USB_COMM (1 << 26) /* USB communications capable */
+#define PDO_FIXED_DATA_SWAP(1 << 25) /* Data role swap supported */
+#define PDO_FIXED_VOLT_SHIFT   10  /* 50mV units */
+#define PDO_FIXED_CURR_SHIFT   0   /* 10mA units */
+
+#define PDO_FIXED_VOLT(mv) mv) / 50) & PDO_VOLT_MASK) << 
PDO_FIXED_VOLT_SHIFT)
+#define PDO_FIXED_CURR(ma) ma) / 10) & PDO_CURR_MASK) << 
PDO_FIXED_CURR_SHIFT)
+
+#define PDO_FIXED(mv, ma, flags)   \
+   (PDO_TYPE(PDO_TYPE_FIXED) | (flags) |   \
+PDO_FIXED_VOLT(mv) | PDO_FIXED_CURR(ma))
+

[PATCH v7 00/14] staging: typec: tcpci: move out of staging

2018-06-25 Thread Li Jun
This patch set attempts to move the tcpci drivers out of staging by fix
some tcpci driver issues and define typec and power delivery device
properties, the changes are verified on NXP PTN5110, which is a standard
tcpci typec port controller device with power delivery support, tested
power source and sink with drp config.

Changes for v7:
- Fix export symbols mismatch in patch:
  usb: typec: add API to get typec basic port power and data config
  rename typec_port_types to be typec_port_power_roles.
- Drop patch:
  usb: typec: tcpm: set cc for drp toggling attach
  It's arguable as it's only to address specific HW problem, and which
  can be resolved by add try.src/snk property.

Changes for v6:
- Change function name to be typec_find_port_power/data_role for find
  capability, and typec_find_power_role for find one specific power role.
- Fix rt1711h driver move missing.
- Add one patch to improve the error checking in tcpci driver, use IS_ERR()
  instead of PTR_ERR_OR_ZERO().
- Add Rob's Reviewed-by for patch [2/15].
- Misc typos fix.
 
Changes for v5:
- Use "power-role" and "data-role" for typec port power and data capability
  property name.  
- Use macro definition instead of cryptic numbers for readability of
  power delivery properties(PDO).
- Add one tcpci driver binding string to be "nxp,ptn5110" to follow binding
  name rule ",".
- Change operating power property name to be op-sink-microwatt.
- Add one patch(remove unused tcpci_tcpc_config) to resolve bisectablitity
  issue of patch usb: typec: add fwnode to tcpc.
- A minor error handling change to keep the error path and success path
  separate in patch: register port before request irq
- Rebase to latest Greg's tree with max_snk_* removed.

Changes for v4:
- Remove max-sink-* properties as we will purge max_snk_* in tcpm,
  see patch set[4].
- Get typec power and data type value via name string(patch 5).
- Move finding typec and pd properties code from tcpci to tcpm(patch 6)
- Add a compatible string for nxp ptn5110 typec controller in tcpci driver.
  (patch 3)
- Add set cc for drp toggling without try.src/snk in tcpm(patch 10), then
  patch 11 can only update CCx bits for keep disconnect cc line open.
- Update op-sink-microwatt-hours example value to be the right value in
  micorwatts, and accordingly divide 1000 to get its miliwatts value
  in patch 6.
- Add Guenter's Reviewed-by for patch(8/9/12)

[4] https://www.spinics.net/lists/linux-usb/msg167261.html

Changes for v3:
- Use 2 properties to separate power and data capability of typec port:
  "power-type" and "data-type", this is based on Heikki's typec class code
  change[2]. use "try-power-role" to present if the typec port can support
  Try.SNK or Try.SRC.
- 4 sink properties(max_sink_mv/ma/mw and op_sink_mw) are kept because the
  counterpart code is back, see revert patch[3], meanwhile I post a patch
  to fix the reported problem of current source pdo select machinism(which
  completely ignored those 4 sink settings), to see if we can keep current
  code, once it was discussed and have conclusion I can update this
  accordingly.
- Use fwnode to get the connector node for dt setting parse.

Main changes for v2:
- Typec properties are based on general usb connector bindings[1] proposed
  by Andrzej Hajda, use the standard unit suffixes as defined in
  property-units.txt.
- Add 2 infra APIs to get power sink and source config from dt.
- Don't change the set_cc api, to keep the uncontacted cc line open,
  set cc1/cc2 to be open in tcpci driver when set polarity.
- Directly enable vbus detect in tcpci driver rather than add a API.
- Details added in each patch.

[1] https://patchwork.kernel.org/patch/10231447/
[2] https://patchwork.kernel.org/patch/10276483/
[3] https://www.spinics.net/lists/linux-usb/msg166366.html

Li Jun (13):
  dt-bindings: connector: add properties for typec
  dt-bindings: usb: add documentation for typec port controller(TCPCI)
  staging: typec: tcpci: add compatible string for nxp ptn5110
  usb: typec: add fwnode to tcpc
  usb: typec: add API to get typec basic port power and data config
  usb: typec: tcpm: support get typec and pd config from device
properties
  staging: typec: tcpci: remove unused tcpci_tcpc_config
  staging: typec: tcpci: use IS_ERR() instead of PTR_ERR_OR_ZERO()
  staging: typec: tcpci: enable vbus detection
  typec: tcpm: add starting value for drp toggling
  staging: typec: tcpci: keep the disconnected cc line open
  staging: typec: tcpci: Only touch target bit when enable vconn
  staging: typec: tcpci: move tcpci drivers out of staging

Peter Chen (1):
  staging: typec: tcpci: register port before request irq

 .../bindings/connector/usb-connector.txt   |  44 +++
 .../devicetree/bindings/usb/typec-tcpci.txt|  49 +++
 drivers/staging/Kconfig|   2 -
 drivers/staging/Makefile   |   1 -
 drivers/

[PATCH v6 10/15] staging: typec: tcpci: enable vbus detection

2018-05-27 Thread Li Jun
TCPCI implementation may need SW to enable VBUS detection to generate
power status events.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 3b35fce..4d3b0ae 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -373,6 +373,12 @@ static int tcpci_init(struct tcpc_dev *tcpc)
if (ret < 0)
return ret;
 
+   /* Enable Vbus detection */
+   ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+  TCPC_CMD_ENABLE_VBUS_DETECT);
+   if (ret < 0)
+   return ret;
+
reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 15/15] staging: typec: tcpci: move tcpci drivers out of staging

2018-05-27 Thread Li Jun
Move TCPCI(Typec port controller interface) driver and rt1711h
driver out of staging.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/Kconfig|  2 --
 drivers/staging/Makefile   |  1 -
 drivers/staging/typec/Kconfig  | 22 --
 drivers/staging/typec/Makefile |  2 --
 drivers/staging/typec/TODO |  5 -
 drivers/usb/typec/Kconfig  | 15 +++
 drivers/usb/typec/Makefile |  2 ++
 drivers/{staging => usb}/typec/tcpci.c |  0
 drivers/{staging => usb}/typec/tcpci.h |  0
 drivers/{staging => usb}/typec/tcpci_rt1711h.c |  0
 10 files changed, 17 insertions(+), 32 deletions(-)

diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index d5926f0..d83ff66 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -112,8 +112,6 @@ source "drivers/staging/greybus/Kconfig"
 
 source "drivers/staging/vc04_services/Kconfig"
 
-source "drivers/staging/typec/Kconfig"
-
 source "drivers/staging/vboxvideo/Kconfig"
 
 source "drivers/staging/pi433/Kconfig"
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index 919753c..a71ec1f 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -2,7 +2,6 @@
 # Makefile for staging directory
 
 obj-y  += media/
-obj-y  += typec/
 obj-$(CONFIG_IPX)  += ipx/
 obj-$(CONFIG_NCP_FS)   += ncpfs/
 obj-$(CONFIG_PRISM2_USB)   += wlan-ng/
diff --git a/drivers/staging/typec/Kconfig b/drivers/staging/typec/Kconfig
deleted file mode 100644
index 3aa981f..000
--- a/drivers/staging/typec/Kconfig
+++ /dev/null
@@ -1,22 +0,0 @@
-menu "USB Power Delivery and Type-C drivers"
-
-if TYPEC_TCPM
-
-config TYPEC_TCPCI
-   tristate "Type-C Port Controller Interface driver"
-   depends on I2C
-   select REGMAP_I2C
-   help
- Type-C Port Controller driver for TCPCI-compliant controller.
-
-config TYPEC_RT1711H
-   tristate "Richtek RT1711H Type-C chip driver"
-   select TYPEC_TCPCI
-   help
- Richtek RT1711H Type-C chip driver that works with
- Type-C Port Controller Manager to provide USB PD and USB
- Type-C functionalities.
-
-endif
-
-endmenu
diff --git a/drivers/staging/typec/Makefile b/drivers/staging/typec/Makefile
deleted file mode 100644
index 7803d48..000
--- a/drivers/staging/typec/Makefile
+++ /dev/null
@@ -1,2 +0,0 @@
-obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
-obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
diff --git a/drivers/staging/typec/TODO b/drivers/staging/typec/TODO
deleted file mode 100644
index 53fe2f7..000
--- a/drivers/staging/typec/TODO
+++ /dev/null
@@ -1,5 +0,0 @@
-tcpci:
-- Test with real hardware
-
-Please send patches to Guenter Roeck <li...@roeck-us.net> and copy
-Heikki Krogerus <heikki.kroge...@linux.intel.com>.
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 2c8eab1..972fd19 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -56,6 +56,21 @@ config TYPEC_TCPM
 
 if TYPEC_TCPM
 
+config TYPEC_TCPCI
+   tristate "Type-C Port Controller Interface driver"
+   depends on I2C
+   select REGMAP_I2C
+   help
+ Type-C Port Controller driver for TCPCI-compliant controller.
+
+config TYPEC_RT1711H
+   tristate "Richtek RT1711H Type-C chip driver"
+   select TYPEC_TCPCI
+   help
+ Richtek RT1711H Type-C chip driver that works with
+ Type-C Port Controller Manager to provide USB PD and USB
+ Type-C functionalities.
+
 source "drivers/usb/typec/fusb302/Kconfig"
 
 config TYPEC_WCOVE
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index 1f599a6..46f86ee 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -7,3 +7,5 @@ obj-$(CONFIG_TYPEC_WCOVE)   += typec_wcove.o
 obj-$(CONFIG_TYPEC_UCSI)   += ucsi/
 obj-$(CONFIG_TYPEC_TPS6598X)   += tps6598x.o
 obj-$(CONFIG_TYPEC)+= mux/
+obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
+obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
diff --git a/drivers/staging/typec/tcpci.c b/drivers/usb/typec/tcpci.c
similarity index 100%
rename from drivers/staging/typec/tcpci.c
rename to drivers/usb/typec/tcpci.c
diff --git a/drivers/staging/typec/tcpci.h b/drivers/usb/typec/tcpci.h
similarity index 100%
rename from drivers/staging/typec/tcpci.h
rename to drivers/usb/typec/tcpci.h
diff --git a/drivers/staging/typec/tcpci_rt1711h.c 
b/drivers/usb/typec/tcpci_rt1711h.c
similarity index 100%
rename from drivers/staging/typec/tcpci_rt1711h.c
rename to drivers/usb/typec/tcpci_rt1711h.c
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 14/15] staging: typec: tcpci: Only touch target bit when enable vconn

2018-05-27 Thread Li Jun
We need regmap_update_bits to avoid touch any other bits when
enable or disable vconn.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 11c2d37..ac6b418 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -218,12 +218,9 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool 
enable)
return ret;
}
 
-   ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
-  enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
-   if (ret < 0)
-   return ret;
-
-   return 0;
+   return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
+   TCPC_POWER_CTRL_VCONN_ENABLE,
+   enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 12/15] usb: typec: tcpm: set cc for drp toggling attach

2018-05-27 Thread Li Jun
In case of drp toggling, we may need set correct cc value for role control
after attach as it may never been set.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index d885bff..98ea916 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2599,6 +2599,7 @@ static void tcpm_reset_port(struct tcpm_port *port)
tcpm_set_attached_state(port, false);
port->try_src_count = 0;
port->try_snk_count = 0;
+   port->cc_req = TYPEC_CC_OPEN;
port->supply_voltage = 0;
port->current_limit = 0;
port->usb_type = POWER_SUPPLY_USB_TYPE_C;
@@ -2831,6 +2832,8 @@ static void run_state_machine(struct tcpm_port *port)
break;
 
case SRC_ATTACHED:
+   if (port->cc_req == TYPEC_CC_OPEN)
+   tcpm_set_cc(port, tcpm_rp_cc(port));
ret = tcpm_src_attach(port);
tcpm_set_state(port, SRC_UNATTACHED,
   ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
@@ -3004,6 +3007,8 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
break;
case SNK_ATTACHED:
+   if (port->cc_req == TYPEC_CC_OPEN)
+   tcpm_set_cc(port, TYPEC_CC_RD);
ret = tcpm_snk_attach(port);
if (ret < 0)
tcpm_set_state(port, SNK_UNATTACHED, 0);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 06/15] usb: typec: tcpm: support get typec and pd config from device properties

2018-05-27 Thread Li Jun
This patch adds support of get typec and power delivery config from
firmware description.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 132 +++
 1 file changed, 110 insertions(+), 22 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index fcd22e8..aa17cd5 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4241,6 +4241,81 @@ static int tcpm_copy_vdos(u32 *dest_vdo, const u32 
*src_vdo,
return nr_vdo;
 }
 
+static int tcpm_fw_get_caps(struct tcpm_port *port,
+   struct fwnode_handle *fwnode)
+{
+   const char *cap_str;
+   int ret;
+   u32 mw;
+
+   if (!port || !fwnode)
+   return -EINVAL;
+
+   /* USB data support is optional */
+   ret = fwnode_property_read_string(fwnode, "data-role", _str);
+   if (ret == 0) {
+   port->typec_caps.data = typec_find_port_data_role(cap_str);
+   if (port->typec_caps.data < 0)
+   return -EINVAL;
+   }
+
+   ret = fwnode_property_read_string(fwnode, "power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.type = typec_find_port_power_role(cap_str);
+   if (port->typec_caps.type < 0)
+   return -EINVAL;
+   port->port_type = port->typec_caps.type;
+
+   if (port->port_type == TYPEC_PORT_SNK)
+   goto sink;
+
+   /* Get soruce pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+port->src_pdo, port->nr_src_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
+   port->nr_src_pdo))
+   return -EINVAL;
+
+   if (port->port_type == TYPEC_PORT_SRC)
+   return 0;
+
+   /* Get the preferred power role for DRP */
+   ret = fwnode_property_read_string(fwnode, "try-power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.prefer_role = typec_find_power_role(cap_str);
+   if (port->typec_caps.prefer_role < 0)
+   return -EINVAL;
+sink:
+   /* Get sink pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+port->snk_pdo, port->nr_snk_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
+   port->nr_snk_pdo))
+   return -EINVAL;
+
+   if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", ) < 0)
+   return -EINVAL;
+   port->operating_snk_mw = mw / 1000;
+
+   return 0;
+}
+
 int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
unsigned int nr_pdo)
 {
@@ -4526,12 +4601,36 @@ static int devm_tcpm_psy_register(struct tcpm_port 
*port)
return PTR_ERR_OR_ZERO(port->psy);
 }
 
+static int tcpm_copy_caps(struct tcpm_port *port,
+ const struct tcpc_config *tcfg)
+{
+   if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
+   tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
+   return -EINVAL;
+
+   port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
+ tcfg->nr_src_pdo);
+   port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
+ tcfg->nr_snk_pdo);
+
+   port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
+ tcfg->nr_snk_vdo);
+
+   port->operating_snk_mw = tcfg->operating_snk_mw;
+
+   port->typec_caps.prefer_role = tcfg->default_role;
+   port->typec_caps.type = tcfg->type;
+   port->typec_caps.data = tcfg->data;
+
+   return 0;
+}
+
 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 {
struct tcpm_port *port;
int i, err;
 
-   if (!dev || !tcpc || !tcpc->config ||
+   if (!dev || !tcpc ||
!tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
!tcpc->set_polarity || !tcpc->set_vco

[PATCH v6 13/15] staging: typec: tcpci: keep the disconnected cc line open

2018-05-27 Thread Li Jun
While set polarity, we should keep the disconnected cc line to be
open.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 4d3b0ae..11c2d37 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -185,15 +185,25 @@ static int tcpci_set_polarity(struct tcpc_dev *tcpc,
  enum typec_cc_polarity polarity)
 {
struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+   unsigned int reg;
int ret;
 
-   ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
-  (polarity == TYPEC_POLARITY_CC2) ?
-  TCPC_TCPC_CTRL_ORIENTATION : 0);
+   /* Keep the disconnect cc line open */
+   ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, );
if (ret < 0)
return ret;
 
-   return 0;
+   if (polarity == TYPEC_POLARITY_CC2)
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT;
+   else
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT;
+   ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+   if (ret < 0)
+   return ret;
+
+   return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+  (polarity == TYPEC_POLARITY_CC2) ?
+  TCPC_TCPC_CTRL_ORIENTATION : 0);
 }
 
 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 11/15] typec: tcpm: add starting value for drp toggling

2018-05-27 Thread Li Jun
As DRP port autonomously toggles the Rp/Rd need a start value to
begin with, so add one parameter for it in tcpm_start_drp_toggling.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index aa17cd5..d885bff 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2436,15 +2436,15 @@ static int tcpm_set_charge(struct tcpm_port *port, bool 
charge)
return 0;
 }
 
-static bool tcpm_start_drp_toggling(struct tcpm_port *port)
+static bool tcpm_start_drp_toggling(struct tcpm_port *port,
+   enum typec_cc_status cc)
 {
int ret;
 
if (port->tcpc->start_drp_toggling &&
port->port_type == TYPEC_PORT_DRP) {
tcpm_log_force(port, "Start DRP toggling");
-   ret = port->tcpc->start_drp_toggling(port->tcpc,
-tcpm_rp_cc(port));
+   ret = port->tcpc->start_drp_toggling(port->tcpc, cc);
if (!ret)
return true;
}
@@ -2752,7 +2752,7 @@ static void run_state_machine(struct tcpm_port *port)
if (!port->non_pd_role_swap)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_src_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, tcpm_rp_cc(port))) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
@@ -2927,7 +2927,7 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_pps_complete(port, -ENOTCONN);
tcpm_snk_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, TYPEC_CC_RD)) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 07/15] staging: typec: tcpci: remove unused tcpci_tcpc_config

2018-05-27 Thread Li Jun
Since we will use config settings via device properties, so
remove the hard code tcpci_tcpc_config.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 7 ---
 1 file changed, 7 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index e59547a..076498a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -464,17 +464,10 @@ static const struct regmap_config tcpci_regmap_config = {
.max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
 };
 
-static const struct tcpc_config tcpci_tcpc_config = {
-   .type = TYPEC_PORT_DFP,
-   .default_role = TYPEC_SINK,
-};
-
 static int tcpci_parse_config(struct tcpci *tcpci)
 {
tcpci->controls_vbus = true; /* XXX */
 
-   /* TODO: Populate struct tcpc_config from ACPI/device-tree */
-   tcpci->tcpc.config = _tcpc_config;
tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
 "connector");
if (!tcpci->tcpc.fwnode) {
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 09/15] staging: typec: tcpci: register port before request irq

2018-05-27 Thread Li Jun
From: Peter Chen <peter.c...@nxp.com>

With that we can clear any pending events and the port is registered
so driver can be ready to handle typec events once we request irq.

Signed-off-by: Peter Chen <peter.c...@nxp.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 15 +--
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index b63f147..3b35fce 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -537,24 +537,27 @@ static int tcpci_probe(struct i2c_client *client,
if (IS_ERR(chip->data.regmap))
return PTR_ERR(chip->data.regmap);
 
+   i2c_set_clientdata(client, chip);
+
/* Disable chip interrupts before requesting irq */
err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, ,
   sizeof(u16));
if (err < 0)
return err;
 
+   chip->tcpci = tcpci_register_port(>dev, >data);
+   if (IS_ERR(chip->tcpci))
+   return PTR_ERR(chip->tcpci);
+
err = devm_request_threaded_irq(>dev, client->irq, NULL,
_tcpci_irq,
IRQF_ONESHOT | IRQF_TRIGGER_LOW,
dev_name(>dev), chip);
-   if (err < 0)
+   if (err < 0) {
+   tcpci_unregister_port(chip->tcpci);
return err;
+   }
 
-   chip->tcpci = tcpci_register_port(>dev, >data);
-   if (IS_ERR(chip->tcpci))
-   return PTR_ERR(chip->tcpci);
-
-   i2c_set_clientdata(client, chip);
return 0;
 }
 
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 05/15] usb: typec: add API to get typec basic port power and data config

2018-05-27 Thread Li Jun
This patch adds 3 APIs to get the typec port power and data type,
and preferred power role by its name string.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/class.c | 50 +++
 include/linux/usb/typec.h |  3 +++
 2 files changed, 53 insertions(+)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 53df10d..4c7d18c 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -802,6 +802,12 @@ static const char * const typec_port_types[] = {
[TYPEC_PORT_DRP] = "dual",
 };
 
+static const char * const typec_data_caps[] = {
+   [TYPEC_PORT_DFP] = "host",
+   [TYPEC_PORT_UFP] = "device",
+   [TYPEC_PORT_DRD] = "dual",
+};
+
 static const char * const typec_port_types_drp[] = {
[TYPEC_PORT_SRC] = "dual [source] sink",
[TYPEC_PORT_SNK] = "dual source [sink]",
@@ -1252,6 +1258,50 @@ void typec_set_pwr_opmode(struct typec_port *port,
 }
 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
 
+/**
+ * typec_find_port_power_role - Get the typec port power capability
+ * @name: port power capability string
+ *
+ * This routine is used to find the typec_port_type by its string name.
+ *
+ * Returns typec_port_type if success, otherwise negative error code.
+ */
+int typec_find_port_power_role(const char *name)
+{
+   return match_string(typec_port_types, ARRAY_SIZE(typec_port_types),
+   name);
+}
+EXPORT_SYMBOL_GPL(typec_find_power_type);
+
+/**
+ * typec_find_power_role - Find the typec one specific power role
+ * @name: power role string
+ *
+ * This routine is used to find the typec_role by its string name.
+ *
+ * Returns typec_role if success, otherwise negative error code.
+ */
+int typec_find_power_role(const char *name)
+{
+   return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_preferred_role);
+
+/**
+ * typec_find_port_data_role - Get the typec port data capability
+ * @name: port data capability string
+ *
+ * This routine is used to find the typec_port_data by its string name.
+ *
+ * Returns typec_port_data if success, otherwise negative error code.
+ */
+int typec_find_port_data_role(const char *name)
+{
+   return match_string(typec_data_caps, ARRAY_SIZE(typec_data_caps),
+   name);
+}
+EXPORT_SYMBOL_GPL(typec_find_data_type);
+
 /* -- */
 /* API for Multiplexer/DeMultiplexer Switches */
 
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 672b39b..15f8d9a 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -267,4 +267,7 @@ int typec_set_orientation(struct typec_port *port,
  enum typec_orientation orientation);
 int typec_set_mode(struct typec_port *port, int mode);
 
+int typec_find_port_power_role(const char *name);
+int typec_find_power_role(const char *name);
+int typec_find_port_data_role(const char *name);
 #endif /* __LINUX_USB_TYPEC_H */
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 03/15] staging: typec: tcpci: add compatible string for nxp ptn5110

2018-05-27 Thread Li Jun
Add nxp ptn5110 typec controller compatible string: "nxp,ptn5110",
which is a standard tcpci chip with power delivery support. Meanwhile
remove "usb,tcpci" because it doesn't follow the binding format rule
and has not been used yet.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076d97e..dd29288 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -575,7 +575,7 @@ MODULE_DEVICE_TABLE(i2c, tcpci_id);
 
 #ifdef CONFIG_OF
 static const struct of_device_id tcpci_of_match[] = {
-   { .compatible = "usb,tcpci", },
+   { .compatible = "nxp,ptn5110", },
{},
 };
 MODULE_DEVICE_TABLE(of, tcpci_of_match);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 00/15] staging: typec: tcpci: move out of staging

2018-05-27 Thread Li Jun
This patch set attempts to move the tcpci drivers out of staging by fix
some tcpci driver issues and define typec and power delivery device
properties, the changes are verified on NXP PTN5110, which is a standard
tcpci typec port controller device with power delivery support, tested
power source and sink with drp config.

Changes for v6:
- Change function name to be typec_find_port_power/data_role for find
  capability, and typec_find_power_role for find one specific power role.
- Fix rt1711h driver move missing.
- Add one patch to improve the error checking in tcpci driver, use IS_ERR()
  instead of PTR_ERR_OR_ZERO().
- Add Rob's Reviewed-by for patch [2/15].
- Misc typos fix.
 
Changes for v5:
- Use "power-role" and "data-role" for typec port power and data capability
  property name.  
- Use macro defintion instead of cryptic numbers for readability of
  power delivery properties(PDO).
- Add one tcpci driver binding string to be "nxp,ptn5110" to follow binding
  name rule ",".
- Change operating power property name to be op-sink-microwatt.
- Add one patch(remove unused tcpci_tcpc_config) to resolve bisectablitity
  issue of patch usb: typec: add fwnode to tcpc.
- A minor error handling change to keep the error path and success path
  separate in patch: register port before request irq
- Rebase to latest Greg's tree with max_snk_* removed.

Changes for v4:
- Remove max-sink-* properties as we will purge max_snk_* in tcpm,
  see patch set[4].
- Get typec power and data type value via name string(patch 5).
- Move finding typec and pd properties code from tcpci to tcpm(patch 6)
- Add a compatible string for nxp ptn5110 typec controller in tcpci driver.
  (patch 3)
- Add set cc for drp toggling without try.src/snk in tcpm(patch 10), then
  patch 11 can only update CCx bits for keep disconnect cc line open.
- Update op-sink-microwatt-hours example value to be the right value in
  micorwatts, and accordingly divide 1000 to get its miliwatts value
  in patch 6.
- Add Guenter's Reviewed-by for patch(8/9/12)

[4] https://www.spinics.net/lists/linux-usb/msg167261.html

Changes for v3:
- Use 2 properties to separate power and data capability of typec port:
  "power-type" and "data-type", this is based on Heikki's typec class code
  change[2]. use "try-power-role" to present if the typec port can support
  Try.SNK or Try.SRC.
- 4 sink properties(max_sink_mv/ma/mw and op_sink_mw) are kept because the
  counterpart code is back, see revert patch[3], meanwhile I post a patch
  to fix the reported problem of current source pdo select machinism(which
  completely ignored those 4 sink settings), to see if we can keep current
  code, once it was discussed and have conclusion I can update this
  accordingly.
- Use fwnode to get the connector node for dt setting parse.

Main changes for v2:
- Typec properties are based on general usb connector bindings[1] proposed
  by Andrzej Hajda, use the standard unit suffixes as defined in
  property-units.txt.
- Add 2 infra APIs to get power sink and source config from dt.
- Don't change the set_cc api, to keep the uncontacted cc line open,
  set cc1/cc2 to be open in tcpci driver when set polarity.
- Directly enable vbus detect in tcpci driver rather than add a API.
- Details added in each patch.

[1] https://patchwork.kernel.org/patch/10231447/
[2] https://patchwork.kernel.org/patch/10276483/
[3] https://www.spinics.net/lists/linux-usb/msg166366.html

Li Jun (14):
  dt-bindings: connector: add properties for typec
  dt-bindings: usb: add documentation for typec port controller(TCPCI)
  staging: typec: tcpci: add compatible string for nxp ptn5110
  usb: typec: add fwnode to tcpc
  usb: typec: add API to get typec basic port power and data config
  usb: typec: tcpm: support get typec and pd config from device
properties
  staging: typec: tcpci: remove unused tcpci_tcpc_config
  staging: typec: tcpci: use IS_ERR() instead of PTR_ERR_OR_ZERO()
  staging: typec: tcpci: enable vbus detection
  typec: tcpm: add starting value for drp toggling
  usb: typec: tcpm: set cc for drp toggling attach
  staging: typec: tcpci: keep the disconnected cc line open
  staging: typec: tcpci: Only touch target bit when enable vconn
  staging: typec: tcpci: move tcpci drivers out of staging

Peter Chen (1):
  staging: typec: tcpci: register port before request irq

 .../bindings/connector/usb-connector.txt   |  44 ++
 .../devicetree/bindings/usb/typec-tcpci.txt|  49 +++
 drivers/staging/Kconfig|   2 -
 drivers/staging/Makefile   |   1 -
 drivers/staging/typec/Kconfig  |  22 ---
 drivers/staging/typec/Makefile |   2 -
 drivers/staging/typec/TODO |   5 -
 drivers/usb/typec/Kconfig  |  15 +++
 drivers/usb/typec/Makefile |   2 +
 drivers/usb/typec/c

[PATCH v6 02/15] dt-bindings: usb: add documentation for typec port controller(TCPCI)

2018-05-27 Thread Li Jun
TCPCI stands for typec port controller interface, its implementation
has full typec port control with power delivery support, it's a
standard i2c slave with GPIO input as irq interface, detail see spec
"Universal Serial Bus Type-C Port Controller Interface Specification
Revision 1.0, Version 1.1"

Reviewed-by: Rob Herring <r...@kernel.org>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 .../devicetree/bindings/usb/typec-tcpci.txt| 49 ++
 1 file changed, 49 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/typec-tcpci.txt 
b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
new file mode 100644
index 000..0dd1469
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
@@ -0,0 +1,49 @@
+TCPCI(Typec port cotroller interface) binding
+-
+
+Required properties:
+- compatible:   should be set one of following:
+   - "nxp,ptn5110" for NXP USB PD TCPC PHY IC ptn5110.
+
+- reg:  the i2c slave address of typec port controller device.
+- interrupt-parent: the phandle to the interrupt controller which provides
+the interrupt.
+- interrupts:   interrupt specification for tcpci alert.
+
+Required sub-node:
+- connector: The "usb-c-connector" attached to the tcpci chip, the bindings
+  of connector node are specified in
+  Documentation/devicetree/bindings/connector/usb-connector.txt
+
+Example:
+
+ptn5110@50 {
+   compatible = "nxp,ptn5110";
+   reg = <0x50>;
+   interrupt-parent = <>;
+   interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   data-role = "dual";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)>;
+   sink-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)
+PDO_VAR(5000, 12000, 2000)>;
+   op-sink-microwatt = <1000>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@1 {
+   reg = <1>;
+   usb_con_ss: endpoint {
+   remote-endpoint = <_data_ss>;
+   };
+   };
+   };
+   };
+};
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 08/15] staging: typec: tcpci: use IS_ERR() instead of PTR_ERR_OR_ZERO()

2018-05-27 Thread Li Jun
As tcpm_register_port() and tcpci_register_port() never return
NULL and NULL is not a success in this case, use IS_ERR() to check
the return value of both.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076498a..b63f147 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -509,7 +509,7 @@ struct tcpci *tcpci_register_port(struct device *dev, 
struct tcpci_data *data)
return ERR_PTR(err);
 
tcpci->port = tcpm_register_port(tcpci->dev, >tcpc);
-   if (PTR_ERR_OR_ZERO(tcpci->port))
+   if (IS_ERR(tcpci->port))
return ERR_CAST(tcpci->port);
 
return tcpci;
@@ -551,7 +551,7 @@ static int tcpci_probe(struct i2c_client *client,
return err;
 
chip->tcpci = tcpci_register_port(>dev, >data);
-   if (PTR_ERR_OR_ZERO(chip->tcpci))
+   if (IS_ERR(chip->tcpci))
return PTR_ERR(chip->tcpci);
 
i2c_set_clientdata(client, chip);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 04/15] usb: typec: add fwnode to tcpc

2018-05-27 Thread Li Jun
Add fwnode handle to get the fwnode so we can get typec configs
it contains.

Suggested-by: Heikki Krogerus <heikki.kroge...@linux.intel.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 7 +++
 drivers/usb/typec/tcpm.c  | 1 +
 include/linux/usb/tcpm.h  | 2 ++
 3 files changed, 10 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index dd29288..e59547a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -474,6 +475,12 @@ static int tcpci_parse_config(struct tcpci *tcpci)
 
/* TODO: Populate struct tcpc_config from ACPI/device-tree */
tcpci->tcpc.config = _tcpc_config;
+   tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
+"connector");
+   if (!tcpci->tcpc.fwnode) {
+   dev_err(tcpci->dev, "Can't find connector node.\n");
+   return -EINVAL;
+   }
 
return 0;
 }
diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 0ccd2ce..fcd22e8 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4581,6 +4581,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
else
port->try_role = TYPEC_NO_PREFERRED_ROLE;
 
+   port->typec_caps.fwnode = tcpc->fwnode;
port->typec_caps.prefer_role = tcpc->config->default_role;
port->typec_caps.type = tcpc->config->type;
port->typec_caps.data = tcpc->config->data;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index b231b93..193920a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -110,6 +110,7 @@ enum tcpc_mux_mode {
 /**
  * struct tcpc_dev - Port configuration and callback functions
  * @config:Pointer to port configuration
+ * @fwnode:Pointer to port fwnode
  * @get_vbus:  Called to read current VBUS state
  * @get_current_limit:
  * Optional; called by the tcpm core when configured as a snk
@@ -138,6 +139,7 @@ enum tcpc_mux_mode {
  */
 struct tcpc_dev {
const struct tcpc_config *config;
+   struct fwnode_handle *fwnode;
 
int (*init)(struct tcpc_dev *dev);
int (*get_vbus)(struct tcpc_dev *dev);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 01/15] dt-bindings: connector: add properties for typec

2018-05-27 Thread Li Jun
Add bindings supported by current typec driver, so user can pass
all those properties via dt.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 .../bindings/connector/usb-connector.txt   | 44 +++
 include/dt-bindings/usb/pd.h   | 62 ++
 2 files changed, 106 insertions(+)

diff --git a/Documentation/devicetree/bindings/connector/usb-connector.txt 
b/Documentation/devicetree/bindings/connector/usb-connector.txt
index e1463f1..8855bfc 100644
--- a/Documentation/devicetree/bindings/connector/usb-connector.txt
+++ b/Documentation/devicetree/bindings/connector/usb-connector.txt
@@ -15,6 +15,33 @@ Optional properties:
 - type: size of the connector, should be specified in case of USB-A, USB-B
   non-fullsize connectors: "mini", "micro".
 
+Optional properties for usb-c-connector:
+- power-role: should be one of "source", "sink" or "dual"(DRP) if typec
+  connector has power support.
+- try-power-role: preferred power role if "dual"(DRP) can support Try.SNK
+  or Try.SRC, should be "sink" for Try.SNK or "source" for Try.SRC.
+- data-role: should be one of "host", "device", "dual"(DRD) if typec
+  connector supports USB data.
+
+Required properties for usb-c-connector with power delivery support:
+- source-pdos: An array of u32 with each entry providing supported power
+  source data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.2
+  Source_Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power source and power dual role.
+  User can specify the source PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- sink-pdos: An array of u32 with each entry providing supported power
+  sink data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.3
+  Sink Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power sink and power dual role.
+  User can specify the sink PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- op-sink-microwatt: Sink required operating power in microwatt, if source
+  can't offer the power, Capability Mismatch is set. Required for power
+  sink and power dual role.
+
 Required nodes:
 - any data bus to the connector should be modeled using the OF graph bindings
   specified in bindings/graph.txt, unless the bus is between parent node and
@@ -73,3 +100,20 @@ ccic: s2mm005@33 {
};
};
 };
+
+3. USB-C connector attached to a typec port controller(ptn5110), which has
+power delivery support and enables drp.
+
+typec: ptn5110@50 {
+   ...
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)>;
+   sink-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)
+PDO_VAR(5000, 12000, 2000)>;
+   op-sink-microwatt = <1000>;
+   };
+};
diff --git a/include/dt-bindings/usb/pd.h b/include/dt-bindings/usb/pd.h
new file mode 100644
index 000..7b7a92f
--- /dev/null
+++ b/include/dt-bindings/usb/pd.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __DT_POWER_DELIVERY_H
+#define __DT_POWER_DELIVERY_H
+
+/* Power delivery Power Data Object definitions */
+#define PDO_TYPE_FIXED 0
+#define PDO_TYPE_BATT  1
+#define PDO_TYPE_VAR   2
+#define PDO_TYPE_APDO  3
+
+#define PDO_TYPE_SHIFT 30
+#define PDO_TYPE_MASK  0x3
+
+#define PDO_TYPE(t)((t) << PDO_TYPE_SHIFT)
+
+#define PDO_VOLT_MASK  0x3ff
+#define PDO_CURR_MASK  0x3ff
+#define PDO_PWR_MASK   0x3ff
+
+#define PDO_FIXED_DUAL_ROLE(1 << 29) /* Power role swap supported */
+#define PDO_FIXED_SUSPEND  (1 << 28) /* USB Suspend supported (Source) */
+#define PDO_FIXED_HIGHER_CAP   (1 << 28) /* Requires more than vSafe5V (Sink) 
*/
+#define PDO_FIXED_EXTPOWER (1 << 27) /* Externally powered */
+#define PDO_FIXED_USB_COMM (1 << 26) /* USB communications capable */
+#define PDO_FIXED_DATA_SWAP(1 << 25) /* Data role swap supported */
+#define PDO_FIXED_VOLT_SHIFT   10  /* 50mV units */
+#define PDO_FIXED_CURR_SHIFT   0   /* 10mA units */
+
+#define PDO_FIXED_VOLT(mv) mv) / 50) & PDO_VOLT_MASK) << 
PDO_FIXED_VOLT_SHIFT)
+#define PDO_FIXED_CURR(ma) ma) / 10) & PDO_CURR_MASK) << 
PDO_FIXED_CURR_SHIFT)
+
+#define PDO_FIXED(mv, m

[PATCH v5 13/14] staging: typec: tcpci: Only touch target bit when enable vconn

2018-05-02 Thread Li Jun
We need regmap_update_bits to avoid touch any other bits when
enable or disable vconn.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 5c0c5e3..2818a4d 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -218,12 +218,9 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool 
enable)
return ret;
}
 
-   ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
-  enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
-   if (ret < 0)
-   return ret;
-
-   return 0;
+   return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
+   TCPC_POWER_CTRL_VCONN_ENABLE,
+   enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 14/14] staging: typec: tcpci: move tcpci driver out of staging

2018-05-02 Thread Li Jun
Move TCPCI(Typec port controller interface) driver out of staging.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/Kconfig| 2 --
 drivers/staging/Makefile   | 1 -
 drivers/staging/typec/TODO | 5 -
 drivers/usb/typec/Kconfig  | 7 +++
 drivers/usb/typec/Makefile | 1 +
 drivers/{staging => usb}/typec/tcpci.c | 0
 drivers/{staging => usb}/typec/tcpci.h | 0
 7 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index d5926f0..d83ff66 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -112,8 +112,6 @@ source "drivers/staging/greybus/Kconfig"
 
 source "drivers/staging/vc04_services/Kconfig"
 
-source "drivers/staging/typec/Kconfig"
-
 source "drivers/staging/vboxvideo/Kconfig"
 
 source "drivers/staging/pi433/Kconfig"
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index 919753c..a71ec1f 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -2,7 +2,6 @@
 # Makefile for staging directory
 
 obj-y  += media/
-obj-y  += typec/
 obj-$(CONFIG_IPX)  += ipx/
 obj-$(CONFIG_NCP_FS)   += ncpfs/
 obj-$(CONFIG_PRISM2_USB)   += wlan-ng/
diff --git a/drivers/staging/typec/TODO b/drivers/staging/typec/TODO
deleted file mode 100644
index 53fe2f7..000
--- a/drivers/staging/typec/TODO
+++ /dev/null
@@ -1,5 +0,0 @@
-tcpci:
-- Test with real hardware
-
-Please send patches to Guenter Roeck <li...@roeck-us.net> and copy
-Heikki Krogerus <heikki.kroge...@linux.intel.com>.
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 2c8eab1..0a862fc 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -56,6 +56,13 @@ config TYPEC_TCPM
 
 if TYPEC_TCPM
 
+config TYPEC_TCPCI
+   tristate "Type-C Port Controller Interface driver"
+   depends on I2C
+   select REGMAP_I2C
+   help
+ Type-C Port Controller driver for TCPCI-compliant controller.
+
 source "drivers/usb/typec/fusb302/Kconfig"
 
 config TYPEC_WCOVE
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index 1f599a6..02758a1 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_TYPEC_WCOVE)   += typec_wcove.o
 obj-$(CONFIG_TYPEC_UCSI)   += ucsi/
 obj-$(CONFIG_TYPEC_TPS6598X)   += tps6598x.o
 obj-$(CONFIG_TYPEC)+= mux/
+obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
diff --git a/drivers/staging/typec/tcpci.c b/drivers/usb/typec/tcpci.c
similarity index 100%
rename from drivers/staging/typec/tcpci.c
rename to drivers/usb/typec/tcpci.c
diff --git a/drivers/staging/typec/tcpci.h b/drivers/usb/typec/tcpci.h
similarity index 100%
rename from drivers/staging/typec/tcpci.h
rename to drivers/usb/typec/tcpci.h
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 12/14] staging: typec: tcpci: keep the not connecting cc line open

2018-05-02 Thread Li Jun
While set polarity, we should keep the not connecting cc line to be
open.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 5c48810..5c0c5e3 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -185,15 +185,25 @@ static int tcpci_set_polarity(struct tcpc_dev *tcpc,
  enum typec_cc_polarity polarity)
 {
struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+   unsigned int reg;
int ret;
 
-   ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
-  (polarity == TYPEC_POLARITY_CC2) ?
-  TCPC_TCPC_CTRL_ORIENTATION : 0);
+   /* Keep the disconnect cc line open */
+   ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, );
if (ret < 0)
return ret;
 
-   return 0;
+   if (polarity == TYPEC_POLARITY_CC2)
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT;
+   else
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT;
+   ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+   if (ret < 0)
+   return ret;
+
+   return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+  (polarity == TYPEC_POLARITY_CC2) ?
+  TCPC_TCPC_CTRL_ORIENTATION : 0);
 }
 
 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 11/14] usb: typec: tcpm: set cc for drp toggling attach

2018-05-02 Thread Li Jun
In case of drp toggling, we may need set correct cc value for role control
after attach as it may never been set.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 0c7fa14..7e7eaca 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2599,6 +2599,7 @@ static void tcpm_reset_port(struct tcpm_port *port)
tcpm_set_attached_state(port, false);
port->try_src_count = 0;
port->try_snk_count = 0;
+   port->cc_req = TYPEC_CC_OPEN;
port->supply_voltage = 0;
port->current_limit = 0;
port->usb_type = POWER_SUPPLY_USB_TYPE_C;
@@ -2831,6 +2832,8 @@ static void run_state_machine(struct tcpm_port *port)
break;
 
case SRC_ATTACHED:
+   if (port->cc_req == TYPEC_CC_OPEN)
+   tcpm_set_cc(port, tcpm_rp_cc(port));
ret = tcpm_src_attach(port);
tcpm_set_state(port, SRC_UNATTACHED,
   ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
@@ -3004,6 +3007,8 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
break;
case SNK_ATTACHED:
+   if (port->cc_req == TYPEC_CC_OPEN)
+   tcpm_set_cc(port, TYPEC_CC_RD);
ret = tcpm_snk_attach(port);
if (ret < 0)
tcpm_set_state(port, SNK_UNATTACHED, 0);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 09/14] staging: typec: tcpci: enable vbus detection

2018-05-02 Thread Li Jun
TCPCI implementation may need SW to enable VBUS detection to generate
power status events.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 139c2ab..5c48810 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -373,6 +373,12 @@ static int tcpci_init(struct tcpc_dev *tcpc)
if (ret < 0)
return ret;
 
+   /* Enable Vbus detection */
+   ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+  TCPC_CMD_ENABLE_VBUS_DETECT);
+   if (ret < 0)
+   return ret;
+
reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 10/14] typec: tcpm: add starting value for drp toggling

2018-05-02 Thread Li Jun
As DRP port autonomously toggles the Rp/Rd need a start value to
begin with, so add one parameter for it in tcpm_start_drp_toggling.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index becedcf..0c7fa14 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2436,15 +2436,15 @@ static int tcpm_set_charge(struct tcpm_port *port, bool 
charge)
return 0;
 }
 
-static bool tcpm_start_drp_toggling(struct tcpm_port *port)
+static bool tcpm_start_drp_toggling(struct tcpm_port *port,
+   enum typec_cc_status cc)
 {
int ret;
 
if (port->tcpc->start_drp_toggling &&
port->port_type == TYPEC_PORT_DRP) {
tcpm_log_force(port, "Start DRP toggling");
-   ret = port->tcpc->start_drp_toggling(port->tcpc,
-tcpm_rp_cc(port));
+   ret = port->tcpc->start_drp_toggling(port->tcpc, cc);
if (!ret)
return true;
}
@@ -2752,7 +2752,7 @@ static void run_state_machine(struct tcpm_port *port)
if (!port->non_pd_role_swap)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_src_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, tcpm_rp_cc(port))) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
@@ -2927,7 +2927,7 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_pps_complete(port, -ENOTCONN);
tcpm_snk_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, TYPEC_CC_RD)) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 08/14] staging: typec: tcpci: register port before request irq

2018-05-02 Thread Li Jun
From: Peter Chen <peter.c...@nxp.com>

With that we can clear any pending events and the port is registered
so driver can be ready to handle typec events once we request irq.

Signed-off-by: Peter Chen <peter.c...@nxp.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 15 +--
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index a21aaf5..139c2ab 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -537,24 +537,27 @@ static int tcpci_probe(struct i2c_client *client,
if (IS_ERR(chip->data.regmap))
return PTR_ERR(chip->data.regmap);
 
+   i2c_set_clientdata(client, chip);
+
/* Disable chip interrupts before requesting irq */
err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, ,
   sizeof(u16));
if (err < 0)
return err;
 
+   chip->tcpci = tcpci_register_port(>dev, >data);
+   if (PTR_ERR_OR_ZERO(chip->tcpci))
+   return PTR_ERR(chip->tcpci);
+
err = devm_request_threaded_irq(>dev, client->irq, NULL,
_tcpci_irq,
IRQF_ONESHOT | IRQF_TRIGGER_LOW,
dev_name(>dev), chip);
-   if (err < 0)
+   if (err < 0) {
+   tcpci_unregister_port(chip->tcpci);
return err;
+   }
 
-   chip->tcpci = tcpci_register_port(>dev, >data);
-   if (PTR_ERR_OR_ZERO(chip->tcpci))
-   return PTR_ERR(chip->tcpci);
-
-   i2c_set_clientdata(client, chip);
return 0;
 }
 
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 04/14] usb: typec: add fwnode to tcpc

2018-05-02 Thread Li Jun
Add fwnode handle to get the fwnode so we can get typec configs
it contains.

Suggested-by: Heikki Krogerus <heikki.kroge...@linux.intel.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 7 +++
 drivers/usb/typec/tcpm.c  | 1 +
 include/linux/usb/tcpm.h  | 2 ++
 3 files changed, 10 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 741a80a..01d14fb 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -474,6 +475,12 @@ static int tcpci_parse_config(struct tcpci *tcpci)
 
/* TODO: Populate struct tcpc_config from ACPI/device-tree */
tcpci->tcpc.config = _tcpc_config;
+   tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
+"connector");
+   if (!tcpci->tcpc.fwnode) {
+   dev_err(tcpci->dev, "Can't find connector node.\n");
+   return -EINVAL;
+   }
 
return 0;
 }
diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 7ee417a..80b2303 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -4579,6 +4579,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
else
port->try_role = TYPEC_NO_PREFERRED_ROLE;
 
+   port->typec_caps.fwnode = tcpc->fwnode;
port->typec_caps.prefer_role = tcpc->config->default_role;
port->typec_caps.type = tcpc->config->type;
port->typec_caps.data = tcpc->config->data;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index b231b93..193920a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -110,6 +110,7 @@ enum tcpc_mux_mode {
 /**
  * struct tcpc_dev - Port configuration and callback functions
  * @config:Pointer to port configuration
+ * @fwnode:Pointer to port fwnode
  * @get_vbus:  Called to read current VBUS state
  * @get_current_limit:
  * Optional; called by the tcpm core when configured as a snk
@@ -138,6 +139,7 @@ enum tcpc_mux_mode {
  */
 struct tcpc_dev {
const struct tcpc_config *config;
+   struct fwnode_handle *fwnode;
 
int (*init)(struct tcpc_dev *dev);
int (*get_vbus)(struct tcpc_dev *dev);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 07/14] staging: typec: tcpci: remove unused tcpci_tcpc_config

2018-05-02 Thread Li Jun
Since we will use config settings via device properties, so
remove the hard code tcpci_tcpc_config.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 7 ---
 1 file changed, 7 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 01d14fb..a21aaf5 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -464,17 +464,10 @@ static const struct regmap_config tcpci_regmap_config = {
.max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
 };
 
-static const struct tcpc_config tcpci_tcpc_config = {
-   .type = TYPEC_PORT_DFP,
-   .default_role = TYPEC_SINK,
-};
-
 static int tcpci_parse_config(struct tcpci *tcpci)
 {
tcpci->controls_vbus = true; /* XXX */
 
-   /* TODO: Populate struct tcpc_config from ACPI/device-tree */
-   tcpci->tcpc.config = _tcpc_config;
tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
 "connector");
if (!tcpci->tcpc.fwnode) {
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 03/14] staging: typec: tcpci: add compatible string for nxp ptn5110

2018-05-02 Thread Li Jun
Add nxp ptn5110 typec controller compatible string: usb-tcpci,ptn5110,
which is a standard tcpci chip with power delivery support.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076d97e..741a80a 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -576,6 +576,7 @@ MODULE_DEVICE_TABLE(i2c, tcpci_id);
 #ifdef CONFIG_OF
 static const struct of_device_id tcpci_of_match[] = {
{ .compatible = "usb,tcpci", },
+   { .compatible = "nxp,ptn5110", },
{},
 };
 MODULE_DEVICE_TABLE(of, tcpci_of_match);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 06/14] usb: typec: tcpm: support get typec and pd config from device properties

2018-05-02 Thread Li Jun
This patch adds support of get typec and power delivery config from
firmware description.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 133 +++
 1 file changed, 111 insertions(+), 22 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 80b2303..becedcf 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -14,6 +14,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -4240,6 +4241,81 @@ static int tcpm_copy_vdos(u32 *dest_vdo, const u32 
*src_vdo,
return nr_vdo;
 }
 
+static int tcpm_fw_get_caps(struct tcpm_port *port,
+   struct fwnode_handle *fwnode)
+{
+   const char *cap_str;
+   int ret;
+   u32 mw;
+
+   if (!port || !fwnode)
+   return -EINVAL;
+
+   /* USB data support is optional */
+   ret = fwnode_property_read_string(fwnode, "data-role", _str);
+   if (ret == 0) {
+   port->typec_caps.data = typec_find_data_type(cap_str);
+   if (port->typec_caps.data < 0)
+   return -EINVAL;
+   }
+
+   ret = fwnode_property_read_string(fwnode, "power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.type = typec_find_power_type(cap_str);
+   if (port->typec_caps.type < 0)
+   return -EINVAL;
+   port->port_type = port->typec_caps.type;
+
+   if (port->port_type == TYPEC_PORT_SNK)
+   goto sink;
+
+   /* Get soruce pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+port->src_pdo, port->nr_src_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
+   port->nr_src_pdo))
+   return -EINVAL;
+
+   if (port->port_type == TYPEC_PORT_SRC)
+   return 0;
+
+   /* Get the preferred power role for DRP */
+   ret = fwnode_property_read_string(fwnode, "try-power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.prefer_role = typec_find_preferred_role(cap_str);
+   if (port->typec_caps.prefer_role < 0)
+   return -EINVAL;
+sink:
+   /* Get sink pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+port->snk_pdo, port->nr_snk_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
+   port->nr_snk_pdo))
+   return -EINVAL;
+
+   if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", ) < 0)
+   return -EINVAL;
+   port->operating_snk_mw = mw / 1000;
+
+   return 0;
+}
+
 int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
unsigned int nr_pdo)
 {
@@ -4524,12 +4600,36 @@ static int devm_tcpm_psy_register(struct tcpm_port 
*port)
return PTR_ERR_OR_ZERO(port->psy);
 }
 
+static int tcpm_copy_caps(struct tcpm_port *port,
+ const struct tcpc_config *tcfg)
+{
+   if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
+   tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
+   return -EINVAL;
+
+   port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
+ tcfg->nr_src_pdo);
+   port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
+ tcfg->nr_snk_pdo);
+
+   port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
+ tcfg->nr_snk_vdo);
+
+   port->operating_snk_mw = tcfg->operating_snk_mw;
+
+   port->typec_caps.prefer_role = tcfg->default_role;
+   port->typec_caps.type = tcfg->type;
+   port->typec_caps.data = tcfg->data;
+
+   return 0;
+}
+
 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 {
struct tcpm_port *port;
int i, err;
 
-   if (!dev || !tcpc || !tcpc->config ||
+   if (!dev || !tcpc ||
!tcpc->get_vbus || !tcpc-&g

[PATCH v5 05/14] usb: typec: add API to get typec basic port power and data config

2018-05-02 Thread Li Jun
This patch adds 3 APIs to get the typec port power and data type,
and preferred power role by its name string.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/class.c | 52 +++
 include/linux/usb/typec.h |  3 +++
 2 files changed, 55 insertions(+)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 53df10d..5981e18 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -9,6 +9,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -802,6 +803,12 @@ static const char * const typec_port_types[] = {
[TYPEC_PORT_DRP] = "dual",
 };
 
+static const char * const typec_data_types[] = {
+   [TYPEC_PORT_DFP] = "host",
+   [TYPEC_PORT_UFP] = "device",
+   [TYPEC_PORT_DRD] = "dual",
+};
+
 static const char * const typec_port_types_drp[] = {
[TYPEC_PORT_SRC] = "dual [source] sink",
[TYPEC_PORT_SNK] = "dual source [sink]",
@@ -1252,6 +1259,51 @@ void typec_set_pwr_opmode(struct typec_port *port,
 }
 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
 
+/**
+ * typec_find_power_type - Get the typec port power type
+ * @name: port type string
+ *
+ * This routine is used to find the typec_port_type by its string name.
+ *
+ * Returns typec_port_type if success, otherwise negative error code.
+ */
+int typec_find_power_type(const char *name)
+{
+   return match_string(typec_port_types, ARRAY_SIZE(typec_port_types),
+   name);
+}
+EXPORT_SYMBOL_GPL(typec_find_power_type);
+
+/**
+ * typec_find_preferred_role - Find the typec drp port preferred power role
+ * @name: power role string
+ *
+ * This routine is used to find the typec_role by its string name of
+ * preferred power role(Try.SRC or Try.SNK).
+ *
+ * Returns typec_role if success, otherwise negative error code.
+ */
+int typec_find_preferred_role(const char *name)
+{
+   return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_preferred_role);
+
+/**
+ * typec_find_data_type - Get the typec port data capability
+ * @name: data type string
+ *
+ * This routine is used to find the typec_port_data by its string name.
+ *
+ * Returns typec_port_data if success, otherwise negative error code.
+ */
+int typec_find_data_type(const char *name)
+{
+   return match_string(typec_data_types, ARRAY_SIZE(typec_data_types),
+   name);
+}
+EXPORT_SYMBOL_GPL(typec_find_data_type);
+
 /* -- */
 /* API for Multiplexer/DeMultiplexer Switches */
 
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 672b39b..00c93e7 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -267,4 +267,7 @@ int typec_set_orientation(struct typec_port *port,
  enum typec_orientation orientation);
 int typec_set_mode(struct typec_port *port, int mode);
 
+int typec_find_power_type(const char *name);
+int typec_find_preferred_role(const char *name);
+int typec_find_data_type(const char *name);
 #endif /* __LINUX_USB_TYPEC_H */
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 02/14] dt-bindings: usb: add documentation for typec port controller(TCPCI)

2018-05-02 Thread Li Jun
TCPCI stands for typec port controller interface, its implementation
has full typec port control with power delivery support, it's a
standard i2c slave with GPIO input as irq interface, detail see spec
"Universal Serial Bus Type-C Port Controller Interface Specification
Revision 1.0, Version 1.1"

Signed-off-by: Li Jun <jun...@nxp.com>
---
 .../devicetree/bindings/usb/typec-tcpci.txt| 49 ++
 1 file changed, 49 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/typec-tcpci.txt 
b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
new file mode 100644
index 000..0dd1469
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
@@ -0,0 +1,49 @@
+TCPCI(Typec port cotroller interface) binding
+-
+
+Required properties:
+- compatible:   should be set one of following:
+   - "nxp,ptn5110" for NXP USB PD TCPC PHY IC ptn5110.
+
+- reg:  the i2c slave address of typec port controller device.
+- interrupt-parent: the phandle to the interrupt controller which provides
+the interrupt.
+- interrupts:   interrupt specification for tcpci alert.
+
+Required sub-node:
+- connector: The "usb-c-connector" attached to the tcpci chip, the bindings
+  of connector node are specified in
+  Documentation/devicetree/bindings/connector/usb-connector.txt
+
+Example:
+
+ptn5110@50 {
+   compatible = "nxp,ptn5110";
+   reg = <0x50>;
+   interrupt-parent = <>;
+   interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   data-role = "dual";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)>;
+   sink-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)
+PDO_VAR(5000, 12000, 2000)>;
+   op-sink-microwatt = <1000>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@1 {
+   reg = <1>;
+   usb_con_ss: endpoint {
+   remote-endpoint = <_data_ss>;
+   };
+   };
+   };
+   };
+};
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 00/14] staging: typec: tcpci: move out of staging

2018-05-02 Thread Li Jun
This patch set attempts to move the tcpci driver out of staging by fix
some tcpci driver issues and define typec and power delivery device
properties, the changes are verified on NXP PTN5110, which is a standard
tcpci typec port controller device with power delivery support, tested
power source and sink with drp config.

Changes for v5:
- Use "power-role" and "data-role" for typec port power and data capability
  property name.  
- Use macro defintion instead of cryptic numbers for readability of
  power delivery properties(PDO).
- Add one tcpci driver binding string to be "nxp,ptn5110" to follow binding
  name rule ",".
- Change operating power property name to be op-sink-microwatt.
- Add one patch(remove unused tcpci_tcpc_config) to resolve bisectablitity
  issue of patch usb: typec: add fwnode to tcpc.
- A minor error handling change to keep the error path and success path
  separate in patch: register port before request irq
- Rebase to latest Greg's tree with max_snk_* removed.

Changes for v4:
- Remove max-sink-* properties as we will purge max_snk_* in tcpm,
  see patch set[4].
- Get typec power and data type value via name string(patch 5).
- Move finding typec and pd properties code from tcpci to tcpm(patch 6)
- Add a compatible string for nxp ptn5110 typec controller in tcpci driver.
  (patch 3)
- Add set cc for drp toggling without try.src/snk in tcpm(patch 10), then
  patch 11 can only update CCx bits for keep disconnect cc line open.
- Update op-sink-microwatt-hours example value to be the right value in
  micorwatts, and accordingly divide 1000 to get its miliwatts value
  in patch 6.
- Add Guenter's Reviewed-by for patch(8/9/12)

[4] https://www.spinics.net/lists/linux-usb/msg167261.html

Changes for v3:
- Use 2 properties to separate power and data capability of typec port:
  "power-type" and "data-type", this is based on Heikki's typec class code
  change[2]. use "try-power-role" to present if the typec port can support
  Try.SNK or Try.SRC.
- 4 sink properties(max_sink_mv/ma/mw and op_sink_mw) are kept because the
  counterpart code is back, see revert patch[3], meanwhile I post a patch
  to fix the reported problem of current source pdo select machinism(which
  completely ignored those 4 sink settings), to see if we can keep current
  code, once it was discussed and have conclusion I can update this
  accordingly.
- Use fwnode to get the connector node for dt setting parse.

Main changes for v2:
- Typec properties are based on general usb connector bindings[1] proposed
  by Andrzej Hajda, use the standard unit suffixes as defined in
  property-units.txt.
- Add 2 infra APIs to get power sink and source config from dt.
- Don't change the set_cc api, to keep the uncontacted cc line open,
  set cc1/cc2 to be open in tcpci driver when set polarity.
- Directly enable vbus detect in tcpci driver rather than add a API.
- Details added in each patch.

[1] https://patchwork.kernel.org/patch/10231447/
[2] https://patchwork.kernel.org/patch/10276483/
[3] https://www.spinics.net/lists/linux-usb/msg166366.html

Li Jun (13):
  dt-bindings: connector: add properties for typec
  dt-bindings: usb: add documentation for typec port controller(TCPCI)
  staging: typec: tcpci: add compatible string for nxp ptn5110
  usb: typec: add fwnode to tcpc
  usb: typec: add API to get typec basic port power and data config
  usb: typec: tcpm: support get typec and pd config from device
properties
  staging: typec: tcpci: remove unused tcpci_tcpc_config
  staging: typec: tcpci: enable vbus detection
  typec: tcpm: add starting value for drp toggling
  usb: typec: tcpm: set cc for drp toggling attach
  staging: typec: tcpci: keep the not connecting cc line open
  staging: typec: tcpci: Only touch target bit when enable vconn
  staging: typec: tcpci: move tcpci driver out of staging

Peter Chen (1):
  staging: typec: tcpci: register port before request irq

 .../bindings/connector/usb-connector.txt   |  44 ++
 .../devicetree/bindings/usb/typec-tcpci.txt|  49 +++
 drivers/staging/Kconfig|   2 -
 drivers/staging/Makefile   |   1 -
 drivers/staging/typec/TODO |   5 -
 drivers/usb/typec/Kconfig  |   7 +
 drivers/usb/typec/Makefile |   1 +
 drivers/usb/typec/class.c  |  52 +++
 drivers/{staging => usb}/typec/tcpci.c |  63 +
 drivers/{staging => usb}/typec/tcpci.h |   0
 drivers/usb/typec/tcpm.c   | 149 +
 include/dt-bindings/usb/pd.h   |  62 +
 include/linux/usb/tcpm.h   |   2 +
 include/linux/usb/typec.h  |   3 +
 14 files changed, 382 insertions(+), 58 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/ty

[PATCH v5 01/14] dt-bindings: connector: add properties for typec

2018-05-02 Thread Li Jun
Add bingdings supported by current typec driver, so user can pass
all those properties via dt.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 .../bindings/connector/usb-connector.txt   | 44 +++
 include/dt-bindings/usb/pd.h   | 62 ++
 2 files changed, 106 insertions(+)

diff --git a/Documentation/devicetree/bindings/connector/usb-connector.txt 
b/Documentation/devicetree/bindings/connector/usb-connector.txt
index e1463f1..4b19de6d0 100644
--- a/Documentation/devicetree/bindings/connector/usb-connector.txt
+++ b/Documentation/devicetree/bindings/connector/usb-connector.txt
@@ -15,6 +15,33 @@ Optional properties:
 - type: size of the connector, should be specified in case of USB-A, USB-B
   non-fullsize connectors: "mini", "micro".
 
+Optional properties for usb-c-connector:
+- power-role: should be one of "source", "sink" or "dual"(DRP) if typec
+  connector has power support.
+- try-power-role: preferred power role if "dual"(DRP) can support Try.SNK
+  or Try.SRC, should be "sink" for Try.SNK or "source" for Try.SRC.
+- data-role: should be one of "host", "device", "dual"(DRD) if typec
+  connector supports USB data.
+
+Required properties for usb-c-connector with power delivery support:
+- source-pdos: An array of u32 with each entry providing supported power
+  source data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.2
+  Source_Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power source and power dual role.
+  User can specify the source PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- sink-pdos: An array of u32 with each entry providing supported power
+  sink data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.3
+  Sink Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power sink and power dual role.
+  User can specify the sink PDO array via PDO_FIXED/BATT/VAR() defined in
+  dt-bindings/usb/pd.h.
+- op-sink-microwatt: Sink required operating power in microwatt, if source
+  can't offer the power, Capability Mismatch is set, required for power
+  sink and power dual role.
+
 Required nodes:
 - any data bus to the connector should be modeled using the OF graph bindings
   specified in bindings/graph.txt, unless the bus is between parent node and
@@ -73,3 +100,20 @@ ccic: s2mm005@33 {
};
};
 };
+
+3. USB-C connector attached to a typec port controller(ptn5110), which has
+power delivery support and enables drp.
+
+typec: ptn5110@50 {
+   ...
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   power-role = "dual";
+   try-power-role = "sink";
+   source-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)>;
+   sink-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)
+PDO_VAR(5000, 12000, 2000)>;
+   op-sink-microwatt = <1000>;
+   };
+};
diff --git a/include/dt-bindings/usb/pd.h b/include/dt-bindings/usb/pd.h
new file mode 100644
index 000..7b7a92f
--- /dev/null
+++ b/include/dt-bindings/usb/pd.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __DT_POWER_DELIVERY_H
+#define __DT_POWER_DELIVERY_H
+
+/* Power delivery Power Data Object definitions */
+#define PDO_TYPE_FIXED 0
+#define PDO_TYPE_BATT  1
+#define PDO_TYPE_VAR   2
+#define PDO_TYPE_APDO  3
+
+#define PDO_TYPE_SHIFT 30
+#define PDO_TYPE_MASK  0x3
+
+#define PDO_TYPE(t)((t) << PDO_TYPE_SHIFT)
+
+#define PDO_VOLT_MASK  0x3ff
+#define PDO_CURR_MASK  0x3ff
+#define PDO_PWR_MASK   0x3ff
+
+#define PDO_FIXED_DUAL_ROLE(1 << 29) /* Power role swap supported */
+#define PDO_FIXED_SUSPEND  (1 << 28) /* USB Suspend supported (Source) */
+#define PDO_FIXED_HIGHER_CAP   (1 << 28) /* Requires more than vSafe5V (Sink) 
*/
+#define PDO_FIXED_EXTPOWER (1 << 27) /* Externally powered */
+#define PDO_FIXED_USB_COMM (1 << 26) /* USB communications capable */
+#define PDO_FIXED_DATA_SWAP(1 << 25) /* Data role swap supported */
+#define PDO_FIXED_VOLT_SHIFT   10  /* 50mV units */
+#define PDO_FIXED_CURR_SHIFT   0   /* 10mA units */
+
+#define PDO_FIXED_VOLT(mv) mv) / 50) & PDO_VOLT_MASK) << 
PDO_FIXED_VOLT_SHIFT)
+#define PDO_FIXED_CURR(ma) ma) / 10) & PDO_CURR_MASK) << 
PDO_FIXED_CURR_SHIFT)
+
+#define PDO_FIXED(mv, m

[PATCH RFC] usb: typec: tcpm: remove operating_snk_mw for rdo

2018-04-17 Thread Li Jun
This patch is a further update for rdo based on [1], which
removed max_snk_ma/mv/mw but kept operating_snk_mw.
operating_snk_mw is only used to judge capability mismatch,
per PD spec, we can achieve this via compare the selected
source PDO and matching sink PDO, also after patch [1], we
don't limit the PDO matching between the same type, so the
rdo operating and max current/power calculation should be
updated accordingly.

[1]https://patchwork.kernel.org/patch/10342299/

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 52 ++--
 1 file changed, 33 insertions(+), 19 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 27192083..0be04b3 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -1854,28 +1854,42 @@ static int tcpm_pd_build_request(struct tcpm_port 
*port, u32 *rdo)
else
mv = pdo_min_voltage(pdo);
 
-   /* Select maximum available current within the sink pdo's limit */
-   if (type == PDO_TYPE_BATT) {
-   mw = min_power(pdo, matching_snk_pdo);
-   ma = 1000 * mw / mv;
-   } else {
-   ma = min_current(pdo, matching_snk_pdo);
-   mw = ma * mv / 1000;
-   }
-
flags = RDO_USB_COMM | RDO_NO_SUSPEND;
 
-   /* Set mismatch bit if offered power is less than operating power */
-   max_ma = ma;
-   max_mw = mw;
-   if (mw < port->operating_snk_mw) {
-   flags |= RDO_CAP_MISMATCH;
-   if (type == PDO_TYPE_BATT &&
-   (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
-   max_mw = pdo_max_power(matching_snk_pdo);
-   else if (pdo_max_current(matching_snk_pdo) >
-pdo_max_current(pdo))
+   switch (type) {
+   case PDO_TYPE_FIXED:
+   case PDO_TYPE_VAR:
+   if (pdo_type(matching_snk_pdo) == PDO_TYPE_BATT)
+   max_ma = pdo_max_power(matching_snk_pdo) * 1000 / mv;
+   else
max_ma = pdo_max_current(matching_snk_pdo);
+
+   if (max_ma > pdo_max_current(pdo)) {
+   flags |= RDO_CAP_MISMATCH;
+   ma = pdo_max_current(pdo);
+   } else {
+   ma = max_ma;
+   }
+   break;
+   case PDO_TYPE_BATT:
+   if (pdo_type(matching_snk_pdo) == PDO_TYPE_BATT)
+   max_mw = pdo_max_power(matching_snk_pdo);
+   else
+   max_mw = pdo_max_current(matching_snk_pdo) *
+pdo_min_voltage(matching_snk_pdo) /
+1000;
+
+   if (max_mw > pdo_max_power(pdo)) {
+   flags |= RDO_CAP_MISMATCH;
+   mw = pdo_max_power(pdo);
+   } else {
+   mw = max_mw;
+   }
+
+   ma = mw * 1000 / mv;
+   break;
+   default:
+   break;
}
 
tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 3/5] dt-bindings: usb: fusb302: remove max-sink-* properties

2018-04-16 Thread Li Jun
Remove max-sink-* properties since they are deprecated.

Reviewed-by: Rob Herring <r...@kernel.org>
Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 Documentation/devicetree/bindings/usb/fcs,fusb302.txt | 6 --
 1 file changed, 6 deletions(-)

diff --git a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt 
b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
index 472facf..6087dc7 100644
--- a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
+++ b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
@@ -6,12 +6,6 @@ Required properties :
 - interrupts : Interrupt specifier
 
 Optional properties :
-- fcs,max-sink-microvolt : Maximum voltage to negotiate when configured as sink
-- fcs,max-sink-microamp  : Maximum current to negotiate when configured as sink
-- fcs,max-sink-microwatt : Maximum power to negotiate when configured as sink
-  If this is less then max-sink-microvolt *
-  max-sink-microamp then the configured current will
-  be clamped.
 - fcs,operating-sink-microwatt :
   Minimum amount of power accepted from a sink
   when negotiating
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 1/5] usb: typec: tcpm: pdo matching optimization

2018-04-16 Thread Li Jun
This patch is a combination of commit 57e6f0d7b804
("typec: tcpm: Only request matching pdos") and source
pdo selection optimization based on it, instead of only
compare between the same pdo type of sink and source,
we should check source pdo voltage range is within the
voltage range of one sink pdo.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 105 ++-
 1 file changed, 67 insertions(+), 38 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 677d121..048b953 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -1772,39 +1772,63 @@ static int tcpm_pd_check_request(struct tcpm_port *port)
return 0;
 }
 
-static int tcpm_pd_select_pdo(struct tcpm_port *port)
+#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
+#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
+
+static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
+ int *src_pdo)
 {
-   unsigned int i, max_mw = 0, max_mv = 0;
+   unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
+max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
+min_snk_mv = 0;
int ret = -EINVAL;
 
/*
-* Select the source PDO providing the most power while staying within
-* the board's voltage limits. Prefer PDO providing exp
+* Select the source PDO providing the most power which has a
+* matchig sink cap.
 */
for (i = 0; i < port->nr_source_caps; i++) {
u32 pdo = port->source_caps[i];
enum pd_pdo_type type = pdo_type(pdo);
-   unsigned int mv, ma, mw;
 
-   if (type == PDO_TYPE_FIXED)
-   mv = pdo_fixed_voltage(pdo);
-   else
-   mv = pdo_min_voltage(pdo);
+   if (type == PDO_TYPE_FIXED) {
+   max_src_mv = pdo_fixed_voltage(pdo);
+   min_src_mv = max_src_mv;
+   } else {
+   max_src_mv = pdo_max_voltage(pdo);
+   min_src_mv = pdo_min_voltage(pdo);
+   }
 
if (type == PDO_TYPE_BATT) {
-   mw = pdo_max_power(pdo);
+   src_mw = pdo_max_power(pdo);
} else {
-   ma = min(pdo_max_current(pdo),
-port->max_snk_ma);
-   mw = ma * mv / 1000;
+   src_ma = pdo_max_current(pdo);
+   src_mw = src_ma * min_src_mv / 1000;
}
 
-   /* Perfer higher voltages if available */
-   if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
-   mv <= port->max_snk_mv) {
-   ret = i;
-   max_mw = mw;
-   max_mv = mv;
+   for (j = 0; j < port->nr_snk_pdo; j++) {
+   pdo = port->snk_pdo[j];
+
+   if (pdo_type(pdo) == PDO_TYPE_FIXED) {
+   min_snk_mv = pdo_fixed_voltage(pdo);
+   max_snk_mv = pdo_fixed_voltage(pdo);
+   } else {
+   min_snk_mv = pdo_min_voltage(pdo);
+   max_snk_mv = pdo_max_voltage(pdo);
+   }
+
+   if (max_src_mv <= max_snk_mv &&
+   min_src_mv >= min_snk_mv) {
+   /* Prefer higher voltages if available */
+   if ((src_mw == max_mw && min_src_mv > max_mv) ||
+   src_mw > max_mw) {
+   *src_pdo = i;
+   *sink_pdo = j;
+   max_mw = src_mw;
+   max_mv = min_src_mv;
+   ret = 0;
+   }
+   }
}
}
 
@@ -1816,13 +1840,16 @@ static int tcpm_pd_build_request(struct tcpm_port 
*port, u32 *rdo)
unsigned int mv, ma, mw, flags;
unsigned int max_ma, max_mw;
enum pd_pdo_type type;
-   int index;
-   u32 pdo;
+   int src_pdo_index, snk_pdo_index;
+   u32 pdo, matching_snk_pdo;
+   int ret;
 
-   index = tcpm_pd_select_pdo(port);
-   if (index < 0)
-   return -EINVAL;
-   pdo = port->source_caps[index];
+   ret = tcpm_pd_select_pdo(port, _pdo_index, _pdo_index);
+   if (ret < 0)
+   return ret;
+
+   pdo = port->s

[PATCH v5 5/5] usb: typec: tcpm: remove max_snk_mv/ma/mw

2018-04-16 Thread Li Jun
Since there is no user of max_snk_*, so we can remove them from tcpm.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 12 
 include/linux/usb/tcpm.h |  9 -
 2 files changed, 21 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 048b953..27192083 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -257,9 +257,6 @@ struct tcpm_port {
u32 snk_vdo[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
/* Requested current / voltage */
@@ -3598,9 +3595,6 @@ EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
 
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw)
 {
if (tcpm_validate_caps(port, pdo, nr_pdo))
@@ -3608,9 +3602,6 @@ int tcpm_update_sink_capabilities(struct tcpm_port *port, 
const u32 *pdo,
 
mutex_lock(>lock);
port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
-   port->max_snk_mv = max_snk_mv;
-   port->max_snk_ma = max_snk_ma;
-   port->max_snk_mw = max_snk_mw;
port->operating_snk_mw = operating_snk_mw;
 
switch (port->state) {
@@ -3676,9 +3667,6 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcpc->config->snk_vdo,
  tcpc->config->nr_snk_vdo);
 
-   port->max_snk_mv = tcpc->config->max_snk_mv;
-   port->max_snk_ma = tcpc->config->max_snk_ma;
-   port->max_snk_mw = tcpc->config->max_snk_mw;
port->operating_snk_mw = tcpc->config->operating_snk_mw;
if (!tcpc->config->try_role_hw)
port->try_role = tcpc->config->default_role;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index f0d839d..f5bda9a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -62,9 +62,6 @@ enum tcpm_transmit_type {
  * @snk_pdo:   PDO parameters sent to partner as response to
  * PD_CTRL_GET_SINK_CAP message
  * @nr_snk_pdo:Number of entries in @snk_pdo
- * @max_snk_mv:Maximum acceptable sink voltage in mV
- * @max_snk_ma:Maximum sink current in mA
- * @max_snk_mw:Maximum required sink power in mW
  * @operating_snk_mw:
  * Required operating sink power in mW
  * @type:  Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
@@ -85,9 +82,6 @@ struct tcpc_config {
const u32 *snk_vdo;
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
enum typec_port_type type;
@@ -174,9 +168,6 @@ int tcpm_update_source_capabilities(struct tcpm_port *port, 
const u32 *pdo,
unsigned int nr_pdo);
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw);
 
 void tcpm_vbus_change(struct tcpm_port *port);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 2/5] usb: typec: fusb302: remove max_snk_* for sink config

2018-04-16 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/fusb302/fusb302.c | 42 -
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/typec/fusb302/fusb302.c 
b/drivers/usb/typec/fusb302/fusb302.c
index 7036171..664463d 100644
--- a/drivers/usb/typec/fusb302/fusb302.c
+++ b/drivers/usb/typec/fusb302/fusb302.c
@@ -120,6 +120,7 @@ struct fusb302_chip {
enum typec_cc_polarity cc_polarity;
enum typec_cc_status cc1;
enum typec_cc_status cc2;
+   u32 snk_pdo[PDO_MAX_OBJECTS];
 
 #ifdef CONFIG_DEBUG_FS
struct dentry *dentry;
@@ -1212,11 +1213,6 @@ static const u32 snk_pdo[] = {
 static const struct tcpc_config fusb302_tcpc_config = {
.src_pdo = src_pdo,
.nr_src_pdo = ARRAY_SIZE(src_pdo),
-   .snk_pdo = snk_pdo,
-   .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
-   .max_snk_mv = 5000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 15000,
.operating_snk_mw = 2500,
.type = TYPEC_PORT_DRP,
.data = TYPEC_PORT_DRD,
@@ -1756,6 +1752,29 @@ static int init_gpio(struct fusb302_chip *chip)
return 0;
 }
 
+static int fusb302_composite_snk_pdo_array(struct fusb302_chip *chip)
+{
+   struct device *dev = chip->dev;
+   u32 max_uv, max_ua;
+
+   chip->snk_pdo[0] = PDO_FIXED(5000, 400, PDO_FIXED_FLAGS);
+
+   /*
+* As max_snk_ma/mv/mw is not needed for tcpc_config,
+* those settings should be passed in via sink PDO, so
+* "fcs, max-sink-*" properties will be deprecated, to
+* perserve compatibility with existing users of them,
+* we read those properties to convert them to be a var
+* PDO.
+*/
+   if (device_property_read_u32(dev, "fcs,max-sink-microvolt", _uv) ||
+   device_property_read_u32(dev, "fcs,max-sink-microamp", _ua))
+   return 1;
+
+   chip->snk_pdo[1] = PDO_VAR(5000, max_uv / 1000, max_ua / 1000);
+   return 2;
+}
+
 static int fusb302_probe(struct i2c_client *client,
 const struct i2c_device_id *id)
 {
@@ -1784,18 +1803,13 @@ static int fusb302_probe(struct i2c_client *client,
chip->tcpc_dev.config = >tcpc_config;
mutex_init(>lock);
 
-   if (!device_property_read_u32(dev, "fcs,max-sink-microvolt", ))
-   chip->tcpc_config.max_snk_mv = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microamp", ))
-   chip->tcpc_config.max_snk_ma = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microwatt", ))
-   chip->tcpc_config.max_snk_mw = v / 1000;
-
if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", ))
chip->tcpc_config.operating_snk_mw = v / 1000;
 
+   /* Composite sink PDO */
+   chip->tcpc_config.nr_snk_pdo = fusb302_composite_snk_pdo_array(chip);
+   chip->tcpc_config.snk_pdo = chip->snk_pdo;
+
/*
 * Devicetree platforms should get extcon via phandle (not yet
 * supported). On ACPI platforms, we get the name from a device prop.
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 4/5] usb: typec: wcove: remove max_snk_* for sink config

2018-04-16 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/typec_wcove.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/typec/typec_wcove.c b/drivers/usb/typec/typec_wcove.c
index 19cca7f..39cff11 100644
--- a/drivers/usb/typec/typec_wcove.c
+++ b/drivers/usb/typec/typec_wcove.c
@@ -558,6 +558,7 @@ static const u32 src_pdo[] = {
 static const u32 snk_pdo[] = {
PDO_FIXED(5000, 500, PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP |
  PDO_FIXED_USB_COMM),
+   PDO_VAR(5000, 12000, 3000),
 };
 
 static struct tcpc_config wcove_typec_config = {
@@ -566,9 +567,6 @@ static struct tcpc_config wcove_typec_config = {
.snk_pdo = snk_pdo,
.nr_snk_pdo = ARRAY_SIZE(snk_pdo),
 
-   .max_snk_mv = 12000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 36000,
.operating_snk_mw = 15000,
 
.type = TYPEC_PORT_DRP,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 0/5] usb: typec: remove max_snk_mv/ma/mw

2018-04-16 Thread Li Jun
This patch set is to remove max_snk_mv/ma/mw configs, as we should
define the sink capability by sink PDOs, the first patch update
the source PDO match policy by compare the voltage range between
source and sink PDOs no matter what type they are, the following
patchs remove those 3 variables from 2 existing users by adding
a variable PDO, then finial patch remove the max_snk_* from tcpm.

Change for v5:
- Fix ret = 0 missing when found matching source pdo, and add
  a variable to get the return value of tcpm_pd_select_pdo()
  in patch 1/5, tested on my platform.
- Add document in fusb302_composite_snk_pdo_array() to explain
  why use deprecated properties in patch 2/5.
 
Change for v4:
- Add Hans's reviewed-by tag for the whole patch set.

Changes for v3:
- Remove 3 variables: nr_fxied, nr_var and nr_batt from tcpm_port;
  so nr_type_pdos() is not needed and removed.
- Simplify fusb302_composite_snk_pdo_array() by only considering
  existing setting as Hans suggested.
- Add Rob's reviewed-by for dt-binding patch.

Changes for v2:
- rebase the 1st patch to be based on commit 6f566af34628
  ("Revert "typec: tcpm: Only request matching pdos"").
- Convert the device properties passing max_snk_* to be a
  variable sink pdo for fusb302.

Li Jun (5):
  usb: typec: tcpm: pdo matching optimization
  usb: typec: fusb302: remove max_snk_* for sink config
  dt-bindings: usb: fusb302: remove max-sink-* properties
  usb: typec: wcove: remove max_snk_* for sink config
  usb: typec: tcpm: remove max_snk_mv/ma/mw

 .../devicetree/bindings/usb/fcs,fusb302.txt|   6 --
 drivers/usb/typec/fusb302/fusb302.c|  42 +---
 drivers/usb/typec/tcpm.c   | 117 -
 drivers/usb/typec/typec_wcove.c|   4 +-
 include/linux/usb/tcpm.h   |   9 --
 5 files changed, 96 insertions(+), 82 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 5/5] usb: typec: tcpm: remove max_snk_mv/ma/mw

2018-04-13 Thread Li Jun
Since there is no user of max_snk_*, so we can remove them from tcpm.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 12 
 include/linux/usb/tcpm.h |  9 -
 2 files changed, 21 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index c127339..3d9cfd6 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -257,9 +257,6 @@ struct tcpm_port {
u32 snk_vdo[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
/* Requested current / voltage */
@@ -3597,9 +3594,6 @@ EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
 
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw)
 {
if (tcpm_validate_caps(port, pdo, nr_pdo))
@@ -3607,9 +3601,6 @@ int tcpm_update_sink_capabilities(struct tcpm_port *port, 
const u32 *pdo,
 
mutex_lock(>lock);
port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
-   port->max_snk_mv = max_snk_mv;
-   port->max_snk_ma = max_snk_ma;
-   port->max_snk_mw = max_snk_mw;
port->operating_snk_mw = operating_snk_mw;
 
switch (port->state) {
@@ -3675,9 +3666,6 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcpc->config->snk_vdo,
  tcpc->config->nr_snk_vdo);
 
-   port->max_snk_mv = tcpc->config->max_snk_mv;
-   port->max_snk_ma = tcpc->config->max_snk_ma;
-   port->max_snk_mw = tcpc->config->max_snk_mw;
port->operating_snk_mw = tcpc->config->operating_snk_mw;
if (!tcpc->config->try_role_hw)
port->try_role = tcpc->config->default_role;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index f0d839d..f5bda9a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -62,9 +62,6 @@ enum tcpm_transmit_type {
  * @snk_pdo:   PDO parameters sent to partner as response to
  * PD_CTRL_GET_SINK_CAP message
  * @nr_snk_pdo:Number of entries in @snk_pdo
- * @max_snk_mv:Maximum acceptable sink voltage in mV
- * @max_snk_ma:Maximum sink current in mA
- * @max_snk_mw:Maximum required sink power in mW
  * @operating_snk_mw:
  * Required operating sink power in mW
  * @type:  Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
@@ -85,9 +82,6 @@ struct tcpc_config {
const u32 *snk_vdo;
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
enum typec_port_type type;
@@ -174,9 +168,6 @@ int tcpm_update_source_capabilities(struct tcpm_port *port, 
const u32 *pdo,
unsigned int nr_pdo);
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw);
 
 void tcpm_vbus_change(struct tcpm_port *port);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 3/5] dt-bindings: usb: fusb302: remove max-sink-* properties

2018-04-13 Thread Li Jun
Remove max-sink-* properties since they are deprecated.

Reviewed-by: Rob Herring <r...@kernel.org>
Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 Documentation/devicetree/bindings/usb/fcs,fusb302.txt | 6 --
 1 file changed, 6 deletions(-)

diff --git a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt 
b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
index 472facf..6087dc7 100644
--- a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
+++ b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
@@ -6,12 +6,6 @@ Required properties :
 - interrupts : Interrupt specifier
 
 Optional properties :
-- fcs,max-sink-microvolt : Maximum voltage to negotiate when configured as sink
-- fcs,max-sink-microamp  : Maximum current to negotiate when configured as sink
-- fcs,max-sink-microwatt : Maximum power to negotiate when configured as sink
-  If this is less then max-sink-microvolt *
-  max-sink-microamp then the configured current will
-  be clamped.
 - fcs,operating-sink-microwatt :
   Minimum amount of power accepted from a sink
   when negotiating
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 4/5] usb: typec: wcove: remove max_snk_* for sink config

2018-04-13 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/typec_wcove.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/typec/typec_wcove.c b/drivers/usb/typec/typec_wcove.c
index 19cca7f..39cff11 100644
--- a/drivers/usb/typec/typec_wcove.c
+++ b/drivers/usb/typec/typec_wcove.c
@@ -558,6 +558,7 @@ static const u32 src_pdo[] = {
 static const u32 snk_pdo[] = {
PDO_FIXED(5000, 500, PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP |
  PDO_FIXED_USB_COMM),
+   PDO_VAR(5000, 12000, 3000),
 };
 
 static struct tcpc_config wcove_typec_config = {
@@ -566,9 +567,6 @@ static struct tcpc_config wcove_typec_config = {
.snk_pdo = snk_pdo,
.nr_snk_pdo = ARRAY_SIZE(snk_pdo),
 
-   .max_snk_mv = 12000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 36000,
.operating_snk_mw = 15000,
 
.type = TYPEC_PORT_DRP,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 1/5] usb: typec: tcpm: pdo matching optimization

2018-04-13 Thread Li Jun
This patch is a combination of commit 57e6f0d7b804
("typec: tcpm: Only request matching pdos") and source
pdo selection optimization based on it, instead of only
compare between the same pdo type of sink and source,
we should check source pdo voltage range is within the
voltage range of one sink pdo.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 102 ++-
 1 file changed, 65 insertions(+), 37 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 677d121..c127339 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -1772,40 +1772,65 @@ static int tcpm_pd_check_request(struct tcpm_port *port)
return 0;
 }
 
-static int tcpm_pd_select_pdo(struct tcpm_port *port)
+#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
+#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
+
+static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
+ int *src_pdo)
 {
-   unsigned int i, max_mw = 0, max_mv = 0;
+   unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
+max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
+min_snk_mv = 0;
int ret = -EINVAL;
 
/*
-* Select the source PDO providing the most power while staying within
-* the board's voltage limits. Prefer PDO providing exp
+* Select the source PDO providing the most power which has a
+* matchig sink cap.
 */
for (i = 0; i < port->nr_source_caps; i++) {
u32 pdo = port->source_caps[i];
enum pd_pdo_type type = pdo_type(pdo);
-   unsigned int mv, ma, mw;
 
-   if (type == PDO_TYPE_FIXED)
-   mv = pdo_fixed_voltage(pdo);
-   else
-   mv = pdo_min_voltage(pdo);
+   if (type == PDO_TYPE_FIXED) {
+   max_src_mv = pdo_fixed_voltage(pdo);
+   min_src_mv = max_src_mv;
+   } else {
+   max_src_mv = pdo_max_voltage(pdo);
+   min_src_mv = pdo_min_voltage(pdo);
+   }
 
if (type == PDO_TYPE_BATT) {
-   mw = pdo_max_power(pdo);
+   src_mw = pdo_max_power(pdo);
} else {
-   ma = min(pdo_max_current(pdo),
-port->max_snk_ma);
-   mw = ma * mv / 1000;
+   src_ma = pdo_max_current(pdo);
+   src_mw = src_ma * min_src_mv / 1000;
}
 
-   /* Perfer higher voltages if available */
-   if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
-   mv <= port->max_snk_mv) {
-   ret = i;
-   max_mw = mw;
-   max_mv = mv;
+   for (j = 0; j < port->nr_snk_pdo; j++) {
+   pdo = port->snk_pdo[j];
+
+   if (pdo_type(pdo) == PDO_TYPE_FIXED) {
+   min_snk_mv = pdo_fixed_voltage(pdo);
+   max_snk_mv = pdo_fixed_voltage(pdo);
+   } else {
+   min_snk_mv = pdo_min_voltage(pdo);
+   max_snk_mv = pdo_max_voltage(pdo);
+   }
+
+   if (max_src_mv <= max_snk_mv &&
+   min_src_mv >= min_snk_mv) {
+   /* Prefer higher voltages if available */
+   if ((src_mw == max_mw && min_src_mv > max_mv) ||
+   src_mw > max_mw) {
+   *src_pdo = i;
+   *sink_pdo = j;
+   max_mw = src_mw;
+   max_mv = min_src_mv;
+   }
+   break;
+   }
}
+
}
 
return ret;
@@ -1816,13 +1841,14 @@ static int tcpm_pd_build_request(struct tcpm_port 
*port, u32 *rdo)
unsigned int mv, ma, mw, flags;
unsigned int max_ma, max_mw;
enum pd_pdo_type type;
-   int index;
-   u32 pdo;
+   int src_pdo_index, snk_pdo_index;
+   u32 pdo, matching_snk_pdo;
 
-   index = tcpm_pd_select_pdo(port);
-   if (index < 0)
+   if (tcpm_pd_select_pdo(port, _pdo_index, _pdo_index) < 0)
return -EINVAL;
-   pdo = port->source_caps[index];
+
+   pdo = port->source_caps[src_pdo_index];
+   matching_snk_pdo = port-&g

[PATCH v4 0/5] usb: typec: remove max_snk_mv/ma/mw

2018-04-13 Thread Li Jun
This patch set is to remove max_snk_mv/ma/mw configs, as we should
define the sink capability by sink PDOs, the first patch update
the source PDO match policy by compare the voltage range between
source and sink PDOs no matter what type they are, the following
patchs remove those 3 variables from 2 existing users by adding
a variable PDO, then finial patch remove the max_snk_* from tcpm.

Change for v4:
- Add Hans's reviewed-by tag for the whole patch set.

Changes for v3:
- Remove 3 variables: nr_fxied, nr_var and nr_batt from tcpm_port;
  so nr_type_pdos() is not needed and removed.
- Simplify fusb302_composite_snk_pdo_array() by only considering
  existing setting as Hans suggested.
- Add Rob's reviewed-by for dt-binding patch.

Changes for v2:
- rebase the 1st patch to be based on commit 6f566af34628
  ("Revert "typec: tcpm: Only request matching pdos"").
- Convert the device properties passing max_snk_* to be a
  variable sink pdo for fusb302.

Li Jun (5):
  usb: typec: tcpm: pdo matching optimization
  usb: typec: fusb302: remove max_snk_* for sink config
  dt-bindings: usb: fusb302: remove max-sink-* properties
  usb: typec: wcove: remove max_snk_* for sink config
  usb: typec: tcpm: remove max_snk_mv/ma/mw

 .../devicetree/bindings/usb/fcs,fusb302.txt|   6 --
 drivers/usb/typec/fusb302/fusb302.c|  34 +++---
 drivers/usb/typec/tcpm.c   | 114 -
 drivers/usb/typec/typec_wcove.c|   4 +-
 include/linux/usb/tcpm.h   |   9 --
 5 files changed, 86 insertions(+), 81 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 2/5] usb: typec: fusb302: remove max_snk_* for sink config

2018-04-13 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Reviewed-by: Hans de Goede <hdego...@redhat.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/fusb302/fusb302.c | 34 --
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/typec/fusb302/fusb302.c 
b/drivers/usb/typec/fusb302/fusb302.c
index 7036171..893f9e5 100644
--- a/drivers/usb/typec/fusb302/fusb302.c
+++ b/drivers/usb/typec/fusb302/fusb302.c
@@ -120,6 +120,7 @@ struct fusb302_chip {
enum typec_cc_polarity cc_polarity;
enum typec_cc_status cc1;
enum typec_cc_status cc2;
+   u32 snk_pdo[PDO_MAX_OBJECTS];
 
 #ifdef CONFIG_DEBUG_FS
struct dentry *dentry;
@@ -1212,11 +1213,6 @@ static const u32 snk_pdo[] = {
 static const struct tcpc_config fusb302_tcpc_config = {
.src_pdo = src_pdo,
.nr_src_pdo = ARRAY_SIZE(src_pdo),
-   .snk_pdo = snk_pdo,
-   .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
-   .max_snk_mv = 5000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 15000,
.operating_snk_mw = 2500,
.type = TYPEC_PORT_DRP,
.data = TYPEC_PORT_DRD,
@@ -1756,6 +1752,21 @@ static int init_gpio(struct fusb302_chip *chip)
return 0;
 }
 
+static int fusb302_composite_snk_pdo_array(struct fusb302_chip *chip)
+{
+   struct device *dev = chip->dev;
+   u32 max_uv, max_ua;
+
+   chip->snk_pdo[0] = PDO_FIXED(5000, 400, PDO_FIXED_FLAGS);
+
+   if (device_property_read_u32(dev, "fcs,max-sink-microvolt", _uv) ||
+   device_property_read_u32(dev, "fcs,max-sink-microamp", _ua))
+   return 1;
+
+   chip->snk_pdo[1] = PDO_VAR(5000, max_uv / 1000, max_ua / 1000);
+   return 2;
+}
+
 static int fusb302_probe(struct i2c_client *client,
 const struct i2c_device_id *id)
 {
@@ -1784,18 +1795,13 @@ static int fusb302_probe(struct i2c_client *client,
chip->tcpc_dev.config = >tcpc_config;
mutex_init(>lock);
 
-   if (!device_property_read_u32(dev, "fcs,max-sink-microvolt", ))
-   chip->tcpc_config.max_snk_mv = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microamp", ))
-   chip->tcpc_config.max_snk_ma = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microwatt", ))
-   chip->tcpc_config.max_snk_mw = v / 1000;
-
if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", ))
chip->tcpc_config.operating_snk_mw = v / 1000;
 
+   /* Composite sink PDO */
+   chip->tcpc_config.nr_snk_pdo = fusb302_composite_snk_pdo_array(chip);
+   chip->tcpc_config.snk_pdo = chip->snk_pdo;
+
/*
 * Devicetree platforms should get extcon via phandle (not yet
 * supported). On ACPI platforms, we get the name from a device prop.
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 2/5] usb: typec: fusb302: remove max_snk_* for sink config

2018-04-13 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/fusb302/fusb302.c | 34 --
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/typec/fusb302/fusb302.c 
b/drivers/usb/typec/fusb302/fusb302.c
index 7036171..893f9e5 100644
--- a/drivers/usb/typec/fusb302/fusb302.c
+++ b/drivers/usb/typec/fusb302/fusb302.c
@@ -120,6 +120,7 @@ struct fusb302_chip {
enum typec_cc_polarity cc_polarity;
enum typec_cc_status cc1;
enum typec_cc_status cc2;
+   u32 snk_pdo[PDO_MAX_OBJECTS];
 
 #ifdef CONFIG_DEBUG_FS
struct dentry *dentry;
@@ -1212,11 +1213,6 @@ static const u32 snk_pdo[] = {
 static const struct tcpc_config fusb302_tcpc_config = {
.src_pdo = src_pdo,
.nr_src_pdo = ARRAY_SIZE(src_pdo),
-   .snk_pdo = snk_pdo,
-   .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
-   .max_snk_mv = 5000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 15000,
.operating_snk_mw = 2500,
.type = TYPEC_PORT_DRP,
.data = TYPEC_PORT_DRD,
@@ -1756,6 +1752,21 @@ static int init_gpio(struct fusb302_chip *chip)
return 0;
 }
 
+static int fusb302_composite_snk_pdo_array(struct fusb302_chip *chip)
+{
+   struct device *dev = chip->dev;
+   u32 max_uv, max_ua;
+
+   chip->snk_pdo[0] = PDO_FIXED(5000, 400, PDO_FIXED_FLAGS);
+
+   if (device_property_read_u32(dev, "fcs,max-sink-microvolt", _uv) ||
+   device_property_read_u32(dev, "fcs,max-sink-microamp", _ua))
+   return 1;
+
+   chip->snk_pdo[1] = PDO_VAR(5000, max_uv / 1000, max_ua / 1000);
+   return 2;
+}
+
 static int fusb302_probe(struct i2c_client *client,
 const struct i2c_device_id *id)
 {
@@ -1784,18 +1795,13 @@ static int fusb302_probe(struct i2c_client *client,
chip->tcpc_dev.config = >tcpc_config;
mutex_init(>lock);
 
-   if (!device_property_read_u32(dev, "fcs,max-sink-microvolt", ))
-   chip->tcpc_config.max_snk_mv = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microamp", ))
-   chip->tcpc_config.max_snk_ma = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microwatt", ))
-   chip->tcpc_config.max_snk_mw = v / 1000;
-
if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", ))
chip->tcpc_config.operating_snk_mw = v / 1000;
 
+   /* Composite sink PDO */
+   chip->tcpc_config.nr_snk_pdo = fusb302_composite_snk_pdo_array(chip);
+   chip->tcpc_config.snk_pdo = chip->snk_pdo;
+
/*
 * Devicetree platforms should get extcon via phandle (not yet
 * supported). On ACPI platforms, we get the name from a device prop.
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 3/5] dt-bindings: usb: fusb302: remove max-sink-* properties

2018-04-13 Thread Li Jun
Remove max-sink-* properties since they are deprecated.

Reviewed-by: Rob Herring <r...@kernel.org>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 Documentation/devicetree/bindings/usb/fcs,fusb302.txt | 6 --
 1 file changed, 6 deletions(-)

diff --git a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt 
b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
index 472facf..6087dc7 100644
--- a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
+++ b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
@@ -6,12 +6,6 @@ Required properties :
 - interrupts : Interrupt specifier
 
 Optional properties :
-- fcs,max-sink-microvolt : Maximum voltage to negotiate when configured as sink
-- fcs,max-sink-microamp  : Maximum current to negotiate when configured as sink
-- fcs,max-sink-microwatt : Maximum power to negotiate when configured as sink
-  If this is less then max-sink-microvolt *
-  max-sink-microamp then the configured current will
-  be clamped.
 - fcs,operating-sink-microwatt :
   Minimum amount of power accepted from a sink
   when negotiating
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 5/5] usb: typec: tcpm: remove max_snk_mv/ma/mw

2018-04-13 Thread Li Jun
Since there is no user of max_snk_*, so we can remove them from tcpm.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 12 
 include/linux/usb/tcpm.h |  9 -
 2 files changed, 21 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index c127339..3d9cfd6 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -257,9 +257,6 @@ struct tcpm_port {
u32 snk_vdo[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
/* Requested current / voltage */
@@ -3597,9 +3594,6 @@ EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
 
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw)
 {
if (tcpm_validate_caps(port, pdo, nr_pdo))
@@ -3607,9 +3601,6 @@ int tcpm_update_sink_capabilities(struct tcpm_port *port, 
const u32 *pdo,
 
mutex_lock(>lock);
port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
-   port->max_snk_mv = max_snk_mv;
-   port->max_snk_ma = max_snk_ma;
-   port->max_snk_mw = max_snk_mw;
port->operating_snk_mw = operating_snk_mw;
 
switch (port->state) {
@@ -3675,9 +3666,6 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcpc->config->snk_vdo,
  tcpc->config->nr_snk_vdo);
 
-   port->max_snk_mv = tcpc->config->max_snk_mv;
-   port->max_snk_ma = tcpc->config->max_snk_ma;
-   port->max_snk_mw = tcpc->config->max_snk_mw;
port->operating_snk_mw = tcpc->config->operating_snk_mw;
if (!tcpc->config->try_role_hw)
port->try_role = tcpc->config->default_role;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index f0d839d..f5bda9a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -62,9 +62,6 @@ enum tcpm_transmit_type {
  * @snk_pdo:   PDO parameters sent to partner as response to
  * PD_CTRL_GET_SINK_CAP message
  * @nr_snk_pdo:Number of entries in @snk_pdo
- * @max_snk_mv:Maximum acceptable sink voltage in mV
- * @max_snk_ma:Maximum sink current in mA
- * @max_snk_mw:Maximum required sink power in mW
  * @operating_snk_mw:
  * Required operating sink power in mW
  * @type:  Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
@@ -85,9 +82,6 @@ struct tcpc_config {
const u32 *snk_vdo;
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
enum typec_port_type type;
@@ -174,9 +168,6 @@ int tcpm_update_source_capabilities(struct tcpm_port *port, 
const u32 *pdo,
unsigned int nr_pdo);
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw);
 
 void tcpm_vbus_change(struct tcpm_port *port);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 0/5] usb: typec: remove max_snk_mv/ma/mw

2018-04-13 Thread Li Jun
This patch set is to remove max_snk_mv/ma/mw configs, as we should
define the sink capability by sink PDOs, the first patch update
the source PDO match policy by compare the voltage range between
source and sink PDOs no matter what type they are, the following
patchs remove those 3 variables from 2 existing users by adding
a variable PDO, then finial patch remove the max_snk_* from tcpm.

Changes for v3:
- Remove 3 variables: nr_fxied, nr_var and nr_batt from tcpm_port;
  so nr_type_pdos() is not needed and removed.
- Simplify fusb302_composite_snk_pdo_array() by only considering
  existing setting as Hans suggested.
- Add Rob's reviewed-by for dt-binding patch.

Changes for v2:
- rebase the 1st patch to be based on commit 6f566af34628
  ("Revert "typec: tcpm: Only request matching pdos"").
- Convert the device properties passing max_snk_* to be a
  variable sink pdo for fusb302.

Li Jun (5):
  usb: typec: tcpm: pdo matching optimization
  usb: typec: fusb302: remove max_snk_* for sink config
  dt-bindings: usb: fusb302: remove max-sink-* properties
  usb: typec: wcove: remove max_snk_* for sink config
  usb: typec: tcpm: remove max_snk_mv/ma/mw

 .../devicetree/bindings/usb/fcs,fusb302.txt|   6 --
 drivers/usb/typec/fusb302/fusb302.c|  34 +++---
 drivers/usb/typec/tcpm.c   | 114 -
 drivers/usb/typec/typec_wcove.c|   4 +-
 include/linux/usb/tcpm.h   |   9 --
 5 files changed, 86 insertions(+), 81 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 4/5] usb: typec: wcove: remove max_snk_* for sink config

2018-04-13 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/typec_wcove.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/typec/typec_wcove.c b/drivers/usb/typec/typec_wcove.c
index 19cca7f..39cff11 100644
--- a/drivers/usb/typec/typec_wcove.c
+++ b/drivers/usb/typec/typec_wcove.c
@@ -558,6 +558,7 @@ static const u32 src_pdo[] = {
 static const u32 snk_pdo[] = {
PDO_FIXED(5000, 500, PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP |
  PDO_FIXED_USB_COMM),
+   PDO_VAR(5000, 12000, 3000),
 };
 
 static struct tcpc_config wcove_typec_config = {
@@ -566,9 +567,6 @@ static struct tcpc_config wcove_typec_config = {
.snk_pdo = snk_pdo,
.nr_snk_pdo = ARRAY_SIZE(snk_pdo),
 
-   .max_snk_mv = 12000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 36000,
.operating_snk_mw = 15000,
 
.type = TYPEC_PORT_DRP,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 1/5] usb: typec: tcpm: pdo matching optimization

2018-04-13 Thread Li Jun
This patch is a combination of commit 57e6f0d7b804
("typec: tcpm: Only request matching pdos") and source
pdo selection optimization based on it, instead of only
compare between the same pdo type of sink and source,
we should check source pdo voltage range is within the
voltage range of one sink pdo.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 102 ++-
 1 file changed, 65 insertions(+), 37 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 677d121..c127339 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -1772,40 +1772,65 @@ static int tcpm_pd_check_request(struct tcpm_port *port)
return 0;
 }
 
-static int tcpm_pd_select_pdo(struct tcpm_port *port)
+#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
+#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
+
+static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
+ int *src_pdo)
 {
-   unsigned int i, max_mw = 0, max_mv = 0;
+   unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
+max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
+min_snk_mv = 0;
int ret = -EINVAL;
 
/*
-* Select the source PDO providing the most power while staying within
-* the board's voltage limits. Prefer PDO providing exp
+* Select the source PDO providing the most power which has a
+* matchig sink cap.
 */
for (i = 0; i < port->nr_source_caps; i++) {
u32 pdo = port->source_caps[i];
enum pd_pdo_type type = pdo_type(pdo);
-   unsigned int mv, ma, mw;
 
-   if (type == PDO_TYPE_FIXED)
-   mv = pdo_fixed_voltage(pdo);
-   else
-   mv = pdo_min_voltage(pdo);
+   if (type == PDO_TYPE_FIXED) {
+   max_src_mv = pdo_fixed_voltage(pdo);
+   min_src_mv = max_src_mv;
+   } else {
+   max_src_mv = pdo_max_voltage(pdo);
+   min_src_mv = pdo_min_voltage(pdo);
+   }
 
if (type == PDO_TYPE_BATT) {
-   mw = pdo_max_power(pdo);
+   src_mw = pdo_max_power(pdo);
} else {
-   ma = min(pdo_max_current(pdo),
-port->max_snk_ma);
-   mw = ma * mv / 1000;
+   src_ma = pdo_max_current(pdo);
+   src_mw = src_ma * min_src_mv / 1000;
}
 
-   /* Perfer higher voltages if available */
-   if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
-   mv <= port->max_snk_mv) {
-   ret = i;
-   max_mw = mw;
-   max_mv = mv;
+   for (j = 0; j < port->nr_snk_pdo; j++) {
+   pdo = port->snk_pdo[j];
+
+   if (pdo_type(pdo) == PDO_TYPE_FIXED) {
+   min_snk_mv = pdo_fixed_voltage(pdo);
+   max_snk_mv = pdo_fixed_voltage(pdo);
+   } else {
+   min_snk_mv = pdo_min_voltage(pdo);
+   max_snk_mv = pdo_max_voltage(pdo);
+   }
+
+   if (max_src_mv <= max_snk_mv &&
+   min_src_mv >= min_snk_mv) {
+   /* Prefer higher voltages if available */
+   if ((src_mw == max_mw && min_src_mv > max_mv) ||
+   src_mw > max_mw) {
+   *src_pdo = i;
+   *sink_pdo = j;
+   max_mw = src_mw;
+   max_mv = min_src_mv;
+   }
+   break;
+   }
}
+
}
 
return ret;
@@ -1816,13 +1841,14 @@ static int tcpm_pd_build_request(struct tcpm_port 
*port, u32 *rdo)
unsigned int mv, ma, mw, flags;
unsigned int max_ma, max_mw;
enum pd_pdo_type type;
-   int index;
-   u32 pdo;
+   int src_pdo_index, snk_pdo_index;
+   u32 pdo, matching_snk_pdo;
 
-   index = tcpm_pd_select_pdo(port);
-   if (index < 0)
+   if (tcpm_pd_select_pdo(port, _pdo_index, _pdo_index) < 0)
return -EINVAL;
-   pdo = port->source_caps[index];
+
+   pdo = port->source_caps[src_pdo_index];
+   matching_snk_pdo = port->snk_pdo[snk_pdo_index];
type = pdo_type(pdo);
 
 

[PATCH v4 12/13] staging: typec: tcpci: Only touch target bit when enable vconn

2018-03-28 Thread Li Jun
We need regmap_update_bits to avoid touch any other bits when
enable or disable vconn.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index b58bd59..0d0c971 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -218,12 +218,9 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool 
enable)
return ret;
}
 
-   ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
-  enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
-   if (ret < 0)
-   return ret;
-
-   return 0;
+   return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
+   TCPC_POWER_CTRL_VCONN_ENABLE,
+   enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 13/13] staging: typec: tcpci: move tcpci driver out of staging

2018-03-28 Thread Li Jun
Move TCPCI(Typec port controller interface) driver out of staging.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/Kconfig|  2 --
 drivers/staging/Makefile   |  1 -
 drivers/staging/typec/Kconfig  | 14 --
 drivers/staging/typec/Makefile |  1 -
 drivers/staging/typec/TODO |  5 -
 drivers/usb/typec/Kconfig  |  7 +++
 drivers/usb/typec/Makefile |  1 +
 drivers/{staging => usb}/typec/tcpci.c |  0
 drivers/{staging => usb}/typec/tcpci.h |  0
 9 files changed, 8 insertions(+), 23 deletions(-)

diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index e95ab68..751937d 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -116,8 +116,6 @@ source "drivers/staging/vc04_services/Kconfig"
 
 source "drivers/staging/ccree/Kconfig"
 
-source "drivers/staging/typec/Kconfig"
-
 source "drivers/staging/vboxvideo/Kconfig"
 
 source "drivers/staging/pi433/Kconfig"
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index af8cd6a..039693e 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -2,7 +2,6 @@
 # Makefile for staging directory
 
 obj-y  += media/
-obj-y  += typec/
 obj-$(CONFIG_IPX)  += ipx/
 obj-$(CONFIG_NCP_FS)   += ncpfs/
 obj-$(CONFIG_IRDA) += irda/net/
diff --git a/drivers/staging/typec/Kconfig b/drivers/staging/typec/Kconfig
deleted file mode 100644
index 5359f55..000
--- a/drivers/staging/typec/Kconfig
+++ /dev/null
@@ -1,14 +0,0 @@
-menu "USB Power Delivery and Type-C drivers"
-
-if TYPEC_TCPM
-
-config TYPEC_TCPCI
-   tristate "Type-C Port Controller Interface driver"
-   depends on I2C
-   select REGMAP_I2C
-   help
- Type-C Port Controller driver for TCPCI-compliant controller.
-
-endif
-
-endmenu
diff --git a/drivers/staging/typec/Makefile b/drivers/staging/typec/Makefile
deleted file mode 100644
index 53d649a..000
--- a/drivers/staging/typec/Makefile
+++ /dev/null
@@ -1 +0,0 @@
-obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
diff --git a/drivers/staging/typec/TODO b/drivers/staging/typec/TODO
deleted file mode 100644
index 53fe2f7..000
--- a/drivers/staging/typec/TODO
+++ /dev/null
@@ -1,5 +0,0 @@
-tcpci:
-- Test with real hardware
-
-Please send patches to Guenter Roeck <li...@roeck-us.net> and copy
-Heikki Krogerus <heikki.kroge...@linux.intel.com>.
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index 030f88c..2b8d3c2 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -55,6 +55,13 @@ config TYPEC_TCPM
 
 if TYPEC_TCPM
 
+config TYPEC_TCPCI
+   tristate "Type-C Port Controller Interface driver"
+   depends on I2C
+   select REGMAP_I2C
+   help
+ Type-C Port Controller driver for TCPCI-compliant controller.
+
 source "drivers/usb/typec/fusb302/Kconfig"
 
 config TYPEC_WCOVE
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index 1f599a6..02758a1 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_TYPEC_WCOVE)   += typec_wcove.o
 obj-$(CONFIG_TYPEC_UCSI)   += ucsi/
 obj-$(CONFIG_TYPEC_TPS6598X)   += tps6598x.o
 obj-$(CONFIG_TYPEC)+= mux/
+obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
diff --git a/drivers/staging/typec/tcpci.c b/drivers/usb/typec/tcpci.c
similarity index 100%
rename from drivers/staging/typec/tcpci.c
rename to drivers/usb/typec/tcpci.c
diff --git a/drivers/staging/typec/tcpci.h b/drivers/usb/typec/tcpci.h
similarity index 100%
rename from drivers/staging/typec/tcpci.h
rename to drivers/usb/typec/tcpci.h
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 11/13] staging: typec: tcpci: keep the not connecting cc line open

2018-03-28 Thread Li Jun
While set polarity, we should keep the not connecting cc line to be
open.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index d5b4e4e..b58bd59 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -185,15 +185,25 @@ static int tcpci_set_polarity(struct tcpc_dev *tcpc,
  enum typec_cc_polarity polarity)
 {
struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+   unsigned int reg;
int ret;
 
-   ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
-  (polarity == TYPEC_POLARITY_CC2) ?
-  TCPC_TCPC_CTRL_ORIENTATION : 0);
+   /* Keep the disconnect cc line open */
+   ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, );
if (ret < 0)
return ret;
 
-   return 0;
+   if (polarity == TYPEC_POLARITY_CC2)
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT;
+   else
+   reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT;
+   ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+   if (ret < 0)
+   return ret;
+
+   return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+  (polarity == TYPEC_POLARITY_CC2) ?
+  TCPC_TCPC_CTRL_ORIENTATION : 0);
 }
 
 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 07/13] staging: typec: tcpci: register port before request irq

2018-03-28 Thread Li Jun
With that we can clear any pending events and the port is registered
so driver can be ready to handle typec events once we request irq.

Signed-off-by: Peter Chen <peter.c...@nxp.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 15 ---
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 4f7ad10..9e0014b 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -537,25 +537,26 @@ static int tcpci_probe(struct i2c_client *client,
if (IS_ERR(chip->data.regmap))
return PTR_ERR(chip->data.regmap);
 
+   i2c_set_clientdata(client, chip);
+
/* Disable chip interrupts before requesting irq */
err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, ,
   sizeof(u16));
if (err < 0)
return err;
 
+   chip->tcpci = tcpci_register_port(>dev, >data);
+   if (PTR_ERR_OR_ZERO(chip->tcpci))
+   return PTR_ERR(chip->tcpci);
+
err = devm_request_threaded_irq(>dev, client->irq, NULL,
_tcpci_irq,
IRQF_ONESHOT | IRQF_TRIGGER_LOW,
dev_name(>dev), chip);
if (err < 0)
-   return err;
+   tcpci_unregister_port(chip->tcpci);
 
-   chip->tcpci = tcpci_register_port(>dev, >data);
-   if (PTR_ERR_OR_ZERO(chip->tcpci))
-   return PTR_ERR(chip->tcpci);
-
-   i2c_set_clientdata(client, chip);
-   return 0;
+   return err;
 }
 
 static int tcpci_remove(struct i2c_client *client)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 09/13] typec: tcpm: add starting value for drp toggling

2018-03-28 Thread Li Jun
As DRP port autonomously toggles the Rp/Rd need a start value to
begin with, so add one parameter for it in tcpm_start_drp_toggling.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 1744041b..218c230 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -1964,15 +1964,15 @@ static int tcpm_set_charge(struct tcpm_port *port, bool 
charge)
return 0;
 }
 
-static bool tcpm_start_drp_toggling(struct tcpm_port *port)
+static bool tcpm_start_drp_toggling(struct tcpm_port *port,
+   enum typec_cc_status cc)
 {
int ret;
 
if (port->tcpc->start_drp_toggling &&
port->port_type == TYPEC_PORT_DRP) {
tcpm_log_force(port, "Start DRP toggling");
-   ret = port->tcpc->start_drp_toggling(port->tcpc,
-tcpm_rp_cc(port));
+   ret = port->tcpc->start_drp_toggling(port->tcpc, cc);
if (!ret)
return true;
}
@@ -2282,7 +2282,7 @@ static void run_state_machine(struct tcpm_port *port)
if (!port->non_pd_role_swap)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_src_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, tcpm_rp_cc(port))) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
@@ -2454,7 +2454,7 @@ static void run_state_machine(struct tcpm_port *port)
if (!port->non_pd_role_swap)
tcpm_swap_complete(port, -ENOTCONN);
tcpm_snk_detach(port);
-   if (tcpm_start_drp_toggling(port)) {
+   if (tcpm_start_drp_toggling(port, TYPEC_CC_RD)) {
tcpm_set_state(port, DRP_TOGGLING, 0);
break;
}
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 08/13] staging: typec: tcpci: enable vbus detection

2018-03-28 Thread Li Jun
TCPCI implementation may need SW to enable VBUS detection to generate
power status events.

Reviewed-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 9e0014b..d5b4e4e 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -373,6 +373,12 @@ static int tcpci_init(struct tcpc_dev *tcpc)
if (ret < 0)
return ret;
 
+   /* Enable Vbus detection */
+   ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+  TCPC_CMD_ENABLE_VBUS_DETECT);
+   if (ret < 0)
+   return ret;
+
reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 10/13] usb: typec: tcpm: set cc for drp toggling attach

2018-03-28 Thread Li Jun
In case of drp toggling, we may need set correct cc value for role control
after attach as it may never been set.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 218c230..72d4232 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -2126,6 +2126,7 @@ static void tcpm_reset_port(struct tcpm_port *port)
tcpm_set_attached_state(port, false);
port->try_src_count = 0;
port->try_snk_count = 0;
+   port->cc_req = 0;
 }
 
 static void tcpm_detach(struct tcpm_port *port)
@@ -2361,6 +2362,8 @@ static void run_state_machine(struct tcpm_port *port)
break;
 
case SRC_ATTACHED:
+   if (!port->cc_req)
+   tcpm_set_cc(port, tcpm_rp_cc(port));
ret = tcpm_src_attach(port);
tcpm_set_state(port, SRC_UNATTACHED,
   ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
@@ -2531,6 +2534,8 @@ static void run_state_machine(struct tcpm_port *port)
tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
break;
case SNK_ATTACHED:
+   if (!port->cc_req)
+   tcpm_set_cc(port, TYPEC_CC_RD);
ret = tcpm_snk_attach(port);
if (ret < 0)
tcpm_set_state(port, SNK_UNATTACHED, 0);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 03/13] staging: typec: tcpci: add compatible string for nxp ptn5110

2018-03-28 Thread Li Jun
Add nxp ptn5110 typec controller compatible string: usb-tcpci,ptn5110,
which is a standard tcpci chip with power delivery support.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index 076d97e..ed76327 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -576,6 +576,7 @@ MODULE_DEVICE_TABLE(i2c, tcpci_id);
 #ifdef CONFIG_OF
 static const struct of_device_id tcpci_of_match[] = {
{ .compatible = "usb,tcpci", },
+   { .compatible = "usb-tcpci,ptn5110", },
{},
 };
 MODULE_DEVICE_TABLE(of, tcpci_of_match);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 06/13] usb: typec: tcpm: support get typec and pd config from device properties

2018-03-28 Thread Li Jun
This patch adds support of get typec and power delivery config from
firmware description.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 140 ++-
 1 file changed, 115 insertions(+), 25 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index be9b009..1744041b 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -13,6 +13,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -3569,6 +3570,82 @@ static int tcpm_copy_vdos(u32 *dest_vdo, const u32 
*src_vdo,
return nr_vdo;
 }
 
+static int tcpm_fw_get_caps(struct tcpm_port *port,
+   struct fwnode_handle *fwnode)
+{
+   const char *cap_str;
+   int ret;
+   u32 mw;
+
+   if (!port || !fwnode)
+   return -EINVAL;
+
+   /* USB data support is optional */
+   ret = fwnode_property_read_string(fwnode, "data-type", _str);
+   if (ret == 0) {
+   port->typec_caps.data = typec_find_data_type(cap_str);
+   if (port->typec_caps.data < 0)
+   return -EINVAL;
+   }
+
+   ret = fwnode_property_read_string(fwnode, "power-type", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.type = typec_find_power_type(cap_str);
+   if (port->typec_caps.type < 0)
+   return -EINVAL;
+   port->port_type = port->typec_caps.type;
+
+   if (port->port_type == TYPEC_PORT_SNK)
+   goto sink;
+
+   /* Get soruce pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+port->src_pdo, port->nr_src_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
+   port->nr_src_pdo))
+   return -EINVAL;
+
+   if (port->port_type == TYPEC_PORT_SRC)
+   return 0;
+
+   /* Get the preferred power role for DRP */
+   ret = fwnode_property_read_string(fwnode, "try-power-role", _str);
+   if (ret < 0)
+   return ret;
+
+   port->typec_caps.prefer_role = typec_find_preferred_role(cap_str);
+   if (port->typec_caps.prefer_role < 0)
+   return -EINVAL;
+sink:
+   /* Get sink pdos */
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+NULL, 0);
+   if (ret <= 0)
+   return -EINVAL;
+
+   port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
+   ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+port->snk_pdo, port->nr_snk_pdo);
+   if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
+   port->nr_snk_pdo))
+   return -EINVAL;
+
+   if (fwnode_property_read_u32(fwnode, "op-sink-microwatt-hours",
+) < 0)
+   return -EINVAL;
+   port->operating_snk_mw = mw / 1000;
+
+   return 0;
+}
+
 int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
unsigned int nr_pdo)
 {
@@ -3643,12 +3720,39 @@ static int nr_type_pdos(const u32 *pdo, unsigned int 
nr_pdo,
return count;
 }
 
+static int tcpm_copy_caps(struct tcpm_port *port,
+ const struct tcpc_config *tcfg)
+{
+   if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
+   tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
+   return -EINVAL;
+
+   port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
+ tcfg->nr_src_pdo);
+   port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
+ tcfg->nr_snk_pdo);
+
+   port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
+ tcfg->nr_snk_vdo);
+
+   port->max_snk_mv = tcfg->max_snk_mv;
+   port->max_snk_ma = tcfg->max_snk_ma;
+   port->max_snk_mw = tcfg->max_snk_mw;
+   port->operating_snk_mw = tcfg->operating_snk_mw;
+
+   port->typec_caps.prefer_role = tcfg->default_role;
+   port->typec_caps.type = tcfg->type;
+   port->typec_caps.data = tcfg->data;
+
+   return 0;
+}
+
 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc

[PATCH v4 02/13] dt-bindings: usb: add documentation for typec port controller(TCPCI)

2018-03-28 Thread Li Jun
TCPCI stands for typec port controller interface, its implementation
has full typec port control with power delivery support, it's a
standard i2c slave with GPIO input as irq interface, detail see spec
"Universal Serial Bus Type-C Port Controller Interface Specification
Revision 1.0, Version 1.1"

Signed-off-by: Li Jun <jun...@nxp.com>
---
 .../devicetree/bindings/usb/typec-tcpci.txt| 33 ++
 1 file changed, 33 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/typec-tcpci.txt 
b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
new file mode 100644
index 000..7a7a8e0
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/typec-tcpci.txt
@@ -0,0 +1,33 @@
+TCPCI(Typec port cotroller interface) binding
+-
+
+Required properties:
+- compatible:   should be "usb-tcpci,chip-specific-string".
+- reg:  the i2c slave address of typec port controller device.
+- interrupt-parent: the phandle to the interrupt controller which provides
+the interrupt.
+- interrupts:   interrupt specification for tcpci alert.
+
+Required sub-node:
+- connector: The "usb-c-connector" attached to the tcpci chip, the bindings
+  of connector node are specified in
+  Documentation/devicetree/bindings/connector/usb-connector.txt
+
+Example:
+
+ptn5110@50 {
+   compatible = "usb-tcpci,ptn5110";
+   reg = <0x50>;
+   interrupt-parent = <>;
+   interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   port-type = "dual";
+   try-power-role = "sink"
+   source-pdos = <0x380190c8>;
+   sink-pdos = <0x380190c8 0x3802d0c8>;
+   op-sink-microwatt-hours = <900>;
+   };
+};
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 05/13] usb: typec: add API to get typec basic port power and data config

2018-03-28 Thread Li Jun
This patch adds 3 APIs to get the typec port power and data type,
and preferred power role by its name string.

Suggested-by: Heikki Krogerus <heikki.kroge...@linux.intel.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/class.c | 52 +++
 include/linux/usb/typec.h |  3 +++
 2 files changed, 55 insertions(+)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 53df10d..5981e18 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -9,6 +9,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -802,6 +803,12 @@ static const char * const typec_port_types[] = {
[TYPEC_PORT_DRP] = "dual",
 };
 
+static const char * const typec_data_types[] = {
+   [TYPEC_PORT_DFP] = "host",
+   [TYPEC_PORT_UFP] = "device",
+   [TYPEC_PORT_DRD] = "dual",
+};
+
 static const char * const typec_port_types_drp[] = {
[TYPEC_PORT_SRC] = "dual [source] sink",
[TYPEC_PORT_SNK] = "dual source [sink]",
@@ -1252,6 +1259,51 @@ void typec_set_pwr_opmode(struct typec_port *port,
 }
 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
 
+/**
+ * typec_find_power_type - Get the typec port power type
+ * @name: port type string
+ *
+ * This routine is used to find the typec_port_type by its string name.
+ *
+ * Returns typec_port_type if success, otherwise negative error code.
+ */
+int typec_find_power_type(const char *name)
+{
+   return match_string(typec_port_types, ARRAY_SIZE(typec_port_types),
+   name);
+}
+EXPORT_SYMBOL_GPL(typec_find_power_type);
+
+/**
+ * typec_find_preferred_role - Find the typec drp port preferred power role
+ * @name: power role string
+ *
+ * This routine is used to find the typec_role by its string name of
+ * preferred power role(Try.SRC or Try.SNK).
+ *
+ * Returns typec_role if success, otherwise negative error code.
+ */
+int typec_find_preferred_role(const char *name)
+{
+   return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
+}
+EXPORT_SYMBOL_GPL(typec_find_preferred_role);
+
+/**
+ * typec_find_data_type - Get the typec port data capability
+ * @name: data type string
+ *
+ * This routine is used to find the typec_port_data by its string name.
+ *
+ * Returns typec_port_data if success, otherwise negative error code.
+ */
+int typec_find_data_type(const char *name)
+{
+   return match_string(typec_data_types, ARRAY_SIZE(typec_data_types),
+   name);
+}
+EXPORT_SYMBOL_GPL(typec_find_data_type);
+
 /* -- */
 /* API for Multiplexer/DeMultiplexer Switches */
 
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 672b39b..00c93e7 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -267,4 +267,7 @@ int typec_set_orientation(struct typec_port *port,
  enum typec_orientation orientation);
 int typec_set_mode(struct typec_port *port, int mode);
 
+int typec_find_power_type(const char *name);
+int typec_find_preferred_role(const char *name);
+int typec_find_data_type(const char *name);
 #endif /* __LINUX_USB_TYPEC_H */
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 04/13] usb: typec: add fwnode to tcpc

2018-03-28 Thread Li Jun
Add fwnode handle to get the fwnode so we can get typec configs
it contains.

Suggested-by: Heikki Krogerus <heikki.kroge...@linux.intel.com>
Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/staging/typec/tcpci.c | 14 +++---
 drivers/usb/typec/tcpm.c  |  1 +
 include/linux/usb/tcpm.h  |  2 ++
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
index ed76327..4f7ad10 100644
--- a/drivers/staging/typec/tcpci.c
+++ b/drivers/staging/typec/tcpci.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -463,17 +464,16 @@ static const struct regmap_config tcpci_regmap_config = {
.max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
 };
 
-static const struct tcpc_config tcpci_tcpc_config = {
-   .type = TYPEC_PORT_DFP,
-   .default_role = TYPEC_SINK,
-};
-
 static int tcpci_parse_config(struct tcpci *tcpci)
 {
tcpci->controls_vbus = true; /* XXX */
 
-   /* TODO: Populate struct tcpc_config from ACPI/device-tree */
-   tcpci->tcpc.config = _tcpc_config;
+   tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
+"connector");
+   if (!tcpci->tcpc.fwnode) {
+   dev_err(tcpci->dev, "Can't find connector node.\n");
+   return -EINVAL;
+   }
 
return 0;
 }
diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index a7b82d9..be9b009 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -3709,6 +3709,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
else
port->try_role = TYPEC_NO_PREFERRED_ROLE;
 
+   port->typec_caps.fwnode = tcpc->fwnode;
port->typec_caps.prefer_role = tcpc->config->default_role;
port->typec_caps.type = tcpc->config->type;
port->typec_caps.data = tcpc->config->data;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index f0d839d..e2e2db3 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -115,6 +115,7 @@ enum tcpc_mux_mode {
 /**
  * struct tcpc_dev - Port configuration and callback functions
  * @config:Pointer to port configuration
+ * @fwnode:Pointer to port fwnode
  * @get_vbus:  Called to read current VBUS state
  * @get_current_limit:
  * Optional; called by the tcpm core when configured as a snk
@@ -143,6 +144,7 @@ enum tcpc_mux_mode {
  */
 struct tcpc_dev {
const struct tcpc_config *config;
+   struct fwnode_handle *fwnode;
 
int (*init)(struct tcpc_dev *dev);
int (*get_vbus)(struct tcpc_dev *dev);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 01/13] dt-bindings: connector: add properties for typec

2018-03-28 Thread Li Jun
Add bingdings supported by current typec driver, so user can pass
all those properties via dt.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 .../bindings/connector/usb-connector.txt   | 39 ++
 1 file changed, 39 insertions(+)

diff --git a/Documentation/devicetree/bindings/connector/usb-connector.txt 
b/Documentation/devicetree/bindings/connector/usb-connector.txt
index e1463f1..922f22b 100644
--- a/Documentation/devicetree/bindings/connector/usb-connector.txt
+++ b/Documentation/devicetree/bindings/connector/usb-connector.txt
@@ -15,6 +15,29 @@ Optional properties:
 - type: size of the connector, should be specified in case of USB-A, USB-B
   non-fullsize connectors: "mini", "micro".
 
+Optional properties for usb-c-connector:
+- power-type: should be one of "source", "sink" or "dual"(DRP) if typec
+  connector has power support.
+- try-power-role: preferred power role if "dual"(DRP) can support Try.SNK
+  or Try.SRC, should be "sink" for Try.SNK or "source" for Try.SRC.
+- data-type: should be one of "host", "device", "dual"(DRD) if typec
+  connector supports USB data.
+
+Required properties for usb-c-connector with power delivery support:
+- source-pdos: An array of u32 with each entry providing supported power
+  source data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.2
+  Source_Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power source and power dual role.
+- sink-pdos: An array of u32 with each entry providing supported power
+  sink data object(PDO), the detailed bit definitions of PDO can be found
+  in "Universal Serial Bus Power Delivery Specification" chapter 6.4.1.3
+  Sink Capabilities Message, the order of each entry(PDO) should follow
+  the PD spec chapter 6.4.1. Required for power sink and power dual role.
+- op-sink-microwatt-hours: Sink required operating power in micro
+  watt-hours, if source offered power is less then it, Capability Mismatch
+  is set, required for power sink and power dual role.
+
 Required nodes:
 - any data bus to the connector should be modeled using the OF graph bindings
   specified in bindings/graph.txt, unless the bus is between parent node and
@@ -73,3 +96,19 @@ ccic: s2mm005@33 {
};
};
 };
+
+3. USB-C connector attached to a typec port controller(ptn5110), which has
+power delivery support and enables drp.
+
+typec: ptn5110@50 {
+   ...
+   usb_con: connector {
+   compatible = "usb-c-connector";
+   label = "USB-C";
+   power-type = "dual";
+   try-power-role = "sink";
+   source-pdos = <0x380190c8>;
+   sink-pdos = <0x380190c8 0x3802d0c8>;
+   op-sink-microwatt-hours = <900>;
+   };
+};
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 00/13] staging: typec: tcpci: move out of staging

2018-03-28 Thread Li Jun
This patch set attempts to move the tcpci driver out of staging by fix
some tcpci driver issues and define typec and power delivery device
properties, the changes are verified on NXP PTN5110, which is a standard
tcpci typec port controller device with power delivery support, tested
power source and sink with drp config.

Changes for v4:
- Remove max-sink-* properties as we will purge max_snk_* in tcpm,
  see patch set[4].
- Get typec power and data type value via name string(patch 5).
- Move finding typec and pd properties code from tcpci to tcpm(patch 6)
- Add a compatible string for nxp ptn5110 typec controller in tcpci driver.
  (patch 3)
- Add set cc for drp toggling without try.src/snk in tcpm(patch 10), then
  patch 11 can only update CCx bits for keep disconnect cc line open.
- Update op-sink-microwatt-hours example value to be the right value in
  micorwatts, and accordingly divide 1000 to get its miliwatts value
  in patch 6.
- Add Guenter's Reviewed-by for patch(8/9/12)

[4] https://www.spinics.net/lists/linux-usb/msg167261.html

Changes for v3:
- Use 2 properties to separate power and data capability of typec port:
  "power-type" and "data-type", this is based on Heikki's typec class code
  change[2]. use "try-power-role" to present if the typec port can support
  Try.SNK or Try.SRC.
- 4 sink properties(max_sink_mv/ma/mw and op_sink_mw) are kept because the
  counterpart code is back, see revert patch[3], meanwhile I post a patch
  to fix the reported problem of current source pdo select machinism(which
  completely ignored those 4 sink settings), to see if we can keep current
  code, once it was discussed and have conclusion I can update this
  accordingly.
- Use fwnode to get the connector node for dt setting parse.

Main changes for v2:
- Typec properties are based on general usb connector bindings[1] proposed
  by Andrzej Hajda, use the standard unit suffixes as defined in
  property-units.txt.
- Add 2 infra APIs to get power sink and source config from dt.
- Don't change the set_cc api, to keep the uncontacted cc line open,
  set cc1/cc2 to be open in tcpci driver when set polarity.
- Directly enable vbus detect in tcpci driver rather than add a API.
- Details added in each patch.

[1] https://patchwork.kernel.org/patch/10231447/
[2] https://patchwork.kernel.org/patch/10276483/
[3] https://www.spinics.net/lists/linux-usb/msg166366.html

Li Jun (13):
  dt-bindings: connector: add properties for typec
  dt-bindings: usb: add documentation for typec port controller(TCPCI)
  staging: typec: tcpci: add compatible string for nxp ptn5110
  usb: typec: add fwnode to tcpc
  usb: typec: add API to get typec basic port power and data config
  usb: typec: tcpm: support get typec and pd config from device
properties
  staging: typec: tcpci: register port before request irq
  staging: typec: tcpci: enable vbus detection
  typec: tcpm: add starting value for drp toggling
  usb: typec: tcpm: set cc for drp toggling attach
  staging: typec: tcpci: keep the not connecting cc line open
  staging: typec: tcpci: Only touch target bit when enable vconn
  staging: typec: tcpci: move tcpci driver out of staging

 .../bindings/connector/usb-connector.txt   |  39 ++
 .../devicetree/bindings/usb/typec-tcpci.txt|  33 ++
 drivers/staging/Kconfig|   2 -
 drivers/staging/Makefile   |   1 -
 drivers/staging/typec/Kconfig  |  14 -
 drivers/staging/typec/Makefile |   1 -
 drivers/staging/typec/TODO |   5 -
 drivers/staging/typec/tcpci.c  | 596 
 drivers/staging/typec/tcpci.h  | 138 -
 drivers/usb/typec/Kconfig  |   7 +
 drivers/usb/typec/Makefile |   1 +
 drivers/usb/typec/class.c  |  52 ++
 drivers/usb/typec/tcpci.c  | 611 +
 drivers/usb/typec/tcpci.h  | 138 +
 drivers/usb/typec/tcpm.c   | 156 +-
 include/linux/usb/tcpm.h   |   2 +
 include/linux/usb/typec.h  |   3 +
 17 files changed, 1012 insertions(+), 787 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/typec-tcpci.txt
 delete mode 100644 drivers/staging/typec/Kconfig
 delete mode 100644 drivers/staging/typec/Makefile
 delete mode 100644 drivers/staging/typec/TODO
 delete mode 100644 drivers/staging/typec/tcpci.c
 delete mode 100644 drivers/staging/typec/tcpci.h
 create mode 100644 drivers/usb/typec/tcpci.c
 create mode 100644 drivers/usb/typec/tcpci.h

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 5/5] usb: typec: tcpm: remove max_snk_mv/ma/mw

2018-03-23 Thread Li Jun
Since there is no user of max_snk_*, so we can remove them from tcpm.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 12 
 include/linux/usb/tcpm.h |  9 -
 2 files changed, 21 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 50a1979..72e7a65 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -260,9 +260,6 @@ struct tcpm_port {
u32 snk_vdo[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
/* Requested current / voltage */
@@ -3600,9 +3597,6 @@ EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
 
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw)
 {
if (tcpm_validate_caps(port, pdo, nr_pdo))
@@ -3610,9 +3604,6 @@ int tcpm_update_sink_capabilities(struct tcpm_port *port, 
const u32 *pdo,
 
mutex_lock(>lock);
port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
-   port->max_snk_mv = max_snk_mv;
-   port->max_snk_ma = max_snk_ma;
-   port->max_snk_mw = max_snk_mw;
port->operating_snk_mw = operating_snk_mw;
 
switch (port->state) {
@@ -3700,9 +3691,6 @@ struct tcpm_port *tcpm_register_port(struct device *dev, 
struct tcpc_dev *tcpc)
port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcpc->config->snk_vdo,
  tcpc->config->nr_snk_vdo);
 
-   port->max_snk_mv = tcpc->config->max_snk_mv;
-   port->max_snk_ma = tcpc->config->max_snk_ma;
-   port->max_snk_mw = tcpc->config->max_snk_mw;
port->operating_snk_mw = tcpc->config->operating_snk_mw;
if (!tcpc->config->try_role_hw)
port->try_role = tcpc->config->default_role;
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index f0d839d..f5bda9a 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -62,9 +62,6 @@ enum tcpm_transmit_type {
  * @snk_pdo:   PDO parameters sent to partner as response to
  * PD_CTRL_GET_SINK_CAP message
  * @nr_snk_pdo:Number of entries in @snk_pdo
- * @max_snk_mv:Maximum acceptable sink voltage in mV
- * @max_snk_ma:Maximum sink current in mA
- * @max_snk_mw:Maximum required sink power in mW
  * @operating_snk_mw:
  * Required operating sink power in mW
  * @type:  Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
@@ -85,9 +82,6 @@ struct tcpc_config {
const u32 *snk_vdo;
unsigned int nr_snk_vdo;
 
-   unsigned int max_snk_mv;
-   unsigned int max_snk_ma;
-   unsigned int max_snk_mw;
unsigned int operating_snk_mw;
 
enum typec_port_type type;
@@ -174,9 +168,6 @@ int tcpm_update_source_capabilities(struct tcpm_port *port, 
const u32 *pdo,
unsigned int nr_pdo);
 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  unsigned int nr_pdo,
- unsigned int max_snk_mv,
- unsigned int max_snk_ma,
- unsigned int max_snk_mw,
  unsigned int operating_snk_mw);
 
 void tcpm_vbus_change(struct tcpm_port *port);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 4/5] usb: typec: wcove: remove max_snk_* for sink config

2018-03-23 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/typec_wcove.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/typec/typec_wcove.c b/drivers/usb/typec/typec_wcove.c
index 19cca7f..39cff11 100644
--- a/drivers/usb/typec/typec_wcove.c
+++ b/drivers/usb/typec/typec_wcove.c
@@ -558,6 +558,7 @@ static const u32 src_pdo[] = {
 static const u32 snk_pdo[] = {
PDO_FIXED(5000, 500, PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP |
  PDO_FIXED_USB_COMM),
+   PDO_VAR(5000, 12000, 3000),
 };
 
 static struct tcpc_config wcove_typec_config = {
@@ -566,9 +567,6 @@ static struct tcpc_config wcove_typec_config = {
.snk_pdo = snk_pdo,
.nr_snk_pdo = ARRAY_SIZE(snk_pdo),
 
-   .max_snk_mv = 12000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 36000,
.operating_snk_mw = 15000,
 
.type = TYPEC_PORT_DRP,
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 3/5] dt-bindings: usb: fusb302: remove max-sink-* properties

2018-03-23 Thread Li Jun
Remove max-sink-* properties since they are deprecated.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 Documentation/devicetree/bindings/usb/fcs,fusb302.txt | 6 --
 1 file changed, 6 deletions(-)

diff --git a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt 
b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
index 472facf..6087dc7 100644
--- a/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
+++ b/Documentation/devicetree/bindings/usb/fcs,fusb302.txt
@@ -6,12 +6,6 @@ Required properties :
 - interrupts : Interrupt specifier
 
 Optional properties :
-- fcs,max-sink-microvolt : Maximum voltage to negotiate when configured as sink
-- fcs,max-sink-microamp  : Maximum current to negotiate when configured as sink
-- fcs,max-sink-microwatt : Maximum power to negotiate when configured as sink
-  If this is less then max-sink-microvolt *
-  max-sink-microamp then the configured current will
-  be clamped.
 - fcs,operating-sink-microwatt :
   Minimum amount of power accepted from a sink
   when negotiating
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 2/5] usb: typec: fusb302: remove max_snk_* for sink config

2018-03-23 Thread Li Jun
Since max_snk_* is to be deprecated, so remove max_snk_* by adding a
variable PDO for sink config.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/fusb302/fusb302.c | 51 +++--
 1 file changed, 37 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/typec/fusb302/fusb302.c 
b/drivers/usb/typec/fusb302/fusb302.c
index 7036171..db4d9cd 100644
--- a/drivers/usb/typec/fusb302/fusb302.c
+++ b/drivers/usb/typec/fusb302/fusb302.c
@@ -120,6 +120,7 @@ struct fusb302_chip {
enum typec_cc_polarity cc_polarity;
enum typec_cc_status cc1;
enum typec_cc_status cc2;
+   u32 snk_pdo[PDO_MAX_OBJECTS];
 
 #ifdef CONFIG_DEBUG_FS
struct dentry *dentry;
@@ -1212,11 +1213,6 @@ static const u32 snk_pdo[] = {
 static const struct tcpc_config fusb302_tcpc_config = {
.src_pdo = src_pdo,
.nr_src_pdo = ARRAY_SIZE(src_pdo),
-   .snk_pdo = snk_pdo,
-   .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
-   .max_snk_mv = 5000,
-   .max_snk_ma = 3000,
-   .max_snk_mw = 15000,
.operating_snk_mw = 2500,
.type = TYPEC_PORT_DRP,
.data = TYPEC_PORT_DRD,
@@ -1756,6 +1752,38 @@ static int init_gpio(struct fusb302_chip *chip)
return 0;
 }
 
+static int fusb302_composite_snk_pdo_array(struct fusb302_chip *chip)
+{
+   struct device *dev = chip->dev;
+   u32 mv, ma, mw, min_mv;
+   unsigned int i;
+
+   /* Copy the static snk pdo */
+   for (i = 0; i < ARRAY_SIZE(snk_pdo); i++)
+   chip->snk_pdo[i] = snk_pdo[i];
+
+   if (device_property_read_u32(dev, "fcs,max-sink-microvolt", ) ||
+   device_property_read_u32(dev, "fcs,max-sink-microamp", ) ||
+   device_property_read_u32(dev, "fcs,max-sink-microwatt", ))
+   return i;
+
+   mv = mv / 1000;
+   ma = ma / 1000;
+   mw = mw / 1000;
+
+   min_mv = 1000 * chip->tcpc_config.operating_snk_mw / ma;
+   if (pdo_type(snk_pdo[i-1] == PDO_TYPE_FIXED))
+   min_mv = max(min_mv, pdo_fixed_voltage(snk_pdo[i-1]));
+   else
+   min_mv = max(min_mv, pdo_max_voltage(snk_pdo[i-1]));
+   ma = min(ma, 1000 * mw / min_mv);
+
+   /* Insert the new pdo to the end */
+   chip->snk_pdo[i] = PDO_VAR(min_mv, mv, ma);
+
+   return i+1;
+}
+
 static int fusb302_probe(struct i2c_client *client,
 const struct i2c_device_id *id)
 {
@@ -1784,18 +1812,13 @@ static int fusb302_probe(struct i2c_client *client,
chip->tcpc_dev.config = >tcpc_config;
mutex_init(>lock);
 
-   if (!device_property_read_u32(dev, "fcs,max-sink-microvolt", ))
-   chip->tcpc_config.max_snk_mv = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microamp", ))
-   chip->tcpc_config.max_snk_ma = v / 1000;
-
-   if (!device_property_read_u32(dev, "fcs,max-sink-microwatt", ))
-   chip->tcpc_config.max_snk_mw = v / 1000;
-
if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", ))
chip->tcpc_config.operating_snk_mw = v / 1000;
 
+   /* Composite sink PDO */
+   chip->tcpc_config.nr_snk_pdo = fusb302_composite_snk_pdo_array(chip);
+   chip->tcpc_config.snk_pdo = chip->snk_pdo;
+
/*
 * Devicetree platforms should get extcon via phandle (not yet
 * supported). On ACPI platforms, we get the name from a device prop.
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 0/5] usb: typec: remove max_snk_mv/ma/mw

2018-03-23 Thread Li Jun
This patch set is to remove max_snk_mv/ma/mw configs, as we should
define the sink capability by sink PDOs, the first patch update
the source PDO match policy by compare the voltage range between
source and sink PDOs no matter what type they are, the following
patchs remove those 3 variables from 2 existing users by adding
a variable PDO, then finial patch remove the max_snk_* from tcpm.

Changes for v2:
- rebase the 1st patch to be based on commit 6f566af34628
  ("Revert "typec: tcpm: Only request matching pdos"").
- Convert the device properties passing max_snk_* to be a
  variable sink pdo for fusb302.
 
Li Jun (5):
  usb: typec: tcpm: pdo matching optimization
  usb: typec: fusb302:  remove max_snk_* for sink config
  dt-bindings: usb: fusb302: remove max-sink-* properties
  usb: typec: wcove: remove max_snk_* for sink config
  usb: typec: tcpm: remove max_snk_mv/ma/mw

 .../devicetree/bindings/usb/fcs,fusb302.txt|   6 -
 drivers/usb/typec/fusb302/fusb302.c|  51 +---
 drivers/usb/typec/tcpm.c   | 139 +
 drivers/usb/typec/typec_wcove.c|   4 +-
 include/linux/usb/tcpm.h   |   9 --
 5 files changed, 128 insertions(+), 81 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 1/5] usb: typec: tcpm: pdo matching optimization

2018-03-23 Thread Li Jun
This patch is a combination of commit 57e6f0d7b804
("typec: tcpm: Only request matching pdos") and source
pdo selection optimization based on it, instead of only
compare between the same pdo type of sink and source,
we should check source pdo voltage range is within the
voltage range of one sink pdo.

Signed-off-by: Li Jun <jun...@nxp.com>
---
 drivers/usb/typec/tcpm.c | 127 +--
 1 file changed, 90 insertions(+), 37 deletions(-)

diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
index 677d121..50a1979 100644
--- a/drivers/usb/typec/tcpm.c
+++ b/drivers/usb/typec/tcpm.c
@@ -254,6 +254,9 @@ struct tcpm_port {
unsigned int nr_src_pdo;
u32 snk_pdo[PDO_MAX_OBJECTS];
unsigned int nr_snk_pdo;
+   unsigned int nr_fixed; /* number of fixed sink PDOs */
+   unsigned int nr_var; /* number of variable sink PDOs */
+   unsigned int nr_batt; /* number of battery sink PDOs */
u32 snk_vdo[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo;
 
@@ -1772,40 +1775,65 @@ static int tcpm_pd_check_request(struct tcpm_port *port)
return 0;
 }
 
-static int tcpm_pd_select_pdo(struct tcpm_port *port)
+#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
+#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
+
+static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
+ int *src_pdo)
 {
-   unsigned int i, max_mw = 0, max_mv = 0;
+   unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
+max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
+min_snk_mv = 0;
int ret = -EINVAL;
 
/*
-* Select the source PDO providing the most power while staying within
-* the board's voltage limits. Prefer PDO providing exp
+* Select the source PDO providing the most power which has a
+* matchig sink cap.
 */
for (i = 0; i < port->nr_source_caps; i++) {
u32 pdo = port->source_caps[i];
enum pd_pdo_type type = pdo_type(pdo);
-   unsigned int mv, ma, mw;
 
-   if (type == PDO_TYPE_FIXED)
-   mv = pdo_fixed_voltage(pdo);
-   else
-   mv = pdo_min_voltage(pdo);
+   if (type == PDO_TYPE_FIXED) {
+   max_src_mv = pdo_fixed_voltage(pdo);
+   min_src_mv = max_src_mv;
+   } else {
+   max_src_mv = pdo_max_voltage(pdo);
+   min_src_mv = pdo_min_voltage(pdo);
+   }
 
if (type == PDO_TYPE_BATT) {
-   mw = pdo_max_power(pdo);
+   src_mw = pdo_max_power(pdo);
} else {
-   ma = min(pdo_max_current(pdo),
-port->max_snk_ma);
-   mw = ma * mv / 1000;
+   src_ma = pdo_max_current(pdo);
+   src_mw = src_ma * min_src_mv / 1000;
}
 
-   /* Perfer higher voltages if available */
-   if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
-   mv <= port->max_snk_mv) {
-   ret = i;
-   max_mw = mw;
-   max_mv = mv;
+   for (j = 0; j < port->nr_snk_pdo; j++) {
+   pdo = port->snk_pdo[j];
+
+   if (pdo_type(pdo) == PDO_TYPE_FIXED) {
+   min_snk_mv = pdo_fixed_voltage(pdo);
+   max_snk_mv = pdo_fixed_voltage(pdo);
+   } else {
+   min_snk_mv = pdo_min_voltage(pdo);
+   max_snk_mv = pdo_max_voltage(pdo);
+   }
+
+   if (max_src_mv <= max_snk_mv &&
+   min_src_mv >= min_snk_mv) {
+   /* Prefer higher voltages if available */
+   if ((src_mw == max_mw && min_src_mv > max_mv) ||
+   src_mw > max_mw) {
+   *src_pdo = i;
+   *sink_pdo = j;
+   max_mw = src_mw;
+   max_mv = min_src_mv;
+   }
+   break;
+   }
}
+
}
 
return ret;
@@ -1816,13 +1844,14 @@ static int tcpm_pd_build_request(struct tcpm_port 
*port, u32 *rdo)
unsigned int mv, ma, mw, flags;
unsigned int max_ma, max_mw;
enum pd_pdo_type type;
-   int index;
-   u32 pdo;
+   int src_pdo_index

  1   2   3   4   5   6   7   8   >