drivers: usb: Import Google's Type-C driver
authordeadman96385 <seanhoyt963@gmail.com>
Thu, 22 Jul 2021 00:19:50 +0000 (19:19 -0500)
committerNolen Johnson <johnsonnolen@gmail.com>
Tue, 21 Dec 2021 18:35:24 +0000 (13:35 -0500)
Change-Id: Ib224baf7f88334a07f1f1d106f4d0af36b59c72d

28 files changed:
drivers/usb/Kconfig
drivers/usb/Makefile
drivers/usb/typec/Kconfig [new file with mode: 0644]
drivers/usb/typec/Makefile [new file with mode: 0644]
drivers/usb/typec/bus.c [new file with mode: 0644]
drivers/usb/typec/bus.h [new file with mode: 0644]
drivers/usb/typec/class.c [new file with mode: 0644]
drivers/usb/typec/fusb302/Kconfig [new file with mode: 0644]
drivers/usb/typec/fusb302/Makefile [new file with mode: 0644]
drivers/usb/typec/fusb302/fusb302.c [new file with mode: 0644]
drivers/usb/typec/fusb302/fusb302_reg.h [new file with mode: 0644]
drivers/usb/typec/mux.c [new file with mode: 0644]
drivers/usb/typec/tcpci.c [new file with mode: 0644]
drivers/usb/typec/tcpci.h [new file with mode: 0644]
drivers/usb/typec/tcpci_rt1711h.c [new file with mode: 0644]
drivers/usb/typec/tcpm.c [new file with mode: 0644]
include/dt-bindings/usb/pd.h [new file with mode: 0644]
include/linux/mod_devicetable.h
include/linux/power_supply.h
include/linux/usb/pd.h [new file with mode: 0644]
include/linux/usb/pd_ado.h [new file with mode: 0644]
include/linux/usb/pd_bdo.h [new file with mode: 0644]
include/linux/usb/pd_ext_sdb.h [new file with mode: 0644]
include/linux/usb/pd_vdo.h [new file with mode: 0644]
include/linux/usb/tcpm.h [new file with mode: 0644]
include/linux/usb/typec.h [new file with mode: 0644]
include/linux/usb/typec_altmode.h [new file with mode: 0644]
include/linux/usb/typec_mux.h [new file with mode: 0644]

index 5d212d8cd91930e8bba25f7f81ad4a91ef368a6b..59deeeb1b24e1af57346db3404b00781eca04557 100644 (file)
@@ -158,6 +158,8 @@ source "drivers/usb/phy/Kconfig"
 
 source "drivers/usb/gadget/Kconfig"
 
+source "drivers/usb/typec/Kconfig"
+
 source "drivers/usb/roles/Kconfig"
 
 config USB_LED_TRIG
index cad191af04754832beca1d53b2e3871a1f8ee00e..35380b1d8d9c9edaefd596421b7f595e43d1de77 100644 (file)
@@ -62,4 +62,6 @@ obj-$(CONFIG_USB_COMMON)      += common/
 
 obj-$(CONFIG_USBIP_CORE)       += usbip/
 
+obj-y          += typec/
+
 obj-y  += roles/
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
new file mode 100644 (file)
index 0000000..aebd075
--- /dev/null
@@ -0,0 +1,105 @@
+
+menuconfig TYPEC
+       tristate "USB Type-C Support"
+       help
+         USB Type-C Specification defines a cable and connector for USB where
+         only one type of plug is supported on both ends, i.e. there will not
+         be Type-A plug on one end of the cable and Type-B plug on the other.
+         Determination of the host-to-device relationship happens through a
+         specific Configuration Channel (CC) which goes through the USB Type-C
+         cable. The Configuration Channel may also be used to detect optional
+         Accessory Modes - Analog Audio and Debug - and if USB Power Delivery
+         is supported, the Alternate Modes, where the connector is used for
+         something else then USB communication.
+
+         USB Power Delivery Specification defines a protocol that can be used
+         to negotiate the voltage and current levels with the connected
+         partners. USB Power Delivery allows higher voltages then the normal
+         5V, up to 20V, and current up to 5A over the cable. The USB Power
+         Delivery protocol is also used to negotiate the optional Alternate
+         Modes when they are supported. USB Power Delivery does not depend on
+         USB Type-C connector, however it is mostly used together with USB
+         Type-C connectors.
+
+         USB Type-C and USB Power Delivery Specifications define a set of state
+         machines that need to be implemented in either software or firmware.
+         Simple USB Type-C PHYs, for example USB Type-C Port Controller
+         Interface Specification compliant "Port Controllers" need the state
+         machines to be handled in the OS, but stand-alone USB Type-C and Power
+         Delivery controllers handle the state machines inside their firmware.
+         The USB Type-C and Power Delivery controllers usually function
+         autonomously, and do not necessarily require drivers.
+
+         Enable this configurations option if you have USB Type-C connectors on
+         your system and 1) you know your USB Type-C hardware requires OS
+         control (a driver) to function, or 2) if you need to be able to read
+         the status of the USB Type-C ports in your system, or 3) if you need
+         to be able to swap the power role (decide are you supplying or
+         consuming power over the cable) or data role (host or device) when
+         both roles are supported.
+
+         For more information, see the kernel documentation for USB Type-C
+         Connector Class API (Documentation/driver-api/usb/typec.rst)
+         <https://www.kernel.org/doc/html/latest/driver-api/usb/typec.html>
+         and ABI (Documentation/ABI/testing/sysfs-class-typec).
+
+if TYPEC
+
+config TYPEC_TCPM
+       tristate "USB Type-C Port Controller Manager"
+       depends on USB
+       select USB_ROLE_SWITCH
+       select POWER_SUPPLY
+       help
+         The Type-C Port Controller Manager provides a USB PD and USB Type-C
+         state machine for use with Type-C Port Controllers.
+
+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"
+       depends on I2C
+       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
+       tristate "Intel WhiskeyCove PMIC USB Type-C PHY driver"
+       depends on ACPI
+       depends on INTEL_SOC_PMIC
+       depends on INTEL_PMC_IPC
+       depends on BXT_WC_PMIC_OPREGION
+       help
+         This driver adds support for USB Type-C detection on Intel Broxton
+         platforms that have Intel Whiskey Cove PMIC. The driver can detect the
+         role and cable orientation.
+
+         To compile this driver as module, choose M here: the module will be
+         called typec_wcove
+
+endif # TYPEC_TCPM
+
+
+config TYPEC_TPS6598X
+       tristate "TI TPS6598x USB Power Delivery controller driver"
+       depends on I2C
+       help
+         Say Y or M here if your system has TI TPS65982 or TPS65983 USB Power
+         Delivery controller.
+
+         If you choose to build this driver as a dynamically linked module, the
+         module will be called tps6598x.ko.
+
+
+endif # TYPEC
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
new file mode 100644 (file)
index 0000000..73fb5d6
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-y          += typec.o
+typec-y                                := class.o mux.o bus.o
+obj-y  += tcpm.o
+obj-y                          += fusb302/
+obj-$(CONFIG_TYPEC_TCPCI)      += tcpci.o
+obj-$(CONFIG_TYPEC_RT1711H)    += tcpci_rt1711h.o
diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
new file mode 100644 (file)
index 0000000..76299b6
--- /dev/null
@@ -0,0 +1,403 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
+ * Bus for USB Type-C Alternate Modes
+ *
+ * Copyright (C) 2018 Intel Corporation
+ * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
+ */
+
+#include <linux/usb/pd_vdo.h>
+
+#include "bus.h"
+
+static inline int typec_altmode_set_mux(struct altmode *alt, u8 state)
+{
+       return alt->mux ? alt->mux->set(alt->mux, state) : 0;
+}
+
+static int typec_altmode_set_state(struct typec_altmode *adev, int state)
+{
+       bool is_port = is_typec_port(adev->dev.parent);
+       struct altmode *port_altmode;
+       int ret;
+
+       port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
+
+       ret = typec_altmode_set_mux(port_altmode, state);
+       if (ret)
+               return ret;
+
+       blocking_notifier_call_chain(&port_altmode->nh, state, NULL);
+
+       return 0;
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * typec_altmode_notify - Communication between the OS and alternate mode driver
+ * @adev: Handle to the alternate mode
+ * @conf: Alternate mode specific configuration value
+ * @data: Alternate mode specific data
+ *
+ * The primary purpose for this function is to allow the alternate mode drivers
+ * to tell which pin configuration has been negotiated with the partner. That
+ * information will then be used for example to configure the muxes.
+ * Communication to the other direction is also possible, and low level device
+ * drivers can also send notifications to the alternate mode drivers. The actual
+ * communication will be specific for every SVID.
+ */
+int typec_altmode_notify(struct typec_altmode *adev,
+                        unsigned long conf, void *data)
+{
+       bool is_port;
+       struct altmode *altmode;
+       struct altmode *partner;
+       int ret;
+
+       if (!adev)
+               return 0;
+
+       altmode = to_altmode(adev);
+
+       if (!altmode->partner)
+               return -ENODEV;
+
+       is_port = is_typec_port(adev->dev.parent);
+       partner = altmode->partner;
+
+       ret = typec_altmode_set_mux(is_port ? altmode : partner, (u8)conf);
+       if (ret)
+               return ret;
+
+       blocking_notifier_call_chain(is_port ? &altmode->nh : &partner->nh,
+                                    conf, data);
+
+       if (partner->adev.ops && partner->adev.ops->notify)
+               return partner->adev.ops->notify(&partner->adev, conf, data);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(typec_altmode_notify);
+
+/**
+ * typec_altmode_enter - Enter Mode
+ * @adev: The alternate mode
+ *
+ * The alternate mode drivers use this function to enter mode. The port drivers
+ * use this to inform the alternate mode drivers that the partner has initiated
+ * Enter Mode command.
+ */
+int typec_altmode_enter(struct typec_altmode *adev)
+{
+       struct altmode *partner = to_altmode(adev)->partner;
+       struct typec_altmode *pdev = &partner->adev;
+       int ret;
+
+       if (!adev || adev->active)
+               return 0;
+
+       if (!pdev->ops || !pdev->ops->enter)
+               return -EOPNOTSUPP;
+
+       /* Moving to USB Safe State */
+       ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE);
+       if (ret)
+               return ret;
+
+       /* Enter Mode */
+       return pdev->ops->enter(pdev);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_enter);
+
+/**
+ * typec_altmode_exit - Exit Mode
+ * @adev: The alternate mode
+ *
+ * The partner of @adev has initiated Exit Mode command.
+ */
+int typec_altmode_exit(struct typec_altmode *adev)
+{
+       struct altmode *partner = to_altmode(adev)->partner;
+       struct typec_altmode *pdev = &partner->adev;
+       int ret;
+
+       if (!adev || !adev->active)
+               return 0;
+
+       if (!pdev->ops || !pdev->ops->enter)
+               return -EOPNOTSUPP;
+
+       /* Moving to USB Safe State */
+       ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE);
+       if (ret)
+               return ret;
+
+       /* Exit Mode command */
+       return pdev->ops->exit(pdev);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_exit);
+
+/**
+ * typec_altmode_attention - Attention command
+ * @adev: The alternate mode
+ * @vdo: VDO for the Attention command
+ *
+ * Notifies the partner of @adev about Attention command.
+ */
+void typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
+{
+       struct typec_altmode *pdev = &to_altmode(adev)->partner->adev;
+
+       if (pdev->ops && pdev->ops->attention)
+               pdev->ops->attention(pdev, vdo);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_attention);
+
+/**
+ * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
+ * @adev: Alternate mode handle
+ * @header: VDM Header
+ * @vdo: Array of Vendor Defined Data Objects
+ * @count: Number of Data Objects
+ *
+ * The alternate mode drivers use this function for SVID specific communication
+ * with the partner. The port drivers use it to deliver the Structured VDMs
+ * received from the partners to the alternate mode drivers.
+ */
+int typec_altmode_vdm(struct typec_altmode *adev,
+                     const u32 header, const u32 *vdo, int count)
+{
+       struct typec_altmode *pdev;
+       struct altmode *altmode;
+
+       if (!adev)
+               return 0;
+
+       altmode = to_altmode(adev);
+
+       if (!altmode->partner)
+               return -ENODEV;
+
+       pdev = &altmode->partner->adev;
+
+       if (!pdev->ops || !pdev->ops->vdm)
+               return -EOPNOTSUPP;
+
+       return pdev->ops->vdm(pdev, header, vdo, count);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_vdm);
+
+const struct typec_altmode *
+typec_altmode_get_partner(struct typec_altmode *adev)
+{
+       return &to_altmode(adev)->partner->adev;
+}
+EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
+
+/* -------------------------------------------------------------------------- */
+/* API for the alternate mode drivers */
+
+/**
+ * typec_altmode_get_plug - Find cable plug alternate mode
+ * @adev: Handle to partner alternate mode
+ * @index: Cable plug index
+ *
+ * Increment reference count for cable plug alternate mode device. Returns
+ * handle to the cable plug alternate mode, or NULL if none is found.
+ */
+struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
+                                            enum typec_plug_index index)
+{
+       struct altmode *port = to_altmode(adev)->partner;
+
+       if (port->plug[index]) {
+               get_device(&port->plug[index]->adev.dev);
+               return &port->plug[index]->adev;
+       }
+
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
+
+/**
+ * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
+ * @plug: Handle to the cable plug alternate mode
+ */
+void typec_altmode_put_plug(struct typec_altmode *plug)
+{
+       if (plug)
+               put_device(&plug->dev);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
+
+int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
+                                   struct module *module)
+{
+       if (!drv->probe)
+               return -EINVAL;
+
+       drv->driver.owner = module;
+       drv->driver.bus = &typec_bus;
+
+       return driver_register(&drv->driver);
+}
+EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
+
+void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
+{
+       driver_unregister(&drv->driver);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
+
+/* -------------------------------------------------------------------------- */
+/* API for the port drivers */
+
+/**
+ * typec_match_altmode - Match SVID and mode to an array of alternate modes
+ * @altmodes: Array of alternate modes
+ * @n: Number of elements in the array, or -1 for NULL terminated arrays
+ * @svid: Standard or Vendor ID to match with
+ * @mode: Mode to match with
+ *
+ * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
+ * match is found.
+ */
+struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
+                                         size_t n, u16 svid, u8 mode)
+{
+       int i;
+
+       for (i = 0; i < n; i++) {
+               if (!altmodes[i])
+                       break;
+               if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
+                       return altmodes[i];
+       }
+
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(typec_match_altmode);
+
+/* -------------------------------------------------------------------------- */
+
+static ssize_t
+description_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_altmode *alt = to_typec_altmode(dev);
+
+       return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
+}
+static DEVICE_ATTR_RO(description);
+
+static struct attribute *typec_attrs[] = {
+       &dev_attr_description.attr,
+       NULL
+};
+ATTRIBUTE_GROUPS(typec);
+
+static int typec_match(struct device *dev, struct device_driver *driver)
+{
+       struct typec_altmode_driver *drv = to_altmode_driver(driver);
+       struct typec_altmode *altmode = to_typec_altmode(dev);
+       const struct typec_device_id *id;
+
+       for (id = drv->id_table; id->svid; id++)
+               if (id->svid == altmode->svid &&
+                   (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
+                       return 1;
+       return 0;
+}
+
+static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
+{
+       struct typec_altmode *altmode = to_typec_altmode(dev);
+
+       if (add_uevent_var(env, "SVID=%04X", altmode->svid))
+               return -ENOMEM;
+
+       if (add_uevent_var(env, "MODE=%u", altmode->mode))
+               return -ENOMEM;
+
+       return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
+                             altmode->svid, altmode->mode);
+}
+
+static int typec_altmode_create_links(struct altmode *alt)
+{
+       struct device *port_dev = &alt->partner->adev.dev;
+       struct device *dev = &alt->adev.dev;
+       int err;
+
+       err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
+       if (err)
+               return err;
+
+       err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
+       if (err)
+               sysfs_remove_link(&dev->kobj, "port");
+
+       return err;
+}
+
+static void typec_altmode_remove_links(struct altmode *alt)
+{
+       sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
+       sysfs_remove_link(&alt->adev.dev.kobj, "port");
+}
+
+static int typec_probe(struct device *dev)
+{
+       struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
+       struct typec_altmode *adev = to_typec_altmode(dev);
+       struct altmode *altmode = to_altmode(adev);
+       int ret;
+
+       /* Fail if the port does not support the alternate mode */
+       if (!altmode->partner)
+               return -ENODEV;
+
+       ret = typec_altmode_create_links(altmode);
+       if (ret) {
+               dev_warn(dev, "failed to create symlinks\n");
+               return ret;
+       }
+
+       ret = drv->probe(adev);
+       if (ret)
+               typec_altmode_remove_links(altmode);
+
+       return ret;
+}
+
+static int typec_remove(struct device *dev)
+{
+       struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
+       struct typec_altmode *adev = to_typec_altmode(dev);
+       struct altmode *altmode = to_altmode(adev);
+
+       typec_altmode_remove_links(altmode);
+
+       if (drv->remove)
+               drv->remove(to_typec_altmode(dev));
+
+       if (adev->active) {
+               WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE));
+               typec_altmode_update_active(adev, false);
+       }
+
+       adev->desc = NULL;
+       adev->ops = NULL;
+
+       return 0;
+}
+
+struct bus_type typec_bus = {
+       .name = "typec",
+       .dev_groups = typec_groups,
+       .match = typec_match,
+       .uevent = typec_uevent,
+       .probe = typec_probe,
+       .remove = typec_remove,
+};
diff --git a/drivers/usb/typec/bus.h b/drivers/usb/typec/bus.h
new file mode 100644 (file)
index 0000000..db40e61
--- /dev/null
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __USB_TYPEC_ALTMODE_H__
+#define __USB_TYPEC_ALTMODE_H__
+
+#include <linux/usb/typec_altmode.h>
+#include <linux/usb/typec_mux.h>
+
+struct bus_type;
+
+struct altmode {
+       unsigned int                    id;
+       struct typec_altmode            adev;
+       struct typec_mux                *mux;
+
+       enum typec_port_data            roles;
+
+       struct attribute                *attrs[5];
+       char                            group_name[8];
+       struct attribute_group          group;
+       const struct attribute_group    *groups[2];
+
+       struct altmode                  *partner;
+       struct altmode                  *plug[2];
+
+       struct blocking_notifier_head   nh;
+};
+
+#define to_altmode(d) container_of(d, struct altmode, adev)
+
+extern struct bus_type typec_bus;
+extern const struct device_type typec_altmode_dev_type;
+extern const struct device_type typec_port_dev_type;
+
+#define is_typec_altmode(_dev_) (_dev_->type == &typec_altmode_dev_type)
+#define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
+
+#endif /* __USB_TYPEC_ALTMODE_H__ */
diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
new file mode 100644 (file)
index 0000000..00141e0
--- /dev/null
@@ -0,0 +1,1654 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * USB Type-C Connector Class
+ *
+ * Copyright (C) 2017, Intel Corporation
+ * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+
+#include "bus.h"
+
+struct typec_plug {
+       struct device                   dev;
+       enum typec_plug_index           index;
+       struct ida                      mode_ids;
+};
+
+struct typec_cable {
+       struct device                   dev;
+       enum typec_plug_type            type;
+       struct usb_pd_identity          *identity;
+       unsigned int                    active:1;
+};
+
+struct typec_partner {
+       struct device                   dev;
+       unsigned int                    usb_pd:1;
+       struct usb_pd_identity          *identity;
+       enum typec_accessory            accessory;
+       struct ida                      mode_ids;
+};
+
+struct typec_port {
+       unsigned int                    id;
+       struct device                   dev;
+       struct ida                      mode_ids;
+
+       int                             prefer_role;
+       enum typec_data_role            data_role;
+       enum typec_role                 pwr_role;
+       enum typec_role                 vconn_role;
+       enum typec_pwr_opmode           pwr_opmode;
+       enum typec_port_type            port_type;
+       struct mutex                    port_type_lock;
+
+       enum typec_orientation          orientation;
+       struct typec_switch             *sw;
+       struct typec_mux                *mux;
+
+       const struct typec_capability   *cap;
+};
+
+#define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
+#define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
+#define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
+#define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
+
+static const struct device_type typec_partner_dev_type;
+static const struct device_type typec_cable_dev_type;
+static const struct device_type typec_plug_dev_type;
+
+#define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
+#define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
+#define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
+
+static DEFINE_IDA(typec_index_ida);
+static struct class *typec_class;
+
+/* ------------------------------------------------------------------------- */
+/* Common attributes */
+
+static const char * const typec_accessory_modes[] = {
+       [TYPEC_ACCESSORY_NONE]          = "none",
+       [TYPEC_ACCESSORY_AUDIO]         = "analog_audio",
+       [TYPEC_ACCESSORY_DEBUG]         = "debug",
+};
+
+static struct usb_pd_identity *get_pd_identity(struct device *dev)
+{
+       if (is_typec_partner(dev)) {
+               struct typec_partner *partner = to_typec_partner(dev);
+
+               return partner->identity;
+       } else if (is_typec_cable(dev)) {
+               struct typec_cable *cable = to_typec_cable(dev);
+
+               return cable->identity;
+       }
+       return NULL;
+}
+
+static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct usb_pd_identity *id = get_pd_identity(dev);
+
+       return sprintf(buf, "0x%08x\n", id->id_header);
+}
+static DEVICE_ATTR_RO(id_header);
+
+static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct usb_pd_identity *id = get_pd_identity(dev);
+
+       return sprintf(buf, "0x%08x\n", id->cert_stat);
+}
+static DEVICE_ATTR_RO(cert_stat);
+
+static ssize_t product_show(struct device *dev, struct device_attribute *attr,
+                           char *buf)
+{
+       struct usb_pd_identity *id = get_pd_identity(dev);
+
+       return sprintf(buf, "0x%08x\n", id->product);
+}
+static DEVICE_ATTR_RO(product);
+
+static struct attribute *usb_pd_id_attrs[] = {
+       &dev_attr_id_header.attr,
+       &dev_attr_cert_stat.attr,
+       &dev_attr_product.attr,
+       NULL
+};
+
+static const struct attribute_group usb_pd_id_group = {
+       .name = "identity",
+       .attrs = usb_pd_id_attrs,
+};
+
+static const struct attribute_group *usb_pd_id_groups[] = {
+       &usb_pd_id_group,
+       NULL,
+};
+
+static void typec_report_identity(struct device *dev)
+{
+       sysfs_notify(&dev->kobj, "identity", "id_header");
+       sysfs_notify(&dev->kobj, "identity", "cert_stat");
+       sysfs_notify(&dev->kobj, "identity", "product");
+}
+
+/* ------------------------------------------------------------------------- */
+/* Alternate Modes */
+
+static int altmode_match(struct device *dev, void *data)
+{
+       struct typec_altmode *adev = to_typec_altmode(dev);
+       struct typec_device_id *id = data;
+
+       if (!is_typec_altmode(dev))
+               return 0;
+
+       return ((adev->svid == id->svid) && (adev->mode == id->mode));
+}
+
+static void typec_altmode_set_partner(struct altmode *altmode)
+{
+       struct typec_altmode *adev = &altmode->adev;
+       struct typec_device_id id = { adev->svid, adev->mode, };
+       struct typec_port *port = typec_altmode2port(adev);
+       struct altmode *partner;
+       struct device *dev;
+
+       dev = device_find_child(&port->dev, &id, altmode_match);
+       if (!dev)
+               return;
+
+       /* Bind the port alt mode to the partner/plug alt mode. */
+       partner = to_altmode(to_typec_altmode(dev));
+       altmode->partner = partner;
+
+       /* Bind the partner/plug alt mode to the port alt mode. */
+       if (is_typec_plug(adev->dev.parent)) {
+               struct typec_plug *plug = to_typec_plug(adev->dev.parent);
+
+               partner->plug[plug->index] = altmode;
+       } else {
+               partner->partner = altmode;
+       }
+}
+
+static void typec_altmode_put_partner(struct altmode *altmode)
+{
+       struct altmode *partner = altmode->partner;
+       struct typec_altmode *adev;
+
+       if (!partner)
+               return;
+
+       adev = &partner->adev;
+
+       if (is_typec_plug(adev->dev.parent)) {
+               struct typec_plug *plug = to_typec_plug(adev->dev.parent);
+
+               partner->plug[plug->index] = NULL;
+       } else {
+               partner->partner = NULL;
+       }
+       put_device(&adev->dev);
+}
+
+static int __typec_port_match(struct device *dev, const void *name)
+{
+       return !strcmp((const char *)name, dev_name(dev));
+}
+
+static void *typec_port_match(struct device_connection *con, int ep, void *data)
+{
+       return class_find_device(typec_class, NULL, con->endpoint[ep],
+                                __typec_port_match);
+}
+
+struct typec_altmode *
+typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
+                               struct notifier_block *nb)
+{
+       struct typec_device_id id = { svid, mode, };
+       struct device *altmode_dev;
+       struct device *port_dev;
+       struct altmode *altmode;
+       int ret;
+
+       /* Find the port linked to the caller */
+       port_dev = device_connection_find_match(dev, NULL, NULL,
+                                               typec_port_match);
+       if (IS_ERR_OR_NULL(port_dev))
+               return port_dev ? ERR_CAST(port_dev) : ERR_PTR(-ENODEV);
+
+       /* Find the altmode with matching svid */
+       altmode_dev = device_find_child(port_dev, &id, altmode_match);
+
+       put_device(port_dev);
+
+       if (!altmode_dev)
+               return ERR_PTR(-ENODEV);
+
+       altmode = to_altmode(to_typec_altmode(altmode_dev));
+
+       /* Register notifier */
+       ret = blocking_notifier_chain_register(&altmode->nh, nb);
+       if (ret) {
+               put_device(altmode_dev);
+               return ERR_PTR(ret);
+       }
+
+       return &altmode->adev;
+}
+EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
+
+void typec_altmode_unregister_notifier(struct typec_altmode *adev,
+                                      struct notifier_block *nb)
+{
+       struct altmode *altmode = to_altmode(adev);
+
+       blocking_notifier_chain_unregister(&altmode->nh, nb);
+       put_device(&adev->dev);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
+
+/**
+ * typec_altmode_update_active - Report Enter/Exit mode
+ * @adev: Handle to the alternate mode
+ * @active: True when the mode has been entered
+ *
+ * If a partner or cable plug executes Enter/Exit Mode command successfully, the
+ * drivers use this routine to report the updated state of the mode.
+ */
+void typec_altmode_update_active(struct typec_altmode *adev, bool active)
+{
+       char dir[6];
+
+       if (adev->active == active)
+               return;
+
+       if (!is_typec_port(adev->dev.parent)) {
+               if (!active)
+                       module_put(adev->dev.driver->owner);
+               else
+                       WARN_ON(!try_module_get(adev->dev.driver->owner));
+       }
+
+       adev->active = active;
+       snprintf(dir, sizeof(dir), "mode%d", adev->mode);
+       sysfs_notify(&adev->dev.kobj, dir, "active");
+       sysfs_notify(&adev->dev.kobj, NULL, "active");
+       kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
+}
+EXPORT_SYMBOL_GPL(typec_altmode_update_active);
+
+/**
+ * typec_altmode2port - Alternate Mode to USB Type-C port
+ * @alt: The Alternate Mode
+ *
+ * Returns handle to the port that a cable plug or partner with @alt is
+ * connected to.
+ */
+struct typec_port *typec_altmode2port(struct typec_altmode *alt)
+{
+       if (is_typec_plug(alt->dev.parent))
+               return to_typec_port(alt->dev.parent->parent->parent);
+       if (is_typec_partner(alt->dev.parent))
+               return to_typec_port(alt->dev.parent->parent);
+       if (is_typec_port(alt->dev.parent))
+               return to_typec_port(alt->dev.parent);
+
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(typec_altmode2port);
+
+static ssize_t
+vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_altmode *alt = to_typec_altmode(dev);
+
+       return sprintf(buf, "0x%08x\n", alt->vdo);
+}
+static DEVICE_ATTR_RO(vdo);
+
+static ssize_t
+description_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_altmode *alt = to_typec_altmode(dev);
+
+       return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
+}
+static DEVICE_ATTR_RO(description);
+
+static ssize_t
+active_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_altmode *alt = to_typec_altmode(dev);
+
+       return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
+}
+
+static ssize_t active_store(struct device *dev, struct device_attribute *attr,
+                           const char *buf, size_t size)
+{
+       struct typec_altmode *adev = to_typec_altmode(dev);
+       struct altmode *altmode = to_altmode(adev);
+       bool enter;
+       int ret;
+
+       ret = kstrtobool(buf, &enter);
+       if (ret)
+               return ret;
+
+       if (adev->active == enter)
+               return size;
+
+       if (is_typec_port(adev->dev.parent)) {
+               typec_altmode_update_active(adev, enter);
+
+               /* Make sure that the partner exits the mode before disabling */
+               if (altmode->partner && !enter && altmode->partner->adev.active)
+                       typec_altmode_exit(&altmode->partner->adev);
+       } else if (altmode->partner) {
+               if (enter && !altmode->partner->adev.active) {
+                       dev_warn(dev, "port has the mode disabled\n");
+                       return -EPERM;
+               }
+       }
+
+       /* Note: If there is no driver, the mode will not be entered */
+       if (adev->ops && adev->ops->activate) {
+               ret = adev->ops->activate(adev, enter);
+               if (ret)
+                       return ret;
+       }
+
+       return size;
+}
+static DEVICE_ATTR_RW(active);
+
+static ssize_t
+supported_roles_show(struct device *dev, struct device_attribute *attr,
+                    char *buf)
+{
+       struct altmode *alt = to_altmode(to_typec_altmode(dev));
+       ssize_t ret;
+
+       switch (alt->roles) {
+       case TYPEC_PORT_SRC:
+               ret = sprintf(buf, "source\n");
+               break;
+       case TYPEC_PORT_SNK:
+               ret = sprintf(buf, "sink\n");
+               break;
+       case TYPEC_PORT_DRP:
+       default:
+               ret = sprintf(buf, "source sink\n");
+               break;
+       }
+       return ret;
+}
+static DEVICE_ATTR_RO(supported_roles);
+
+static ssize_t
+mode_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_altmode *adev = to_typec_altmode(dev);
+
+       return sprintf(buf, "%u\n", adev->mode);
+}
+static DEVICE_ATTR_RO(mode);
+
+static ssize_t
+svid_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_altmode *adev = to_typec_altmode(dev);
+
+       return sprintf(buf, "%04x\n", adev->svid);
+}
+static DEVICE_ATTR_RO(svid);
+
+static struct attribute *typec_altmode_attrs[] = {
+       &dev_attr_active.attr,
+       &dev_attr_mode.attr,
+       &dev_attr_svid.attr,
+       &dev_attr_vdo.attr,
+       NULL
+};
+ATTRIBUTE_GROUPS(typec_altmode);
+
+static int altmode_id_get(struct device *dev)
+{
+       struct ida *ids;
+
+       if (is_typec_partner(dev))
+               ids = &to_typec_partner(dev)->mode_ids;
+       else if (is_typec_plug(dev))
+               ids = &to_typec_plug(dev)->mode_ids;
+       else
+               ids = &to_typec_port(dev)->mode_ids;
+
+       return ida_simple_get(ids, 0, 0, GFP_KERNEL);
+}
+
+static void altmode_id_remove(struct device *dev, int id)
+{
+       struct ida *ids;
+
+       if (is_typec_partner(dev))
+               ids = &to_typec_partner(dev)->mode_ids;
+       else if (is_typec_plug(dev))
+               ids = &to_typec_plug(dev)->mode_ids;
+       else
+               ids = &to_typec_port(dev)->mode_ids;
+
+       ida_simple_remove(ids, id);
+}
+
+static void typec_altmode_release(struct device *dev)
+{
+       struct altmode *alt = to_altmode(to_typec_altmode(dev));
+
+       typec_altmode_put_partner(alt);
+
+       altmode_id_remove(alt->adev.dev.parent, alt->id);
+       kfree(alt);
+}
+
+const struct device_type typec_altmode_dev_type = {
+       .name = "typec_alternate_mode",
+       .groups = typec_altmode_groups,
+       .release = typec_altmode_release,
+};
+
+static struct typec_altmode *
+typec_register_altmode(struct device *parent,
+                      const struct typec_altmode_desc *desc)
+{
+       unsigned int id = altmode_id_get(parent);
+       bool is_port = is_typec_port(parent);
+       struct altmode *alt;
+       int ret;
+
+       alt = kzalloc(sizeof(*alt), GFP_KERNEL);
+       if (!alt)
+               return ERR_PTR(-ENOMEM);
+
+       alt->adev.svid = desc->svid;
+       alt->adev.mode = desc->mode;
+       alt->adev.vdo = desc->vdo;
+       alt->roles = desc->roles;
+       alt->id = id;
+
+       alt->attrs[0] = &dev_attr_vdo.attr;
+       alt->attrs[1] = &dev_attr_description.attr;
+       alt->attrs[2] = &dev_attr_active.attr;
+
+       if (is_port) {
+               alt->attrs[3] = &dev_attr_supported_roles.attr;
+               alt->adev.active = true; /* Enabled by default */
+       }
+
+       sprintf(alt->group_name, "mode%d", desc->mode);
+       alt->group.name = alt->group_name;
+       alt->group.attrs = alt->attrs;
+       alt->groups[0] = &alt->group;
+
+       alt->adev.dev.parent = parent;
+       alt->adev.dev.groups = alt->groups;
+       alt->adev.dev.type = &typec_altmode_dev_type;
+       dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
+
+       /* Link partners and plugs with the ports */
+       if (is_port)
+               BLOCKING_INIT_NOTIFIER_HEAD(&alt->nh);
+       else
+               typec_altmode_set_partner(alt);
+
+       /* The partners are bind to drivers */
+       if (is_typec_partner(parent))
+               alt->adev.dev.bus = &typec_bus;
+
+       ret = device_register(&alt->adev.dev);
+       if (ret) {
+               dev_err(parent, "failed to register alternate mode (%d)\n",
+                       ret);
+               put_device(&alt->adev.dev);
+               return ERR_PTR(ret);
+       }
+
+       return &alt->adev;
+}
+
+/**
+ * typec_unregister_altmode - Unregister Alternate Mode
+ * @adev: The alternate mode to be unregistered
+ *
+ * Unregister device created with typec_partner_register_altmode(),
+ * typec_plug_register_altmode() or typec_port_register_altmode().
+ */
+void typec_unregister_altmode(struct typec_altmode *adev)
+{
+       if (IS_ERR_OR_NULL(adev))
+               return;
+       typec_mux_put(to_altmode(adev)->mux);
+       device_unregister(&adev->dev);
+}
+EXPORT_SYMBOL_GPL(typec_unregister_altmode);
+
+/* ------------------------------------------------------------------------- */
+/* Type-C Partners */
+
+static ssize_t accessory_mode_show(struct device *dev,
+                                  struct device_attribute *attr,
+                                  char *buf)
+{
+       struct typec_partner *p = to_typec_partner(dev);
+
+       return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
+}
+static DEVICE_ATTR_RO(accessory_mode);
+
+static ssize_t supports_usb_power_delivery_show(struct device *dev,
+                                               struct device_attribute *attr,
+                                               char *buf)
+{
+       struct typec_partner *p = to_typec_partner(dev);
+
+       return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
+}
+static DEVICE_ATTR_RO(supports_usb_power_delivery);
+
+static struct attribute *typec_partner_attrs[] = {
+       &dev_attr_accessory_mode.attr,
+       &dev_attr_supports_usb_power_delivery.attr,
+       NULL
+};
+ATTRIBUTE_GROUPS(typec_partner);
+
+static void typec_partner_release(struct device *dev)
+{
+       struct typec_partner *partner = to_typec_partner(dev);
+
+       ida_destroy(&partner->mode_ids);
+       kfree(partner);
+}
+
+static const struct device_type typec_partner_dev_type = {
+       .name = "typec_partner",
+       .groups = typec_partner_groups,
+       .release = typec_partner_release,
+};
+
+/**
+ * typec_partner_set_identity - Report result from Discover Identity command
+ * @partner: The partner updated identity values
+ *
+ * This routine is used to report that the result of Discover Identity USB power
+ * delivery command has become available.
+ */
+int typec_partner_set_identity(struct typec_partner *partner)
+{
+       if (!partner->identity)
+               return -EINVAL;
+
+       typec_report_identity(&partner->dev);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(typec_partner_set_identity);
+
+/**
+ * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
+ * @partner: USB Type-C Partner that supports the alternate mode
+ * @desc: Description of the alternate mode
+ *
+ * This routine is used to register each alternate mode individually that
+ * @partner has listed in response to Discover SVIDs command. The modes for a
+ * SVID listed in response to Discover Modes command need to be listed in an
+ * array in @desc.
+ *
+ * Returns handle to the alternate mode on success or NULL on failure.
+ */
+struct typec_altmode *
+typec_partner_register_altmode(struct typec_partner *partner,
+                              const struct typec_altmode_desc *desc)
+{
+       return typec_register_altmode(&partner->dev, desc);
+}
+EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
+
+/**
+ * typec_register_partner - Register a USB Type-C Partner
+ * @port: The USB Type-C Port the partner is connected to
+ * @desc: Description of the partner
+ *
+ * Registers a device for USB Type-C Partner described in @desc.
+ *
+ * Returns handle to the partner on success or ERR_PTR on failure.
+ */
+struct typec_partner *typec_register_partner(struct typec_port *port,
+                                            struct typec_partner_desc *desc)
+{
+       struct typec_partner *partner;
+       int ret;
+
+       partner = kzalloc(sizeof(*partner), GFP_KERNEL);
+       if (!partner)
+               return ERR_PTR(-ENOMEM);
+
+       ida_init(&partner->mode_ids);
+       partner->usb_pd = desc->usb_pd;
+       partner->accessory = desc->accessory;
+
+       if (desc->identity) {
+               /*
+                * Creating directory for the identity only if the driver is
+                * able to provide data to it.
+                */
+               partner->dev.groups = usb_pd_id_groups;
+               partner->identity = desc->identity;
+       }
+
+       partner->dev.class = typec_class;
+       partner->dev.parent = &port->dev;
+       partner->dev.type = &typec_partner_dev_type;
+       dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
+
+       ret = device_register(&partner->dev);
+       if (ret) {
+               dev_err(&port->dev, "failed to register partner (%d)\n", ret);
+               put_device(&partner->dev);
+               return ERR_PTR(ret);
+       }
+
+       return partner;
+}
+EXPORT_SYMBOL_GPL(typec_register_partner);
+
+/**
+ * typec_unregister_partner - Unregister a USB Type-C Partner
+ * @partner: The partner to be unregistered
+ *
+ * Unregister device created with typec_register_partner().
+ */
+void typec_unregister_partner(struct typec_partner *partner)
+{
+       if (!IS_ERR_OR_NULL(partner))
+               device_unregister(&partner->dev);
+}
+EXPORT_SYMBOL_GPL(typec_unregister_partner);
+
+/* ------------------------------------------------------------------------- */
+/* Type-C Cable Plugs */
+
+static void typec_plug_release(struct device *dev)
+{
+       struct typec_plug *plug = to_typec_plug(dev);
+
+       ida_destroy(&plug->mode_ids);
+       kfree(plug);
+}
+
+static const struct device_type typec_plug_dev_type = {
+       .name = "typec_plug",
+       .release = typec_plug_release,
+};
+
+/**
+ * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
+ * @plug: USB Type-C Cable Plug that supports the alternate mode
+ * @desc: Description of the alternate mode
+ *
+ * This routine is used to register each alternate mode individually that @plug
+ * has listed in response to Discover SVIDs command. The modes for a SVID that
+ * the plug lists in response to Discover Modes command need to be listed in an
+ * array in @desc.
+ *
+ * Returns handle to the alternate mode on success or ERR_PTR on failure.
+ */
+struct typec_altmode *
+typec_plug_register_altmode(struct typec_plug *plug,
+                           const struct typec_altmode_desc *desc)
+{
+       return typec_register_altmode(&plug->dev, desc);
+}
+EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
+
+/**
+ * typec_register_plug - Register a USB Type-C Cable Plug
+ * @cable: USB Type-C Cable with the plug
+ * @desc: Description of the cable plug
+ *
+ * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
+ * Cable Plug represents a plug with electronics in it that can response to USB
+ * Power Delivery SOP Prime or SOP Double Prime packages.
+ *
+ * Returns handle to the cable plug on success or ERR_PTR on failure.
+ */
+struct typec_plug *typec_register_plug(struct typec_cable *cable,
+                                      struct typec_plug_desc *desc)
+{
+       struct typec_plug *plug;
+       char name[8];
+       int ret;
+
+       plug = kzalloc(sizeof(*plug), GFP_KERNEL);
+       if (!plug)
+               return ERR_PTR(-ENOMEM);
+
+       sprintf(name, "plug%d", desc->index);
+
+       ida_init(&plug->mode_ids);
+       plug->index = desc->index;
+       plug->dev.class = typec_class;
+       plug->dev.parent = &cable->dev;
+       plug->dev.type = &typec_plug_dev_type;
+       dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
+
+       ret = device_register(&plug->dev);
+       if (ret) {
+               dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
+               put_device(&plug->dev);
+               return ERR_PTR(ret);
+       }
+
+       return plug;
+}
+EXPORT_SYMBOL_GPL(typec_register_plug);
+
+/**
+ * typec_unregister_plug - Unregister a USB Type-C Cable Plug
+ * @plug: The cable plug to be unregistered
+ *
+ * Unregister device created with typec_register_plug().
+ */
+void typec_unregister_plug(struct typec_plug *plug)
+{
+       if (!IS_ERR_OR_NULL(plug))
+               device_unregister(&plug->dev);
+}
+EXPORT_SYMBOL_GPL(typec_unregister_plug);
+
+/* Type-C Cables */
+
+static ssize_t
+type_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct typec_cable *cable = to_typec_cable(dev);
+
+       return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
+}
+static DEVICE_ATTR_RO(type);
+
+static const char * const typec_plug_types[] = {
+       [USB_PLUG_NONE]         = "unknown",
+       [USB_PLUG_TYPE_A]       = "type-a",
+       [USB_PLUG_TYPE_B]       = "type-b",
+       [USB_PLUG_TYPE_C]       = "type-c",
+       [USB_PLUG_CAPTIVE]      = "captive",
+};
+
+static ssize_t plug_type_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+       struct typec_cable *cable = to_typec_cable(dev);
+
+       return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
+}
+static DEVICE_ATTR_RO(plug_type);
+
+static struct attribute *typec_cable_attrs[] = {
+       &dev_attr_type.attr,
+       &dev_attr_plug_type.attr,
+       NULL
+};
+ATTRIBUTE_GROUPS(typec_cable);
+
+static void typec_cable_release(struct device *dev)
+{
+       struct typec_cable *cable = to_typec_cable(dev);
+
+       kfree(cable);
+}
+
+static const struct device_type typec_cable_dev_type = {
+       .name = "typec_cable",
+       .groups = typec_cable_groups,
+       .release = typec_cable_release,
+};
+
+/**
+ * typec_cable_set_identity - Report result from Discover Identity command
+ * @cable: The cable updated identity values
+ *
+ * This routine is used to report that the result of Discover Identity USB power
+ * delivery command has become available.
+ */
+int typec_cable_set_identity(struct typec_cable *cable)
+{
+       if (!cable->identity)
+               return -EINVAL;
+
+       typec_report_identity(&cable->dev);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(typec_cable_set_identity);
+
+/**
+ * typec_register_cable - Register a USB Type-C Cable
+ * @port: The USB Type-C Port the cable is connected to
+ * @desc: Description of the cable
+ *
+ * Registers a device for USB Type-C Cable described in @desc. The cable will be
+ * parent for the optional cable plug devises.
+ *
+ * Returns handle to the cable on success or ERR_PTR on failure.
+ */
+struct typec_cable *typec_register_cable(struct typec_port *port,
+                                        struct typec_cable_desc *desc)
+{
+       struct typec_cable *cable;
+       int ret;
+
+       cable = kzalloc(sizeof(*cable), GFP_KERNEL);
+       if (!cable)
+               return ERR_PTR(-ENOMEM);
+
+       cable->type = desc->type;
+       cable->active = desc->active;
+
+       if (desc->identity) {
+               /*
+                * Creating directory for the identity only if the driver is
+                * able to provide data to it.
+                */
+               cable->dev.groups = usb_pd_id_groups;
+               cable->identity = desc->identity;
+       }
+
+       cable->dev.class = typec_class;
+       cable->dev.parent = &port->dev;
+       cable->dev.type = &typec_cable_dev_type;
+       dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
+
+       ret = device_register(&cable->dev);
+       if (ret) {
+               dev_err(&port->dev, "failed to register cable (%d)\n", ret);
+               put_device(&cable->dev);
+               return ERR_PTR(ret);
+       }
+
+       return cable;
+}
+EXPORT_SYMBOL_GPL(typec_register_cable);
+
+/**
+ * typec_unregister_cable - Unregister a USB Type-C Cable
+ * @cable: The cable to be unregistered
+ *
+ * Unregister device created with typec_register_cable().
+ */
+void typec_unregister_cable(struct typec_cable *cable)
+{
+       if (!IS_ERR_OR_NULL(cable))
+               device_unregister(&cable->dev);
+}
+EXPORT_SYMBOL_GPL(typec_unregister_cable);
+
+/* ------------------------------------------------------------------------- */
+/* USB Type-C ports */
+
+static const char * const typec_roles[] = {
+       [TYPEC_SINK]    = "sink",
+       [TYPEC_SOURCE]  = "source",
+};
+
+static const char * const typec_data_roles[] = {
+       [TYPEC_DEVICE]  = "device",
+       [TYPEC_HOST]    = "host",
+};
+
+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]",
+       [TYPEC_PORT_DRP] = "[dual] source sink",
+};
+
+static ssize_t
+preferred_role_store(struct device *dev, struct device_attribute *attr,
+                    const char *buf, size_t size)
+{
+       struct typec_port *port = to_typec_port(dev);
+       int role;
+       int ret;
+
+       if (port->cap->type != TYPEC_PORT_DRP) {
+               dev_dbg(dev, "Preferred role only supported with DRP ports\n");
+               return -EOPNOTSUPP;
+       }
+
+       if (!port->cap->try_role) {
+               dev_dbg(dev, "Setting preferred role not supported\n");
+               return -EOPNOTSUPP;
+       }
+
+       role = sysfs_match_string(typec_roles, buf);
+       if (role < 0) {
+               if (sysfs_streq(buf, "none"))
+                       role = TYPEC_NO_PREFERRED_ROLE;
+               else
+                       return -EINVAL;
+       }
+
+       ret = port->cap->try_role(port->cap, role);
+       if (ret)
+               return ret;
+
+       port->prefer_role = role;
+       return size;
+}
+
+static ssize_t
+preferred_role_show(struct device *dev, struct device_attribute *attr,
+                   char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       if (port->cap->type != TYPEC_PORT_DRP)
+               return 0;
+
+       if (port->prefer_role < 0)
+               return 0;
+
+       return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
+}
+static DEVICE_ATTR_RW(preferred_role);
+
+static ssize_t data_role_store(struct device *dev,
+                              struct device_attribute *attr,
+                              const char *buf, size_t size)
+{
+       struct typec_port *port = to_typec_port(dev);
+       int ret;
+
+       if (!port->cap->dr_set) {
+               dev_dbg(dev, "data role swapping not supported\n");
+               return -EOPNOTSUPP;
+       }
+
+       ret = sysfs_match_string(typec_data_roles, buf);
+       if (ret < 0)
+               return ret;
+
+       mutex_lock(&port->port_type_lock);
+       if (port->cap->data != TYPEC_PORT_DRD) {
+               ret = -EOPNOTSUPP;
+               goto unlock_and_ret;
+       }
+
+       ret = port->cap->dr_set(port->cap, ret);
+       if (ret)
+               goto unlock_and_ret;
+
+       ret = size;
+unlock_and_ret:
+       mutex_unlock(&port->port_type_lock);
+       return ret;
+}
+
+static ssize_t data_role_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       if (port->cap->data == TYPEC_PORT_DRD)
+               return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
+                              "[host] device" : "host [device]");
+
+       return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
+}
+static DEVICE_ATTR_RW(data_role);
+
+static ssize_t power_role_store(struct device *dev,
+                               struct device_attribute *attr,
+                               const char *buf, size_t size)
+{
+       struct typec_port *port = to_typec_port(dev);
+       int ret;
+
+       if (!port->cap->pd_revision) {
+               dev_dbg(dev, "USB Power Delivery not supported\n");
+               return -EOPNOTSUPP;
+       }
+
+       if (!port->cap->pr_set) {
+               dev_dbg(dev, "power role swapping not supported\n");
+               return -EOPNOTSUPP;
+       }
+
+       if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
+               dev_dbg(dev, "partner unable to swap power role\n");
+               return -EIO;
+       }
+
+       ret = sysfs_match_string(typec_roles, buf);
+       if (ret < 0)
+               return ret;
+
+       mutex_lock(&port->port_type_lock);
+       if (port->port_type != TYPEC_PORT_DRP) {
+               dev_dbg(dev, "port type fixed at \"%s\"",
+                            typec_port_power_roles[port->port_type]);
+               ret = -EOPNOTSUPP;
+               goto unlock_and_ret;
+       }
+
+       ret = port->cap->pr_set(port->cap, ret);
+       if (ret)
+               goto unlock_and_ret;
+
+       ret = size;
+unlock_and_ret:
+       mutex_unlock(&port->port_type_lock);
+       return ret;
+}
+
+static ssize_t power_role_show(struct device *dev,
+                              struct device_attribute *attr, char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       if (port->cap->type == TYPEC_PORT_DRP)
+               return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
+                              "[source] sink" : "source [sink]");
+
+       return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
+}
+static DEVICE_ATTR_RW(power_role);
+
+static ssize_t
+port_type_store(struct device *dev, struct device_attribute *attr,
+                       const char *buf, size_t size)
+{
+       struct typec_port *port = to_typec_port(dev);
+       int ret;
+       enum typec_port_type type;
+
+       if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
+               dev_dbg(dev, "changing port type not supported\n");
+               return -EOPNOTSUPP;
+       }
+
+       ret = sysfs_match_string(typec_port_power_roles, buf);
+       if (ret < 0)
+               return ret;
+
+       type = ret;
+       mutex_lock(&port->port_type_lock);
+
+       if (port->port_type == type) {
+               ret = size;
+               goto unlock_and_ret;
+       }
+
+       ret = port->cap->port_type_set(port->cap, type);
+       if (ret)
+               goto unlock_and_ret;
+
+       port->port_type = type;
+       ret = size;
+
+unlock_and_ret:
+       mutex_unlock(&port->port_type_lock);
+       return ret;
+}
+
+static ssize_t
+port_type_show(struct device *dev, struct device_attribute *attr,
+               char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       if (port->cap->type == TYPEC_PORT_DRP)
+               return sprintf(buf, "%s\n",
+                              typec_port_types_drp[port->port_type]);
+
+       return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
+}
+static DEVICE_ATTR_RW(port_type);
+
+static const char * const typec_pwr_opmodes[] = {
+       [TYPEC_PWR_MODE_USB]    = "default",
+       [TYPEC_PWR_MODE_1_5A]   = "1.5A",
+       [TYPEC_PWR_MODE_3_0A]   = "3.0A",
+       [TYPEC_PWR_MODE_PD]     = "usb_power_delivery",
+};
+
+static ssize_t power_operation_mode_show(struct device *dev,
+                                        struct device_attribute *attr,
+                                        char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
+}
+static DEVICE_ATTR_RO(power_operation_mode);
+
+static ssize_t vconn_source_store(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t size)
+{
+       struct typec_port *port = to_typec_port(dev);
+       bool source;
+       int ret;
+
+       if (!port->cap->pd_revision) {
+               dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
+               return -EOPNOTSUPP;
+       }
+
+       if (!port->cap->vconn_set) {
+               dev_dbg(dev, "VCONN swapping not supported\n");
+               return -EOPNOTSUPP;
+       }
+
+       ret = kstrtobool(buf, &source);
+       if (ret)
+               return ret;
+
+       ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
+       if (ret)
+               return ret;
+
+       return size;
+}
+
+static ssize_t vconn_source_show(struct device *dev,
+                                struct device_attribute *attr, char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       return sprintf(buf, "%s\n",
+                      port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
+}
+static DEVICE_ATTR_RW(vconn_source);
+
+static ssize_t supported_accessory_modes_show(struct device *dev,
+                                             struct device_attribute *attr,
+                                             char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+       ssize_t ret = 0;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
+               if (port->cap->accessory[i])
+                       ret += sprintf(buf + ret, "%s ",
+                              typec_accessory_modes[port->cap->accessory[i]]);
+       }
+
+       if (!ret)
+               return sprintf(buf, "none\n");
+
+       buf[ret - 1] = '\n';
+
+       return ret;
+}
+static DEVICE_ATTR_RO(supported_accessory_modes);
+
+static ssize_t usb_typec_revision_show(struct device *dev,
+                                      struct device_attribute *attr,
+                                      char *buf)
+{
+       struct typec_port *port = to_typec_port(dev);
+       u16 rev = port->cap->revision;
+
+       return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
+}
+static DEVICE_ATTR_RO(usb_typec_revision);
+
+static ssize_t usb_power_delivery_revision_show(struct device *dev,
+                                               struct device_attribute *attr,
+                                               char *buf)
+{
+       struct typec_port *p = to_typec_port(dev);
+
+       return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
+}
+static DEVICE_ATTR_RO(usb_power_delivery_revision);
+
+static struct attribute *typec_attrs[] = {
+       &dev_attr_data_role.attr,
+       &dev_attr_power_operation_mode.attr,
+       &dev_attr_power_role.attr,
+       &dev_attr_preferred_role.attr,
+       &dev_attr_supported_accessory_modes.attr,
+       &dev_attr_usb_power_delivery_revision.attr,
+       &dev_attr_usb_typec_revision.attr,
+       &dev_attr_vconn_source.attr,
+       &dev_attr_port_type.attr,
+       NULL,
+};
+ATTRIBUTE_GROUPS(typec);
+
+static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
+{
+       int ret;
+
+       ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
+       if (ret)
+               dev_err(dev, "failed to add uevent TYPEC_PORT\n");
+
+       return ret;
+}
+
+static void typec_release(struct device *dev)
+{
+       struct typec_port *port = to_typec_port(dev);
+
+       ida_simple_remove(&typec_index_ida, port->id);
+       ida_destroy(&port->mode_ids);
+       typec_switch_put(port->sw);
+       typec_mux_put(port->mux);
+       kfree(port);
+}
+
+const struct device_type typec_port_dev_type = {
+       .name = "typec_port",
+       .groups = typec_groups,
+       .uevent = typec_uevent,
+       .release = typec_release,
+};
+
+/* --------------------------------------- */
+/* Driver callbacks to report role updates */
+
+/**
+ * typec_set_data_role - Report data role change
+ * @port: The USB Type-C Port where the role was changed
+ * @role: The new data role
+ *
+ * This routine is used by the port drivers to report data role changes.
+ */
+void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
+{
+       if (port->data_role == role)
+               return;
+
+       port->data_role = role;
+       sysfs_notify(&port->dev.kobj, NULL, "data_role");
+       kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
+}
+EXPORT_SYMBOL_GPL(typec_set_data_role);
+
+/**
+ * typec_set_pwr_role - Report power role change
+ * @port: The USB Type-C Port where the role was changed
+ * @role: The new data role
+ *
+ * This routine is used by the port drivers to report power role changes.
+ */
+void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
+{
+       if (port->pwr_role == role)
+               return;
+
+       port->pwr_role = role;
+       sysfs_notify(&port->dev.kobj, NULL, "power_role");
+       kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
+}
+EXPORT_SYMBOL_GPL(typec_set_pwr_role);
+
+/**
+ * typec_set_pwr_role - Report VCONN source change
+ * @port: The USB Type-C Port which VCONN role changed
+ * @role: Source when @port is sourcing VCONN, or Sink when it's not
+ *
+ * This routine is used by the port drivers to report if the VCONN source is
+ * changes.
+ */
+void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
+{
+       if (port->vconn_role == role)
+               return;
+
+       port->vconn_role = role;
+       sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
+       kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
+}
+EXPORT_SYMBOL_GPL(typec_set_vconn_role);
+
+static int partner_match(struct device *dev, void *data)
+{
+       return is_typec_partner(dev);
+}
+
+/**
+ * typec_set_pwr_opmode - Report changed power operation mode
+ * @port: The USB Type-C Port where the mode was changed
+ * @opmode: New power operation mode
+ *
+ * This routine is used by the port drivers to report changed power operation
+ * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
+ * Type-C specification, and "USB Power Delivery" when the power levels are
+ * negotiated with methods defined in USB Power Delivery specification.
+ */
+void typec_set_pwr_opmode(struct typec_port *port,
+                         enum typec_pwr_opmode opmode)
+{
+       struct device *partner_dev;
+
+       if (port->pwr_opmode == opmode)
+               return;
+
+       port->pwr_opmode = opmode;
+       sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
+       kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
+
+       partner_dev = device_find_child(&port->dev, NULL, partner_match);
+       if (partner_dev) {
+               struct typec_partner *partner = to_typec_partner(partner_dev);
+
+               if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
+                       partner->usb_pd = 1;
+                       sysfs_notify(&partner_dev->kobj, NULL,
+                                    "supports_usb_power_delivery");
+               }
+               put_device(partner_dev);
+       }
+}
+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 */
+
+/**
+ * typec_set_orientation - Set USB Type-C cable plug orientation
+ * @port: USB Type-C Port
+ * @orientation: USB Type-C cable plug orientation
+ *
+ * Set cable plug orientation for @port.
+ */
+int typec_set_orientation(struct typec_port *port,
+                         enum typec_orientation orientation)
+{
+       int ret;
+
+       if (port->sw) {
+               ret = port->sw->set(port->sw, orientation);
+               if (ret)
+                       return ret;
+       }
+
+       port->orientation = orientation;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(typec_set_orientation);
+
+/**
+ * typec_get_orientation - Get USB Type-C cable plug orientation
+ * @port: USB Type-C Port
+ *
+ * Get current cable plug orientation for @port.
+ */
+enum typec_orientation typec_get_orientation(struct typec_port *port)
+{
+       return port->orientation;
+}
+EXPORT_SYMBOL_GPL(typec_get_orientation);
+
+/**
+ * typec_set_mode - Set mode of operation for USB Type-C connector
+ * @port: USB Type-C connector
+ * @mode: Accessory Mode, USB Operation or Safe State
+ *
+ * Configure @port for Accessory Mode @mode. This function will configure the
+ * muxes needed for @mode.
+ */
+int typec_set_mode(struct typec_port *port, int mode)
+{
+       return port->mux ? port->mux->set(port->mux, mode) : 0;
+}
+EXPORT_SYMBOL_GPL(typec_set_mode);
+
+/* --------------------------------------- */
+
+/**
+ * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
+ * @port: USB Type-C Port that supports the alternate mode
+ * @desc: Description of the alternate mode
+ *
+ * This routine is used to register an alternate mode that @port is capable of
+ * supporting.
+ *
+ * Returns handle to the alternate mode on success or ERR_PTR on failure.
+ */
+struct typec_altmode *
+typec_port_register_altmode(struct typec_port *port,
+                           const struct typec_altmode_desc *desc)
+{
+       struct typec_altmode *adev;
+       struct typec_mux *mux;
+       char id[10];
+
+       sprintf(id, "id%04xm%02x", desc->svid, desc->mode);
+
+       mux = typec_mux_get(&port->dev, id);
+       if (IS_ERR(mux))
+               return ERR_CAST(mux);
+
+       adev = typec_register_altmode(&port->dev, desc);
+       if (IS_ERR(adev))
+               typec_mux_put(mux);
+       else
+               to_altmode(adev)->mux = mux;
+
+       return adev;
+}
+EXPORT_SYMBOL_GPL(typec_port_register_altmode);
+
+/**
+ * typec_register_port - Register a USB Type-C Port
+ * @parent: Parent device
+ * @cap: Description of the port
+ *
+ * Registers a device for USB Type-C Port described in @cap.
+ *
+ * Returns handle to the port on success or ERR_PTR on failure.
+ */
+struct typec_port *typec_register_port(struct device *parent,
+                                      const struct typec_capability *cap)
+{
+       struct typec_port *port;
+       int ret;
+       int id;
+
+       port = kzalloc(sizeof(*port), GFP_KERNEL);
+       if (!port)
+               return ERR_PTR(-ENOMEM);
+
+       id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
+       if (id < 0) {
+               kfree(port);
+               return ERR_PTR(id);
+       }
+
+       switch (cap->type) {
+       case TYPEC_PORT_SRC:
+               port->pwr_role = TYPEC_SOURCE;
+               port->vconn_role = TYPEC_SOURCE;
+               break;
+       case TYPEC_PORT_SNK:
+               port->pwr_role = TYPEC_SINK;
+               port->vconn_role = TYPEC_SINK;
+               break;
+       case TYPEC_PORT_DRP:
+               if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
+                       port->pwr_role = cap->prefer_role;
+               else
+                       port->pwr_role = TYPEC_SINK;
+               break;
+       }
+
+       switch (cap->data) {
+       case TYPEC_PORT_DFP:
+               port->data_role = TYPEC_HOST;
+               break;
+       case TYPEC_PORT_UFP:
+               port->data_role = TYPEC_DEVICE;
+               break;
+       case TYPEC_PORT_DRD:
+               if (cap->prefer_role == TYPEC_SOURCE)
+                       port->data_role = TYPEC_HOST;
+               else
+                       port->data_role = TYPEC_DEVICE;
+               break;
+       }
+
+       ida_init(&port->mode_ids);
+       mutex_init(&port->port_type_lock);
+
+       port->id = id;
+       port->cap = cap;
+       port->port_type = cap->type;
+       port->prefer_role = cap->prefer_role;
+
+       device_initialize(&port->dev);
+       port->dev.class = typec_class;
+       port->dev.parent = parent;
+       port->dev.fwnode = cap->fwnode;
+       port->dev.type = &typec_port_dev_type;
+       dev_set_name(&port->dev, "port%d", id);
+
+       port->sw = typec_switch_get(&port->dev);
+       if (IS_ERR(port->sw)) {
+               put_device(&port->dev);
+               return ERR_CAST(port->sw);
+       }
+
+       port->mux = typec_mux_get(&port->dev, "typec-mux");
+       if (IS_ERR(port->mux)) {
+               put_device(&port->dev);
+               return ERR_CAST(port->mux);
+       }
+
+       ret = device_add(&port->dev);
+       if (ret) {
+               dev_err(parent, "failed to register port (%d)\n", ret);
+               put_device(&port->dev);
+               return ERR_PTR(ret);
+       }
+
+       return port;
+}
+EXPORT_SYMBOL_GPL(typec_register_port);
+
+/**
+ * typec_unregister_port - Unregister a USB Type-C Port
+ * @port: The port to be unregistered
+ *
+ * Unregister device created with typec_register_port().
+ */
+void typec_unregister_port(struct typec_port *port)
+{
+       if (!IS_ERR_OR_NULL(port))
+               device_unregister(&port->dev);
+}
+EXPORT_SYMBOL_GPL(typec_unregister_port);
+
+static int __init typec_init(void)
+{
+       int ret;
+
+       ret = bus_register(&typec_bus);
+       if (ret)
+               return ret;
+
+       typec_class = class_create(THIS_MODULE, "typec");
+       if (IS_ERR(typec_class)) {
+               bus_unregister(&typec_bus);
+               return PTR_ERR(typec_class);
+       }
+
+       return 0;
+}
+subsys_initcall(typec_init);
+
+static void __exit typec_exit(void)
+{
+       class_destroy(typec_class);
+       ida_destroy(&typec_index_ida);
+       bus_unregister(&typec_bus);
+}
+module_exit(typec_exit);
+
+MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("USB Type-C Connector Class");
diff --git a/drivers/usb/typec/fusb302/Kconfig b/drivers/usb/typec/fusb302/Kconfig
new file mode 100644 (file)
index 0000000..fce099f
--- /dev/null
@@ -0,0 +1,7 @@
+config TYPEC_FUSB302
+       tristate "Fairchild FUSB302 Type-C chip driver"
+       depends on I2C
+       help
+         The Fairchild FUSB302 Type-C chip driver that works with
+         Type-C Port Controller Manager to provide USB PD and USB
+         Type-C functionalities.
diff --git a/drivers/usb/typec/fusb302/Makefile b/drivers/usb/typec/fusb302/Makefile
new file mode 100644 (file)
index 0000000..e56804d
--- /dev/null
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-y  += fusb302.o
diff --git a/drivers/usb/typec/fusb302/fusb302.c b/drivers/usb/typec/fusb302/fusb302.c
new file mode 100644 (file)
index 0000000..3557efa
--- /dev/null
@@ -0,0 +1,1851 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2016-2017 Google, Inc
+ *
+ * Fairchild FUSB302 Type-C Chip Driver
+ */
+
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of_device.h>
+#include <linux/of_gpio.h>
+#include <linux/pinctrl/consumer.h>
+#include <linux/proc_fs.h>
+#include <linux/regulator/consumer.h>
+#include <linux/sched.h>
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/usb/typec.h>
+#include <linux/usb/tcpm.h>
+#include <linux/usb/pd.h>
+#include <linux/workqueue.h>
+
+#include "fusb302_reg.h"
+
+/*
+ * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
+ * for the current capability offered by the SRC. As FUSB302 chip fires
+ * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
+ * a delay to avoid measuring on PD activities. The delay is slightly
+ * longer than PD_T_PD_DEBPUNCE (10-20ms).
+ */
+#define T_BC_LVL_DEBOUNCE_DELAY_MS 30
+
+enum toggling_mode {
+       TOGGLINE_MODE_OFF,
+       TOGGLING_MODE_DRP,
+       TOGGLING_MODE_SNK,
+       TOGGLING_MODE_SRC,
+};
+
+static const char * const toggling_mode_name[] = {
+       [TOGGLINE_MODE_OFF]     = "toggling_OFF",
+       [TOGGLING_MODE_DRP]     = "toggling_DRP",
+       [TOGGLING_MODE_SNK]     = "toggling_SNK",
+       [TOGGLING_MODE_SRC]     = "toggling_SRC",
+};
+
+enum src_current_status {
+       SRC_CURRENT_DEFAULT,
+       SRC_CURRENT_MEDIUM,
+       SRC_CURRENT_HIGH,
+};
+
+static const u8 ra_mda_value[] = {
+       [SRC_CURRENT_DEFAULT] = 4,      /* 210mV */
+       [SRC_CURRENT_MEDIUM] = 9,       /* 420mV */
+       [SRC_CURRENT_HIGH] = 18,        /* 798mV */
+};
+
+static const u8 rd_mda_value[] = {
+       [SRC_CURRENT_DEFAULT] = 38,     /* 1638mV */
+       [SRC_CURRENT_MEDIUM] = 38,      /* 1638mV */
+       [SRC_CURRENT_HIGH] = 61,        /* 2604mV */
+};
+
+#define LOG_BUFFER_ENTRIES     1024
+#define LOG_BUFFER_ENTRY_SIZE  128
+
+struct fusb302_chip {
+       struct device *dev;
+       struct i2c_client *i2c_client;
+       struct tcpm_port *tcpm_port;
+       struct tcpc_dev tcpc_dev;
+       struct tcpc_config tcpc_config;
+
+       struct regulator *vbus;
+
+       int gpio_int_n;
+       int gpio_int_n_irq;
+       struct extcon_dev *extcon;
+
+       struct workqueue_struct *wq;
+       struct delayed_work bc_lvl_handler;
+
+       atomic_t pm_suspend;
+       atomic_t i2c_busy;
+
+       /* lock for sharing chip states */
+       struct mutex lock;
+
+       /* chip status */
+       enum toggling_mode toggling_mode;
+       enum src_current_status src_current_status;
+       bool intr_togdone;
+       bool intr_bc_lvl;
+       bool intr_comp_chng;
+
+       /* port status */
+       bool pull_up;
+       bool vconn_on;
+       bool vbus_on;
+       bool charge_on;
+       bool vbus_present;
+       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;
+       /* lock for log buffer access */
+       struct mutex logbuffer_lock;
+       int logbuffer_head;
+       int logbuffer_tail;
+       u8 *logbuffer[LOG_BUFFER_ENTRIES];
+#endif
+};
+
+/*
+ * Logging
+ */
+#ifdef CONFIG_DEBUG_FS
+
+static bool fusb302_log_full(struct fusb302_chip *chip)
+{
+       return chip->logbuffer_tail ==
+               (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
+}
+
+static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
+                        va_list args)
+{
+       char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
+       u64 ts_nsec = local_clock();
+       unsigned long rem_nsec;
+
+       if (!chip->logbuffer[chip->logbuffer_head]) {
+               chip->logbuffer[chip->logbuffer_head] =
+                               kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
+               if (!chip->logbuffer[chip->logbuffer_head])
+                       return;
+       }
+
+       vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
+
+       mutex_lock(&chip->logbuffer_lock);
+
+       if (fusb302_log_full(chip)) {
+               chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
+               strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
+       }
+
+       if (chip->logbuffer_head < 0 ||
+           chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
+               dev_warn(chip->dev,
+                        "Bad log buffer index %d\n", chip->logbuffer_head);
+               goto abort;
+       }
+
+       if (!chip->logbuffer[chip->logbuffer_head]) {
+               dev_warn(chip->dev,
+                        "Log buffer index %d is NULL\n", chip->logbuffer_head);
+               goto abort;
+       }
+
+       rem_nsec = do_div(ts_nsec, 1000000000);
+       scnprintf(chip->logbuffer[chip->logbuffer_head],
+                 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
+                 (unsigned long)ts_nsec, rem_nsec / 1000,
+                 tmpbuffer);
+       chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
+
+abort:
+       mutex_unlock(&chip->logbuffer_lock);
+}
+
+static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       _fusb302_log(chip, fmt, args);
+       va_end(args);
+}
+
+static int fusb302_debug_show(struct seq_file *s, void *v)
+{
+       struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
+       int tail;
+
+       mutex_lock(&chip->logbuffer_lock);
+       tail = chip->logbuffer_tail;
+       while (tail != chip->logbuffer_head) {
+               seq_printf(s, "%s\n", chip->logbuffer[tail]);
+               tail = (tail + 1) % LOG_BUFFER_ENTRIES;
+       }
+       if (!seq_has_overflowed(s))
+               chip->logbuffer_tail = tail;
+       mutex_unlock(&chip->logbuffer_lock);
+
+       return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
+
+static struct dentry *rootdir;
+
+static void fusb302_debugfs_init(struct fusb302_chip *chip)
+{
+       mutex_init(&chip->logbuffer_lock);
+       if (!rootdir)
+               rootdir = debugfs_create_dir("fusb302", NULL);
+
+       chip->dentry = debugfs_create_file(dev_name(chip->dev),
+                                          S_IFREG | 0444, rootdir,
+                                          chip, &fusb302_debug_fops);
+}
+
+static void fusb302_debugfs_exit(struct fusb302_chip *chip)
+{
+       debugfs_remove(chip->dentry);
+       debugfs_remove(rootdir);
+}
+
+#else
+
+static void fusb302_log(const struct fusb302_chip *chip,
+                       const char *fmt, ...) { }
+static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
+static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
+
+#endif
+
+#define FUSB302_RESUME_RETRY 10
+#define FUSB302_RESUME_RETRY_SLEEP 50
+
+static bool fusb302_is_suspended(struct fusb302_chip *chip)
+{
+       int retry_cnt;
+
+       for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
+               if (atomic_read(&chip->pm_suspend)) {
+                       dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n",
+                               retry_cnt + 1, FUSB302_RESUME_RETRY);
+                       msleep(FUSB302_RESUME_RETRY_SLEEP);
+               } else {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+static int fusb302_i2c_write(struct fusb302_chip *chip,
+                            u8 address, u8 data)
+{
+       int ret = 0;
+
+       atomic_set(&chip->i2c_busy, 1);
+
+       if (fusb302_is_suspended(chip)) {
+               atomic_set(&chip->i2c_busy, 0);
+               return -ETIMEDOUT;
+       }
+
+       ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
+       if (ret < 0)
+               fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
+                           data, address, ret);
+       atomic_set(&chip->i2c_busy, 0);
+
+       return ret;
+}
+
+static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
+                                  u8 length, const u8 *data)
+{
+       int ret = 0;
+
+       if (length <= 0)
+               return ret;
+       atomic_set(&chip->i2c_busy, 1);
+
+       if (fusb302_is_suspended(chip)) {
+               atomic_set(&chip->i2c_busy, 0);
+               return -ETIMEDOUT;
+       }
+
+       ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
+                                            length, data);
+       if (ret < 0)
+               fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
+                           address, length, ret);
+       atomic_set(&chip->i2c_busy, 0);
+
+       return ret;
+}
+
+static int fusb302_i2c_read(struct fusb302_chip *chip,
+                           u8 address, u8 *data)
+{
+       int ret = 0;
+
+       atomic_set(&chip->i2c_busy, 1);
+
+       if (fusb302_is_suspended(chip)) {
+               atomic_set(&chip->i2c_busy, 0);
+               return -ETIMEDOUT;
+       }
+
+       ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
+       *data = (u8)ret;
+       if (ret < 0)
+               fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
+       atomic_set(&chip->i2c_busy, 0);
+
+       return ret;
+}
+
+static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
+                                 u8 length, u8 *data)
+{
+       int ret = 0;
+
+       if (length <= 0)
+               return ret;
+       atomic_set(&chip->i2c_busy, 1);
+
+       if (fusb302_is_suspended(chip)) {
+               atomic_set(&chip->i2c_busy, 0);
+               return -ETIMEDOUT;
+       }
+
+       ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
+                                           length, data);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
+                           address, length, ret);
+               goto done;
+       }
+       if (ret != length) {
+               fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
+                           ret, length, address);
+               ret = -EIO;
+       }
+
+done:
+       atomic_set(&chip->i2c_busy, 0);
+
+       return ret;
+}
+
+static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
+                                 u8 mask, u8 value)
+{
+       int ret = 0;
+       u8 data;
+
+       ret = fusb302_i2c_read(chip, address, &data);
+       if (ret < 0)
+               return ret;
+       data &= ~mask;
+       data |= value;
+       ret = fusb302_i2c_write(chip, address, data);
+       if (ret < 0)
+               return ret;
+
+       return ret;
+}
+
+static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
+                               u8 set_bits)
+{
+       return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
+}
+
+static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
+                                 u8 clear_bits)
+{
+       return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
+}
+
+static int fusb302_sw_reset(struct fusb302_chip *chip)
+{
+       int ret = 0;
+
+       ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
+                               FUSB_REG_RESET_SW_RESET);
+       if (ret < 0)
+               fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
+       else
+               fusb302_log(chip, "sw reset");
+
+       return ret;
+}
+
+static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
+{
+       int ret = 0;
+
+       ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
+                                  FUSB_REG_CONTROL3_N_RETRIES_3 |
+                                  FUSB_REG_CONTROL3_AUTO_RETRY);
+
+       return ret;
+}
+
+/*
+ * initialize interrupt on the chip
+ * - unmasked interrupt: VBUS_OK
+ */
+static int fusb302_init_interrupt(struct fusb302_chip *chip)
+{
+       int ret = 0;
+
+       ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
+                               0xFF & ~FUSB_REG_MASK_VBUSOK);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
+                                    FUSB_REG_CONTROL0_INT_MASK);
+       if (ret < 0)
+               return ret;
+
+       return ret;
+}
+
+static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
+{
+       int ret = 0;
+
+       ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
+
+       return ret;
+}
+
+static int tcpm_init(struct tcpc_dev *dev)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+       u8 data;
+
+       ret = fusb302_sw_reset(chip);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_enable_tx_auto_retries(chip);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_init_interrupt(chip);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
+       if (ret < 0)
+               return ret;
+       chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
+       ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
+       if (ret < 0)
+               return ret;
+       fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
+
+       return ret;
+}
+
+static int tcpm_get_vbus(struct tcpc_dev *dev)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+
+       mutex_lock(&chip->lock);
+       ret = chip->vbus_present ? 1 : 0;
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static int tcpm_get_current_limit(struct tcpc_dev *dev)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int current_limit = 0;
+       unsigned long timeout;
+
+       if (!chip->extcon)
+               return 0;
+
+       /*
+        * USB2 Charger detection may still be in progress when we get here,
+        * this can take upto 600ms, wait 800ms max.
+        */
+       timeout = jiffies + msecs_to_jiffies(800);
+       do {
+               if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
+                       current_limit = 500;
+
+               if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
+                   extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
+                       current_limit = 1500;
+
+               if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
+                       current_limit = 2000;
+
+               msleep(50);
+       } while (current_limit == 0 && time_before(jiffies, timeout));
+
+       return current_limit;
+}
+
+static int fusb302_set_cc_pull(struct fusb302_chip *chip,
+                              bool pull_up, bool pull_down)
+{
+       int ret = 0;
+       u8 data = 0x00;
+       u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
+                 FUSB_REG_SWITCHES0_CC2_PU_EN |
+                 FUSB_REG_SWITCHES0_CC1_PD_EN |
+                 FUSB_REG_SWITCHES0_CC2_PD_EN;
+
+       if (pull_up)
+               data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
+                       FUSB_REG_SWITCHES0_CC1_PU_EN :
+                       FUSB_REG_SWITCHES0_CC2_PU_EN;
+       if (pull_down)
+               data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
+                       FUSB_REG_SWITCHES0_CC2_PD_EN;
+       ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
+                                    mask, data);
+       if (ret < 0)
+               return ret;
+       chip->pull_up = pull_up;
+
+       return ret;
+}
+
+static int fusb302_set_src_current(struct fusb302_chip *chip,
+                                  enum src_current_status status)
+{
+       int ret = 0;
+
+       chip->src_current_status = status;
+       switch (status) {
+       case SRC_CURRENT_DEFAULT:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
+                                            FUSB_REG_CONTROL0_HOST_CUR_MASK,
+                                            FUSB_REG_CONTROL0_HOST_CUR_DEF);
+               break;
+       case SRC_CURRENT_MEDIUM:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
+                                            FUSB_REG_CONTROL0_HOST_CUR_MASK,
+                                            FUSB_REG_CONTROL0_HOST_CUR_MED);
+               break;
+       case SRC_CURRENT_HIGH:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
+                                            FUSB_REG_CONTROL0_HOST_CUR_MASK,
+                                            FUSB_REG_CONTROL0_HOST_CUR_HIGH);
+               break;
+       default:
+               break;
+       }
+
+       return ret;
+}
+
+static int fusb302_set_toggling(struct fusb302_chip *chip,
+                               enum toggling_mode mode)
+{
+       int ret = 0;
+
+       /* first disable toggling */
+       ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
+                                    FUSB_REG_CONTROL2_TOGGLE);
+       if (ret < 0)
+               return ret;
+       /* mask interrupts for SRC or SNK */
+       ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
+                                  FUSB_REG_MASK_BC_LVL |
+                                  FUSB_REG_MASK_COMP_CHNG);
+       if (ret < 0)
+               return ret;
+       chip->intr_bc_lvl = false;
+       chip->intr_comp_chng = false;
+       /* configure toggling mode: none/snk/src/drp */
+       switch (mode) {
+       case TOGGLINE_MODE_OFF:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
+                                            FUSB_REG_CONTROL2_MODE_MASK,
+                                            FUSB_REG_CONTROL2_MODE_NONE);
+               if (ret < 0)
+                       return ret;
+               break;
+       case TOGGLING_MODE_SNK:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
+                                            FUSB_REG_CONTROL2_MODE_MASK,
+                                            FUSB_REG_CONTROL2_MODE_UFP);
+               if (ret < 0)
+                       return ret;
+               break;
+       case TOGGLING_MODE_SRC:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
+                                            FUSB_REG_CONTROL2_MODE_MASK,
+                                            FUSB_REG_CONTROL2_MODE_DFP);
+               if (ret < 0)
+                       return ret;
+               break;
+       case TOGGLING_MODE_DRP:
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
+                                            FUSB_REG_CONTROL2_MODE_MASK,
+                                            FUSB_REG_CONTROL2_MODE_DRP);
+               if (ret < 0)
+                       return ret;
+               break;
+       default:
+               break;
+       }
+
+       if (mode == TOGGLINE_MODE_OFF) {
+               /* mask TOGDONE interrupt */
+               ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
+                                          FUSB_REG_MASKA_TOGDONE);
+               if (ret < 0)
+                       return ret;
+               chip->intr_togdone = false;
+       } else {
+               /* Datasheet says vconn MUST be off when toggling */
+               WARN(chip->vconn_on, "Vconn is on during toggle start");
+               /* unmask TOGDONE interrupt */
+               ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
+                                            FUSB_REG_MASKA_TOGDONE);
+               if (ret < 0)
+                       return ret;
+               chip->intr_togdone = true;
+               /* start toggling */
+               ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
+                                          FUSB_REG_CONTROL2_TOGGLE);
+               if (ret < 0)
+                       return ret;
+               /* during toggling, consider cc as Open */
+               chip->cc1 = TYPEC_CC_OPEN;
+               chip->cc2 = TYPEC_CC_OPEN;
+       }
+       chip->toggling_mode = mode;
+
+       return ret;
+}
+
+static const char * const typec_cc_status_name[] = {
+       [TYPEC_CC_OPEN]         = "Open",
+       [TYPEC_CC_RA]           = "Ra",
+       [TYPEC_CC_RD]           = "Rd",
+       [TYPEC_CC_RP_DEF]       = "Rp-def",
+       [TYPEC_CC_RP_1_5]       = "Rp-1.5",
+       [TYPEC_CC_RP_3_0]       = "Rp-3.0",
+};
+
+static const enum src_current_status cc_src_current[] = {
+       [TYPEC_CC_OPEN]         = SRC_CURRENT_DEFAULT,
+       [TYPEC_CC_RA]           = SRC_CURRENT_DEFAULT,
+       [TYPEC_CC_RD]           = SRC_CURRENT_DEFAULT,
+       [TYPEC_CC_RP_DEF]       = SRC_CURRENT_DEFAULT,
+       [TYPEC_CC_RP_1_5]       = SRC_CURRENT_MEDIUM,
+       [TYPEC_CC_RP_3_0]       = SRC_CURRENT_HIGH,
+};
+
+static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+       bool pull_up, pull_down;
+       u8 rd_mda;
+
+       mutex_lock(&chip->lock);
+       switch (cc) {
+       case TYPEC_CC_OPEN:
+               pull_up = false;
+               pull_down = false;
+               break;
+       case TYPEC_CC_RD:
+               pull_up = false;
+               pull_down = true;
+               break;
+       case TYPEC_CC_RP_DEF:
+       case TYPEC_CC_RP_1_5:
+       case TYPEC_CC_RP_3_0:
+               pull_up = true;
+               pull_down = false;
+               break;
+       default:
+               fusb302_log(chip, "unsupported cc value %s",
+                           typec_cc_status_name[cc]);
+               ret = -EINVAL;
+               goto done;
+       }
+       ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
+               goto done;
+       }
+       ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
+       if (ret < 0) {
+               fusb302_log(chip,
+                           "cannot set cc pulling up %s, down %s, ret = %d",
+                           pull_up ? "True" : "False",
+                           pull_down ? "True" : "False",
+                           ret);
+               goto done;
+       }
+       /* reset the cc status */
+       chip->cc1 = TYPEC_CC_OPEN;
+       chip->cc2 = TYPEC_CC_OPEN;
+       /* adjust current for SRC */
+       if (pull_up) {
+               ret = fusb302_set_src_current(chip, cc_src_current[cc]);
+               if (ret < 0) {
+                       fusb302_log(chip, "cannot set src current %s, ret=%d",
+                                   typec_cc_status_name[cc], ret);
+                       goto done;
+               }
+       }
+       /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
+       if (pull_up) {
+               rd_mda = rd_mda_value[cc_src_current[cc]];
+               ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
+               if (ret < 0) {
+                       fusb302_log(chip,
+                                   "cannot set SRC measure value, ret=%d",
+                                   ret);
+                       goto done;
+               }
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
+                                            FUSB_REG_MASK_BC_LVL |
+                                            FUSB_REG_MASK_COMP_CHNG,
+                                            FUSB_REG_MASK_COMP_CHNG);
+               if (ret < 0) {
+                       fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
+                                   ret);
+                       goto done;
+               }
+               chip->intr_bc_lvl = false;
+               chip->intr_comp_chng = true;
+       }
+       if (pull_down) {
+               ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
+                                            FUSB_REG_MASK_BC_LVL |
+                                            FUSB_REG_MASK_COMP_CHNG,
+                                            FUSB_REG_MASK_BC_LVL);
+               if (ret < 0) {
+                       fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
+                                   ret);
+                       goto done;
+               }
+               chip->intr_bc_lvl = true;
+               chip->intr_comp_chng = false;
+       }
+       fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
+done:
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
+                      enum typec_cc_status *cc2)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+
+       mutex_lock(&chip->lock);
+       *cc1 = chip->cc1;
+       *cc2 = chip->cc2;
+       fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
+                   typec_cc_status_name[*cc2]);
+       mutex_unlock(&chip->lock);
+
+       return 0;
+}
+
+static int tcpm_set_polarity(struct tcpc_dev *dev,
+                            enum typec_cc_polarity polarity)
+{
+       return 0;
+}
+
+static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+       u8 switches0_data = 0x00;
+       u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
+                           FUSB_REG_SWITCHES0_VCONN_CC2;
+
+       mutex_lock(&chip->lock);
+       if (chip->vconn_on == on) {
+               fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
+               goto done;
+       }
+       if (on) {
+               switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
+                                FUSB_REG_SWITCHES0_VCONN_CC2 :
+                                FUSB_REG_SWITCHES0_VCONN_CC1;
+       }
+       ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
+                                    switches0_mask, switches0_data);
+       if (ret < 0)
+               goto done;
+       chip->vconn_on = on;
+       fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
+done:
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+
+       mutex_lock(&chip->lock);
+       if (chip->vbus_on == on) {
+               fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
+       } else {
+               if (on)
+                       ret = regulator_enable(chip->vbus);
+               else
+                       ret = regulator_disable(chip->vbus);
+               if (ret < 0) {
+                       fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
+                                   on ? "enable" : "disable", ret);
+                       goto done;
+               }
+               chip->vbus_on = on;
+               fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
+       }
+       if (chip->charge_on == charge)
+               fusb302_log(chip, "charge is already %s",
+                           charge ? "On" : "Off");
+       else
+               chip->charge_on = charge;
+
+done:
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
+{
+       return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
+                                   FUSB_REG_CONTROL0_TX_FLUSH);
+}
+
+static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
+{
+       return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
+                                   FUSB_REG_CONTROL1_RX_FLUSH);
+}
+
+static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
+{
+       if (on)
+               return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
+                                           FUSB_REG_SWITCHES1_AUTO_GCRC);
+       return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
+                                           FUSB_REG_SWITCHES1_AUTO_GCRC);
+}
+
+static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
+{
+       int ret = 0;
+       u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
+       u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
+                             FUSB_REG_MASKA_HARDSENT |
+                             FUSB_REG_MASKA_TX_SUCCESS |
+                             FUSB_REG_MASKA_HARDRESET;
+       u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
+
+       ret = on ?
+               fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
+               fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
+       if (ret < 0)
+               return ret;
+       ret = on ?
+               fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
+               fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
+       if (ret < 0)
+               return ret;
+       ret = on ?
+               fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
+               fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
+       return ret;
+}
+
+static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+
+       mutex_lock(&chip->lock);
+       ret = fusb302_pd_rx_flush(chip);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
+               goto done;
+       }
+       ret = fusb302_pd_tx_flush(chip);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
+               goto done;
+       }
+       ret = fusb302_pd_set_auto_goodcrc(chip, on);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
+                           on ? "on" : "off", ret);
+               goto done;
+       }
+       ret = fusb302_pd_set_interrupts(chip, on);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
+                           on ? "on" : "off", ret);
+               goto done;
+       }
+       fusb302_log(chip, "pd := %s", on ? "on" : "off");
+done:
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static const char * const typec_role_name[] = {
+       [TYPEC_SINK]            = "Sink",
+       [TYPEC_SOURCE]          = "Source",
+};
+
+static const char * const typec_data_role_name[] = {
+       [TYPEC_DEVICE]          = "Device",
+       [TYPEC_HOST]            = "Host",
+};
+
+static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
+                         enum typec_role pwr, enum typec_data_role data)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+       u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
+                           FUSB_REG_SWITCHES1_DATAROLE;
+       u8 switches1_data = 0x00;
+
+       mutex_lock(&chip->lock);
+       if (pwr == TYPEC_SOURCE)
+               switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
+       if (data == TYPEC_HOST)
+               switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
+       ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
+                                    switches1_mask, switches1_data);
+       if (ret < 0) {
+               fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
+                           typec_role_name[pwr], typec_data_role_name[data],
+                           ret);
+               goto done;
+       }
+       printk("pd header := %s, %s\n", typec_role_name[pwr],
+                   typec_data_role_name[data]);
+done:
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static int tcpm_start_drp_toggling(struct tcpc_dev *dev,
+                                  enum typec_cc_status cc)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+
+       mutex_lock(&chip->lock);
+       ret = fusb302_set_src_current(chip, cc_src_current[cc]);
+       if (ret < 0) {
+               fusb302_log(chip, "unable to set src current %s, ret=%d",
+                           typec_cc_status_name[cc], ret);
+               goto done;
+       }
+       ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
+       if (ret < 0) {
+               fusb302_log(chip,
+                           "unable to start drp toggling, ret=%d", ret);
+               goto done;
+       }
+       fusb302_log(chip, "start drp toggling");
+done:
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static int fusb302_pd_send_message(struct fusb302_chip *chip,
+                                  const struct pd_message *msg)
+{
+       int ret = 0;
+       u8 buf[40];
+       u8 pos = 0;
+       int len;
+
+       /* SOP tokens */
+       buf[pos++] = FUSB302_TKN_SYNC1;
+       buf[pos++] = FUSB302_TKN_SYNC1;
+       buf[pos++] = FUSB302_TKN_SYNC1;
+       buf[pos++] = FUSB302_TKN_SYNC2;
+
+       len = pd_header_cnt_le(msg->header) * 4;
+       /* plug 2 for header */
+       len += 2;
+       if (len > 0x1F) {
+               fusb302_log(chip,
+                           "PD message too long %d (incl. header)", len);
+               return -EINVAL;
+       }
+       /* packsym tells the FUSB302 chip that the next X bytes are payload */
+       buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
+       memcpy(&buf[pos], &msg->header, sizeof(msg->header));
+       pos += sizeof(msg->header);
+
+       len -= 2;
+       memcpy(&buf[pos], msg->payload, len);
+       pos += len;
+
+       /* CRC */
+       buf[pos++] = FUSB302_TKN_JAMCRC;
+       /* EOP */
+       buf[pos++] = FUSB302_TKN_EOP;
+       /* turn tx off after sending message */
+       buf[pos++] = FUSB302_TKN_TXOFF;
+       /* start transmission */
+       buf[pos++] = FUSB302_TKN_TXON;
+
+       ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
+       if (ret < 0)
+               return ret;
+       fusb302_log(chip, "sending PD message header: %x", msg->header);
+       fusb302_log(chip, "sending PD message len: %d", len);
+
+       return ret;
+}
+
+static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
+{
+       return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
+                                   FUSB_REG_CONTROL3_SEND_HARDRESET);
+}
+
+static const char * const transmit_type_name[] = {
+       [TCPC_TX_SOP]                   = "SOP",
+       [TCPC_TX_SOP_PRIME]             = "SOP'",
+       [TCPC_TX_SOP_PRIME_PRIME]       = "SOP''",
+       [TCPC_TX_SOP_DEBUG_PRIME]       = "DEBUG'",
+       [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
+       [TCPC_TX_HARD_RESET]            = "HARD_RESET",
+       [TCPC_TX_CABLE_RESET]           = "CABLE_RESET",
+       [TCPC_TX_BIST_MODE_2]           = "BIST_MODE_2",
+};
+
+static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
+                           const struct pd_message *msg)
+{
+       struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
+                                                tcpc_dev);
+       int ret = 0;
+
+       mutex_lock(&chip->lock);
+       switch (type) {
+       case TCPC_TX_SOP:
+               ret = fusb302_pd_send_message(chip, msg);
+               if (ret < 0)
+                       fusb302_log(chip,
+                                   "cannot send PD message, ret=%d", ret);
+               break;
+       case TCPC_TX_HARD_RESET:
+               ret = fusb302_pd_send_hardreset(chip);
+               if (ret < 0)
+                       fusb302_log(chip,
+                                   "cannot send hardreset, ret=%d", ret);
+               break;
+       default:
+               fusb302_log(chip, "type %s not supported",
+                           transmit_type_name[type]);
+               ret = -EINVAL;
+       }
+       mutex_unlock(&chip->lock);
+
+       return ret;
+}
+
+static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
+{
+       if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
+               return TYPEC_CC_RP_3_0;
+       if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
+               return TYPEC_CC_RP_1_5;
+       if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
+               return TYPEC_CC_RP_DEF;
+       return TYPEC_CC_OPEN;
+}
+
+static void fusb302_bc_lvl_handler_work(struct work_struct *work)
+{
+       struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
+                                                bc_lvl_handler.work);
+       int ret = 0;
+       u8 status0;
+       u8 bc_lvl;
+       enum typec_cc_status cc_status;
+
+       mutex_lock(&chip->lock);
+       if (!chip->intr_bc_lvl) {
+               fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
+               goto done;
+       }
+       ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
+       if (ret < 0)
+               goto done;
+       fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
+       if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
+               fusb302_log(chip, "CC activities detected, delay handling");
+               mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
+                                msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
+               goto done;
+       }
+       bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
+       cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
+       if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
+               if (chip->cc1 != cc_status) {
+                       fusb302_log(chip, "cc1: %s -> %s",
+                                   typec_cc_status_name[chip->cc1],
+                                   typec_cc_status_name[cc_status]);
+                       chip->cc1 = cc_status;
+                       tcpm_cc_change(chip->tcpm_port);
+               }
+       } else {
+               if (chip->cc2 != cc_status) {
+                       fusb302_log(chip, "cc2: %s -> %s",
+                                   typec_cc_status_name[chip->cc2],
+                                   typec_cc_status_name[cc_status]);
+                       chip->cc2 = cc_status;
+                       tcpm_cc_change(chip->tcpm_port);
+               }
+       }
+
+done:
+       mutex_unlock(&chip->lock);
+}
+
+#define PDO_FIXED_FLAGS \
+       (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
+
+static const u32 src_pdo[] = {
+       PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
+};
+
+static const u32 snk_pdo[] = {
+       PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
+};
+
+static const struct tcpc_config fusb302_tcpc_config = {
+       .src_pdo = src_pdo,
+       .nr_src_pdo = ARRAY_SIZE(src_pdo),
+       .operating_snk_mw = 2500,
+       .type = TYPEC_PORT_DRP,
+       .data = TYPEC_PORT_DRD,
+       .default_role = TYPEC_SINK,
+       .alt_modes = NULL,
+};
+
+static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
+{
+       fusb302_tcpc_dev->init = tcpm_init;
+       fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
+       fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
+       fusb302_tcpc_dev->set_cc = tcpm_set_cc;
+       fusb302_tcpc_dev->get_cc = tcpm_get_cc;
+       fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
+       fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
+       fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
+       fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
+       fusb302_tcpc_dev->set_roles = tcpm_set_roles;
+       fusb302_tcpc_dev->start_drp_toggling = tcpm_start_drp_toggling;
+       fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
+}
+
+static const char * const cc_polarity_name[] = {
+       [TYPEC_POLARITY_CC1]    = "Polarity_CC1",
+       [TYPEC_POLARITY_CC2]    = "Polarity_CC2",
+};
+
+static int fusb302_set_cc_polarity(struct fusb302_chip *chip,
+                                  enum typec_cc_polarity cc_polarity)
+{
+       int ret = 0;
+       u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
+                           FUSB_REG_SWITCHES0_CC2_PU_EN |
+                           FUSB_REG_SWITCHES0_VCONN_CC1 |
+                           FUSB_REG_SWITCHES0_VCONN_CC2 |
+                           FUSB_REG_SWITCHES0_MEAS_CC1 |
+                           FUSB_REG_SWITCHES0_MEAS_CC2;
+       u8 switches0_data = 0x00;
+       u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
+                           FUSB_REG_SWITCHES1_TXCC2_EN;
+       u8 switches1_data = 0x00;
+
+       if (cc_polarity == TYPEC_POLARITY_CC1) {
+               switches0_data = FUSB_REG_SWITCHES0_MEAS_CC1;
+               if (chip->vconn_on)
+                       switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
+               if (chip->pull_up)
+                       switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
+               switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
+       } else {
+               switches0_data = FUSB_REG_SWITCHES0_MEAS_CC2;
+               if (chip->vconn_on)
+                       switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
+               if (chip->pull_up)
+                       switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
+               switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
+       }
+       ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
+                                    switches0_mask, switches0_data);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
+                                    switches1_mask, switches1_data);
+       if (ret < 0)
+               return ret;
+       chip->cc_polarity = cc_polarity;
+
+       return ret;
+}
+
+static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
+                                     u8 togdone_result)
+{
+       int ret = 0;
+       u8 status0;
+       u8 bc_lvl;
+       enum typec_cc_polarity cc_polarity;
+       enum typec_cc_status cc_status_active, cc1, cc2;
+
+       /* set pull_up, pull_down */
+       ret = fusb302_set_cc_pull(chip, false, true);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot set cc to pull down, ret=%d", ret);
+               return ret;
+       }
+       /* set polarity */
+       cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
+                     TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
+       ret = fusb302_set_cc_polarity(chip, cc_polarity);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
+                           cc_polarity_name[cc_polarity], ret);
+               return ret;
+       }
+       /* fusb302_set_cc_polarity() has set the correct measure block */
+       ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
+       if (ret < 0)
+               return ret;
+       bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
+       cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
+       /* restart toggling if the cc status on the active line is OPEN */
+       if (cc_status_active == TYPEC_CC_OPEN) {
+               fusb302_log(chip, "restart toggling as CC_OPEN detected");
+               ret = fusb302_set_toggling(chip, chip->toggling_mode);
+               return ret;
+       }
+       /* update tcpm with the new cc value */
+       cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
+             cc_status_active : TYPEC_CC_OPEN;
+       cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
+             cc_status_active : TYPEC_CC_OPEN;
+       if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
+               chip->cc1 = cc1;
+               chip->cc2 = cc2;
+               tcpm_cc_change(chip->tcpm_port);
+       }
+       /* turn off toggling */
+       ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
+       if (ret < 0) {
+               fusb302_log(chip,
+                           "cannot set toggling mode off, ret=%d", ret);
+               return ret;
+       }
+       /* unmask bc_lvl interrupt */
+       ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
+       if (ret < 0) {
+               fusb302_log(chip,
+                           "cannot unmask bc_lcl interrupt, ret=%d", ret);
+               return ret;
+       }
+       chip->intr_bc_lvl = true;
+       fusb302_log(chip, "detected cc1=%s, cc2=%s",
+                   typec_cc_status_name[cc1],
+                   typec_cc_status_name[cc2]);
+
+       return ret;
+}
+
+static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
+                                     u8 togdone_result)
+{
+       /*
+        * - set polarity (measure cc, vconn, tx)
+        * - set pull_up, pull_down
+        * - set cc1, cc2, and update to tcpm_port
+        * - set I_COMP interrupt on
+        */
+       int ret = 0;
+       u8 status0;
+       u8 ra_mda = ra_mda_value[chip->src_current_status];
+       u8 rd_mda = rd_mda_value[chip->src_current_status];
+       bool ra_comp, rd_comp;
+       enum typec_cc_polarity cc_polarity;
+       enum typec_cc_status cc_status_active, cc1, cc2;
+
+       /* set pull_up, pull_down */
+       ret = fusb302_set_cc_pull(chip, true, false);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot set cc to pull up, ret=%d", ret);
+               return ret;
+       }
+       /* set polarity */
+       cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1) ?
+                     TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
+       ret = fusb302_set_cc_polarity(chip, cc_polarity);
+       if (ret < 0) {
+               fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
+                           cc_polarity_name[cc_polarity], ret);
+               return ret;
+       }
+       /* fusb302_set_cc_polarity() has set the correct measure block */
+       ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
+       if (ret < 0)
+               return ret;
+       usleep_range(50, 100);
+       ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
+       if (ret < 0)
+               return ret;
+       rd_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
+       if (!rd_comp) {
+               ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
+               if (ret < 0)
+                       return ret;
+               usleep_range(50, 100);
+               ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
+               if (ret < 0)
+                       return ret;
+               ra_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
+       }
+       if (rd_comp)
+               cc_status_active = TYPEC_CC_OPEN;
+       else if (ra_comp)
+               cc_status_active = TYPEC_CC_RD;
+       else
+               /* Ra is not supported, report as Open */
+               cc_status_active = TYPEC_CC_OPEN;
+       /* restart toggling if the cc status on the active line is OPEN */
+       if (cc_status_active == TYPEC_CC_OPEN) {
+               fusb302_log(chip, "restart toggling as CC_OPEN detected");
+               ret = fusb302_set_toggling(chip, chip->toggling_mode);
+               return ret;
+       }
+       /* update tcpm with the new cc value */
+       cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
+             cc_status_active : TYPEC_CC_OPEN;
+       cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
+             cc_status_active : TYPEC_CC_OPEN;
+       if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
+               chip->cc1 = cc1;
+               chip->cc2 = cc2;
+               tcpm_cc_change(chip->tcpm_port);
+       }
+       /* turn off toggling */
+       ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
+       if (ret < 0) {
+               fusb302_log(chip,
+                           "cannot set toggling mode off, ret=%d", ret);
+               return ret;
+       }
+       /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
+       ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
+       if (ret < 0)
+               return ret;
+       /* unmask comp_chng interrupt */
+       ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
+                                    FUSB_REG_MASK_COMP_CHNG);
+       if (ret < 0) {
+               fusb302_log(chip,
+                           "cannot unmask bc_lcl interrupt, ret=%d", ret);
+               return ret;
+       }
+       chip->intr_comp_chng = true;
+       fusb302_log(chip, "detected cc1=%s, cc2=%s",
+                   typec_cc_status_name[cc1],
+                   typec_cc_status_name[cc2]);
+
+       return ret;
+}
+
+static int fusb302_handle_togdone(struct fusb302_chip *chip)
+{
+       int ret = 0;
+       u8 status1a;
+       u8 togdone_result;
+
+       ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
+       if (ret < 0)
+               return ret;
+       togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
+                        FUSB_REG_STATUS1A_TOGSS_MASK;
+       switch (togdone_result) {
+       case FUSB_REG_STATUS1A_TOGSS_SNK1:
+       case FUSB_REG_STATUS1A_TOGSS_SNK2:
+               return fusb302_handle_togdone_snk(chip, togdone_result);
+       case FUSB_REG_STATUS1A_TOGSS_SRC1:
+       case FUSB_REG_STATUS1A_TOGSS_SRC2:
+               return fusb302_handle_togdone_src(chip, togdone_result);
+       case FUSB_REG_STATUS1A_TOGSS_AA:
+               /* doesn't support */
+               fusb302_log(chip, "AudioAccessory not supported");
+               fusb302_set_toggling(chip, chip->toggling_mode);
+               break;
+       default:
+               fusb302_log(chip, "TOGDONE with an invalid state: %d",
+                           togdone_result);
+               fusb302_set_toggling(chip, chip->toggling_mode);
+               break;
+       }
+       return ret;
+}
+
+static int fusb302_pd_reset(struct fusb302_chip *chip)
+{
+       return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
+                                   FUSB_REG_RESET_PD_RESET);
+}
+
+static int fusb302_pd_read_message(struct fusb302_chip *chip,
+                                  struct pd_message *msg)
+{
+       int ret = 0;
+       u8 token;
+       u8 crc[4];
+       int len;
+
+       /* first SOP token */
+       ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
+       if (ret < 0)
+               return ret;
+       ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
+                                    (u8 *)&msg->header);
+       if (ret < 0)
+               return ret;
+       len = pd_header_cnt_le(msg->header) * 4;
+       /* add 4 to length to include the CRC */
+       if (len > PD_MAX_PAYLOAD * 4) {
+               fusb302_log(chip, "PD message too long %d", len);
+               return -EINVAL;
+       }
+       if (len > 0) {
+               ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
+                                            (u8 *)msg->payload);
+               if (ret < 0)
+                       return ret;
+       }
+       /* another 4 bytes to read CRC out */
+       ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
+       if (ret < 0)
+               return ret;
+       fusb302_log(chip, "PD message header: %x", msg->header);
+       fusb302_log(chip, "PD message len: %d", len);
+
+       /*
+        * Check if we've read off a GoodCRC message. If so then indicate to
+        * TCPM that the previous transmission has completed. Otherwise we pass
+        * the received message over to TCPM for processing.
+        *
+        * We make this check here instead of basing the reporting decision on
+        * the IRQ event type, as it's possible for the chip to report the
+        * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
+        * to check the message type to ensure correct reporting to TCPM.
+        */
+       if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
+               tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
+       else
+               tcpm_pd_receive(chip->tcpm_port, msg);
+
+       return ret;
+}
+
+static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
+{
+       struct fusb302_chip *chip = dev_id;
+       int ret = 0;
+       u8 interrupt;
+       u8 interrupta;
+       u8 interruptb;
+       u8 status0;
+       bool vbus_present;
+       bool comp_result;
+       bool intr_togdone;
+       bool intr_bc_lvl;
+       bool intr_comp_chng;
+       struct pd_message pd_msg;
+
+       mutex_lock(&chip->lock);
+       /* grab a snapshot of intr flags */
+       intr_togdone = chip->intr_togdone;
+       intr_bc_lvl = chip->intr_bc_lvl;
+       intr_comp_chng = chip->intr_comp_chng;
+
+       ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
+       if (ret < 0)
+               goto done;
+       ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
+       if (ret < 0)
+               goto done;
+       ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
+       if (ret < 0)
+               goto done;
+       ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
+       if (ret < 0)
+               goto done;
+       fusb302_log(chip,
+                   "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
+                   interrupt, interrupta, interruptb, status0);
+
+       if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
+               vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
+               fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
+                           vbus_present ? "On" : "Off");
+               if (vbus_present != chip->vbus_present) {
+                       chip->vbus_present = vbus_present;
+                       tcpm_vbus_change(chip->tcpm_port);
+               }
+       }
+
+       if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
+               fusb302_log(chip, "IRQ: TOGDONE");
+               ret = fusb302_handle_togdone(chip);
+               if (ret < 0) {
+                       fusb302_log(chip,
+                                   "handle togdone error, ret=%d", ret);
+                       goto done;
+               }
+       }
+
+       if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
+               fusb302_log(chip, "IRQ: BC_LVL, handler pending");
+               /*
+                * as BC_LVL interrupt can be affected by PD activity,
+                * apply delay to for the handler to wait for the PD
+                * signaling to finish.
+                */
+               mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
+                                msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
+       }
+
+       if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
+               comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
+               fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
+                           comp_result ? "true" : "false");
+               if (comp_result) {
+                       /* cc level > Rd_threashold, detach */
+                       if (chip->cc_polarity == TYPEC_POLARITY_CC1)
+                               chip->cc1 = TYPEC_CC_OPEN;
+                       else
+                               chip->cc2 = TYPEC_CC_OPEN;
+                       tcpm_cc_change(chip->tcpm_port);
+               }
+       }
+
+       if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
+               fusb302_log(chip, "IRQ: PD collision");
+               tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
+       }
+
+       if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
+               fusb302_log(chip, "IRQ: PD retry failed");
+               tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
+       }
+
+       if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
+               fusb302_log(chip, "IRQ: PD hardreset sent");
+               ret = fusb302_pd_reset(chip);
+               if (ret < 0) {
+                       fusb302_log(chip, "cannot PD reset, ret=%d", ret);
+                       goto done;
+               }
+               tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
+       }
+
+       if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
+               fusb302_log(chip, "IRQ: PD tx success");
+               ret = fusb302_pd_read_message(chip, &pd_msg);
+               if (ret < 0) {
+                       fusb302_log(chip,
+                                   "cannot read in PD message, ret=%d", ret);
+                       goto done;
+               }
+       }
+
+       if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
+               fusb302_log(chip, "IRQ: PD received hardreset");
+               ret = fusb302_pd_reset(chip);
+               if (ret < 0) {
+                       fusb302_log(chip, "cannot PD reset, ret=%d", ret);
+                       goto done;
+               }
+               tcpm_pd_hard_reset(chip->tcpm_port);
+       }
+
+       if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
+               fusb302_log(chip, "IRQ: PD sent good CRC");
+               ret = fusb302_pd_read_message(chip, &pd_msg);
+               if (ret < 0) {
+                       fusb302_log(chip,
+                                   "cannot read in PD message, ret=%d", ret);
+                       goto done;
+               }
+       }
+done:
+       mutex_unlock(&chip->lock);
+
+       return IRQ_HANDLED;
+}
+
+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", &max_uv) ||
+               device_property_read_u32(dev, "fcs,max-sink-microamp", &max_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)
+{
+       struct fusb302_chip *chip;
+       struct i2c_adapter *adapter;
+       struct device *dev = &client->dev;
+       const char *name;
+       int ret = 0;
+       u32 v;
+       struct gpio_desc *desc;
+       int id_irqnr;
+
+       adapter = to_i2c_adapter(client->dev.parent);
+       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
+               dev_err(&client->dev,
+                       "I2C/SMBus block functionality not supported!\n");
+               return -ENODEV;
+       }
+       chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
+       if (!chip)
+               return -ENOMEM;
+
+       chip->i2c_client = client;
+       i2c_set_clientdata(client, chip);
+       chip->dev = &client->dev;
+       chip->tcpc_config = fusb302_tcpc_config;
+       chip->tcpc_dev.config = &chip->tcpc_config;
+       mutex_init(&chip->lock);
+
+       if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", &v))
+               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.
+        * This device prop is for kernel internal use only and is expected
+        * to be set by the platform code which also registers the i2c client
+        * for the fusb302.
+        */
+       if (device_property_read_string(dev, "fcs,extcon-name", &name) == 0) {
+               chip->extcon = extcon_get_extcon_dev(name);
+               if (!chip->extcon)
+                       return -EPROBE_DEFER;
+       }
+
+       fusb302_debugfs_init(chip);
+
+       chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
+       if (!chip->wq) {
+               ret = -ENOMEM;
+               goto clear_client_data;
+       }
+       INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
+       init_tcpc_dev(&chip->tcpc_dev);
+
+       chip->vbus = devm_regulator_get(chip->dev, "vbus");
+
+       chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
+       if (IS_ERR(chip->tcpm_port)) {
+               ret = PTR_ERR(chip->tcpm_port);
+               if (ret != -EPROBE_DEFER)
+                       dev_err(dev, "cannot register tcpm port, ret=%d", ret);
+               goto destroy_workqueue;
+       }
+
+
+       desc = gpiod_get_index(chip->dev, NULL, 0, GPIOD_IN);
+       if (IS_ERR(desc)) {
+               pr_err("fail to get id-gpioirq\n");
+               return -1;
+       }
+
+       id_irqnr = gpiod_to_irq(desc);
+
+       ret = devm_request_threaded_irq(chip->dev, id_irqnr, NULL,
+                              fusb302_irq_intn,
+                              IRQF_ONESHOT | IRQF_TRIGGER_LOW,
+                              "fsc_interrupt_int_n", chip);
+
+       if (ret) {
+               pr_err("failed to request ret=%d, id_irqnr=%d\n",
+                      ret, id_irqnr);
+               goto tcpm_unregister_port;
+       }
+       chip->gpio_int_n_irq = id_irqnr;
+
+       pr_info("<%s> ok\n", __func__);
+
+       enable_irq_wake(id_irqnr);
+
+       return ret;
+
+tcpm_unregister_port:
+       tcpm_unregister_port(chip->tcpm_port);
+destroy_workqueue:
+       destroy_workqueue(chip->wq);
+clear_client_data:
+       i2c_set_clientdata(client, NULL);
+       fusb302_debugfs_exit(chip);
+
+       return ret;
+}
+
+static int fusb302_remove(struct i2c_client *client)
+{
+       struct fusb302_chip *chip = i2c_get_clientdata(client);
+
+       tcpm_unregister_port(chip->tcpm_port);
+       destroy_workqueue(chip->wq);
+       i2c_set_clientdata(client, NULL);
+       fusb302_debugfs_exit(chip);
+
+       return 0;
+}
+
+static int fusb302_pm_suspend(struct device *dev)
+{
+       struct fusb302_chip *chip = dev->driver_data;
+
+       if (atomic_read(&chip->i2c_busy))
+               return -EBUSY;
+       atomic_set(&chip->pm_suspend, 1);
+
+       return 0;
+}
+
+static int fusb302_pm_resume(struct device *dev)
+{
+       struct fusb302_chip *chip = dev->driver_data;
+
+       atomic_set(&chip->pm_suspend, 0);
+
+       return 0;
+}
+
+static const struct of_device_id fusb302_dt_match[] = {
+       {.compatible = "fcs,fusb302"},
+       {},
+};
+MODULE_DEVICE_TABLE(of, fusb302_dt_match);
+
+static const struct i2c_device_id fusb302_i2c_device_id[] = {
+       {"typec_fusb302", 0},
+       {},
+};
+MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
+
+static const struct dev_pm_ops fusb302_pm_ops = {
+       .suspend = fusb302_pm_suspend,
+       .resume = fusb302_pm_resume,
+};
+
+static struct i2c_driver fusb302_driver = {
+       .driver = {
+                  .name = "typec_fusb302",
+                  .pm = &fusb302_pm_ops,
+                  .of_match_table = of_match_ptr(fusb302_dt_match),
+                  },
+       .probe = fusb302_probe,
+       .remove = fusb302_remove,
+       .id_table = fusb302_i2c_device_id,
+};
+module_i2c_driver(fusb302_driver);
+
+MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
+MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/typec/fusb302/fusb302_reg.h b/drivers/usb/typec/fusb302/fusb302_reg.h
new file mode 100644 (file)
index 0000000..00b39d3
--- /dev/null
@@ -0,0 +1,177 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2016-2017 Google, Inc
+ *
+ * Fairchild FUSB302 Type-C Chip Driver
+ */
+
+#ifndef FUSB302_REG_H
+#define FUSB302_REG_H
+
+#define FUSB_REG_DEVICE_ID                     0x01
+#define FUSB_REG_SWITCHES0                     0x02
+#define FUSB_REG_SWITCHES0_CC2_PU_EN           BIT(7)
+#define FUSB_REG_SWITCHES0_CC1_PU_EN           BIT(6)
+#define FUSB_REG_SWITCHES0_VCONN_CC2           BIT(5)
+#define FUSB_REG_SWITCHES0_VCONN_CC1           BIT(4)
+#define FUSB_REG_SWITCHES0_MEAS_CC2            BIT(3)
+#define FUSB_REG_SWITCHES0_MEAS_CC1            BIT(2)
+#define FUSB_REG_SWITCHES0_CC2_PD_EN           BIT(1)
+#define FUSB_REG_SWITCHES0_CC1_PD_EN           BIT(0)
+#define FUSB_REG_SWITCHES1                     0x03
+#define FUSB_REG_SWITCHES1_POWERROLE           BIT(7)
+#define FUSB_REG_SWITCHES1_SPECREV1            BIT(6)
+#define FUSB_REG_SWITCHES1_SPECREV0            BIT(5)
+#define FUSB_REG_SWITCHES1_DATAROLE            BIT(4)
+#define FUSB_REG_SWITCHES1_AUTO_GCRC           BIT(2)
+#define FUSB_REG_SWITCHES1_TXCC2_EN            BIT(1)
+#define FUSB_REG_SWITCHES1_TXCC1_EN            BIT(0)
+#define FUSB_REG_MEASURE                       0x04
+#define FUSB_REG_MEASURE_MDAC5                 BIT(7)
+#define FUSB_REG_MEASURE_MDAC4                 BIT(6)
+#define FUSB_REG_MEASURE_MDAC3                 BIT(5)
+#define FUSB_REG_MEASURE_MDAC2                 BIT(4)
+#define FUSB_REG_MEASURE_MDAC1                 BIT(3)
+#define FUSB_REG_MEASURE_MDAC0                 BIT(2)
+#define FUSB_REG_MEASURE_VBUS                  BIT(1)
+#define FUSB_REG_MEASURE_XXXX5                 BIT(0)
+#define FUSB_REG_CONTROL0                      0x06
+#define FUSB_REG_CONTROL0_TX_FLUSH             BIT(6)
+#define FUSB_REG_CONTROL0_INT_MASK             BIT(5)
+#define FUSB_REG_CONTROL0_HOST_CUR_MASK                (0xC)
+#define FUSB_REG_CONTROL0_HOST_CUR_HIGH                (0xC)
+#define FUSB_REG_CONTROL0_HOST_CUR_MED         (0x8)
+#define FUSB_REG_CONTROL0_HOST_CUR_DEF         (0x4)
+#define FUSB_REG_CONTROL0_TX_START             BIT(0)
+#define FUSB_REG_CONTROL1                      0x07
+#define FUSB_REG_CONTROL1_ENSOP2DB             BIT(6)
+#define FUSB_REG_CONTROL1_ENSOP1DB             BIT(5)
+#define FUSB_REG_CONTROL1_BIST_MODE2           BIT(4)
+#define FUSB_REG_CONTROL1_RX_FLUSH             BIT(2)
+#define FUSB_REG_CONTROL1_ENSOP2               BIT(1)
+#define FUSB_REG_CONTROL1_ENSOP1               BIT(0)
+#define FUSB_REG_CONTROL2                      0x08
+#define FUSB_REG_CONTROL2_MODE                 BIT(1)
+#define FUSB_REG_CONTROL2_MODE_MASK            (0x6)
+#define FUSB_REG_CONTROL2_MODE_DFP             (0x6)
+#define FUSB_REG_CONTROL2_MODE_UFP             (0x4)
+#define FUSB_REG_CONTROL2_MODE_DRP             (0x2)
+#define FUSB_REG_CONTROL2_MODE_NONE            (0x0)
+#define FUSB_REG_CONTROL2_TOGGLE               BIT(0)
+#define FUSB_REG_CONTROL3                      0x09
+#define FUSB_REG_CONTROL3_SEND_HARDRESET       BIT(6)
+#define FUSB_REG_CONTROL3_BIST_TMODE           BIT(5)  /* 302B Only */
+#define FUSB_REG_CONTROL3_AUTO_HARDRESET       BIT(4)
+#define FUSB_REG_CONTROL3_AUTO_SOFTRESET       BIT(3)
+#define FUSB_REG_CONTROL3_N_RETRIES            BIT(1)
+#define FUSB_REG_CONTROL3_N_RETRIES_MASK       (0x6)
+#define FUSB_REG_CONTROL3_N_RETRIES_3          (0x6)
+#define FUSB_REG_CONTROL3_N_RETRIES_2          (0x4)
+#define FUSB_REG_CONTROL3_N_RETRIES_1          (0x2)
+#define FUSB_REG_CONTROL3_AUTO_RETRY           BIT(0)
+#define FUSB_REG_MASK                          0x0A
+#define FUSB_REG_MASK_VBUSOK                   BIT(7)
+#define FUSB_REG_MASK_ACTIVITY                 BIT(6)
+#define FUSB_REG_MASK_COMP_CHNG                        BIT(5)
+#define FUSB_REG_MASK_CRC_CHK                  BIT(4)
+#define FUSB_REG_MASK_ALERT                    BIT(3)
+#define FUSB_REG_MASK_WAKE                     BIT(2)
+#define FUSB_REG_MASK_COLLISION                        BIT(1)
+#define FUSB_REG_MASK_BC_LVL                   BIT(0)
+#define FUSB_REG_POWER                         0x0B
+#define FUSB_REG_POWER_PWR                     BIT(0)
+#define FUSB_REG_POWER_PWR_LOW                 0x1
+#define FUSB_REG_POWER_PWR_MEDIUM              0x3
+#define FUSB_REG_POWER_PWR_HIGH                        0x7
+#define FUSB_REG_POWER_PWR_ALL                 0xF
+#define FUSB_REG_RESET                         0x0C
+#define FUSB_REG_RESET_PD_RESET                        BIT(1)
+#define FUSB_REG_RESET_SW_RESET                        BIT(0)
+#define FUSB_REG_MASKA                         0x0E
+#define FUSB_REG_MASKA_OCP_TEMP                        BIT(7)
+#define FUSB_REG_MASKA_TOGDONE                 BIT(6)
+#define FUSB_REG_MASKA_SOFTFAIL                        BIT(5)
+#define FUSB_REG_MASKA_RETRYFAIL               BIT(4)
+#define FUSB_REG_MASKA_HARDSENT                        BIT(3)
+#define FUSB_REG_MASKA_TX_SUCCESS              BIT(2)
+#define FUSB_REG_MASKA_SOFTRESET               BIT(1)
+#define FUSB_REG_MASKA_HARDRESET               BIT(0)
+#define FUSB_REG_MASKB                         0x0F
+#define FUSB_REG_MASKB_GCRCSENT                        BIT(0)
+#define FUSB_REG_STATUS0A                      0x3C
+#define FUSB_REG_STATUS0A_SOFTFAIL             BIT(5)
+#define FUSB_REG_STATUS0A_RETRYFAIL            BIT(4)
+#define FUSB_REG_STATUS0A_POWER                        BIT(2)
+#define FUSB_REG_STATUS0A_RX_SOFT_RESET                BIT(1)
+#define FUSB_REG_STATUS0A_RX_HARD_RESET                BIT(0)
+#define FUSB_REG_STATUS1A                      0x3D
+#define FUSB_REG_STATUS1A_TOGSS                        BIT(3)
+#define FUSB_REG_STATUS1A_TOGSS_RUNNING                0x0
+#define FUSB_REG_STATUS1A_TOGSS_SRC1           0x1
+#define FUSB_REG_STATUS1A_TOGSS_SRC2           0x2
+#define FUSB_REG_STATUS1A_TOGSS_SNK1           0x5
+#define FUSB_REG_STATUS1A_TOGSS_SNK2           0x6
+#define FUSB_REG_STATUS1A_TOGSS_AA             0x7
+#define FUSB_REG_STATUS1A_TOGSS_POS            (3)
+#define FUSB_REG_STATUS1A_TOGSS_MASK           (0x7)
+#define FUSB_REG_STATUS1A_RXSOP2DB             BIT(2)
+#define FUSB_REG_STATUS1A_RXSOP1DB             BIT(1)
+#define FUSB_REG_STATUS1A_RXSOP                        BIT(0)
+#define FUSB_REG_INTERRUPTA                    0x3E
+#define FUSB_REG_INTERRUPTA_OCP_TEMP           BIT(7)
+#define FUSB_REG_INTERRUPTA_TOGDONE            BIT(6)
+#define FUSB_REG_INTERRUPTA_SOFTFAIL           BIT(5)
+#define FUSB_REG_INTERRUPTA_RETRYFAIL          BIT(4)
+#define FUSB_REG_INTERRUPTA_HARDSENT           BIT(3)
+#define FUSB_REG_INTERRUPTA_TX_SUCCESS         BIT(2)
+#define FUSB_REG_INTERRUPTA_SOFTRESET          BIT(1)
+#define FUSB_REG_INTERRUPTA_HARDRESET          BIT(0)
+#define FUSB_REG_INTERRUPTB                    0x3F
+#define FUSB_REG_INTERRUPTB_GCRCSENT           BIT(0)
+#define FUSB_REG_STATUS0                       0x40
+#define FUSB_REG_STATUS0_VBUSOK                        BIT(7)
+#define FUSB_REG_STATUS0_ACTIVITY              BIT(6)
+#define FUSB_REG_STATUS0_COMP                  BIT(5)
+#define FUSB_REG_STATUS0_CRC_CHK               BIT(4)
+#define FUSB_REG_STATUS0_ALERT                 BIT(3)
+#define FUSB_REG_STATUS0_WAKE                  BIT(2)
+#define FUSB_REG_STATUS0_BC_LVL_MASK           0x03
+#define FUSB_REG_STATUS0_BC_LVL_0_200          0x0
+#define FUSB_REG_STATUS0_BC_LVL_200_600                0x1
+#define FUSB_REG_STATUS0_BC_LVL_600_1230       0x2
+#define FUSB_REG_STATUS0_BC_LVL_1230_MAX       0x3
+#define FUSB_REG_STATUS0_BC_LVL1               BIT(1)
+#define FUSB_REG_STATUS0_BC_LVL0               BIT(0)
+#define FUSB_REG_STATUS1                       0x41
+#define FUSB_REG_STATUS1_RXSOP2                        BIT(7)
+#define FUSB_REG_STATUS1_RXSOP1                        BIT(6)
+#define FUSB_REG_STATUS1_RX_EMPTY              BIT(5)
+#define FUSB_REG_STATUS1_RX_FULL               BIT(4)
+#define FUSB_REG_STATUS1_TX_EMPTY              BIT(3)
+#define FUSB_REG_STATUS1_TX_FULL               BIT(2)
+#define FUSB_REG_INTERRUPT                     0x42
+#define FUSB_REG_INTERRUPT_VBUSOK              BIT(7)
+#define FUSB_REG_INTERRUPT_ACTIVITY            BIT(6)
+#define FUSB_REG_INTERRUPT_COMP_CHNG           BIT(5)
+#define FUSB_REG_INTERRUPT_CRC_CHK             BIT(4)
+#define FUSB_REG_INTERRUPT_ALERT               BIT(3)
+#define FUSB_REG_INTERRUPT_WAKE                        BIT(2)
+#define FUSB_REG_INTERRUPT_COLLISION           BIT(1)
+#define FUSB_REG_INTERRUPT_BC_LVL              BIT(0)
+#define FUSB_REG_FIFOS                         0x43
+
+/* Tokens defined for the FUSB302 TX FIFO */
+enum fusb302_txfifo_tokens {
+       FUSB302_TKN_TXON = 0xA1,
+       FUSB302_TKN_SYNC1 = 0x12,
+       FUSB302_TKN_SYNC2 = 0x13,
+       FUSB302_TKN_SYNC3 = 0x1B,
+       FUSB302_TKN_RST1 = 0x15,
+       FUSB302_TKN_RST2 = 0x16,
+       FUSB302_TKN_PACKSYM = 0x80,
+       FUSB302_TKN_JAMCRC = 0xFF,
+       FUSB302_TKN_EOP = 0x14,
+       FUSB302_TKN_TXOFF = 0xFE,
+};
+
+#endif
diff --git a/drivers/usb/typec/mux.c b/drivers/usb/typec/mux.c
new file mode 100644 (file)
index 0000000..d990aa5
--- /dev/null
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
+ * USB Type-C Multiplexer/DeMultiplexer Switch support
+ *
+ * Copyright (C) 2018 Intel Corporation
+ * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
+ *         Hans de Goede <hdegoede@redhat.com>
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/usb/typec_mux.h>
+
+static DEFINE_MUTEX(switch_lock);
+static DEFINE_MUTEX(mux_lock);
+static LIST_HEAD(switch_list);
+static LIST_HEAD(mux_list);
+
+static void *typec_switch_match(struct device_connection *con, int ep,
+                               void *data)
+{
+       struct typec_switch *sw;
+
+       list_for_each_entry(sw, &switch_list, entry)
+               if (!strcmp(con->endpoint[ep], dev_name(sw->dev)))
+                       return sw;
+
+       /*
+        * We only get called if a connection was found, tell the caller to
+        * wait for the switch to show up.
+        */
+       return ERR_PTR(-EPROBE_DEFER);
+}
+
+/**
+ * typec_switch_get - Find USB Type-C orientation switch
+ * @dev: The caller device
+ *
+ * Finds a switch linked with @dev. Returns a reference to the switch on
+ * success, NULL if no matching connection was found, or
+ * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch
+ * has not been enumerated yet.
+ */
+struct typec_switch *typec_switch_get(struct device *dev)
+{
+       struct typec_switch *sw;
+
+       mutex_lock(&switch_lock);
+       sw = device_connection_find_match(dev, "typec-switch", NULL,
+                                         typec_switch_match);
+       if (!IS_ERR_OR_NULL(sw)) {
+               WARN_ON(!try_module_get(sw->dev->driver->owner));
+               get_device(sw->dev);
+       }
+       mutex_unlock(&switch_lock);
+
+       return sw;
+}
+EXPORT_SYMBOL_GPL(typec_switch_get);
+
+/**
+ * typec_put_switch - Release USB Type-C orientation switch
+ * @sw: USB Type-C orientation switch
+ *
+ * Decrement reference count for @sw.
+ */
+void typec_switch_put(struct typec_switch *sw)
+{
+       if (!IS_ERR_OR_NULL(sw)) {
+               module_put(sw->dev->driver->owner);
+               put_device(sw->dev);
+       }
+}
+EXPORT_SYMBOL_GPL(typec_switch_put);
+
+/**
+ * typec_switch_register - Register USB Type-C orientation switch
+ * @sw: USB Type-C orientation switch
+ *
+ * This function registers a switch that can be used for routing the correct
+ * data pairs depending on the cable plug orientation from the USB Type-C
+ * connector to the USB controllers. USB Type-C plugs can be inserted
+ * right-side-up or upside-down.
+ */
+int typec_switch_register(struct typec_switch *sw)
+{
+       mutex_lock(&switch_lock);
+       list_add_tail(&sw->entry, &switch_list);
+       mutex_unlock(&switch_lock);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(typec_switch_register);
+
+/**
+ * typec_switch_unregister - Unregister USB Type-C orientation switch
+ * @sw: USB Type-C orientation switch
+ *
+ * Unregister switch that was registered with typec_switch_register().
+ */
+void typec_switch_unregister(struct typec_switch *sw)
+{
+       mutex_lock(&switch_lock);
+       list_del(&sw->entry);
+       mutex_unlock(&switch_lock);
+}
+EXPORT_SYMBOL_GPL(typec_switch_unregister);
+
+/* ------------------------------------------------------------------------- */
+
+static void *typec_mux_match(struct device_connection *con, int ep, void *data)
+{
+       struct typec_mux *mux;
+
+       list_for_each_entry(mux, &mux_list, entry)
+               if (!strcmp(con->endpoint[ep], dev_name(mux->dev)))
+                       return mux;
+
+       /*
+        * We only get called if a connection was found, tell the caller to
+        * wait for the switch to show up.
+        */
+       return ERR_PTR(-EPROBE_DEFER);
+}
+
+/**
+ * typec_mux_get - Find USB Type-C Multiplexer
+ * @dev: The caller device
+ * @name: Mux identifier
+ *
+ * Finds a mux linked to the caller. This function is primarily meant for the
+ * Type-C drivers. Returns a reference to the mux on success, NULL if no
+ * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
+ * was found but the mux has not been enumerated yet.
+ */
+struct typec_mux *typec_mux_get(struct device *dev, const char *name)
+{
+       struct typec_mux *mux;
+
+       mutex_lock(&mux_lock);
+       mux = device_connection_find_match(dev, name, NULL, typec_mux_match);
+       if (!IS_ERR_OR_NULL(mux)) {
+               WARN_ON(!try_module_get(mux->dev->driver->owner));
+               get_device(mux->dev);
+       }
+       mutex_unlock(&mux_lock);
+
+       return mux;
+}
+EXPORT_SYMBOL_GPL(typec_mux_get);
+
+/**
+ * typec_mux_put - Release handle to a Multiplexer
+ * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
+ *
+ * Decrements reference count for @mux.
+ */
+void typec_mux_put(struct typec_mux *mux)
+{
+       if (!IS_ERR_OR_NULL(mux)) {
+               module_put(mux->dev->driver->owner);
+               put_device(mux->dev);
+       }
+}
+EXPORT_SYMBOL_GPL(typec_mux_put);
+
+/**
+ * typec_mux_register - Register Multiplexer routing USB Type-C pins
+ * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
+ *
+ * USB Type-C connectors can be used for alternate modes of operation besides
+ * USB when Accessory/Alternate Modes are supported. With some of those modes,
+ * the pins on the connector need to be reconfigured. This function registers
+ * multiplexer switches routing the pins on the connector.
+ */
+int typec_mux_register(struct typec_mux *mux)
+{
+       mutex_lock(&mux_lock);
+       list_add_tail(&mux->entry, &mux_list);
+       mutex_unlock(&mux_lock);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(typec_mux_register);
+
+/**
+ * typec_mux_unregister - Unregister Multiplexer Switch
+ * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
+ *
+ * Unregister mux that was registered with typec_mux_register().
+ */
+void typec_mux_unregister(struct typec_mux *mux)
+{
+       mutex_lock(&mux_lock);
+       list_del(&mux->entry);
+       mutex_unlock(&mux_lock);
+}
+EXPORT_SYMBOL_GPL(typec_mux_unregister);
diff --git a/drivers/usb/typec/tcpci.c b/drivers/usb/typec/tcpci.c
new file mode 100644 (file)
index 0000000..3db0e28
--- /dev/null
@@ -0,0 +1,656 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * USB Type-C Port Controller Interface.
+ */
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/usb/pd.h>
+#include <linux/usb/tcpm.h>
+#include <linux/usb/typec.h>
+
+#include "tcpci.h"
+
+#define PD_RETRY_COUNT 3
+/* FIXME: bypass warn_on for pd testing */
+#if defined(WARN_ON)
+#undef WARN_ON
+#define WARN_ON(_x) _x
+#endif
+
+struct tcpci {
+       struct device *dev;
+
+       struct tcpm_port *port;
+
+       struct regmap *regmap;
+
+       bool controls_vbus;
+
+       struct tcpc_dev tcpc;
+       struct tcpci_data *data;
+};
+
+struct tcpci_chip {
+       struct tcpci *tcpci;
+       struct tcpci_data data;
+};
+
+static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc)
+{
+       return container_of(tcpc, struct tcpci, tcpc);
+}
+
+static int tcpci_read16(struct tcpci *tcpci, unsigned int reg, u16 *val)
+{
+       return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16));
+}
+
+static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val)
+{
+       return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16));
+}
+
+static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned int reg;
+       int ret;
+
+       switch (cc) {
+       case TYPEC_CC_RA:
+               reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                       (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC2_SHIFT);
+               break;
+       case TYPEC_CC_RD:
+               reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                       (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
+               break;
+       case TYPEC_CC_RP_DEF:
+               reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                       (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+                       (TCPC_ROLE_CTRL_RP_VAL_DEF <<
+                        TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_RP_1_5:
+               reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                       (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+                       (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                        TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_RP_3_0:
+               reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                       (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+                       (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
+                        TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_OPEN:
+       default:
+               reg = (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                       (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
+               break;
+       }
+
+       ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int tcpci_start_drp_toggling(struct tcpc_dev *tcpc,
+                                   enum typec_cc_status cc)
+{
+       int ret;
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned int reg = TCPC_ROLE_CTRL_DRP;
+
+       /* Handle vendor drp toggling */
+       if (tcpci->data->start_drp_toggling) {
+               ret = tcpci->data->start_drp_toggling(tcpci, tcpci->data, cc);
+               if (ret < 0)
+                       return ret;
+       }
+
+       switch (cc) {
+       default:
+       case TYPEC_CC_RP_DEF:
+               reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
+                       TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_RP_1_5:
+               reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                       TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_RP_3_0:
+               reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
+                       TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       }
+
+       if (cc == TYPEC_CC_RD)
+               reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                          (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
+       else
+               reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                          (TCPC_ROLE_CTRL_CC_RP << 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_COMMAND,
+                           TCPC_CMD_LOOK4CONNECTION);
+}
+
+static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool sink)
+{
+       switch (cc) {
+       case 0x1:
+               return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
+       case 0x2:
+               return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
+       case 0x3:
+               if (sink)
+                       return TYPEC_CC_RP_3_0;
+               /* fall through */
+       case 0x0:
+       default:
+               return TYPEC_CC_OPEN;
+       }
+}
+
+static int tcpci_get_cc(struct tcpc_dev *tcpc,
+                       enum typec_cc_status *cc1, enum typec_cc_status *cc2)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned int reg;
+       int ret;
+       int cc1_sink = 0, cc2_sink = 0;
+
+       if (tcpci->data->get_cc)
+               return tcpci->data->get_cc(tcpci, tcpci->data, cc1, cc2);
+
+       ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, &reg);
+       if (!ret) {
+               if (((reg >> TCPC_ROLE_CTRL_CC1_SHIFT) &
+                    TCPC_ROLE_CTRL_CC1_MASK) == TCPC_ROLE_CTRL_CC_RD)
+                       cc1_sink = 1;
+               if (((reg >> TCPC_ROLE_CTRL_CC2_SHIFT) &
+                    TCPC_ROLE_CTRL_CC2_MASK) == TCPC_ROLE_CTRL_CC_RD)
+                       cc2_sink = 1;
+       }
+
+       ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
+       if (ret < 0)
+               return ret;
+
+       *cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
+                                TCPC_CC_STATUS_CC1_MASK,
+                                (reg & TCPC_CC_STATUS_TERM) | cc1_sink);
+       *cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
+                                TCPC_CC_STATUS_CC2_MASK,
+                                (reg & TCPC_CC_STATUS_TERM) | cc2_sink);
+
+       return 0;
+}
+
+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;
+
+       if (tcpci->data->set_polarity)
+               return tcpci->data->set_polarity(tcpci, tcpci->data, polarity);
+
+       /* Keep the disconnect cc line open */
+       ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, &reg);
+       if (ret < 0)
+               return ret;
+
+       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_bist(struct tcpc_dev *tcpc, bool enable)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       int ret;
+
+       if (tcpci->data->set_bist) {
+               ret = tcpci->data->set_bist(tcpci, tcpci->data, enable);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       int ret;
+
+       /* Handle vendor set vconn */
+       if (tcpci->data->set_vconn) {
+               ret = tcpci->data->set_vconn(tcpci, tcpci->data, enable);
+               if (ret < 0)
+                       return ret;
+       }
+
+       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,
+                          enum typec_role role, enum typec_data_role data)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned int reg;
+       int ret;
+
+       reg = PD_REV20 << TCPC_MSG_HDR_INFO_REV_SHIFT;
+       if (role == TYPEC_SOURCE)
+               reg |= TCPC_MSG_HDR_INFO_PWR_ROLE;
+       if (data == TYPEC_HOST)
+               reg |= TCPC_MSG_HDR_INFO_DATA_ROLE;
+       ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned int reg = 0;
+       int ret;
+
+       if (enable)
+               reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET;
+       ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int tcpci_get_vbus(struct tcpc_dev *tcpc)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned int reg;
+       int ret;
+
+       ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+       if (ret < 0)
+               return ret;
+
+       return !!(reg & TCPC_POWER_STATUS_VBUS_PRES);
+}
+
+static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool sink)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       int ret;
+
+       /* Disable both source and sink first before enabling anything */
+
+       if (!source) {
+               ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                  TCPC_CMD_DISABLE_SRC_VBUS);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (!sink) {
+               ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                  TCPC_CMD_DISABLE_SINK_VBUS);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (source) {
+               ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                  TCPC_CMD_SRC_VBUS_DEFAULT);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (sink) {
+               ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                  TCPC_CMD_SINK_VBUS);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int tcpci_pd_transmit(struct tcpc_dev *tcpc,
+                            enum tcpm_transmit_type type,
+                            const struct pd_message *msg)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       u16 header = msg ? le16_to_cpu(msg->header) : 0;
+       unsigned int reg, cnt;
+       int ret;
+
+       cnt = msg ? pd_header_cnt(header) * 4 : 0;
+       ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt + 2);
+       if (ret < 0)
+               return ret;
+
+       ret = tcpci_write16(tcpci, TCPC_TX_HDR, header);
+       if (ret < 0)
+               return ret;
+
+       if (cnt > 0) {
+               ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA,
+                                      &msg->payload, cnt);
+               if (ret < 0)
+                       return ret;
+       }
+
+       reg = (PD_RETRY_COUNT << TCPC_TRANSMIT_RETRY_SHIFT) |
+               (type << TCPC_TRANSMIT_TYPE_SHIFT);
+       ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int tcpci_init(struct tcpc_dev *tcpc)
+{
+       struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+       unsigned long timeout = jiffies + msecs_to_jiffies(2000); /* XXX */
+       unsigned int reg;
+       int ret;
+        //u16 mask = 0;
+
+       while (time_before_eq(jiffies, timeout)) {
+               ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+               if (ret < 0)
+                       return ret;
+               if (!(reg & TCPC_POWER_STATUS_UNINIT))
+                       break;
+               usleep_range(10000, 20000);
+       }
+       if (time_after(jiffies, timeout))
+               return -ETIMEDOUT;
+
+       /* Handle vendor init */
+       if (tcpci->data->init) {
+               ret = tcpci->data->init(tcpci, tcpci->data);
+               if (ret < 0)
+                       return ret;
+       }
+
+       /* Clear all events */
+       ret = tcpci_write16(tcpci, TCPC_ALERT, 0xffff);
+       if (ret < 0)
+               return ret;
+
+       if (tcpci->controls_vbus)
+               reg = TCPC_POWER_STATUS_VBUS_PRES;
+       else
+               reg = 0;
+       ret = regmap_write(tcpci->regmap, TCPC_POWER_STATUS_MASK, reg);
+       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;
+       if (tcpci->controls_vbus)
+               reg |= TCPC_ALERT_POWER_STATUS;
+
+       //tcpci_read16(tcpci, TCPC_ALERT_MASK, &mask);
+       ret = tcpci_write16(tcpci, TCPC_ALERT_MASK, reg);
+       //tcpci_read16(tcpci, TCPC_ALERT_MASK, &mask);
+       return ret;
+}
+
+irqreturn_t tcpci_irq(struct tcpci *tcpci)
+{
+       u16 status;
+
+       tcpci_read16(tcpci, TCPC_ALERT, &status);
+
+       /*
+        * Clear alert status for everything except RX_STATUS, which shouldn't
+        * be cleared until we have successfully retrieved message.
+        */
+       if (status & ~TCPC_ALERT_RX_STATUS)
+               tcpci_write16(tcpci, TCPC_ALERT,
+                             status & ~TCPC_ALERT_RX_STATUS);
+
+       if (status & TCPC_ALERT_CC_STATUS)
+               tcpm_cc_change(tcpci->port);
+
+       if (status & TCPC_ALERT_POWER_STATUS) {
+               unsigned int reg;
+
+               regmap_read(tcpci->regmap, TCPC_POWER_STATUS_MASK, &reg);
+
+               /*
+                * If power status mask has been reset, then the TCPC
+                * has reset.
+                */
+               if (reg == 0xff)
+                       tcpm_tcpc_reset(tcpci->port);
+               else
+                       tcpm_vbus_change(tcpci->port);
+       }
+
+       if (status & TCPC_ALERT_RX_STATUS) {
+               struct pd_message msg;
+               unsigned int cnt;
+               u16 header;
+
+               regmap_read(tcpci->regmap, TCPC_RX_BYTE_CNT, &cnt);
+
+               tcpci_read16(tcpci, TCPC_RX_HDR, &header);
+               msg.header = cpu_to_le16(header);
+
+               if (WARN_ON(cnt > sizeof(msg.payload))) {
+                       pr_err("WARN_ON: cnt=%u > msg.payload=%d\n !!!!!", cnt, (int)sizeof(msg.payload));
+                       cnt = sizeof(msg.payload);
+               }
+
+               if (cnt > 0)
+                       regmap_raw_read(tcpci->regmap, TCPC_RX_DATA,
+                                       &msg.payload, cnt);
+
+               /* Read complete, clear RX status alert bit */
+               tcpci_write16(tcpci, TCPC_ALERT, TCPC_ALERT_RX_STATUS);
+
+               tcpm_pd_receive(tcpci->port, &msg);
+       }
+
+       if (status & TCPC_ALERT_RX_HARD_RST)
+               tcpm_pd_hard_reset(tcpci->port);
+
+       if (status & TCPC_ALERT_TX_SUCCESS)
+               tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_SUCCESS);
+       else if (status & TCPC_ALERT_TX_DISCARDED)
+               tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_DISCARDED);
+       else if (status & TCPC_ALERT_TX_FAILED)
+               tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_FAILED);
+
+       return IRQ_HANDLED;
+}
+EXPORT_SYMBOL_GPL(tcpci_irq);
+
+static irqreturn_t _tcpci_irq(int irq, void *dev_id)
+{
+       struct tcpci_chip *chip = dev_id;
+
+       return tcpci_irq(chip->tcpci);
+}
+
+static const struct regmap_config tcpci_regmap_config = {
+       .reg_bits = 8,
+       .val_bits = 8,
+
+       .max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
+};
+
+static int tcpci_parse_config(struct tcpci *tcpci)
+{
+       tcpci->controls_vbus = true; /* XXX */
+
+       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;
+}
+
+struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data)
+{
+       struct tcpci *tcpci;
+       int err;
+
+       tcpci = devm_kzalloc(dev, sizeof(*tcpci), GFP_KERNEL);
+       if (!tcpci)
+               return ERR_PTR(-ENOMEM);
+
+       tcpci->dev = dev;
+       tcpci->data = data;
+       tcpci->regmap = data->regmap;
+
+       tcpci->tcpc.init = tcpci_init;
+       tcpci->tcpc.get_vbus = tcpci_get_vbus;
+       tcpci->tcpc.set_vbus = tcpci_set_vbus;
+       tcpci->tcpc.set_cc = tcpci_set_cc;
+       tcpci->tcpc.get_cc = tcpci_get_cc;
+       tcpci->tcpc.set_bist = tcpci_set_bist;
+       tcpci->tcpc.set_polarity = tcpci_set_polarity;
+       tcpci->tcpc.set_vconn = tcpci_set_vconn;
+       tcpci->tcpc.start_drp_toggling = tcpci_start_drp_toggling;
+
+       tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx;
+       tcpci->tcpc.set_roles = tcpci_set_roles;
+       tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
+
+       err = tcpci_parse_config(tcpci);
+       if (err < 0)
+               return ERR_PTR(err);
+
+       tcpci->port = tcpm_register_port(tcpci->dev, &tcpci->tcpc);
+       if (IS_ERR(tcpci->port))
+               return ERR_CAST(tcpci->port);
+
+       return tcpci;
+}
+EXPORT_SYMBOL_GPL(tcpci_register_port);
+
+void tcpci_unregister_port(struct tcpci *tcpci)
+{
+       tcpm_unregister_port(tcpci->port);
+}
+EXPORT_SYMBOL_GPL(tcpci_unregister_port);
+
+static int tcpci_probe(struct i2c_client *client,
+                      const struct i2c_device_id *i2c_id)
+{
+       struct tcpci_chip *chip;
+       int err;
+       u16 val = 0;
+
+       chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
+       if (!chip)
+               return -ENOMEM;
+
+       chip->data.regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config);
+       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, &val,
+                              sizeof(u16));
+       if (err < 0)
+               return err;
+
+       chip->tcpci = tcpci_register_port(&client->dev, &chip->data);
+       if (IS_ERR(chip->tcpci))
+               return PTR_ERR(chip->tcpci);
+
+       err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
+                                       _tcpci_irq,
+                                       IRQF_ONESHOT | IRQF_TRIGGER_LOW,
+                                       dev_name(&client->dev), chip);
+       if (err < 0) {
+               tcpci_unregister_port(chip->tcpci);
+               return err;
+       }
+
+       return 0;
+}
+
+static int tcpci_remove(struct i2c_client *client)
+{
+       struct tcpci_chip *chip = i2c_get_clientdata(client);
+
+       tcpci_unregister_port(chip->tcpci);
+
+       return 0;
+}
+
+static const struct i2c_device_id tcpci_id[] = {
+       { "tcpci", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, tcpci_id);
+
+#ifdef CONFIG_OF
+static const struct of_device_id tcpci_of_match[] = {
+       { .compatible = "nxp,ptn5110", },
+       {},
+};
+MODULE_DEVICE_TABLE(of, tcpci_of_match);
+#endif
+
+static struct i2c_driver tcpci_i2c_driver = {
+       .driver = {
+               .name = "tcpci",
+               .of_match_table = of_match_ptr(tcpci_of_match),
+       },
+       .probe = tcpci_probe,
+       .remove = tcpci_remove,
+       .id_table = tcpci_id,
+};
+module_i2c_driver(tcpci_i2c_driver);
+
+MODULE_DESCRIPTION("USB Type-C Port Controller Interface driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/typec/tcpci.h b/drivers/usb/typec/tcpci.h
new file mode 100644 (file)
index 0000000..9d8b987
--- /dev/null
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * USB Type-C Port Controller Interface.
+ */
+
+#ifndef __LINUX_USB_TCPCI_H
+#define __LINUX_USB_TCPCI_H
+
+#define TCPC_VENDOR_ID                 0x0
+#define TCPC_PRODUCT_ID                        0x2
+#define TCPC_BCD_DEV                   0x4
+#define TCPC_TC_REV                    0x6
+#define TCPC_PD_REV                    0x8
+#define TCPC_PD_INT_REV                        0xa
+
+#define TCPC_ALERT                     0x10
+#define TCPC_ALERT_VBUS_DISCNCT                BIT(11)
+#define TCPC_ALERT_RX_BUF_OVF          BIT(10)
+#define TCPC_ALERT_FAULT               BIT(9)
+#define TCPC_ALERT_V_ALARM_LO          BIT(8)
+#define TCPC_ALERT_V_ALARM_HI          BIT(7)
+#define TCPC_ALERT_TX_SUCCESS          BIT(6)
+#define TCPC_ALERT_TX_DISCARDED                BIT(5)
+#define TCPC_ALERT_TX_FAILED           BIT(4)
+#define TCPC_ALERT_RX_HARD_RST         BIT(3)
+#define TCPC_ALERT_RX_STATUS           BIT(2)
+#define TCPC_ALERT_POWER_STATUS                BIT(1)
+#define TCPC_ALERT_CC_STATUS           BIT(0)
+
+#define TCPC_ALERT_MASK                        0x12
+#define TCPC_POWER_STATUS_MASK         0x14
+#define TCPC_FAULT_STATUS_MASK         0x15
+#define TCPC_CONFIG_STD_OUTPUT         0x18
+
+#define TCPC_TCPC_CTRL                 0x19
+#define TCPC_TCPC_CTRL_ORIENTATION     BIT(0)
+
+#define TCPC_ROLE_CTRL                 0x1a
+#define TCPC_ROLE_CTRL_DRP             BIT(6)
+#define TCPC_ROLE_CTRL_RP_VAL_SHIFT    4
+#define TCPC_ROLE_CTRL_RP_VAL_MASK     0x3
+#define TCPC_ROLE_CTRL_RP_VAL_DEF      0x0
+#define TCPC_ROLE_CTRL_RP_VAL_1_5      0x1
+#define TCPC_ROLE_CTRL_RP_VAL_3_0      0x2
+#define TCPC_ROLE_CTRL_CC2_SHIFT       2
+#define TCPC_ROLE_CTRL_CC2_MASK                0x3
+#define TCPC_ROLE_CTRL_CC1_SHIFT       0
+#define TCPC_ROLE_CTRL_CC1_MASK                0x3
+#define TCPC_ROLE_CTRL_CC_RA           0x0
+#define TCPC_ROLE_CTRL_CC_RP           0x1
+#define TCPC_ROLE_CTRL_CC_RD           0x2
+#define TCPC_ROLE_CTRL_CC_OPEN         0x3
+
+#define TCPC_FAULT_CTRL                        0x1b
+
+#define TCPC_POWER_CTRL                        0x1c
+#define TCPC_POWER_CTRL_VCONN_ENABLE   BIT(0)
+
+#define TCPC_CC_STATUS                 0x1d
+#define TCPC_CC_STATUS_TOGGLING                BIT(5)
+#define TCPC_CC_STATUS_TERM            BIT(4)
+#define TCPC_CC_STATUS_CC2_SHIFT       2
+#define TCPC_CC_STATUS_CC2_MASK                0x3
+#define TCPC_CC_STATUS_CC1_SHIFT       0
+#define TCPC_CC_STATUS_CC1_MASK                0x3
+
+#define TCPC_POWER_STATUS              0x1e
+#define TCPC_POWER_STATUS_UNINIT       BIT(6)
+#define TCPC_POWER_STATUS_VBUS_DET     BIT(3)
+#define TCPC_POWER_STATUS_VBUS_PRES    BIT(2)
+
+#define TCPC_FAULT_STATUS              0x1f
+
+#define TCPC_COMMAND                   0x23
+#define TCPC_CMD_WAKE_I2C              0x11
+#define TCPC_CMD_DISABLE_VBUS_DETECT   0x22
+#define TCPC_CMD_ENABLE_VBUS_DETECT    0x33
+#define TCPC_CMD_DISABLE_SINK_VBUS     0x44
+#define TCPC_CMD_SINK_VBUS             0x55
+#define TCPC_CMD_DISABLE_SRC_VBUS      0x66
+#define TCPC_CMD_SRC_VBUS_DEFAULT      0x77
+#define TCPC_CMD_SRC_VBUS_HIGH         0x88
+#define TCPC_CMD_LOOK4CONNECTION       0x99
+#define TCPC_CMD_RXONEMORE             0xAA
+#define TCPC_CMD_I2C_IDLE              0xFF
+
+#define TCPC_DEV_CAP_1                 0x24
+#define TCPC_DEV_CAP_2                 0x26
+#define TCPC_STD_INPUT_CAP             0x28
+#define TCPC_STD_OUTPUT_CAP            0x29
+
+#define TCPC_MSG_HDR_INFO              0x2e
+#define TCPC_MSG_HDR_INFO_DATA_ROLE    BIT(3)
+#define TCPC_MSG_HDR_INFO_PWR_ROLE     BIT(0)
+#define TCPC_MSG_HDR_INFO_REV_SHIFT    1
+#define TCPC_MSG_HDR_INFO_REV_MASK     0x3
+
+#define TCPC_RX_DETECT                 0x2f
+#define TCPC_RX_DETECT_HARD_RESET      BIT(5)
+#define TCPC_RX_DETECT_SOP             BIT(0)
+
+#define TCPC_RX_BYTE_CNT               0x30
+#define TCPC_RX_BUF_FRAME_TYPE         0x31
+#define TCPC_RX_HDR                    0x32
+#define TCPC_RX_DATA                   0x34 /* through 0x4f */
+
+#define TCPC_TRANSMIT                  0x50
+#define TCPC_TRANSMIT_RETRY_SHIFT      4
+#define TCPC_TRANSMIT_RETRY_MASK       0x3
+#define TCPC_TRANSMIT_TYPE_SHIFT       0
+#define TCPC_TRANSMIT_TYPE_MASK                0x7
+
+#define TCPC_TX_BYTE_CNT               0x51
+#define TCPC_TX_HDR                    0x52
+#define TCPC_TX_DATA                   0x54 /* through 0x6f */
+
+#define TCPC_VBUS_VOLTAGE                      0x70
+#define TCPC_VBUS_SINK_DISCONNECT_THRESH       0x72
+#define TCPC_VBUS_STOP_DISCHARGE_THRESH                0x74
+#define TCPC_VBUS_VOLTAGE_ALARM_HI_CFG         0x76
+#define TCPC_VBUS_VOLTAGE_ALARM_LO_CFG         0x78
+
+struct tcpci;
+struct tcpci_data {
+       struct regmap *regmap;
+       int (*init)(struct tcpci *tcpci, struct tcpci_data *data);
+       int (*get_cc)(struct tcpci *tcpci, struct tcpci_data *data,
+                     enum typec_cc_status *cc1, enum typec_cc_status *cc2);
+       int (*set_bist)(struct tcpci *tcpci, struct tcpci_data *data,
+                       bool enable);
+       int (*set_polarity)(struct tcpci *tcpci, struct tcpci_data *data,
+                           enum typec_cc_polarity polarity);
+       int (*set_vconn)(struct tcpci *tcpci, struct tcpci_data *data,
+                        bool enable);
+       int (*start_drp_toggling)(struct tcpci *tcpci, struct tcpci_data *data,
+                                 enum typec_cc_status cc);
+};
+
+struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data);
+void tcpci_unregister_port(struct tcpci *tcpci);
+irqreturn_t tcpci_irq(struct tcpci *tcpci);
+
+#endif /* __LINUX_USB_TCPCI_H */
diff --git a/drivers/usb/typec/tcpci_rt1711h.c b/drivers/usb/typec/tcpci_rt1711h.c
new file mode 100644 (file)
index 0000000..9825ee5
--- /dev/null
@@ -0,0 +1,487 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, Richtek Technology Corporation
+ *
+ * Richtek RT1711H Type-C Chip Driver
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/gpio/consumer.h>
+#include <linux/usb/tcpm.h>
+#include <linux/regmap.h>
+#include "tcpci.h"
+
+#define RT1711H_VID            0x29CF
+#define RT1711H_PID            0x1711
+#define RT1711H_DID_D          0x2173
+
+#define RT1711H_RTCTRL8                0x9B
+
+/* Autoidle timeout = (tout * 2 + 1) * 6.4ms */
+#define RT1711H_RTCTRL8_SET(ck300, ship_off, auto_idle, tout) \
+                           (((ck300) << 7) | ((ship_off) << 5) | \
+                           ((auto_idle) << 3) | ((tout) & 0x07))
+
+#define RT1711H_RTCTRL11       0x9E
+
+/* I2C timeout = (tout + 1) * 12.5ms */
+#define RT1711H_RTCTRL11_SET(en, tout) \
+                            (((en) << 7) | ((tout) & 0x0F))
+
+#define RT1711H_RTRXDZSEL      0x93
+#define RT1711H_RTCTRL13       0xA0
+#define RT1711H_RTCTRL14       0xA1
+#define RT1711H_RTCTRL15       0xA2
+#define RT1711H_RTCTRL16       0xA3
+#define RT1711H_RTRXDZEN       0xAF
+
+/* Port controller BIST TESTDATA mode support */
+#define RT1711H_CTRL_BIST_EN   BIT(1)
+
+/* 1: use gpios element to look up IRQ number. 0: use interrupts element. */
+#define USE_GPIOS_INTR 1
+
+struct rt1711h_chip {
+       struct tcpci_data data;
+       struct tcpci *tcpci;
+       struct device *dev;
+       u16 chip_id;
+       u8 deadzone_enable;
+       u8 deadzone_select;
+       u8 en_bist;
+#if USE_GPIOS_INTR
+       int gpio_int_n_irq;
+#endif
+};
+
+static int rt1711h_read16(struct rt1711h_chip *chip, unsigned int reg, u16 *val)
+{
+       return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u16));
+}
+
+static int rt1711h_write16(struct rt1711h_chip *chip, unsigned int reg, u16 val)
+{
+       return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u16));
+}
+
+static int rt1711h_read8(struct rt1711h_chip *chip, unsigned int reg, u8 *val)
+{
+       return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u8));
+}
+
+static int rt1711h_write8(struct rt1711h_chip *chip, unsigned int reg, u8 val)
+{
+       return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u8));
+}
+
+static const struct regmap_config rt1711h_regmap_config = {
+       .reg_bits = 8,
+       .val_bits = 8,
+
+       .max_register = 0xFF, /* 0x80 .. 0xFF are vendor defined */
+};
+
+static struct rt1711h_chip *tdata_to_rt1711h(struct tcpci_data *tdata)
+{
+       return container_of(tdata, struct rt1711h_chip, data);
+}
+
+static int rt1711h_init(struct tcpci *tcpci, struct tcpci_data *tdata)
+{
+       int ret;
+       struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
+
+       /* Clear BIST TESTDATA mode */
+       chip->en_bist = 0;
+
+       /* CK 300K from 320K, shipping off, auto_idle enable, tout = 32ms */
+       ret = rt1711h_write8(chip, RT1711H_RTCTRL8,
+                            RT1711H_RTCTRL8_SET(0, 1, 1, 2));
+       if (ret < 0)
+               return ret;
+
+       /* I2C reset : (val + 1) * 12.5ms */
+       ret = rt1711h_write8(chip, RT1711H_RTCTRL11,
+                            RT1711H_RTCTRL11_SET(1, 0x0F));
+       if (ret < 0)
+               return ret;
+
+       /* tTCPCfilter : (26.7 * val) us */
+       ret = rt1711h_write8(chip, RT1711H_RTCTRL14, 0x0F);
+       if (ret < 0)
+               return ret;
+
+       /*  tDRP : (51.2 + 6.4 * val) ms */
+       ret = rt1711h_write8(chip, RT1711H_RTCTRL15, 0x04);
+       if (ret < 0)
+               return ret;
+
+       /* dcSRC.DRP : 33% */
+       return rt1711h_write16(chip, RT1711H_RTCTRL16, 330);
+}
+
+static enum typec_cc_status rt1711h_reg_to_cc(unsigned int cc, bool sink)
+{
+       switch (cc) {
+       case 1:
+               return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RD;
+       case 2:
+               return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RA;
+       case 3:
+               return sink ? TYPEC_CC_RP_3_0 : TYPEC_CC_OPEN;
+       default:
+               return TYPEC_CC_OPEN;
+       }
+}
+
+static int rt1711h_get_cc(struct tcpci *tcpci, struct tcpci_data *tdata,
+                         enum typec_cc_status *cc1, enum typec_cc_status *cc2)
+{
+       struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
+       u8 reg, cc1_state, cc2_state;
+       bool cc1_sink, cc2_sink, tcpc_presenting_rd;
+       enum typec_cc_status cc;
+       u8 sel;
+       int ret;
+
+       /* Read CC1 & CC2 ROLE */
+       ret = rt1711h_read8(chip, TCPC_ROLE_CTRL, &reg);
+       if (ret < 0)
+               return ret;
+
+       cc1_sink = ((reg >> TCPC_ROLE_CTRL_CC1_SHIFT) &
+            TCPC_ROLE_CTRL_CC1_MASK) == TCPC_ROLE_CTRL_CC_RD;
+       cc2_sink = ((reg >> TCPC_ROLE_CTRL_CC2_SHIFT) &
+            TCPC_ROLE_CTRL_CC2_MASK) == TCPC_ROLE_CTRL_CC_RD;
+       /* Read CC STATUS */
+       ret = rt1711h_read8(chip, TCPC_CC_STATUS, &reg);
+       if (ret < 0)
+               return ret;
+
+       /* When auto toggling, BIT(4) is set on presenting Rd */
+       tcpc_presenting_rd = !!(reg & TCPC_CC_STATUS_TERM);
+       cc1_sink |= tcpc_presenting_rd;
+       cc2_sink |= tcpc_presenting_rd;
+       cc1_state = (reg >> TCPC_CC_STATUS_CC1_SHIFT) & TCPC_CC_STATUS_CC1_MASK;
+       cc2_state = (reg >> TCPC_CC_STATUS_CC2_SHIFT) & TCPC_CC_STATUS_CC2_MASK;
+
+       *cc1 = rt1711h_reg_to_cc(cc1_state, cc1_sink);
+       *cc2 = rt1711h_reg_to_cc(cc2_state, cc2_sink);
+
+       /* Set RT1711H deadzone */
+       cc = (*cc1 == TYPEC_CC_OPEN) ? *cc2 : *cc1;
+
+       if (cc == TYPEC_CC_RP_DEF) {
+               if (chip->deadzone_enable != 0) {
+                       chip->deadzone_enable = 0;
+                       rt1711h_write8(chip, RT1711H_RTRXDZEN, 0);
+               }
+               if (chip->deadzone_select != 0x81) {
+                       chip->deadzone_select = 0x81;
+                       rt1711h_write8(chip, RT1711H_RTRXDZSEL, 0x81);
+               }
+       } else {
+               sel = (chip->chip_id >= RT1711H_DID_D) ? 0x81 : 0x80;
+               if (chip->deadzone_enable != 1) {
+                       chip->deadzone_enable = 1;
+                       rt1711h_write8(chip, RT1711H_RTRXDZEN, 1);
+               }
+               if (chip->deadzone_select != sel) {
+                       chip->deadzone_select = sel;
+                       rt1711h_write8(chip, RT1711H_RTRXDZSEL, sel);
+               }
+       }
+
+       return 0;
+}
+
+static int rt1711h_set_polarity(struct tcpci *tcpci, struct tcpci_data *tdata,
+                               enum typec_cc_polarity polarity)
+{
+       struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
+       u8 reg;
+
+       /* Polarity */
+       reg = (polarity == TYPEC_POLARITY_CC2) ?
+             TCPC_TCPC_CTRL_ORIENTATION : 0;
+       /* HW BIST feature */
+       reg |= chip->en_bist ? RT1711H_CTRL_BIST_EN : 0;
+
+       return rt1711h_write8(chip, TCPC_TCPC_CTRL, reg);
+}
+
+static int rt1711h_set_bist(struct tcpci *tcpci, struct tcpci_data *tdata,
+                           bool enable)
+{
+       struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
+       int ret;
+       u8 reg;
+
+       ret = rt1711h_read8(chip, TCPC_TCPC_CTRL, &reg);
+       if (ret)
+               return ret;
+
+       if (enable)
+               reg |= RT1711H_CTRL_BIST_EN;
+       else
+               reg &= ~RT1711H_CTRL_BIST_EN;
+
+       ret = rt1711h_write8(chip, TCPC_TCPC_CTRL, reg);
+       if (!ret)
+               chip->en_bist = enable;
+
+       return ret;
+}
+
+static int rt1711h_set_vconn(struct tcpci *tcpci, struct tcpci_data *tdata,
+                            bool enable)
+{
+       struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
+
+       return rt1711h_write8(chip, RT1711H_RTCTRL8,
+                             RT1711H_RTCTRL8_SET(0, 1, !enable, 2));
+}
+
+static int rt1711h_start_drp_toggling(struct tcpci *tcpci,
+                                     struct tcpci_data *tdata,
+                                     enum typec_cc_status cc)
+{
+       struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
+       int ret;
+       unsigned int reg = 0;
+
+       switch (cc) {
+       default:
+       case TYPEC_CC_RP_DEF:
+               reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
+                       TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_RP_1_5:
+               reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                       TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       case TYPEC_CC_RP_3_0:
+               reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
+                       TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+               break;
+       }
+
+       if (cc == TYPEC_CC_RD)
+               reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                          (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
+       else
+               reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                          (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT);
+
+       ret = rt1711h_write8(chip, TCPC_ROLE_CTRL, reg);
+       if (ret < 0)
+               return ret;
+       usleep_range(500, 1000);
+
+       return 0;
+}
+
+static irqreturn_t rt1711h_irq(int irq, void *dev_id)
+{
+       int ret;
+       u16 alert;
+       u8 status;
+       struct rt1711h_chip *chip = dev_id;
+
+       if (!chip->tcpci)
+               return IRQ_HANDLED;
+
+       ret = rt1711h_read16(chip, TCPC_ALERT, &alert);
+       if (ret < 0)
+               goto out;
+
+       if (alert & TCPC_ALERT_CC_STATUS) {
+               ret = rt1711h_read8(chip, TCPC_CC_STATUS, &status);
+               if (ret < 0)
+                       goto out;
+               /* Clear cc change event triggered by starting toggling */
+               if (status & TCPC_CC_STATUS_TOGGLING)
+                       rt1711h_write8(chip, TCPC_ALERT, TCPC_ALERT_CC_STATUS);
+       }
+
+out:
+       return tcpci_irq(chip->tcpci);
+}
+
+static int rt1711h_init_alert(struct rt1711h_chip *chip,
+                             struct i2c_client *client)
+{
+       int ret;
+#if USE_GPIOS_INTR
+       struct gpio_desc *desc;
+       int id_irqnr;
+#endif
+
+       /* Disable chip interrupts before requesting irq */
+       ret = rt1711h_write16(chip, TCPC_ALERT_MASK, 0);
+       if (ret < 0)
+               return ret;
+
+#if USE_GPIOS_INTR
+       desc = gpiod_get_index(chip->dev, NULL, 0, GPIOD_IN);
+       if (IS_ERR(desc)) {
+               pr_err("fail to get id-gpioirq\n");
+               return -1;
+       }
+       id_irqnr = gpiod_to_irq(desc);
+
+       ret = devm_request_threaded_irq(chip->dev, id_irqnr, NULL,
+                                       rt1711h_irq,
+                                       IRQF_ONESHOT | IRQF_TRIGGER_LOW,
+                                       "rt1711h_interrupt_int_n", chip);
+       if (ret) {
+               pr_err("failed to request ret=%d, id_irqnr=%d\n",
+                       ret, id_irqnr);
+               return ret;
+       }
+       chip->gpio_int_n_irq = id_irqnr;
+       pr_info("<%s> ok\n", __func__);
+       enable_irq_wake(id_irqnr);
+       return 0;
+#else
+       ret = devm_request_threaded_irq(chip->dev, client->irq, NULL,
+                                       rt1711h_irq,
+                                       IRQF_ONESHOT | IRQF_TRIGGER_LOW,
+                                       dev_name(chip->dev), chip);
+       if (ret < 0)
+               return ret;
+       enable_irq_wake(client->irq);
+       return 0;
+#endif
+}
+
+static int rt1711h_sw_reset(struct rt1711h_chip *chip)
+{
+       int ret;
+
+       ret = rt1711h_write8(chip, RT1711H_RTCTRL13, 0x01);
+       if (ret < 0)
+               return ret;
+
+       usleep_range(1000, 2000);
+       return 0;
+}
+
+static int rt1711h_check_revision(struct i2c_client *i2c)
+{
+       int ret;
+
+       ret = i2c_smbus_read_word_data(i2c, TCPC_VENDOR_ID);
+       if (ret < 0)
+               return ret;
+       if (ret != RT1711H_VID) {
+               dev_err(&i2c->dev, "vid is not correct, 0x%04x\n", ret);
+               return -ENODEV;
+       }
+       ret = i2c_smbus_read_word_data(i2c, TCPC_PRODUCT_ID);
+       if (ret < 0)
+               return ret;
+       if (ret != RT1711H_PID) {
+               dev_err(&i2c->dev, "pid is not correct, 0x%04x\n", ret);
+               return -ENODEV;
+       }
+       return 0;
+}
+
+static int rt1711h_probe(struct i2c_client *client,
+                        const struct i2c_device_id *i2c_id)
+{
+       int ret;
+       struct rt1711h_chip *chip;
+       u16 chip_id;
+
+       ret = rt1711h_check_revision(client);
+       if (ret < 0) {
+               dev_err(&client->dev, "check vid/pid fail\n");
+               return ret;
+       }
+
+       chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
+       if (!chip)
+               return -ENOMEM;
+
+       chip->data.regmap = devm_regmap_init_i2c(client,
+                                                &rt1711h_regmap_config);
+       if (IS_ERR(chip->data.regmap))
+               return PTR_ERR(chip->data.regmap);
+
+       chip->dev = &client->dev;
+       i2c_set_clientdata(client, chip);
+
+       ret = rt1711h_sw_reset(chip);
+       if (ret < 0)
+               return ret;
+
+       /* Get device id and initialize deadzone */
+       ret = rt1711h_read16(chip, TCPC_BCD_DEV, &chip_id);
+       if (ret < 0)
+               return ret;
+       chip->chip_id = chip_id;
+       ret = rt1711h_read8(chip, RT1711H_RTRXDZEN, &chip->deadzone_enable);
+       if (ret < 0)
+               return ret;
+       ret = rt1711h_read8(chip, RT1711H_RTRXDZSEL, &chip->deadzone_select);
+       if (ret < 0)
+               return ret;
+
+       ret = rt1711h_init_alert(chip, client);
+       if (ret < 0)
+               return ret;
+
+       chip->data.init = rt1711h_init;
+       chip->data.get_cc = rt1711h_get_cc;
+       chip->data.set_bist = rt1711h_set_bist;
+       chip->data.set_polarity = rt1711h_set_polarity;
+       chip->data.set_vconn = rt1711h_set_vconn;
+       chip->data.start_drp_toggling = rt1711h_start_drp_toggling;
+       chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
+       if (IS_ERR_OR_NULL(chip->tcpci))
+               return PTR_ERR(chip->tcpci);
+
+       return 0;
+}
+
+static int rt1711h_remove(struct i2c_client *client)
+{
+       struct rt1711h_chip *chip = i2c_get_clientdata(client);
+
+       tcpci_unregister_port(chip->tcpci);
+       return 0;
+}
+
+static const struct i2c_device_id rt1711h_id[] = {
+       { "rt1711h", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, rt1711h_id);
+
+#ifdef CONFIG_OF
+static const struct of_device_id rt1711h_of_match[] = {
+       { .compatible = "richtek,rt1711h", },
+       {},
+};
+MODULE_DEVICE_TABLE(of, rt1711h_of_match);
+#endif
+
+static struct i2c_driver rt1711h_i2c_driver = {
+       .driver = {
+               .name = "rt1711h",
+               .of_match_table = of_match_ptr(rt1711h_of_match),
+       },
+       .probe = rt1711h_probe,
+       .remove = rt1711h_remove,
+       .id_table = rt1711h_id,
+};
+module_i2c_driver(rt1711h_i2c_driver);
+
+MODULE_AUTHOR("ShuFan Lee <shufan_lee@richtek.com>");
+MODULE_DESCRIPTION("RT1711H USB Type-C Port Controller Interface Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c
new file mode 100644 (file)
index 0000000..977491c
--- /dev/null
@@ -0,0 +1,4951 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * USB Power Delivery protocol stack.
+ */
+
+#include <linux/completion.h>
+#include <linux/debugfs.h>
+#include <linux/device.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/power_supply.h>
+#include <linux/proc_fs.h>
+#include <linux/property.h>
+#include <linux/sched.h>
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/usb/pd.h>
+#include <linux/usb/pd_ado.h>
+#include <linux/usb/pd_bdo.h>
+#include <linux/usb/pd_ext_sdb.h>
+#include <linux/usb/pd_vdo.h>
+#include <linux/usb/role.h>
+#include <linux/usb/tcpm.h>
+#include <linux/usb/typec_altmode.h>
+#include <linux/workqueue.h>
+
+#define FOREACH_STATE(S)                       \
+       S(INVALID_STATE),                       \
+       S(DRP_TOGGLING),                        \
+       S(SRC_UNATTACHED),                      \
+       S(SRC_ATTACH_WAIT),                     \
+       S(SRC_ATTACHED),                        \
+       S(SRC_STARTUP),                         \
+       S(SRC_SEND_CAPABILITIES),               \
+       S(SRC_NEGOTIATE_CAPABILITIES),          \
+       S(SRC_TRANSITION_SUPPLY),               \
+       S(SRC_READY),                           \
+       S(SRC_WAIT_NEW_CAPABILITIES),           \
+                                               \
+       S(SNK_UNATTACHED),                      \
+       S(SNK_ATTACH_WAIT),                     \
+       S(SNK_DEBOUNCED),                       \
+       S(SNK_ATTACHED),                        \
+       S(SNK_STARTUP),                         \
+       S(SNK_DISCOVERY),                       \
+       S(SNK_DISCOVERY_DEBOUNCE),              \
+       S(SNK_DISCOVERY_DEBOUNCE_DONE),         \
+       S(SNK_WAIT_CAPABILITIES),               \
+       S(SNK_NEGOTIATE_CAPABILITIES),          \
+       S(SNK_NEGOTIATE_PPS_CAPABILITIES),      \
+       S(SNK_TRANSITION_SINK),                 \
+       S(SNK_TRANSITION_SINK_VBUS),            \
+       S(SNK_READY),                           \
+                                               \
+       S(ACC_UNATTACHED),                      \
+       S(DEBUG_ACC_ATTACHED),                  \
+       S(AUDIO_ACC_ATTACHED),                  \
+       S(AUDIO_ACC_DEBOUNCE),                  \
+                                               \
+       S(HARD_RESET_SEND),                     \
+       S(HARD_RESET_START),                    \
+       S(SRC_HARD_RESET_VBUS_OFF),             \
+       S(SRC_HARD_RESET_VBUS_ON),              \
+       S(SNK_HARD_RESET_SINK_OFF),             \
+       S(SNK_HARD_RESET_WAIT_VBUS),            \
+       S(SNK_HARD_RESET_SINK_ON),              \
+                                               \
+       S(SOFT_RESET),                          \
+       S(SOFT_RESET_SEND),                     \
+                                               \
+       S(DR_SWAP_ACCEPT),                      \
+       S(DR_SWAP_SEND),                        \
+       S(DR_SWAP_SEND_TIMEOUT),                \
+       S(DR_SWAP_CANCEL),                      \
+       S(DR_SWAP_CHANGE_DR),                   \
+                                               \
+       S(PR_SWAP_ACCEPT),                      \
+       S(PR_SWAP_SEND),                        \
+       S(PR_SWAP_SEND_TIMEOUT),                \
+       S(PR_SWAP_CANCEL),                      \
+       S(PR_SWAP_START),                       \
+       S(PR_SWAP_SRC_SNK_TRANSITION_OFF),      \
+       S(PR_SWAP_SRC_SNK_SOURCE_OFF),          \
+       S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
+       S(PR_SWAP_SRC_SNK_SINK_ON),             \
+       S(PR_SWAP_SNK_SRC_SINK_OFF),            \
+       S(PR_SWAP_SNK_SRC_SOURCE_ON),           \
+       S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP),    \
+                                               \
+       S(VCONN_SWAP_ACCEPT),                   \
+       S(VCONN_SWAP_SEND),                     \
+       S(VCONN_SWAP_SEND_TIMEOUT),             \
+       S(VCONN_SWAP_CANCEL),                   \
+       S(VCONN_SWAP_START),                    \
+       S(VCONN_SWAP_WAIT_FOR_VCONN),           \
+       S(VCONN_SWAP_TURN_ON_VCONN),            \
+       S(VCONN_SWAP_TURN_OFF_VCONN),           \
+                                               \
+       S(SNK_TRY),                             \
+       S(SNK_TRY_WAIT),                        \
+       S(SNK_TRY_WAIT_DEBOUNCE),               \
+       S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS),    \
+       S(SRC_TRYWAIT),                         \
+       S(SRC_TRYWAIT_DEBOUNCE),                \
+       S(SRC_TRYWAIT_UNATTACHED),              \
+                                               \
+       S(SRC_TRY),                             \
+       S(SRC_TRY_WAIT),                        \
+       S(SRC_TRY_DEBOUNCE),                    \
+       S(SNK_TRYWAIT),                         \
+       S(SNK_TRYWAIT_DEBOUNCE),                \
+       S(SNK_TRYWAIT_VBUS),                    \
+       S(BIST_RX),                             \
+                                               \
+       S(GET_STATUS_SEND),                     \
+       S(GET_STATUS_SEND_TIMEOUT),             \
+       S(GET_PPS_STATUS_SEND),                 \
+       S(GET_PPS_STATUS_SEND_TIMEOUT),         \
+                                               \
+       S(ERROR_RECOVERY),                      \
+       S(PORT_RESET),                          \
+       S(PORT_RESET_WAIT_OFF)
+
+#define GENERATE_ENUM(e)       e
+#define GENERATE_STRING(s)     #s
+
+enum tcpm_state {
+       FOREACH_STATE(GENERATE_ENUM)
+};
+
+static const char * const tcpm_states[] = {
+       FOREACH_STATE(GENERATE_STRING)
+};
+
+enum vdm_states {
+       VDM_STATE_ERR_BUSY = -3,
+       VDM_STATE_ERR_SEND = -2,
+       VDM_STATE_ERR_TMOUT = -1,
+       VDM_STATE_DONE = 0,
+       /* Anything >0 represents an active state */
+       VDM_STATE_READY = 1,
+       VDM_STATE_BUSY = 2,
+       VDM_STATE_WAIT_RSP_BUSY = 3,
+};
+
+enum pd_msg_request {
+       PD_MSG_NONE = 0,
+       PD_MSG_CTRL_REJECT,
+       PD_MSG_CTRL_WAIT,
+       PD_MSG_CTRL_NOT_SUPP,
+       PD_MSG_DATA_SINK_CAP,
+       PD_MSG_DATA_SOURCE_CAP,
+};
+
+/* Events from low level driver */
+
+#define TCPM_CC_EVENT          BIT(0)
+#define TCPM_VBUS_EVENT                BIT(1)
+#define TCPM_RESET_EVENT       BIT(2)
+
+#define LOG_BUFFER_ENTRIES     1024
+#define LOG_BUFFER_ENTRY_SIZE  128
+
+/* Alternate mode support */
+
+#define SVID_DISCOVERY_MAX     16
+#define ALTMODE_DISCOVERY_MAX  (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
+
+struct pd_mode_data {
+       int svid_index;         /* current SVID index           */
+       int nsvids;
+       u16 svids[SVID_DISCOVERY_MAX];
+       int altmodes;           /* number of alternate modes    */
+       struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
+};
+
+struct pd_pps_data {
+       u32 min_volt;
+       u32 max_volt;
+       u32 max_curr;
+       u32 out_volt;
+       u32 op_curr;
+       bool supported;
+       bool active;
+};
+
+struct tcpm_port {
+       struct device *dev;
+
+       struct mutex lock;              /* tcpm state machine lock */
+       struct workqueue_struct *wq;
+
+       struct typec_capability typec_caps;
+       struct typec_port *typec_port;
+
+       struct tcpc_dev *tcpc;
+       struct usb_role_switch *role_sw;
+
+       enum typec_role vconn_role;
+       enum typec_role pwr_role;
+       enum typec_data_role data_role;
+       enum typec_pwr_opmode pwr_opmode;
+
+       struct usb_pd_identity partner_ident;
+       struct typec_partner_desc partner_desc;
+       struct typec_partner *partner;
+
+       enum typec_cc_status cc_req;
+
+       enum typec_cc_status cc1;
+       enum typec_cc_status cc2;
+       enum typec_cc_polarity polarity;
+
+       bool attached;
+       bool connected;
+       enum typec_port_type port_type;
+       bool vbus_present;
+       bool vbus_never_low;
+       bool vbus_source;
+       bool vbus_charge;
+
+       bool send_discover;
+       bool op_vsafe5v;
+
+       int try_role;
+       int try_snk_count;
+       int try_src_count;
+
+       enum pd_msg_request queued_message;
+
+       enum tcpm_state enter_state;
+       enum tcpm_state prev_state;
+       enum tcpm_state state;
+       enum tcpm_state delayed_state;
+       unsigned long delayed_runtime;
+       unsigned long delay_ms;
+
+       spinlock_t pd_event_lock;
+       u32 pd_events;
+
+       struct work_struct event_work;
+       struct delayed_work state_machine;
+       struct delayed_work vdm_state_machine;
+       bool state_machine_running;
+
+       struct completion tx_complete;
+       enum tcpm_transmit_status tx_status;
+
+       struct mutex swap_lock;         /* swap command lock */
+       bool swap_pending;
+       bool non_pd_role_swap;
+       struct completion swap_complete;
+       int swap_status;
+
+       unsigned int negotiated_rev;
+       unsigned int message_id;
+       unsigned int caps_count;
+       unsigned int hard_reset_count;
+       bool pd_capable;
+       bool explicit_contract;
+       unsigned int rx_msgid;
+
+       /* Partner capabilities/requests */
+       u32 sink_request;
+       u32 source_caps[PDO_MAX_OBJECTS];
+       unsigned int nr_source_caps;
+       u32 sink_caps[PDO_MAX_OBJECTS];
+       unsigned int nr_sink_caps;
+
+       /* Local capabilities */
+       u32 src_pdo[PDO_MAX_OBJECTS];
+       unsigned int nr_src_pdo;
+       u32 snk_pdo[PDO_MAX_OBJECTS];
+       unsigned int nr_snk_pdo;
+       u32 snk_vdo[VDO_MAX_OBJECTS];
+       unsigned int nr_snk_vdo;
+
+       unsigned int operating_snk_mw;
+       bool update_sink_caps;
+
+       /* Requested current / voltage */
+       u32 current_limit;
+       u32 supply_voltage;
+
+       /* Used to export TA voltage and current */
+       struct power_supply *psy;
+       struct power_supply_desc psy_desc;
+       enum power_supply_usb_type usb_type;
+
+       u32 bist_request;
+
+       /* PD state for Vendor Defined Messages */
+       enum vdm_states vdm_state;
+       u32 vdm_retries;
+       /* next Vendor Defined Message to send */
+       u32 vdo_data[VDO_MAX_SIZE];
+       u8 vdo_count;
+       /* VDO to retry if UFP responder replied busy */
+       u32 vdo_retry;
+
+       /* PPS */
+       struct pd_pps_data pps_data;
+       struct completion pps_complete;
+       bool pps_pending;
+       int pps_status;
+
+       /* Alternate mode data */
+       struct pd_mode_data mode_data;
+       struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
+       struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
+
+       /* Deadline in jiffies to exit src_try_wait state */
+       unsigned long max_wait;
+
+       /* port belongs to a self powered device */
+       bool self_powered;
+
+#ifdef CONFIG_DEBUG_FS
+       struct dentry *dentry;
+       struct mutex logbuffer_lock;    /* log buffer access lock */
+       int logbuffer_head;
+       int logbuffer_tail;
+       u8 *logbuffer[LOG_BUFFER_ENTRIES];
+#endif
+};
+
+struct pd_rx_event {
+       struct work_struct work;
+       struct tcpm_port *port;
+       struct pd_message msg;
+};
+
+#define tcpm_cc_is_sink(cc) \
+       ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
+        (cc) == TYPEC_CC_RP_3_0)
+
+#define tcpm_port_is_sink(port) \
+       ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
+        (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
+
+#define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
+#define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
+#define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
+
+#define tcpm_port_is_source(port) \
+       ((tcpm_cc_is_source((port)->cc1) && \
+        !tcpm_cc_is_source((port)->cc2)) || \
+        (tcpm_cc_is_source((port)->cc2) && \
+         !tcpm_cc_is_source((port)->cc1)))
+
+#define tcpm_port_is_debug(port) \
+       (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
+
+#define tcpm_port_is_audio(port) \
+       (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
+
+#define tcpm_port_is_audio_detached(port) \
+       ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
+        (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
+
+#define tcpm_try_snk(port) \
+       ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
+       (port)->port_type == TYPEC_PORT_DRP)
+
+#define tcpm_try_src(port) \
+       ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
+       (port)->port_type == TYPEC_PORT_DRP)
+
+static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
+{
+       if (port->port_type == TYPEC_PORT_DRP) {
+               if (port->try_role == TYPEC_SINK)
+                       return SNK_UNATTACHED;
+               else if (port->try_role == TYPEC_SOURCE)
+                       return SRC_UNATTACHED;
+               else if (port->tcpc->config &&
+                        port->tcpc->config->default_role == TYPEC_SINK)
+                       return SNK_UNATTACHED;
+               /* Fall through to return SRC_UNATTACHED */
+       } else if (port->port_type == TYPEC_PORT_SNK) {
+               return SNK_UNATTACHED;
+       }
+       return SRC_UNATTACHED;
+}
+
+static inline
+struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
+{
+       return container_of(cap, struct tcpm_port, typec_caps);
+}
+
+static bool tcpm_port_is_disconnected(struct tcpm_port *port)
+{
+       return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
+               port->cc2 == TYPEC_CC_OPEN) ||
+              (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
+                                   port->cc1 == TYPEC_CC_OPEN) ||
+                                  (port->polarity == TYPEC_POLARITY_CC2 &&
+                                   port->cc2 == TYPEC_CC_OPEN)));
+}
+
+/*
+ * Logging
+ */
+
+#ifdef CONFIG_DEBUG_FS
+
+static bool tcpm_log_full(struct tcpm_port *port)
+{
+       return port->logbuffer_tail ==
+               (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
+}
+
+__printf(2, 0)
+static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
+{
+       char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
+       u64 ts_nsec = local_clock();
+       unsigned long rem_nsec;
+
+       mutex_lock(&port->logbuffer_lock);
+       if (!port->logbuffer[port->logbuffer_head]) {
+               port->logbuffer[port->logbuffer_head] =
+                               kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
+               if (!port->logbuffer[port->logbuffer_head]) {
+                       mutex_unlock(&port->logbuffer_lock);
+                       return;
+               }
+       }
+
+       vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
+
+       if (tcpm_log_full(port)) {
+               port->logbuffer_head = max(port->logbuffer_head - 1, 0);
+               strcpy(tmpbuffer, "overflow");
+       }
+
+       if (port->logbuffer_head < 0 ||
+           port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
+               dev_warn(port->dev,
+                        "Bad log buffer index %d\n", port->logbuffer_head);
+               goto abort;
+       }
+
+       if (!port->logbuffer[port->logbuffer_head]) {
+               dev_warn(port->dev,
+                        "Log buffer index %d is NULL\n", port->logbuffer_head);
+               goto abort;
+       }
+
+       rem_nsec = do_div(ts_nsec, 1000000000);
+       scnprintf(port->logbuffer[port->logbuffer_head],
+                 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
+                 (unsigned long)ts_nsec, rem_nsec / 1000,
+                 tmpbuffer);
+       port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
+
+abort:
+       mutex_unlock(&port->logbuffer_lock);
+}
+
+__printf(2, 3)
+static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
+{
+       va_list args;
+
+       /* Do not log while disconnected and unattached */
+       if (tcpm_port_is_disconnected(port) &&
+           (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
+            port->state == DRP_TOGGLING))
+               return;
+
+       va_start(args, fmt);
+       _tcpm_log(port, fmt, args);
+       va_end(args);
+}
+
+__printf(2, 3)
+static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       _tcpm_log(port, fmt, args);
+       va_end(args);
+}
+
+static unsigned int tcpm_get_max_fixed_current(struct tcpm_port *port)
+{
+       int i;
+       unsigned int max_current_ma = 0;
+
+       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 pdo_current_ma;
+
+               if (type != PDO_TYPE_FIXED)
+                       continue;
+               if (pdo_fixed_voltage(pdo) < port->supply_voltage)
+                       continue;
+
+               pdo_current_ma = pdo_max_current(pdo);
+               if (pdo_current_ma > max_current_ma)
+                       max_current_ma = pdo_current_ma;
+       }
+
+       return max_current_ma;
+}
+
+static void tcpm_log_source_caps(struct tcpm_port *port)
+{
+       int i;
+
+       for (i = 0; i < port->nr_source_caps; i++) {
+               u32 pdo = port->source_caps[i];
+               enum pd_pdo_type type = pdo_type(pdo);
+               char msg[64];
+
+               switch (type) {
+               case PDO_TYPE_FIXED:
+                       scnprintf(msg, sizeof(msg),
+                                 "%u mV, %u mA [%s%s%s%s%s%s]",
+                                 pdo_fixed_voltage(pdo),
+                                 pdo_max_current(pdo),
+                                 (pdo & PDO_FIXED_DUAL_ROLE) ?
+                                                       "R" : "",
+                                 (pdo & PDO_FIXED_SUSPEND) ?
+                                                       "S" : "",
+                                 (pdo & PDO_FIXED_HIGHER_CAP) ?
+                                                       "H" : "",
+                                 (pdo & PDO_FIXED_USB_COMM) ?
+                                                       "U" : "",
+                                 (pdo & PDO_FIXED_DATA_SWAP) ?
+                                                       "D" : "",
+                                 (pdo & PDO_FIXED_EXTPOWER) ?
+                                                       "E" : "");
+                       break;
+               case PDO_TYPE_VAR:
+                       scnprintf(msg, sizeof(msg),
+                                 "%u-%u mV, %u mA",
+                                 pdo_min_voltage(pdo),
+                                 pdo_max_voltage(pdo),
+                                 pdo_max_current(pdo));
+                       break;
+               case PDO_TYPE_BATT:
+                       scnprintf(msg, sizeof(msg),
+                                 "%u-%u mV, %u mW",
+                                 pdo_min_voltage(pdo),
+                                 pdo_max_voltage(pdo),
+                                 pdo_max_power(pdo));
+                       break;
+               case PDO_TYPE_APDO:
+                       if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
+                               scnprintf(msg, sizeof(msg),
+                                         "%u-%u mV, %u mA",
+                                         pdo_pps_apdo_min_voltage(pdo),
+                                         pdo_pps_apdo_max_voltage(pdo),
+                                         pdo_pps_apdo_max_current(pdo));
+                       else
+                               strcpy(msg, "undefined APDO");
+                       break;
+               default:
+                       strcpy(msg, "undefined");
+                       break;
+               }
+               tcpm_log(port, " PDO %d: type %d, %s",
+                        i, type, msg);
+       }
+}
+
+static int tcpm_debug_show(struct seq_file *s, void *v)
+{
+       struct tcpm_port *port = (struct tcpm_port *)s->private;
+       int tail;
+
+       mutex_lock(&port->logbuffer_lock);
+       tail = port->logbuffer_tail;
+       while (tail != port->logbuffer_head) {
+               seq_printf(s, "%s\n", port->logbuffer[tail]);
+               tail = (tail + 1) % LOG_BUFFER_ENTRIES;
+       }
+       if (!seq_has_overflowed(s))
+               port->logbuffer_tail = tail;
+       mutex_unlock(&port->logbuffer_lock);
+
+       return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
+
+static struct dentry *rootdir;
+
+static void tcpm_debugfs_init(struct tcpm_port *port)
+{
+       mutex_init(&port->logbuffer_lock);
+       /* /sys/kernel/debug/tcpm/usbcX */
+       if (!rootdir)
+               rootdir = debugfs_create_dir("tcpm", NULL);
+
+       port->dentry = debugfs_create_file(dev_name(port->dev),
+                                          S_IFREG | 0444, rootdir,
+                                          port, &tcpm_debug_fops);
+}
+
+static void tcpm_debugfs_exit(struct tcpm_port *port)
+{
+       int i;
+
+       mutex_lock(&port->logbuffer_lock);
+       for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
+               kfree(port->logbuffer[i]);
+               port->logbuffer[i] = NULL;
+       }
+       mutex_unlock(&port->logbuffer_lock);
+
+       debugfs_remove(port->dentry);
+       if (list_empty(&rootdir->d_subdirs)) {
+               debugfs_remove(rootdir);
+               rootdir = NULL;
+       }
+}
+
+#else
+
+__printf(2, 3)
+static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
+__printf(2, 3)
+static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
+static void tcpm_log_source_caps(struct tcpm_port *port) { }
+static void tcpm_debugfs_init(const struct tcpm_port *port) { }
+static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
+
+#endif
+
+static int tcpm_pd_transmit(struct tcpm_port *port,
+                           enum tcpm_transmit_type type,
+                           const struct pd_message *msg)
+{
+       unsigned long timeout;
+       int ret;
+
+       if (msg)
+               tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
+       else
+               tcpm_log(port, "PD TX, type: %#x", type);
+
+       reinit_completion(&port->tx_complete);
+       ret = port->tcpc->pd_transmit(port->tcpc, type, msg);
+       if (ret < 0)
+               return ret;
+
+       mutex_unlock(&port->lock);
+       timeout = wait_for_completion_timeout(&port->tx_complete,
+                               msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
+       mutex_lock(&port->lock);
+       if (!timeout)
+               return -ETIMEDOUT;
+
+       switch (port->tx_status) {
+       case TCPC_TX_SUCCESS:
+               port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
+               return 0;
+       case TCPC_TX_DISCARDED:
+               return -EAGAIN;
+       case TCPC_TX_FAILED:
+       default:
+               return -EIO;
+       }
+}
+
+void tcpm_pd_transmit_complete(struct tcpm_port *port,
+                              enum tcpm_transmit_status status)
+{
+       tcpm_log(port, "PD TX complete, status: %u", status);
+       port->tx_status = status;
+       complete(&port->tx_complete);
+}
+EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
+
+static int tcpm_mux_set(struct tcpm_port *port, int state,
+                       enum usb_role usb_role,
+                       enum typec_orientation orientation)
+{
+       int ret;
+
+       tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
+                state, usb_role, orientation);
+
+       ret = typec_set_orientation(port->typec_port, orientation);
+       if (ret)
+               return ret;
+
+       if (port->role_sw) {
+               ret = usb_role_switch_set_role(port->role_sw, usb_role);
+               if (ret)
+                       return ret;
+       }
+
+       return typec_set_mode(port->typec_port, state);
+}
+
+static int tcpm_set_polarity(struct tcpm_port *port,
+                            enum typec_cc_polarity polarity)
+{
+       int ret;
+
+       tcpm_log(port, "polarity %d", polarity);
+
+       ret = port->tcpc->set_polarity(port->tcpc, polarity);
+       if (ret < 0)
+               return ret;
+
+       port->polarity = polarity;
+
+       return 0;
+}
+
+static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
+{
+       int ret;
+
+       tcpm_log(port, "vconn:=%d", enable);
+
+       ret = port->tcpc->set_vconn(port->tcpc, enable);
+       if (!ret) {
+               port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
+               typec_set_vconn_role(port->typec_port, port->vconn_role);
+       }
+
+       return ret;
+}
+
+static u32 tcpm_get_current_limit(struct tcpm_port *port)
+{
+       enum typec_cc_status cc;
+       u32 limit;
+
+       cc = port->polarity ? port->cc2 : port->cc1;
+       switch (cc) {
+       case TYPEC_CC_RP_1_5:
+               limit = 1500;
+               break;
+       case TYPEC_CC_RP_3_0:
+               limit = 3000;
+               break;
+       case TYPEC_CC_RP_DEF:
+       default:
+               if (port->tcpc->get_current_limit)
+                       limit = port->tcpc->get_current_limit(port->tcpc);
+               else
+                       limit = 0;
+               break;
+       }
+
+       return limit;
+}
+
+static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
+{
+       int ret = -EOPNOTSUPP;
+
+       tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
+
+       port->supply_voltage = mv;
+       port->current_limit = max_ma;
+
+       if (port->tcpc->set_current_limit)
+               ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
+
+       return ret;
+}
+
+/*
+ * Determine RP value to set based on maximum current supported
+ * by a port if configured as source.
+ * Returns CC value to report to link partner.
+ */
+static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
+{
+       const u32 *src_pdo = port->src_pdo;
+       int nr_pdo = port->nr_src_pdo;
+       int i;
+
+       /*
+        * Search for first entry with matching voltage.
+        * It should report the maximum supported current.
+        */
+       for (i = 0; i < nr_pdo; i++) {
+               const u32 pdo = src_pdo[i];
+
+               if (pdo_type(pdo) == PDO_TYPE_FIXED &&
+                   pdo_fixed_voltage(pdo) == 5000) {
+                       unsigned int curr = pdo_max_current(pdo);
+
+                       if (curr >= 3000)
+                               return TYPEC_CC_RP_3_0;
+                       else if (curr >= 1500)
+                               return TYPEC_CC_RP_1_5;
+                       return TYPEC_CC_RP_DEF;
+               }
+       }
+
+       return TYPEC_CC_RP_DEF;
+}
+
+static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
+{
+       return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
+                                    port->data_role);
+}
+
+extern void amlogic_typec_set_mode(int mode);
+
+static int tcpm_set_roles(struct tcpm_port *port, bool attached,
+                         enum typec_role role, enum typec_data_role data)
+{
+       enum typec_orientation orientation;
+       enum usb_role usb_role;
+       int ret;
+
+       if (port->polarity == TYPEC_POLARITY_CC1)
+               orientation = TYPEC_ORIENTATION_NORMAL;
+       else
+               orientation = TYPEC_ORIENTATION_REVERSE;
+
+       if (data == TYPEC_HOST)
+               usb_role = USB_ROLE_HOST;
+       else
+               usb_role = USB_ROLE_DEVICE;
+
+       ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
+       if (ret < 0)
+               return ret;
+
+       ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
+       if (ret < 0)
+               return ret;
+
+       port->pwr_role = role;
+       port->data_role = data;
+       pr_info("tcpm_set_roles(): pwr_role=%d, data_role=%d\n", (int)role, (int)data);
+       typec_set_data_role(port->typec_port, data);
+       typec_set_pwr_role(port->typec_port, role);
+       if (usb_role == USB_ROLE_HOST) {
+               printk("############# set host mode\n");
+               amlogic_typec_set_mode(0);
+       } else {
+               printk("############# set device mode\n");
+               amlogic_typec_set_mode(1);
+       }
+
+       return 0;
+}
+
+static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
+{
+       int ret;
+
+       ret = port->tcpc->set_roles(port->tcpc, true, role,
+                                   port->data_role);
+       if (ret < 0)
+               return ret;
+
+       port->pwr_role = role;
+       typec_set_pwr_role(port->typec_port, role);
+
+       return 0;
+}
+
+static int tcpm_pd_send_source_caps(struct tcpm_port *port)
+{
+       struct pd_message msg;
+       int i;
+
+       memset(&msg, 0, sizeof(msg));
+       if (!port->nr_src_pdo) {
+               /* No source capabilities defined, sink only */
+               msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
+                                         port->pwr_role,
+                                         port->data_role,
+                                         port->negotiated_rev,
+                                         port->message_id, 0);
+       } else {
+               msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
+                                         port->pwr_role,
+                                         port->data_role,
+                                         port->negotiated_rev,
+                                         port->message_id,
+                                         port->nr_src_pdo);
+       }
+       for (i = 0; i < port->nr_src_pdo; i++)
+               msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
+
+       return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+}
+
+static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
+{
+       struct pd_message msg;
+       int i;
+
+       memset(&msg, 0, sizeof(msg));
+       if (!port->nr_snk_pdo) {
+               /* No sink capabilities defined, source only */
+               msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
+                                         port->pwr_role,
+                                         port->data_role,
+                                         port->negotiated_rev,
+                                         port->message_id, 0);
+       } else {
+               msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
+                                         port->pwr_role,
+                                         port->data_role,
+                                         port->negotiated_rev,
+                                         port->message_id,
+                                         port->nr_snk_pdo);
+       }
+       for (i = 0; i < port->nr_snk_pdo; i++)
+               msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
+
+       return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+}
+
+static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
+                          unsigned int delay_ms)
+{
+       if (delay_ms) {
+               tcpm_log(port, "pending state change %s -> %s @ %u ms",
+                        tcpm_states[port->state], tcpm_states[state],
+                        delay_ms);
+               port->delayed_state = state;
+               if (delay_ms != PD_T_PS_SOURCE_ON)
+                       mod_delayed_work(port->wq, &port->state_machine,
+                                msecs_to_jiffies(delay_ms));
+               port->delayed_runtime = jiffies + msecs_to_jiffies(delay_ms);
+               port->delay_ms = delay_ms;
+       } else {
+               tcpm_log(port, "state change %s -> %s",
+                        tcpm_states[port->state], tcpm_states[state]);
+               port->delayed_state = INVALID_STATE;
+               port->prev_state = port->state;
+               port->state = state;
+               /*
+                * Don't re-queue the state machine work item if we're currently
+                * in the state machine and we're immediately changing states.
+                * tcpm_state_machine_work() will continue running the state
+                * machine.
+                */
+               if (!port->state_machine_running)
+                       mod_delayed_work(port->wq, &port->state_machine, 0);
+       }
+}
+
+static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
+                               unsigned int delay_ms)
+{
+       if (port->enter_state == port->state)
+               tcpm_set_state(port, state, delay_ms);
+       else
+               tcpm_log(port,
+                        "skipped %sstate change %s -> %s [%u ms], context state %s",
+                        delay_ms ? "delayed " : "",
+                        tcpm_states[port->state], tcpm_states[state],
+                        delay_ms, tcpm_states[port->enter_state]);
+}
+
+static void tcpm_queue_message(struct tcpm_port *port,
+                              enum pd_msg_request message)
+{
+       port->queued_message = message;
+       mod_delayed_work(port->wq, &port->state_machine, 0);
+}
+
+/*
+ * VDM/VDO handling functions
+ */
+static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
+                          const u32 *data, int cnt)
+{
+       port->vdo_count = cnt + 1;
+       port->vdo_data[0] = header;
+       memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
+       /* Set ready, vdm state machine will actually send */
+       port->vdm_retries = 0;
+       port->vdm_state = VDM_STATE_READY;
+}
+
+static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload,
+                                 int cnt)
+{
+       u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]);
+       u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]);
+
+       memset(&port->mode_data, 0, sizeof(port->mode_data));
+
+       port->partner_ident.id_header = vdo;
+       port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]);
+       port->partner_ident.product = product;
+
+       typec_partner_set_identity(port->partner);
+
+       tcpm_log(port, "Identity: %04x:%04x.%04x",
+                PD_IDH_VID(vdo),
+                PD_PRODUCT_PID(product), product & 0xffff);
+}
+
+static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload,
+                              int cnt)
+{
+       struct pd_mode_data *pmdata = &port->mode_data;
+       int i;
+
+       for (i = 1; i < cnt; i++) {
+               u32 p = le32_to_cpu(payload[i]);
+               u16 svid;
+
+               svid = (p >> 16) & 0xffff;
+               if (!svid)
+                       return false;
+
+               if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
+                       goto abort;
+
+               pmdata->svids[pmdata->nsvids++] = svid;
+               tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
+
+               svid = p & 0xffff;
+               if (!svid)
+                       return false;
+
+               if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
+                       goto abort;
+
+               pmdata->svids[pmdata->nsvids++] = svid;
+               tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
+       }
+       return true;
+abort:
+       tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
+       return false;
+}
+
+static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload,
+                              int cnt)
+{
+       struct pd_mode_data *pmdata = &port->mode_data;
+       struct typec_altmode_desc *paltmode;
+       int i;
+
+       if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
+               /* Already logged in svdm_consume_svids() */
+               return;
+       }
+
+       for (i = 1; i < cnt; i++) {
+               paltmode = &pmdata->altmode_desc[pmdata->altmodes];
+               memset(paltmode, 0, sizeof(*paltmode));
+
+               paltmode->svid = pmdata->svids[pmdata->svid_index];
+               paltmode->mode = i;
+               paltmode->vdo = le32_to_cpu(payload[i]);
+
+               tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
+                        pmdata->altmodes, paltmode->svid,
+                        paltmode->mode, paltmode->vdo);
+
+               pmdata->altmodes++;
+       }
+}
+
+static void tcpm_register_partner_altmodes(struct tcpm_port *port)
+{
+       struct pd_mode_data *modep = &port->mode_data;
+       struct typec_altmode *altmode;
+       int i;
+
+       for (i = 0; i < modep->altmodes; i++) {
+               altmode = typec_partner_register_altmode(port->partner,
+                                               &modep->altmode_desc[i]);
+               if (!altmode)
+                       tcpm_log(port, "Failed to register partner SVID 0x%04x",
+                                modep->altmode_desc[i].svid);
+               port->partner_altmode[i] = altmode;
+       }
+}
+
+#define supports_modal(port)   PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
+
+static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt,
+                       u32 *response)
+{
+       struct typec_altmode *adev;
+       struct typec_altmode *pdev;
+       struct pd_mode_data *modep;
+       u32 p[PD_MAX_PAYLOAD];
+       int rlen = 0;
+       int cmd_type;
+       int cmd;
+       int i;
+
+       for (i = 0; i < cnt; i++)
+               p[i] = le32_to_cpu(payload[i]);
+
+       cmd_type = PD_VDO_CMDT(p[0]);
+       cmd = PD_VDO_CMD(p[0]);
+
+       tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
+                p[0], cmd_type, cmd, cnt);
+
+       modep = &port->mode_data;
+
+       adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
+                                  PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
+
+       pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX,
+                                  PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
+
+       switch (cmd_type) {
+       case CMDT_INIT:
+               switch (cmd) {
+               case CMD_DISCOVER_IDENT:
+                       /* 6.4.4.3.1: Only respond as UFP (device) */
+                       if (port->data_role == TYPEC_DEVICE &&
+                           port->nr_snk_vdo) {
+                               for (i = 0; i <  port->nr_snk_vdo; i++)
+                                       response[i + 1] = port->snk_vdo[i];
+                               rlen = port->nr_snk_vdo + 1;
+                       }
+                       break;
+               case CMD_DISCOVER_SVID:
+                       break;
+               case CMD_DISCOVER_MODES:
+                       break;
+               case CMD_ENTER_MODE:
+                       break;
+               case CMD_EXIT_MODE:
+                       break;
+               case CMD_ATTENTION:
+                       /* Attention command does not have response */
+                       if (adev)
+                               typec_altmode_attention(adev, p[1]);
+                       return 0;
+               default:
+                       break;
+               }
+               if (rlen >= 1) {
+                       response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
+               } else if (rlen == 0) {
+                       response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
+                       rlen = 1;
+               } else {
+                       response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
+                       rlen = 1;
+               }
+               break;
+       case CMDT_RSP_ACK:
+               /* silently drop message if we are not connected */
+               if (IS_ERR_OR_NULL(port->partner))
+                       break;
+
+               switch (cmd) {
+               case CMD_DISCOVER_IDENT:
+                       /* 6.4.4.3.1 */
+                       svdm_consume_identity(port, payload, cnt);
+                       response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID);
+                       rlen = 1;
+                       break;
+               case CMD_DISCOVER_SVID:
+                       /* 6.4.4.3.2 */
+                       if (svdm_consume_svids(port, payload, cnt)) {
+                               response[0] = VDO(USB_SID_PD, 1,
+                                                 CMD_DISCOVER_SVID);
+                               rlen = 1;
+                       } else if (modep->nsvids && supports_modal(port)) {
+                               response[0] = VDO(modep->svids[0], 1,
+                                                 CMD_DISCOVER_MODES);
+                               rlen = 1;
+                       }
+                       break;
+               case CMD_DISCOVER_MODES:
+                       /* 6.4.4.3.3 */
+                       svdm_consume_modes(port, payload, cnt);
+                       modep->svid_index++;
+                       if (modep->svid_index < modep->nsvids) {
+                               u16 svid = modep->svids[modep->svid_index];
+                               response[0] = VDO(svid, 1, CMD_DISCOVER_MODES);
+                               rlen = 1;
+                       } else {
+                               tcpm_register_partner_altmodes(port);
+                       }
+                       break;
+               case CMD_ENTER_MODE:
+                       if (adev && pdev) {
+                               typec_altmode_update_active(pdev, true);
+
+                               if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
+                                       response[0] = VDO(adev->svid, 1,
+                                                         CMD_EXIT_MODE);
+                                       response[0] |= VDO_OPOS(adev->mode);
+                                       return 1;
+                               }
+                       }
+                       return 0;
+               case CMD_EXIT_MODE:
+                       if (adev && pdev) {
+                               typec_altmode_update_active(pdev, false);
+
+                               /* Back to USB Operation */
+                               WARN_ON(typec_altmode_notify(adev,
+                                                            TYPEC_STATE_USB,
+                                                            NULL));
+                       }
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case CMDT_RSP_NAK:
+               switch (cmd) {
+               case CMD_ENTER_MODE:
+                       /* Back to USB Operation */
+                       if (adev)
+                               WARN_ON(typec_altmode_notify(adev,
+                                                            TYPEC_STATE_USB,
+                                                            NULL));
+                       break;
+               default:
+                       break;
+               }
+               break;
+       default:
+               break;
+       }
+
+       /* Informing the alternate mode drivers about everything */
+       if (adev)
+               typec_altmode_vdm(adev, p[0], &p[1], cnt);
+
+       return rlen;
+}
+
+static void tcpm_handle_vdm_request(struct tcpm_port *port,
+                                   const __le32 *payload, int cnt)
+{
+       int rlen = 0;
+       u32 response[8] = { };
+       u32 p0 = le32_to_cpu(payload[0]);
+
+       if (port->vdm_state == VDM_STATE_BUSY) {
+               /* If UFP responded busy retry after timeout */
+               if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) {
+                       port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
+                       port->vdo_retry = (p0 & ~VDO_CMDT_MASK) |
+                               CMDT_INIT;
+                       mod_delayed_work(port->wq, &port->vdm_state_machine,
+                                        msecs_to_jiffies(PD_T_VDM_BUSY));
+                       return;
+               }
+               port->vdm_state = VDM_STATE_DONE;
+       }
+
+       if (PD_VDO_SVDM(p0))
+               rlen = tcpm_pd_svdm(port, payload, cnt, response);
+
+       if (rlen > 0) {
+               tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
+               mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
+       }
+}
+
+static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
+                         const u32 *data, int count)
+{
+       u32 header;
+
+       if (WARN_ON(count > VDO_MAX_SIZE - 1))
+               count = VDO_MAX_SIZE - 1;
+
+       /* set VDM header with VID & CMD */
+       header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
+                       1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd);
+       tcpm_queue_vdm(port, header, data, count);
+
+       mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
+}
+
+static unsigned int vdm_ready_timeout(u32 vdm_hdr)
+{
+       unsigned int timeout;
+       int cmd = PD_VDO_CMD(vdm_hdr);
+
+       /* its not a structured VDM command */
+       if (!PD_VDO_SVDM(vdm_hdr))
+               return PD_T_VDM_UNSTRUCTURED;
+
+       switch (PD_VDO_CMDT(vdm_hdr)) {
+       case CMDT_INIT:
+               if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
+                       timeout = PD_T_VDM_WAIT_MODE_E;
+               else
+                       timeout = PD_T_VDM_SNDR_RSP;
+               break;
+       default:
+               if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
+                       timeout = PD_T_VDM_E_MODE;
+               else
+                       timeout = PD_T_VDM_RCVR_RSP;
+               break;
+       }
+       return timeout;
+}
+
+static void vdm_run_state_machine(struct tcpm_port *port)
+{
+       struct pd_message msg;
+       int i, res;
+
+       switch (port->vdm_state) {
+       case VDM_STATE_READY:
+               /* Only transmit VDM if attached */
+               if (!port->attached) {
+                       port->vdm_state = VDM_STATE_ERR_BUSY;
+                       break;
+               }
+
+               /*
+                * if there's traffic or we're not in PDO ready state don't send
+                * a VDM.
+                */
+               if (port->state != SRC_READY && port->state != SNK_READY)
+                       break;
+
+               /* Prepare and send VDM */
+               memset(&msg, 0, sizeof(msg));
+               msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
+                                         port->pwr_role,
+                                         port->data_role,
+                                         port->negotiated_rev,
+                                         port->message_id, port->vdo_count);
+               for (i = 0; i < port->vdo_count; i++)
+                       msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
+               res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+               if (res < 0) {
+                       port->vdm_state = VDM_STATE_ERR_SEND;
+               } else {
+                       unsigned long timeout;
+
+                       port->vdm_retries = 0;
+                       port->vdm_state = VDM_STATE_BUSY;
+                       timeout = vdm_ready_timeout(port->vdo_data[0]);
+                       mod_delayed_work(port->wq, &port->vdm_state_machine,
+                                        timeout);
+               }
+               break;
+       case VDM_STATE_WAIT_RSP_BUSY:
+               port->vdo_data[0] = port->vdo_retry;
+               port->vdo_count = 1;
+               port->vdm_state = VDM_STATE_READY;
+               break;
+       case VDM_STATE_BUSY:
+               port->vdm_state = VDM_STATE_ERR_TMOUT;
+               break;
+       case VDM_STATE_ERR_SEND:
+               /*
+                * A partner which does not support USB PD will not reply,
+                * so this is not a fatal error. At the same time, some
+                * devices may not return GoodCRC under some circumstances,
+                * so we need to retry.
+                */
+               if (port->vdm_retries < 3) {
+                       tcpm_log(port, "VDM Tx error, retry");
+                       port->vdm_retries++;
+                       port->vdm_state = VDM_STATE_READY;
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static void vdm_state_machine_work(struct work_struct *work)
+{
+       struct tcpm_port *port = container_of(work, struct tcpm_port,
+                                             vdm_state_machine.work);
+       enum vdm_states prev_state;
+
+       mutex_lock(&port->lock);
+
+       /*
+        * Continue running as long as the port is not busy and there was
+        * a state change.
+        */
+       do {
+               prev_state = port->vdm_state;
+               vdm_run_state_machine(port);
+       } while (port->vdm_state != prev_state &&
+                port->vdm_state != VDM_STATE_BUSY);
+
+       mutex_unlock(&port->lock);
+}
+
+enum pdo_err {
+       PDO_NO_ERR,
+       PDO_ERR_NO_VSAFE5V,
+       PDO_ERR_VSAFE5V_NOT_FIRST,
+       PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
+       PDO_ERR_FIXED_NOT_SORTED,
+       PDO_ERR_VARIABLE_BATT_NOT_SORTED,
+       PDO_ERR_DUPE_PDO,
+       PDO_ERR_PPS_APDO_NOT_SORTED,
+       PDO_ERR_DUPE_PPS_APDO,
+};
+
+static const char * const pdo_err_msg[] = {
+       [PDO_ERR_NO_VSAFE5V] =
+       " err: source/sink caps should atleast have vSafe5V",
+       [PDO_ERR_VSAFE5V_NOT_FIRST] =
+       " err: vSafe5V Fixed Supply Object Shall always be the first object",
+       [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
+       " err: PDOs should be in the following order: Fixed; Battery; Variable",
+       [PDO_ERR_FIXED_NOT_SORTED] =
+       " err: Fixed supply pdos should be in increasing order of their fixed voltage",
+       [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
+       " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
+       [PDO_ERR_DUPE_PDO] =
+       " err: Variable/Batt supply pdos cannot have same min/max voltage",
+       [PDO_ERR_PPS_APDO_NOT_SORTED] =
+       " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
+       [PDO_ERR_DUPE_PPS_APDO] =
+       " err: Programmable power supply apdos cannot have same min/max voltage and max current",
+};
+
+static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
+                                 unsigned int nr_pdo)
+{
+       unsigned int i;
+
+       /* Should at least contain vSafe5v */
+       if (nr_pdo < 1)
+               return PDO_ERR_NO_VSAFE5V;
+
+       /* The vSafe5V Fixed Supply Object Shall always be the first object */
+       if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
+           pdo_fixed_voltage(pdo[0]) != VSAFE5V)
+               return PDO_ERR_VSAFE5V_NOT_FIRST;
+
+       for (i = 1; i < nr_pdo; i++) {
+               if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
+                       return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
+               } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
+                       enum pd_pdo_type type = pdo_type(pdo[i]);
+
+                       switch (type) {
+                       /*
+                        * The remaining Fixed Supply Objects, if
+                        * present, shall be sent in voltage order;
+                        * lowest to highest.
+                        */
+                       case PDO_TYPE_FIXED:
+                               if (pdo_fixed_voltage(pdo[i]) <=
+                                   pdo_fixed_voltage(pdo[i - 1]))
+                                       return PDO_ERR_FIXED_NOT_SORTED;
+                               break;
+                       /*
+                        * The Battery Supply Objects and Variable
+                        * supply, if present shall be sent in Minimum
+                        * Voltage order; lowest to highest.
+                        */
+                       case PDO_TYPE_VAR:
+                       case PDO_TYPE_BATT:
+                               if (pdo_min_voltage(pdo[i]) <
+                                   pdo_min_voltage(pdo[i - 1]))
+                                       return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
+                               else if ((pdo_min_voltage(pdo[i]) ==
+                                         pdo_min_voltage(pdo[i - 1])) &&
+                                        (pdo_max_voltage(pdo[i]) ==
+                                         pdo_min_voltage(pdo[i - 1])))
+                                       return PDO_ERR_DUPE_PDO;
+                               break;
+                       /*
+                        * The Programmable Power Supply APDOs, if present,
+                        * shall be sent in Maximum Voltage order;
+                        * lowest to highest.
+                        */
+                       case PDO_TYPE_APDO:
+                               if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
+                                       break;
+
+                               if (pdo_pps_apdo_max_voltage(pdo[i]) <
+                                   pdo_pps_apdo_max_voltage(pdo[i - 1]))
+                                       return PDO_ERR_PPS_APDO_NOT_SORTED;
+                               else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
+                                         pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
+                                        pdo_pps_apdo_max_voltage(pdo[i]) ==
+                                         pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
+                                        pdo_pps_apdo_max_current(pdo[i]) ==
+                                         pdo_pps_apdo_max_current(pdo[i - 1]))
+                                       return PDO_ERR_DUPE_PPS_APDO;
+                               break;
+                       default:
+                               tcpm_log_force(port, " Unknown pdo type");
+                       }
+               }
+       }
+
+       return PDO_NO_ERR;
+}
+
+static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
+                             unsigned int nr_pdo)
+{
+       enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
+
+       if (err_index != PDO_NO_ERR) {
+               tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int tcpm_altmode_enter(struct typec_altmode *altmode)
+{
+       struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
+       u32 header;
+
+       mutex_lock(&port->lock);
+       header = VDO(altmode->svid, 1, CMD_ENTER_MODE);
+       header |= VDO_OPOS(altmode->mode);
+
+       tcpm_queue_vdm(port, header, NULL, 0);
+       mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
+       mutex_unlock(&port->lock);
+
+       return 0;
+}
+
+static int tcpm_altmode_exit(struct typec_altmode *altmode)
+{
+       struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
+       u32 header;
+
+       mutex_lock(&port->lock);
+       header = VDO(altmode->svid, 1, CMD_EXIT_MODE);
+       header |= VDO_OPOS(altmode->mode);
+
+       tcpm_queue_vdm(port, header, NULL, 0);
+       mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
+       mutex_unlock(&port->lock);
+
+       return 0;
+}
+
+static int tcpm_altmode_vdm(struct typec_altmode *altmode,
+                           u32 header, const u32 *data, int count)
+{
+       struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
+
+       mutex_lock(&port->lock);
+       tcpm_queue_vdm(port, header, data, count - 1);
+       mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
+       mutex_unlock(&port->lock);
+
+       return 0;
+}
+
+static const struct typec_altmode_ops tcpm_altmode_ops = {
+       .enter = tcpm_altmode_enter,
+       .exit = tcpm_altmode_exit,
+       .vdm = tcpm_altmode_vdm,
+};
+
+/*
+ * PD (data, control) command handling functions
+ */
+static inline enum tcpm_state ready_state(struct tcpm_port *port)
+{
+       if (port->pwr_role == TYPEC_SOURCE)
+               return SRC_READY;
+       else
+               return SNK_READY;
+}
+
+static int tcpm_pd_send_control(struct tcpm_port *port,
+                               enum pd_ctrl_msg_type type);
+
+static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
+                             int cnt)
+{
+       u32 p0 = le32_to_cpu(payload[0]);
+       unsigned int type = usb_pd_ado_type(p0);
+
+       if (!type) {
+               tcpm_log(port, "Alert message received with no type");
+               return;
+       }
+
+       /* Just handling non-battery alerts for now */
+       if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
+               switch (port->state) {
+               case SRC_READY:
+               case SNK_READY:
+                       tcpm_set_state(port, GET_STATUS_SEND, 0);
+                       break;
+               default:
+                       tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
+                       break;
+               }
+       }
+}
+
+static void tcpm_pd_data_request(struct tcpm_port *port,
+                                const struct pd_message *msg)
+{
+       enum pd_data_msg_type type = pd_header_type_le(msg->header);
+       unsigned int cnt = pd_header_cnt_le(msg->header);
+       unsigned int rev = pd_header_rev_le(msg->header);
+       unsigned int i;
+
+       switch (type) {
+       case PD_DATA_SOURCE_CAP:
+               if (port->pwr_role != TYPEC_SINK)
+                       break;
+
+               for (i = 0; i < cnt; i++)
+                       port->source_caps[i] = le32_to_cpu(msg->payload[i]);
+
+               port->nr_source_caps = cnt;
+
+               tcpm_log_source_caps(port);
+
+               tcpm_validate_caps(port, port->source_caps,
+                                  port->nr_source_caps);
+
+               /*
+                * Adjust revision in subsequent message headers, as required,
+                * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
+                * support Rev 1.0 so just do nothing in that scenario.
+                */
+               if (rev == PD_REV10)
+                       break;
+
+               if (rev < PD_MAX_REV)
+                       port->negotiated_rev = rev;
+
+               /*
+                * This message may be received even if VBUS is not
+                * present. This is quite unexpected; see USB PD
+                * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
+                * However, at the same time, we must be ready to
+                * receive this message and respond to it 15ms after
+                * receiving PS_RDY during power swap operations, no matter
+                * if VBUS is available or not (USB PD specification,
+                * section 6.5.9.2).
+                * So we need to accept the message either way,
+                * but be prepared to keep waiting for VBUS after it was
+                * handled.
+                */
+               tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
+               break;
+       case PD_DATA_REQUEST:
+               if (port->pwr_role != TYPEC_SOURCE ||
+                   cnt != 1) {
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+
+               /*
+                * Adjust revision in subsequent message headers, as required,
+                * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
+                * support Rev 1.0 so just reject in that scenario.
+                */
+               if (rev == PD_REV10) {
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+
+               if (rev < PD_MAX_REV)
+                       port->negotiated_rev = rev;
+
+               port->sink_request = le32_to_cpu(msg->payload[0]);
+               tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
+               break;
+       case PD_DATA_SINK_CAP:
+               /* We don't do anything with this at the moment... */
+               for (i = 0; i < cnt; i++)
+                       port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
+               port->nr_sink_caps = cnt;
+               break;
+       case PD_DATA_VENDOR_DEF:
+               tcpm_handle_vdm_request(port, msg->payload, cnt);
+               break;
+       case PD_DATA_BIST:
+               /* Enable TCPC BIST support and ignore BDO_MODE_TESTDATA */
+               if (le32_to_cpu(msg->payload[0]) == BDO_MODE_TESTDATA) {
+                       /* Hardware BIST will be turned off on port reset */
+                       port->tcpc->set_bist(port->tcpc, true);
+                       break;
+               }
+               if (port->state == SRC_READY || port->state == SNK_READY) {
+                       port->bist_request = le32_to_cpu(msg->payload[0]);
+                       tcpm_set_state(port, BIST_RX, 0);
+               }
+               break;
+       case PD_DATA_ALERT:
+               tcpm_handle_alert(port, msg->payload, cnt);
+               break;
+       case PD_DATA_BATT_STATUS:
+       case PD_DATA_GET_COUNTRY_INFO:
+               /* Currently unsupported */
+               tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
+               break;
+       default:
+               tcpm_log(port, "Unhandled data message type %#x", type);
+               break;
+       }
+}
+
+static void tcpm_pps_complete(struct tcpm_port *port, int result)
+{
+       if (port->pps_pending) {
+               port->pps_status = result;
+               port->pps_pending = false;
+               complete(&port->pps_complete);
+       }
+}
+
+static void tcpm_pd_ctrl_request(struct tcpm_port *port,
+                                const struct pd_message *msg)
+{
+       enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
+       enum tcpm_state next_state;
+
+       switch (type) {
+       case PD_CTRL_GOOD_CRC:
+       case PD_CTRL_PING:
+               break;
+       case PD_CTRL_GET_SOURCE_CAP:
+               switch (port->state) {
+               case SRC_READY:
+               case SNK_READY:
+                       tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP);
+                       break;
+               default:
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+               break;
+       case PD_CTRL_GET_SINK_CAP:
+               switch (port->state) {
+               case SRC_READY:
+               case SNK_READY:
+                       tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP);
+                       break;
+               default:
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+               break;
+       case PD_CTRL_GOTO_MIN:
+               break;
+       case PD_CTRL_PS_RDY:
+               switch (port->state) {
+               case SNK_TRANSITION_SINK:
+                       if (port->vbus_present) {
+                               tcpm_set_current_limit(port,
+                                                      port->current_limit,
+                                                      port->supply_voltage);
+                               port->explicit_contract = true;
+                               tcpm_set_state(port, SNK_READY, 0);
+                       } else {
+                               /*
+                                * Seen after power swap. Keep waiting for VBUS
+                                * in a transitional state.
+                                */
+                               tcpm_set_state(port,
+                                              SNK_TRANSITION_SINK_VBUS, 0);
+                       }
+                       break;
+               case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
+                       tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
+                       break;
+               case PR_SWAP_SNK_SRC_SINK_OFF:
+                       tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
+                       break;
+               case VCONN_SWAP_WAIT_FOR_VCONN:
+                       tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case PD_CTRL_REJECT:
+       case PD_CTRL_WAIT:
+       case PD_CTRL_NOT_SUPP:
+               switch (port->state) {
+               case SNK_NEGOTIATE_CAPABILITIES:
+                       /* USB PD specification, Figure 8-43 */
+                       if (port->explicit_contract)
+                               next_state = SNK_READY;
+                       else
+                               next_state = SNK_WAIT_CAPABILITIES;
+                       tcpm_set_state(port, next_state, 0);
+                       break;
+               case SNK_NEGOTIATE_PPS_CAPABILITIES:
+                       /* Revert data back from any requested PPS updates */
+                       port->pps_data.out_volt = port->supply_voltage;
+                       port->pps_data.op_curr = port->current_limit;
+                       port->pps_status = (type == PD_CTRL_WAIT ?
+                                           -EAGAIN : -EOPNOTSUPP);
+                       tcpm_set_state(port, SNK_READY, 0);
+                       break;
+               case DR_SWAP_SEND:
+                       port->swap_status = (type == PD_CTRL_WAIT ?
+                                            -EAGAIN : -EOPNOTSUPP);
+                       tcpm_set_state(port, DR_SWAP_CANCEL, 0);
+                       break;
+               case PR_SWAP_SEND:
+                       port->swap_status = (type == PD_CTRL_WAIT ?
+                                            -EAGAIN : -EOPNOTSUPP);
+                       tcpm_set_state(port, PR_SWAP_CANCEL, 0);
+                       break;
+               case VCONN_SWAP_SEND:
+                       port->swap_status = (type == PD_CTRL_WAIT ?
+                                            -EAGAIN : -EOPNOTSUPP);
+                       tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case PD_CTRL_ACCEPT:
+               switch (port->state) {
+               case SNK_NEGOTIATE_CAPABILITIES:
+                       port->pps_data.active = false;
+                       tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
+                       break;
+               case SNK_NEGOTIATE_PPS_CAPABILITIES:
+                       port->pps_data.active = true;
+                       port->supply_voltage = port->pps_data.out_volt;
+                       port->current_limit = port->pps_data.op_curr;
+                       tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
+                       break;
+               case SOFT_RESET_SEND:
+                       port->message_id = 0;
+                       port->rx_msgid = -1;
+                       if (port->pwr_role == TYPEC_SOURCE)
+                               next_state = SRC_SEND_CAPABILITIES;
+                       else
+                               next_state = SNK_WAIT_CAPABILITIES;
+                       tcpm_set_state(port, next_state, 0);
+                       break;
+               case DR_SWAP_SEND:
+                       tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
+                       break;
+               case PR_SWAP_SEND:
+                       tcpm_set_state(port, PR_SWAP_START, 0);
+                       break;
+               case VCONN_SWAP_SEND:
+                       tcpm_set_state(port, VCONN_SWAP_START, 0);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case PD_CTRL_SOFT_RESET:
+               tcpm_set_state(port, SOFT_RESET, 0);
+               break;
+       case PD_CTRL_DR_SWAP:
+               if ((port->port_type != TYPEC_PORT_DRP) &&
+                   (port->typec_caps.data != TYPEC_PORT_DRD)) {
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+               /*
+                * XXX
+                * 6.3.9: If an alternate mode is active, a request to swap
+                * alternate modes shall trigger a port reset.
+                */
+               switch (port->state) {
+               case SRC_READY:
+               case SNK_READY:
+                       tcpm_set_state(port, DR_SWAP_ACCEPT, 0);
+                       break;
+               default:
+                       tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
+                       break;
+               }
+               break;
+       case PD_CTRL_PR_SWAP:
+               if (port->port_type != TYPEC_PORT_DRP) {
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+               switch (port->state) {
+               case SRC_READY:
+               case SNK_READY:
+                       tcpm_set_state(port, PR_SWAP_ACCEPT, 0);
+                       break;
+               default:
+                       tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
+                       break;
+               }
+               break;
+       case PD_CTRL_VCONN_SWAP:
+               if (port->typec_caps.no_vconn) {
+                       tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
+                       break;
+               }
+
+               switch (port->state) {
+               case SRC_READY:
+               case SNK_READY:
+                       tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0);
+                       break;
+               default:
+                       tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
+                       break;
+               }
+               break;
+       case PD_CTRL_GET_SOURCE_CAP_EXT:
+       case PD_CTRL_GET_STATUS:
+       case PD_CTRL_FR_SWAP:
+       case PD_CTRL_GET_PPS_STATUS:
+       case PD_CTRL_GET_COUNTRY_CODES:
+               /* Currently not supported */
+               tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
+               break;
+       default:
+               tcpm_log(port, "Unhandled ctrl message type %#x", type);
+               break;
+       }
+}
+
+static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
+                                   const struct pd_message *msg)
+{
+       enum pd_ext_msg_type type = pd_header_type_le(msg->header);
+       unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
+
+       if (!(msg->ext_msg.header & PD_EXT_HDR_CHUNKED)) {
+               tcpm_log(port, "Unchunked extended messages unsupported");
+               return;
+       }
+
+       if (data_size > PD_EXT_MAX_CHUNK_DATA) {
+               tcpm_log(port, "Chunk handling not yet supported");
+               return;
+       }
+
+       switch (type) {
+       case PD_EXT_STATUS:
+               /*
+                * If PPS related events raised then get PPS status to clear
+                * (see USB PD 3.0 Spec, 6.5.2.4)
+                */
+               if (msg->ext_msg.data[USB_PD_EXT_SDB_EVENT_FLAGS] &
+                   USB_PD_EXT_SDB_PPS_EVENTS)
+                       tcpm_set_state(port, GET_PPS_STATUS_SEND, 0);
+               else
+                       tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case PD_EXT_PPS_STATUS:
+               /*
+                * For now the PPS status message is used to clear events
+                * and nothing more.
+                */
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case PD_EXT_SOURCE_CAP_EXT:
+       case PD_EXT_GET_BATT_CAP:
+       case PD_EXT_GET_BATT_STATUS:
+       case PD_EXT_BATT_CAP:
+       case PD_EXT_GET_MANUFACTURER_INFO:
+       case PD_EXT_MANUFACTURER_INFO:
+       case PD_EXT_SECURITY_REQUEST:
+       case PD_EXT_SECURITY_RESPONSE:
+       case PD_EXT_FW_UPDATE_REQUEST:
+       case PD_EXT_FW_UPDATE_RESPONSE:
+       case PD_EXT_COUNTRY_INFO:
+       case PD_EXT_COUNTRY_CODES:
+               tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
+               break;
+       default:
+               tcpm_log(port, "Unhandled extended message type %#x", type);
+               break;
+       }
+}
+
+static void tcpm_pd_rx_handler(struct work_struct *work)
+{
+       struct pd_rx_event *event = container_of(work,
+                                                struct pd_rx_event, work);
+       const struct pd_message *msg = &event->msg;
+       unsigned int cnt = pd_header_cnt_le(msg->header);
+       struct tcpm_port *port = event->port;
+
+       mutex_lock(&port->lock);
+
+       tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
+                port->attached);
+
+       if (port->attached) {
+               enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
+               unsigned int msgid = pd_header_msgid_le(msg->header);
+
+               /*
+                * USB PD standard, 6.6.1.2:
+                * "... if MessageID value in a received Message is the
+                * same as the stored value, the receiver shall return a
+                * GoodCRC Message with that MessageID value and drop
+                * the Message (this is a retry of an already received
+                * Message). Note: this shall not apply to the Soft_Reset
+                * Message which always has a MessageID value of zero."
+                */
+               if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
+                       goto done;
+               port->rx_msgid = msgid;
+
+               /*
+                * If both ends believe to be DFP/host, we have a data role
+                * mismatch.
+                */
+               if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
+                   (port->data_role == TYPEC_HOST)) {
+                       tcpm_log(port,
+                                "Data role mismatch, initiating error recovery");
+                       tcpm_set_state(port, ERROR_RECOVERY, 0);
+               } else {
+                       if (msg->header & PD_HEADER_EXT_HDR)
+                               tcpm_pd_ext_msg_request(port, msg);
+                       else if (cnt)
+                               tcpm_pd_data_request(port, msg);
+                       else
+                               tcpm_pd_ctrl_request(port, msg);
+               }
+       }
+
+done:
+       mutex_unlock(&port->lock);
+       kfree(event);
+}
+
+void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
+{
+       struct pd_rx_event *event;
+
+       event = kzalloc(sizeof(*event), GFP_ATOMIC);
+       if (!event)
+               return;
+
+       INIT_WORK(&event->work, tcpm_pd_rx_handler);
+       event->port = port;
+       memcpy(&event->msg, msg, sizeof(*msg));
+       queue_work(port->wq, &event->work);
+}
+EXPORT_SYMBOL_GPL(tcpm_pd_receive);
+
+static int tcpm_pd_send_control(struct tcpm_port *port,
+                               enum pd_ctrl_msg_type type)
+{
+       struct pd_message msg;
+
+       memset(&msg, 0, sizeof(msg));
+       msg.header = PD_HEADER_LE(type, port->pwr_role,
+                                 port->data_role,
+                                 port->negotiated_rev,
+                                 port->message_id, 0);
+
+       return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+}
+
+/*
+ * Send queued message without affecting state.
+ * Return true if state machine should go back to sleep,
+ * false otherwise.
+ */
+static bool tcpm_send_queued_message(struct tcpm_port *port)
+{
+       enum pd_msg_request queued_message;
+
+       do {
+               queued_message = port->queued_message;
+               port->queued_message = PD_MSG_NONE;
+
+               switch (queued_message) {
+               case PD_MSG_CTRL_WAIT:
+                       tcpm_pd_send_control(port, PD_CTRL_WAIT);
+                       break;
+               case PD_MSG_CTRL_REJECT:
+                       tcpm_pd_send_control(port, PD_CTRL_REJECT);
+                       break;
+               case PD_MSG_CTRL_NOT_SUPP:
+                       tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
+                       break;
+               case PD_MSG_DATA_SINK_CAP:
+                       tcpm_pd_send_sink_caps(port);
+                       break;
+               case PD_MSG_DATA_SOURCE_CAP:
+                       tcpm_pd_send_source_caps(port);
+                       break;
+               default:
+                       break;
+               }
+       } while (port->queued_message != PD_MSG_NONE);
+
+       if (port->delayed_state != INVALID_STATE) {
+               if (time_is_after_jiffies(port->delayed_runtime)) {
+                       mod_delayed_work(port->wq, &port->state_machine,
+                                        port->delayed_runtime - jiffies);
+                       return true;
+               }
+               port->delayed_state = INVALID_STATE;
+       }
+       return false;
+}
+
+static int tcpm_pd_check_request(struct tcpm_port *port)
+{
+       u32 pdo, rdo = port->sink_request;
+       unsigned int max, op, pdo_max, index;
+       enum pd_pdo_type type;
+
+       index = rdo_index(rdo);
+       if (!index || index > port->nr_src_pdo)
+               return -EINVAL;
+
+       pdo = port->src_pdo[index - 1];
+       type = pdo_type(pdo);
+       switch (type) {
+       case PDO_TYPE_FIXED:
+       case PDO_TYPE_VAR:
+               max = rdo_max_current(rdo);
+               op = rdo_op_current(rdo);
+               pdo_max = pdo_max_current(pdo);
+
+               if (op > pdo_max)
+                       return -EINVAL;
+               if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
+                       return -EINVAL;
+
+               if (type == PDO_TYPE_FIXED)
+                       tcpm_log(port,
+                                "Requested %u mV, %u mA for %u / %u mA",
+                                pdo_fixed_voltage(pdo), pdo_max, op, max);
+               else
+                       tcpm_log(port,
+                                "Requested %u -> %u mV, %u mA for %u / %u mA",
+                                pdo_min_voltage(pdo), pdo_max_voltage(pdo),
+                                pdo_max, op, max);
+               break;
+       case PDO_TYPE_BATT:
+               max = rdo_max_power(rdo);
+               op = rdo_op_power(rdo);
+               pdo_max = pdo_max_power(pdo);
+
+               if (op > pdo_max)
+                       return -EINVAL;
+               if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
+                       return -EINVAL;
+               tcpm_log(port,
+                        "Requested %u -> %u mV, %u mW for %u / %u mW",
+                        pdo_min_voltage(pdo), pdo_max_voltage(pdo),
+                        pdo_max, op, max);
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       port->op_vsafe5v = index == 1;
+
+       return 0;
+}
+
+#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, 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;
+
+       port->pps_data.supported = false;
+       port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
+
+       /*
+        * 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);
+
+               switch (type) {
+               case PDO_TYPE_FIXED:
+                       max_src_mv = pdo_fixed_voltage(pdo);
+                       min_src_mv = max_src_mv;
+                       break;
+               case PDO_TYPE_BATT:
+               case PDO_TYPE_VAR:
+                       max_src_mv = pdo_max_voltage(pdo);
+                       min_src_mv = pdo_min_voltage(pdo);
+                       break;
+               case PDO_TYPE_APDO:
+                       if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
+                               port->pps_data.supported = true;
+                               port->usb_type =
+                                       POWER_SUPPLY_USB_TYPE_PD_PPS;
+                       }
+                       continue;
+               default:
+                       tcpm_log(port, "Invalid source PDO type, ignoring");
+                       continue;
+               }
+
+               switch (type) {
+               case PDO_TYPE_FIXED:
+               case PDO_TYPE_VAR:
+                       src_ma = pdo_max_current(pdo);
+                       src_mw = src_ma * min_src_mv / 1000;
+                       break;
+               case PDO_TYPE_BATT:
+                       src_mw = pdo_max_power(pdo);
+                       break;
+               case PDO_TYPE_APDO:
+                       continue;
+               default:
+                       tcpm_log(port, "Invalid source PDO type, ignoring");
+                       continue;
+               }
+
+               for (j = 0; j < port->nr_snk_pdo; j++) {
+                       pdo = port->snk_pdo[j];
+
+                       switch (pdo_type(pdo)) {
+                       case PDO_TYPE_FIXED:
+                               max_snk_mv = pdo_fixed_voltage(pdo);
+                               min_snk_mv = max_snk_mv;
+                               break;
+                       case PDO_TYPE_BATT:
+                       case PDO_TYPE_VAR:
+                               max_snk_mv = pdo_max_voltage(pdo);
+                               min_snk_mv = pdo_min_voltage(pdo);
+                               break;
+                       case PDO_TYPE_APDO:
+                               continue;
+                       default:
+                               tcpm_log(port, "Invalid sink PDO type, ignoring");
+                               continue;
+                       }
+
+                       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;
+                               }
+                       }
+               }
+       }
+
+       return ret;
+}
+
+#define min_pps_apdo_current(x, y)     \
+       min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y))
+
+static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
+{
+       unsigned int i, j, max_mw = 0, max_mv = 0;
+       unsigned int min_src_mv, max_src_mv, src_ma, src_mw;
+       unsigned int min_snk_mv, max_snk_mv, snk_ma;
+       u32 pdo;
+       unsigned int src_pdo = 0, snk_pdo = 0;
+
+       /*
+        * Select the source PPS APDO providing the most power while staying
+        * within the board's limits. We skip the first PDO as this is always
+        * 5V 3A.
+        */
+       for (i = 1; i < port->nr_source_caps; ++i) {
+               pdo = port->source_caps[i];
+
+               switch (pdo_type(pdo)) {
+               case PDO_TYPE_APDO:
+                       if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
+                               tcpm_log(port, "Not PPS APDO (source), ignoring");
+                               continue;
+                       }
+
+                       min_src_mv = pdo_pps_apdo_min_voltage(pdo);
+                       max_src_mv = pdo_pps_apdo_max_voltage(pdo);
+                       src_ma = pdo_pps_apdo_max_current(pdo);
+                       src_mw = (src_ma * max_src_mv) / 1000;
+
+                       /*
+                        * Now search through the sink PDOs to find a matching
+                        * PPS APDO. Again skip the first sink PDO as this will
+                        * always be 5V 3A.
+                        */
+                       for (j = 1; j < port->nr_snk_pdo; j++) {
+                               pdo = port->snk_pdo[j];
+
+                               switch (pdo_type(pdo)) {
+                               case PDO_TYPE_APDO:
+                                       if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
+                                               tcpm_log(port,
+                                                        "Not PPS APDO (sink), ignoring");
+                                               continue;
+                                       }
+
+                                       min_snk_mv =
+                                               pdo_pps_apdo_min_voltage(pdo);
+                                       max_snk_mv =
+                                               pdo_pps_apdo_max_voltage(pdo);
+                                       snk_ma =
+                                               pdo_pps_apdo_max_current(pdo);
+                                       break;
+                               default:
+                                       tcpm_log(port,
+                                                "Not APDO type (sink), ignoring");
+                                       continue;
+                               }
+
+                               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;
+                                               snk_pdo = j;
+                                               max_mw = src_mw;
+                                               max_mv = max_src_mv;
+                                       }
+                               }
+                       }
+
+                       break;
+               default:
+                       tcpm_log(port, "Not APDO type (source), ignoring");
+                       continue;
+               }
+       }
+
+       if (src_pdo) {
+               pdo = port->source_caps[src_pdo];
+
+               port->pps_data.min_volt = pdo_pps_apdo_min_voltage(pdo);
+               port->pps_data.max_volt = pdo_pps_apdo_max_voltage(pdo);
+               port->pps_data.max_curr =
+                       min_pps_apdo_current(pdo, port->snk_pdo[snk_pdo]);
+               port->pps_data.out_volt =
+                       min(pdo_pps_apdo_max_voltage(pdo), port->pps_data.out_volt);
+               port->pps_data.op_curr =
+                       min(port->pps_data.max_curr, port->pps_data.op_curr);
+       }
+
+       return src_pdo;
+}
+
+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;
+       u32 pdo, matching_snk_pdo;
+       int src_pdo_index = 0;
+       int snk_pdo_index = 0;
+       int ret;
+
+       ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
+       if (ret < 0)
+               return ret;
+
+       pdo = port->source_caps[src_pdo_index];
+       matching_snk_pdo = port->snk_pdo[snk_pdo_index];
+       type = pdo_type(pdo);
+
+       switch (type) {
+       case PDO_TYPE_FIXED:
+               mv = pdo_fixed_voltage(pdo);
+               break;
+       case PDO_TYPE_BATT:
+       case PDO_TYPE_VAR:
+               mv = pdo_min_voltage(pdo);
+               break;
+       default:
+               tcpm_log(port, "Invalid PDO selected!");
+               return -EINVAL;
+       }
+
+       /* 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))
+                       max_ma = pdo_max_current(matching_snk_pdo);
+       }
+
+       tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
+                port->cc_req, port->cc1, port->cc2, port->vbus_source,
+                port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
+                port->polarity);
+
+       if (type == PDO_TYPE_BATT) {
+               *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
+
+               tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
+                        src_pdo_index, mv, mw,
+                        flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
+       } else {
+               *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
+
+               tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
+                        src_pdo_index, mv, ma,
+                        flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
+       }
+
+       port->current_limit = ma;
+       port->supply_voltage = mv;
+
+       return 0;
+}
+
+static int tcpm_pd_send_request(struct tcpm_port *port)
+{
+       struct pd_message msg;
+       int ret;
+       u32 rdo;
+
+       ret = tcpm_pd_build_request(port, &rdo);
+       if (ret < 0)
+               return ret;
+
+       memset(&msg, 0, sizeof(msg));
+       msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
+                                 port->pwr_role,
+                                 port->data_role,
+                                 port->negotiated_rev,
+                                 port->message_id, 1);
+       msg.payload[0] = cpu_to_le32(rdo);
+
+       return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+}
+
+static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
+{
+       unsigned int out_mv, op_ma, op_mw, min_mv, max_mv, max_ma, flags;
+       enum pd_pdo_type type;
+       unsigned int src_pdo_index;
+       u32 pdo;
+
+       src_pdo_index = tcpm_pd_select_pps_apdo(port);
+       if (!src_pdo_index)
+               return -EOPNOTSUPP;
+
+       pdo = port->source_caps[src_pdo_index];
+       type = pdo_type(pdo);
+
+       switch (type) {
+       case PDO_TYPE_APDO:
+               if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
+                       tcpm_log(port, "Invalid APDO selected!");
+                       return -EINVAL;
+               }
+               min_mv = port->pps_data.min_volt;
+               max_mv = port->pps_data.max_volt;
+               max_ma = port->pps_data.max_curr;
+               out_mv = port->pps_data.out_volt;
+               op_ma = port->pps_data.op_curr;
+               break;
+       default:
+               tcpm_log(port, "Invalid PDO selected!");
+               return -EINVAL;
+       }
+
+       flags = RDO_USB_COMM | RDO_NO_SUSPEND;
+
+       op_mw = (op_ma * out_mv) / 1000;
+       if (op_mw < port->operating_snk_mw) {
+               /*
+                * Try raising current to meet power needs. If that's not enough
+                * then try upping the voltage. If that's still not enough
+                * then we've obviously chosen a PPS APDO which really isn't
+                * suitable so abandon ship.
+                */
+               op_ma = (port->operating_snk_mw * 1000) / out_mv;
+               if ((port->operating_snk_mw * 1000) % out_mv)
+                       ++op_ma;
+               op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
+
+               if (op_ma > max_ma) {
+                       op_ma = max_ma;
+                       out_mv = (port->operating_snk_mw * 1000) / op_ma;
+                       if ((port->operating_snk_mw * 1000) % op_ma)
+                               ++out_mv;
+                       out_mv += RDO_PROG_VOLT_MV_STEP -
+                                 (out_mv % RDO_PROG_VOLT_MV_STEP);
+
+                       if (out_mv > max_mv) {
+                               tcpm_log(port, "Invalid PPS APDO selected!");
+                               return -EINVAL;
+                       }
+               }
+       }
+
+       tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
+                port->cc_req, port->cc1, port->cc2, port->vbus_source,
+                port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
+                port->polarity);
+
+       *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
+
+       tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
+                src_pdo_index, out_mv, op_ma);
+
+       port->pps_data.op_curr = op_ma;
+       port->pps_data.out_volt = out_mv;
+
+       return 0;
+}
+
+static int tcpm_pd_send_pps_request(struct tcpm_port *port)
+{
+       struct pd_message msg;
+       int ret;
+       u32 rdo;
+
+       ret = tcpm_pd_build_pps_request(port, &rdo);
+       if (ret < 0)
+               return ret;
+
+       memset(&msg, 0, sizeof(msg));
+       msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
+                                 port->pwr_role,
+                                 port->data_role,
+                                 port->negotiated_rev,
+                                 port->message_id, 1);
+       msg.payload[0] = cpu_to_le32(rdo);
+
+       return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+}
+
+static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
+{
+       int ret;
+
+       if (enable && port->vbus_charge)
+               return -EINVAL;
+
+       tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
+
+       ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
+       if (ret < 0)
+               return ret;
+
+       port->vbus_source = enable;
+       return 0;
+}
+
+static int tcpm_set_charge(struct tcpm_port *port, bool charge)
+{
+       int ret;
+
+       if (charge && port->vbus_source)
+               return -EINVAL;
+
+       if (charge != port->vbus_charge) {
+               tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
+               ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
+                                          charge);
+               if (ret < 0)
+                       return ret;
+       }
+       port->vbus_charge = charge;
+       return 0;
+}
+
+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, cc);
+               if (!ret)
+                       return true;
+       }
+
+       return false;
+}
+
+static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
+{
+       tcpm_log(port, "cc:=%d", cc);
+       port->cc_req = cc;
+       port->tcpc->set_cc(port->tcpc, cc);
+}
+
+static int tcpm_init_vbus(struct tcpm_port *port)
+{
+       int ret;
+
+       ret = port->tcpc->set_vbus(port->tcpc, false, false);
+       port->vbus_source = false;
+       port->vbus_charge = false;
+       return ret;
+}
+
+static int tcpm_init_vconn(struct tcpm_port *port)
+{
+       int ret;
+
+       ret = port->tcpc->set_vconn(port->tcpc, false);
+       port->vconn_role = TYPEC_SINK;
+       return ret;
+}
+
+static void tcpm_typec_connect(struct tcpm_port *port)
+{
+       if (!port->connected) {
+               /* Make sure we don't report stale identity information */
+               memset(&port->partner_ident, 0, sizeof(port->partner_ident));
+               port->partner_desc.usb_pd = port->pd_capable;
+               if (tcpm_port_is_debug(port))
+                       port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
+               else if (tcpm_port_is_audio(port))
+                       port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
+               else
+                       port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
+               port->partner = typec_register_partner(port->typec_port,
+                                                      &port->partner_desc);
+               port->connected = true;
+       }
+}
+
+static int tcpm_src_attach(struct tcpm_port *port)
+{
+       enum typec_cc_polarity polarity =
+                               port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
+                                                        : TYPEC_POLARITY_CC1;
+       int ret;
+
+       if (port->attached)
+               return 0;
+
+       ret = tcpm_set_polarity(port, polarity);
+       if (ret < 0)
+               return ret;
+
+       ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
+       if (ret < 0)
+               return ret;
+
+       ret = port->tcpc->set_pd_rx(port->tcpc, true);
+       if (ret < 0)
+               goto out_disable_mux;
+
+       /*
+        * USB Type-C specification, version 1.2,
+        * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
+        * Enable VCONN only if the non-RD port is set to RA.
+        */
+       if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
+           (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
+               ret = tcpm_set_vconn(port, true);
+               if (ret < 0)
+                       goto out_disable_pd;
+       }
+
+       ret = tcpm_set_vbus(port, true);
+       if (ret < 0)
+               goto out_disable_vconn;
+
+       port->pd_capable = false;
+
+       port->partner = NULL;
+
+       port->attached = true;
+       port->send_discover = true;
+
+       return 0;
+
+out_disable_vconn:
+       tcpm_set_vconn(port, false);
+out_disable_pd:
+       port->tcpc->set_pd_rx(port->tcpc, false);
+out_disable_mux:
+       tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
+                    TYPEC_ORIENTATION_NONE);
+       return ret;
+}
+
+static void tcpm_typec_disconnect(struct tcpm_port *port)
+{
+       if (port->connected) {
+               typec_unregister_partner(port->partner);
+               port->partner = NULL;
+               port->connected = false;
+       }
+}
+
+static void tcpm_unregister_altmodes(struct tcpm_port *port)
+{
+       struct pd_mode_data *modep = &port->mode_data;
+       int i;
+
+       for (i = 0; i < modep->altmodes; i++) {
+               typec_unregister_altmode(port->partner_altmode[i]);
+               port->partner_altmode[i] = NULL;
+       }
+
+       memset(modep, 0, sizeof(*modep));
+}
+
+static void tcpm_reset_port(struct tcpm_port *port)
+{
+       tcpm_unregister_altmodes(port);
+       tcpm_typec_disconnect(port);
+       port->attached = false;
+       port->pd_capable = false;
+       port->pps_data.supported = false;
+
+       /*
+        * First Rx ID should be 0; set this to a sentinel of -1 so that
+        * we can check tcpm_pd_rx_handler() if we had seen it before.
+        */
+       port->rx_msgid = -1;
+
+       port->tcpc->set_pd_rx(port->tcpc, false);
+       tcpm_init_vbus(port);   /* also disables charging */
+       tcpm_init_vconn(port);
+       tcpm_set_current_limit(port, 0, 0);
+       tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
+       tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
+                    TYPEC_ORIENTATION_NONE);
+       tcpm_set_attached_state(port, false);
+       port->try_src_count = 0;
+       port->try_snk_count = 0;
+       port->usb_type = POWER_SUPPLY_USB_TYPE_C;
+
+       power_supply_changed(port->psy);
+}
+
+static void tcpm_detach(struct tcpm_port *port)
+{
+       if (!port->attached)
+               return;
+
+       if (tcpm_port_is_disconnected(port))
+               port->hard_reset_count = 0;
+
+       tcpm_reset_port(port);
+}
+
+static void tcpm_src_detach(struct tcpm_port *port)
+{
+       tcpm_detach(port);
+}
+
+static int tcpm_snk_attach(struct tcpm_port *port)
+{
+       int ret;
+
+       if (port->attached)
+               return 0;
+
+       ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
+                               TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
+       if (ret < 0)
+               return ret;
+
+       ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
+       if (ret < 0)
+               return ret;
+
+       port->pd_capable = false;
+
+       port->partner = NULL;
+
+       port->attached = true;
+       port->send_discover = true;
+
+       return 0;
+}
+
+static void tcpm_snk_detach(struct tcpm_port *port)
+{
+       tcpm_detach(port);
+}
+
+static int tcpm_acc_attach(struct tcpm_port *port)
+{
+       int ret;
+
+       if (port->attached)
+               return 0;
+
+       ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
+       if (ret < 0)
+               return ret;
+
+       port->partner = NULL;
+
+       tcpm_typec_connect(port);
+
+       port->attached = true;
+
+       return 0;
+}
+
+static void tcpm_acc_detach(struct tcpm_port *port)
+{
+       tcpm_detach(port);
+}
+
+static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
+{
+       if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
+               return HARD_RESET_SEND;
+       if (port->pd_capable)
+               return ERROR_RECOVERY;
+       if (port->pwr_role == TYPEC_SOURCE)
+               return SRC_UNATTACHED;
+       if (port->state == SNK_WAIT_CAPABILITIES)
+               return SNK_READY;
+       return SNK_UNATTACHED;
+}
+
+static inline enum tcpm_state unattached_state(struct tcpm_port *port)
+{
+       if (port->port_type == TYPEC_PORT_DRP) {
+               if (port->pwr_role == TYPEC_SOURCE)
+                       return SRC_UNATTACHED;
+               else
+                       return SNK_UNATTACHED;
+       } else if (port->port_type == TYPEC_PORT_SRC) {
+               return SRC_UNATTACHED;
+       }
+
+       return SNK_UNATTACHED;
+}
+
+static void tcpm_check_send_discover(struct tcpm_port *port)
+{
+       if (port->data_role == TYPEC_HOST && port->send_discover &&
+           port->pd_capable) {
+               tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
+               port->send_discover = false;
+       }
+}
+
+static void tcpm_swap_complete(struct tcpm_port *port, int result)
+{
+       if (port->swap_pending) {
+               port->swap_status = result;
+               port->swap_pending = false;
+               port->non_pd_role_swap = false;
+               complete(&port->swap_complete);
+       }
+}
+
+static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
+{
+       switch (cc) {
+       case TYPEC_CC_RP_1_5:
+               return TYPEC_PWR_MODE_1_5A;
+       case TYPEC_CC_RP_3_0:
+               return TYPEC_PWR_MODE_3_0A;
+       case TYPEC_CC_RP_DEF:
+       default:
+               return TYPEC_PWR_MODE_USB;
+       }
+}
+
+static void run_state_machine(struct tcpm_port *port)
+{
+       int ret;
+       enum typec_pwr_opmode opmode;
+       unsigned int msecs;
+
+       port->enter_state = port->state;
+       switch (port->state) {
+       case DRP_TOGGLING:
+               break;
+       /* SRC states */
+       case SRC_UNATTACHED:
+               if (!port->non_pd_role_swap)
+                       tcpm_swap_complete(port, -ENOTCONN);
+               tcpm_src_detach(port);
+               if (tcpm_start_drp_toggling(port, tcpm_rp_cc(port))) {
+                       tcpm_set_state(port, DRP_TOGGLING, 0);
+                       break;
+               }
+               tcpm_set_cc(port, tcpm_rp_cc(port));
+               if (port->port_type == TYPEC_PORT_DRP)
+                       tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
+               break;
+       case SRC_ATTACH_WAIT:
+               if (tcpm_port_is_debug(port))
+                       tcpm_set_state(port, DEBUG_ACC_ATTACHED,
+                                      PD_T_CC_DEBOUNCE);
+               else if (tcpm_port_is_audio(port))
+                       tcpm_set_state(port, AUDIO_ACC_ATTACHED,
+                                      PD_T_CC_DEBOUNCE);
+               else if (tcpm_port_is_source(port))
+                       tcpm_set_state(port,
+                                      tcpm_try_snk(port) ? SNK_TRY
+                                                         : SRC_ATTACHED,
+                                      PD_T_CC_DEBOUNCE);
+               break;
+
+       case SNK_TRY:
+               port->try_snk_count++;
+               /*
+                * Requirements:
+                * - Do not drive vconn or vbus
+                * - Terminate CC pins (both) to Rd
+                * Action:
+                * - Wait for tDRPTry (PD_T_DRP_TRY).
+                *   Until then, ignore any state changes.
+                */
+               tcpm_set_cc(port, TYPEC_CC_RD);
+               tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
+               break;
+       case SNK_TRY_WAIT:
+               if (tcpm_port_is_sink(port)) {
+                       tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
+               } else {
+                       tcpm_set_state(port, SRC_TRYWAIT, 0);
+                       port->max_wait = 0;
+               }
+               break;
+       case SNK_TRY_WAIT_DEBOUNCE:
+               tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
+                              PD_T_PD_DEBOUNCE);
+               break;
+       case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
+               if (port->vbus_present && tcpm_port_is_sink(port)) {
+                       tcpm_set_state(port, SNK_ATTACHED, 0);
+               } else {
+                       tcpm_set_state(port, SRC_TRYWAIT, 0);
+                       port->max_wait = 0;
+               }
+               break;
+       case SRC_TRYWAIT:
+               tcpm_set_cc(port, tcpm_rp_cc(port));
+               if (port->max_wait == 0) {
+                       port->max_wait = jiffies +
+                                        msecs_to_jiffies(PD_T_DRP_TRY);
+                       tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
+                                      PD_T_DRP_TRY);
+               } else {
+                       if (time_is_after_jiffies(port->max_wait))
+                               tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
+                                              jiffies_to_msecs(port->max_wait -
+                                                               jiffies));
+                       else
+                               tcpm_set_state(port, SNK_UNATTACHED, 0);
+               }
+               break;
+       case SRC_TRYWAIT_DEBOUNCE:
+               tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
+               break;
+       case SRC_TRYWAIT_UNATTACHED:
+               tcpm_set_state(port, SNK_UNATTACHED, 0);
+               break;
+
+       case SRC_ATTACHED:
+               ret = tcpm_src_attach(port);
+               tcpm_set_state(port, SRC_UNATTACHED,
+                              ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
+               break;
+       case SRC_STARTUP:
+               opmode =  tcpm_get_pwr_opmode(tcpm_rp_cc(port));
+               typec_set_pwr_opmode(port->typec_port, opmode);
+               port->pwr_opmode = TYPEC_PWR_MODE_USB;
+               port->caps_count = 0;
+               port->negotiated_rev = PD_MAX_REV;
+               port->message_id = 0;
+               port->rx_msgid = -1;
+               port->explicit_contract = false;
+               tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
+               break;
+       case SRC_SEND_CAPABILITIES:
+               port->caps_count++;
+               if (port->caps_count > PD_N_CAPS_COUNT) {
+                       tcpm_set_state(port, SRC_READY, 0);
+                       break;
+               }
+               ret = tcpm_pd_send_source_caps(port);
+               if (ret < 0) {
+                       tcpm_set_state(port, SRC_SEND_CAPABILITIES,
+                                      PD_T_SEND_SOURCE_CAP);
+               } else {
+                       /*
+                        * Per standard, we should clear the reset counter here.
+                        * However, that can result in state machine hang-ups.
+                        * Reset it only in READY state to improve stability.
+                        */
+                       /* port->hard_reset_count = 0; */
+                       port->caps_count = 0;
+                       port->pd_capable = true;
+                       tcpm_set_state_cond(port, hard_reset_state(port),
+                                           PD_T_SEND_SOURCE_CAP);
+               }
+               break;
+       case SRC_NEGOTIATE_CAPABILITIES:
+               ret = tcpm_pd_check_request(port);
+               if (ret < 0) {
+                       tcpm_pd_send_control(port, PD_CTRL_REJECT);
+                       if (!port->explicit_contract) {
+                               tcpm_set_state(port,
+                                              SRC_WAIT_NEW_CAPABILITIES, 0);
+                       } else {
+                               tcpm_set_state(port, SRC_READY, 0);
+                       }
+               } else {
+                       tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
+                       tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
+                                      PD_T_SRC_TRANSITION);
+               }
+               break;
+       case SRC_TRANSITION_SUPPLY:
+               /* XXX: regulator_set_voltage(vbus, ...) */
+               tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
+               port->explicit_contract = true;
+               typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
+               port->pwr_opmode = TYPEC_PWR_MODE_PD;
+               tcpm_set_state_cond(port, SRC_READY, 0);
+               break;
+       case SRC_READY:
+#if 1
+               port->hard_reset_count = 0;
+#endif
+               port->try_src_count = 0;
+
+               tcpm_swap_complete(port, 0);
+               tcpm_typec_connect(port);
+
+               tcpm_check_send_discover(port);
+               /*
+                * 6.3.5
+                * Sending ping messages is not necessary if
+                * - the source operates at vSafe5V
+                * or
+                * - The system is not operating in PD mode
+                * or
+                * - Both partners are connected using a Type-C connector
+                *
+                * There is no actual need to send PD messages since the local
+                * port type-c and the spec does not clearly say whether PD is
+                * possible when type-c is connected to Type-A/B
+                */
+               break;
+       case SRC_WAIT_NEW_CAPABILITIES:
+               /* Nothing to do... */
+               break;
+
+       /* SNK states */
+       case SNK_UNATTACHED:
+               if (!port->non_pd_role_swap)
+                       tcpm_swap_complete(port, -ENOTCONN);
+               tcpm_pps_complete(port, -ENOTCONN);
+               tcpm_snk_detach(port);
+               if (tcpm_start_drp_toggling(port, TYPEC_CC_RD)) {
+                       tcpm_set_state(port, DRP_TOGGLING, 0);
+                       break;
+               }
+               tcpm_set_cc(port, TYPEC_CC_RD);
+               if (port->port_type == TYPEC_PORT_DRP)
+                       tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
+               else if (port->port_type == TYPEC_PORT_SNK)
+                       tcpm_set_state(port, SNK_ATTACH_WAIT, PD_T_DRP_SNK); // TODO: sync this with Rong.
+               break;
+       case SNK_ATTACH_WAIT:
+               if ((port->cc1 == TYPEC_CC_OPEN &&
+                    port->cc2 != TYPEC_CC_OPEN) ||
+                   (port->cc1 != TYPEC_CC_OPEN &&
+                    port->cc2 == TYPEC_CC_OPEN))
+                       tcpm_set_state(port, SNK_DEBOUNCED,
+                                      PD_T_CC_DEBOUNCE);
+               else if (tcpm_port_is_disconnected(port))
+                       tcpm_set_state(port, SNK_UNATTACHED,
+                                      PD_T_PD_DEBOUNCE);
+               break;
+       case SNK_DEBOUNCED:
+               if (tcpm_port_is_disconnected(port))
+                       tcpm_set_state(port, SNK_UNATTACHED,
+                                      PD_T_PD_DEBOUNCE);
+               else if (port->vbus_present)
+                       tcpm_set_state(port,
+                                      tcpm_try_src(port) ? SRC_TRY
+                                                         : SNK_ATTACHED,
+                                      0);
+               else
+                       /* Wait for VBUS, but not forever */
+                       tcpm_set_state(port, PORT_RESET, PD_T_PS_SOURCE_ON);
+               break;
+
+       case SRC_TRY:
+               port->try_src_count++;
+               tcpm_set_cc(port, tcpm_rp_cc(port));
+               port->max_wait = 0;
+               tcpm_set_state(port, SRC_TRY_WAIT, 0);
+               break;
+       case SRC_TRY_WAIT:
+               if (port->max_wait == 0) {
+                       port->max_wait = jiffies +
+                                        msecs_to_jiffies(PD_T_DRP_TRY);
+                       msecs = PD_T_DRP_TRY;
+               } else {
+                       if (time_is_after_jiffies(port->max_wait))
+                               msecs = jiffies_to_msecs(port->max_wait -
+                                                        jiffies);
+                       else
+                               msecs = 0;
+               }
+               tcpm_set_state(port, SNK_TRYWAIT, msecs);
+               break;
+       case SRC_TRY_DEBOUNCE:
+               tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
+               break;
+       case SNK_TRYWAIT:
+               tcpm_set_cc(port, TYPEC_CC_RD);
+               tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE);
+               break;
+       case SNK_TRYWAIT_VBUS:
+               /*
+                * TCPM stays in this state indefinitely until VBUS
+                * is detected as long as Rp is not detected for
+                * more than a time period of tPDDebounce.
+                */
+               if (port->vbus_present && tcpm_port_is_sink(port)) {
+                       tcpm_set_state(port, SNK_ATTACHED, 0);
+                       break;
+               }
+               if (!tcpm_port_is_sink(port))
+                       tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
+               break;
+       case SNK_TRYWAIT_DEBOUNCE:
+               tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
+               break;
+       case SNK_ATTACHED:
+               ret = tcpm_snk_attach(port);
+               if (ret < 0)
+                       tcpm_set_state(port, SNK_UNATTACHED, 0);
+               else
+                       tcpm_set_state(port, SNK_STARTUP, 0);
+               break;
+       case SNK_STARTUP:
+               opmode =  tcpm_get_pwr_opmode(port->polarity ?
+                                             port->cc2 : port->cc1);
+               typec_set_pwr_opmode(port->typec_port, opmode);
+               port->pwr_opmode = TYPEC_PWR_MODE_USB;
+               port->negotiated_rev = PD_MAX_REV;
+               port->message_id = 0;
+               port->rx_msgid = -1;
+               port->explicit_contract = false;
+               tcpm_set_state(port, SNK_DISCOVERY, 0);
+               break;
+       case SNK_DISCOVERY:
+               if (port->vbus_present) {
+                       tcpm_set_current_limit(port,
+                                              tcpm_get_current_limit(port),
+                                              5000);
+                       tcpm_set_charge(port, true);
+                       tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
+                       break;
+               }
+               /*
+                * For DRP, timeouts differ. Also, handling is supposed to be
+                * different and much more complex (dead battery detection;
+                * see USB power delivery specification, section 8.3.3.6.1.5.1).
+                */
+               tcpm_set_state(port, hard_reset_state(port),
+                              port->port_type == TYPEC_PORT_DRP ?
+                                       PD_T_DB_DETECT : PD_T_NO_RESPONSE);
+               break;
+       case SNK_DISCOVERY_DEBOUNCE:
+               tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
+                              PD_T_CC_DEBOUNCE);
+               break;
+       case SNK_DISCOVERY_DEBOUNCE_DONE:
+               if (!tcpm_port_is_disconnected(port) &&
+                   tcpm_port_is_sink(port) &&
+                   time_is_after_jiffies(port->delayed_runtime)) {
+                       tcpm_set_state(port, SNK_DISCOVERY,
+                                      jiffies_to_msecs(port->delayed_runtime -
+                                                       jiffies));
+                       break;
+               }
+               tcpm_set_state(port, unattached_state(port), 0);
+               break;
+       case SNK_WAIT_CAPABILITIES:
+               ret = port->tcpc->set_pd_rx(port->tcpc, true);
+               if (ret < 0) {
+                       tcpm_set_state(port, SNK_READY, 0);
+                       break;
+               }
+               /*
+                * If VBUS has never been low, and we time out waiting
+                * for source cap, try a soft reset first, in case we
+                * were already in a stable contract before this boot.
+                * Do this only once.
+                */
+               if (port->vbus_never_low) {
+                       port->vbus_never_low = false;
+                       tcpm_set_state(port, SOFT_RESET_SEND,
+                                      PD_T_SINK_WAIT_CAP);
+               } else {
+                       tcpm_set_state(port, hard_reset_state(port),
+                                      PD_T_SINK_WAIT_CAP);
+               }
+               break;
+       case SNK_NEGOTIATE_CAPABILITIES:
+               port->pd_capable = true;
+               port->hard_reset_count = 0;
+               ret = tcpm_pd_send_request(port);
+               if (ret < 0) {
+                       /* Let the Source send capabilities again. */
+                       tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
+               } else {
+                       tcpm_set_state_cond(port, hard_reset_state(port),
+                                           PD_T_SENDER_RESPONSE);
+               }
+               break;
+       case SNK_NEGOTIATE_PPS_CAPABILITIES:
+               ret = tcpm_pd_send_pps_request(port);
+               if (ret < 0) {
+                       port->pps_status = ret;
+                       /*
+                        * If this was called due to updates to sink
+                        * capabilities, and pps is no longer valid, we should
+                        * safely fall back to a standard PDO.
+                        */
+                       if (port->update_sink_caps)
+                               tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
+                       else
+                               tcpm_set_state(port, SNK_READY, 0);
+               } else {
+                       tcpm_set_state_cond(port, hard_reset_state(port),
+                                           PD_T_SENDER_RESPONSE);
+               }
+               break;
+       case SNK_TRANSITION_SINK:
+       case SNK_TRANSITION_SINK_VBUS:
+               tcpm_set_state(port, hard_reset_state(port),
+                              PD_T_PS_TRANSITION);
+               break;
+       case SNK_READY:
+               port->try_snk_count = 0;
+               port->update_sink_caps = false;
+               if (port->explicit_contract) {
+                       typec_set_pwr_opmode(port->typec_port,
+                                            TYPEC_PWR_MODE_PD);
+                       port->pwr_opmode = TYPEC_PWR_MODE_PD;
+               }
+
+               tcpm_swap_complete(port, 0);
+               tcpm_typec_connect(port);
+               tcpm_check_send_discover(port);
+               tcpm_pps_complete(port, port->pps_status);
+
+               power_supply_changed(port->psy);
+
+               break;
+
+       /* Accessory states */
+       case ACC_UNATTACHED:
+               tcpm_acc_detach(port);
+               tcpm_set_state(port, SRC_UNATTACHED, 0);
+               break;
+       case DEBUG_ACC_ATTACHED:
+       case AUDIO_ACC_ATTACHED:
+               ret = tcpm_acc_attach(port);
+               if (ret < 0)
+                       tcpm_set_state(port, ACC_UNATTACHED, 0);
+               break;
+       case AUDIO_ACC_DEBOUNCE:
+               tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
+               break;
+
+       /* Hard_Reset states */
+       case HARD_RESET_SEND:
+                /* TODO: Sync this with Rong.
+               tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
+               tcpm_set_state(port, HARD_RESET_START, 0);
+                */
+               tcpm_set_state(port, HARD_RESET_START, 0);
+               //tcpm_set_attached_state(port, true);
+               //tcpm_set_state(port, SNK_STARTUP, 0);
+               break;
+       case HARD_RESET_START:
+               port->hard_reset_count++;
+               port->tcpc->set_pd_rx(port->tcpc, false);
+               tcpm_unregister_altmodes(port);
+               port->send_discover = true;
+               if (port->pwr_role == TYPEC_SOURCE)
+                       tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
+                                      PD_T_PS_HARD_RESET);
+               else
+                       tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
+               break;
+       case SRC_HARD_RESET_VBUS_OFF:
+               tcpm_set_vconn(port, true);
+               tcpm_set_vbus(port, false);
+               tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
+                              TYPEC_HOST);
+               tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
+               break;
+       case SRC_HARD_RESET_VBUS_ON:
+               tcpm_set_vbus(port, true);
+               port->tcpc->set_pd_rx(port->tcpc, true);
+               tcpm_set_attached_state(port, true);
+               tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
+               break;
+       case SNK_HARD_RESET_SINK_OFF:
+               memset(&port->pps_data, 0, sizeof(port->pps_data));
+               tcpm_set_vconn(port, false);
+               tcpm_set_charge(port, false);
+               tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
+                              TYPEC_DEVICE);
+               /*
+                * VBUS may or may not toggle, depending on the adapter.
+                * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
+                * directly after timeout.
+                */
+               tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
+               break;
+       case SNK_HARD_RESET_WAIT_VBUS:
+               /* Assume we're disconnected if VBUS doesn't come back. */
+               tcpm_set_state(port, SNK_UNATTACHED,
+                              PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
+               break;
+       case SNK_HARD_RESET_SINK_ON:
+               /* Note: There is no guarantee that VBUS is on in this state */
+               /*
+                * XXX:
+                * The specification suggests that dual mode ports in sink
+                * mode should transition to state PE_SRC_Transition_to_default.
+                * See USB power delivery specification chapter 8.3.3.6.1.3.
+                * This would mean to to
+                * - turn off VCONN, reset power supply
+                * - request hardware reset
+                * - turn on VCONN
+                * - Transition to state PE_Src_Startup
+                * SNK only ports shall transition to state Snk_Startup
+                * (see chapter 8.3.3.3.8).
+                * Similar, dual-mode ports in source mode should transition
+                * to PE_SNK_Transition_to_default.
+                */
+               tcpm_set_attached_state(port, true);
+               tcpm_set_state(port, SNK_STARTUP, 0);
+               break;
+
+       /* Soft_Reset states */
+       case SOFT_RESET:
+               port->message_id = 0;
+               port->rx_msgid = -1;
+               tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
+               if (port->pwr_role == TYPEC_SOURCE)
+                       tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
+               else
+                       tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
+               break;
+       case SOFT_RESET_SEND:
+               port->message_id = 0;
+               port->rx_msgid = -1;
+               if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
+                       tcpm_set_state_cond(port, hard_reset_state(port), 0);
+               else
+                       tcpm_set_state_cond(port, hard_reset_state(port),
+                                           PD_T_SENDER_RESPONSE);
+               break;
+
+       /* DR_Swap states */
+       case DR_SWAP_SEND:
+               tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
+               tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
+                                   PD_T_SENDER_RESPONSE);
+               break;
+       case DR_SWAP_ACCEPT:
+               tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
+               tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
+               break;
+       case DR_SWAP_SEND_TIMEOUT:
+               tcpm_swap_complete(port, -ETIMEDOUT);
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case DR_SWAP_CHANGE_DR:
+               if (port->data_role == TYPEC_HOST) {
+                       tcpm_unregister_altmodes(port);
+                       tcpm_set_roles(port, true, port->pwr_role,
+                                      TYPEC_DEVICE);
+               } else {
+                       tcpm_set_roles(port, true, port->pwr_role,
+                                      TYPEC_HOST);
+                       port->send_discover = true;
+               }
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+
+       /* PR_Swap states */
+       case PR_SWAP_ACCEPT:
+               tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
+               tcpm_set_state(port, PR_SWAP_START, 0);
+               break;
+       case PR_SWAP_SEND:
+               tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
+               tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
+                                   PD_T_SENDER_RESPONSE);
+               break;
+       case PR_SWAP_SEND_TIMEOUT:
+               tcpm_swap_complete(port, -ETIMEDOUT);
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case PR_SWAP_START:
+               if (port->pwr_role == TYPEC_SOURCE)
+                       tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
+                                      PD_T_SRC_TRANSITION);
+               else
+                       tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
+               break;
+       case PR_SWAP_SRC_SNK_TRANSITION_OFF:
+               tcpm_set_vbus(port, false);
+               port->explicit_contract = false;
+               /* allow time for Vbus discharge, must be < tSrcSwapStdby */
+               tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
+                              PD_T_SRCSWAPSTDBY);
+               break;
+       case PR_SWAP_SRC_SNK_SOURCE_OFF:
+               tcpm_set_cc(port, TYPEC_CC_RD);
+               /* allow CC debounce */
+               tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
+                              PD_T_CC_DEBOUNCE);
+               break;
+       case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
+               /*
+                * USB-PD standard, 6.2.1.4, Port Power Role:
+                * "During the Power Role Swap Sequence, for the initial Source
+                * Port, the Port Power Role field shall be set to Sink in the
+                * PS_RDY Message indicating that the initial Source’s power
+                * supply is turned off"
+                */
+               tcpm_set_pwr_role(port, TYPEC_SINK);
+               if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
+                       tcpm_set_state(port, ERROR_RECOVERY, 0);
+                       break;
+               }
+               tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
+               break;
+       case PR_SWAP_SRC_SNK_SINK_ON:
+               tcpm_set_state(port, SNK_STARTUP, 0);
+               break;
+       case PR_SWAP_SNK_SRC_SINK_OFF:
+               tcpm_set_charge(port, false);
+               tcpm_set_state(port, hard_reset_state(port),
+                              PD_T_PS_SOURCE_OFF);
+               break;
+       case PR_SWAP_SNK_SRC_SOURCE_ON:
+               tcpm_set_cc(port, tcpm_rp_cc(port));
+               tcpm_set_vbus(port, true);
+               /*
+                * allow time VBUS ramp-up, must be < tNewSrc
+                * Also, this window overlaps with CC debounce as well.
+                * So, Wait for the max of two which is PD_T_NEWSRC
+                */
+               tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
+                              PD_T_NEWSRC);
+               break;
+       case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
+               /*
+                * USB PD standard, 6.2.1.4:
+                * "Subsequent Messages initiated by the Policy Engine,
+                * such as the PS_RDY Message sent to indicate that Vbus
+                * is ready, will have the Port Power Role field set to
+                * Source."
+                */
+               tcpm_set_pwr_role(port, TYPEC_SOURCE);
+               tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
+               tcpm_set_state(port, SRC_STARTUP, 0);
+               break;
+
+       case VCONN_SWAP_ACCEPT:
+               tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
+               tcpm_set_state(port, VCONN_SWAP_START, 0);
+               break;
+       case VCONN_SWAP_SEND:
+               tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
+               tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
+                              PD_T_SENDER_RESPONSE);
+               break;
+       case VCONN_SWAP_SEND_TIMEOUT:
+               tcpm_swap_complete(port, -ETIMEDOUT);
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case VCONN_SWAP_START:
+               if (port->vconn_role == TYPEC_SOURCE)
+                       tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
+               else
+                       tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
+               break;
+       case VCONN_SWAP_WAIT_FOR_VCONN:
+               tcpm_set_state(port, hard_reset_state(port),
+                              PD_T_VCONN_SOURCE_ON);
+               break;
+       case VCONN_SWAP_TURN_ON_VCONN:
+               tcpm_set_vconn(port, true);
+               tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case VCONN_SWAP_TURN_OFF_VCONN:
+               tcpm_set_vconn(port, false);
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+
+       case DR_SWAP_CANCEL:
+       case PR_SWAP_CANCEL:
+       case VCONN_SWAP_CANCEL:
+               tcpm_swap_complete(port, port->swap_status);
+               if (port->pwr_role == TYPEC_SOURCE)
+                       tcpm_set_state(port, SRC_READY, 0);
+               else
+                       tcpm_set_state(port, SNK_READY, 0);
+               break;
+
+       case BIST_RX:
+               switch (BDO_MODE_MASK(port->bist_request)) {
+               case BDO_MODE_CARRIER2:
+                       tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
+                       break;
+               default:
+                       break;
+               }
+               /* Always switch to unattached state */
+               tcpm_set_state(port, unattached_state(port), 0);
+               break;
+       case GET_STATUS_SEND:
+               tcpm_pd_send_control(port, PD_CTRL_GET_STATUS);
+               tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
+                              PD_T_SENDER_RESPONSE);
+               break;
+       case GET_STATUS_SEND_TIMEOUT:
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case GET_PPS_STATUS_SEND:
+               tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS);
+               tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
+                              PD_T_SENDER_RESPONSE);
+               break;
+       case GET_PPS_STATUS_SEND_TIMEOUT:
+               tcpm_set_state(port, ready_state(port), 0);
+               break;
+       case ERROR_RECOVERY:
+               tcpm_swap_complete(port, -EPROTO);
+               tcpm_pps_complete(port, -EPROTO);
+               tcpm_set_state(port, PORT_RESET, 0);
+               break;
+       case PORT_RESET:
+               tcpm_reset_port(port);
+               //tcpm_set_cc(port, TYPEC_CC_OPEN);
+               if (port->port_type == TYPEC_PORT_SNK) {
+                       tcpm_set_state(port, tcpm_default_state(port), 0);
+               } else {
+                       tcpm_set_state(port, PORT_RESET_WAIT_OFF,
+                                      PD_T_ERROR_RECOVERY);
+               }
+               break;
+       case PORT_RESET_WAIT_OFF:
+               tcpm_set_state(port,
+                              tcpm_default_state(port),
+                              port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
+               break;
+       default:
+               WARN(1, "Unexpected port state %d\n", port->state);
+               break;
+       }
+}
+
+static void tcpm_state_machine_work(struct work_struct *work)
+{
+       struct tcpm_port *port = container_of(work, struct tcpm_port,
+                                             state_machine.work);
+       enum tcpm_state prev_state;
+
+       mutex_lock(&port->lock);
+       port->state_machine_running = true;
+
+       if (port->queued_message && tcpm_send_queued_message(port))
+               goto done;
+
+       /* If we were queued due to a delayed state change, update it now */
+       if (port->delayed_state) {
+               tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
+                        tcpm_states[port->state],
+                        tcpm_states[port->delayed_state], port->delay_ms);
+               port->prev_state = port->state;
+               port->state = port->delayed_state;
+               port->delayed_state = INVALID_STATE;
+       }
+
+       /*
+        * Continue running as long as we have (non-delayed) state changes
+        * to make.
+        */
+       do {
+               prev_state = port->state;
+               run_state_machine(port);
+               if (port->queued_message)
+                       tcpm_send_queued_message(port);
+       } while (port->state != prev_state && !port->delayed_state);
+
+done:
+       port->state_machine_running = false;
+       mutex_unlock(&port->lock);
+}
+
+static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
+                           enum typec_cc_status cc2)
+{
+       enum typec_cc_status old_cc1, old_cc2;
+       enum tcpm_state new_state;
+
+       old_cc1 = port->cc1;
+       old_cc2 = port->cc2;
+       port->cc1 = cc1;
+       port->cc2 = cc2;
+
+       tcpm_log_force(port,
+                      "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
+                      old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
+                      port->polarity,
+                      tcpm_port_is_disconnected(port) ? "disconnected"
+                                                      : "connected");
+
+       switch (port->state) {
+       case DRP_TOGGLING:
+               if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
+                   tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
+               else if (tcpm_port_is_sink(port))
+                       tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
+               break;
+       case SRC_UNATTACHED:
+       case ACC_UNATTACHED:
+               if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
+                   tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
+               break;
+       case SRC_ATTACH_WAIT:
+               if (tcpm_port_is_disconnected(port) ||
+                   tcpm_port_is_audio_detached(port))
+                       tcpm_set_state(port, SRC_UNATTACHED, 0);
+               else if (cc1 != old_cc1 || cc2 != old_cc2)
+                       tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
+               break;
+       case SRC_ATTACHED:
+       case SRC_SEND_CAPABILITIES:
+       case SRC_READY:
+               if (tcpm_port_is_disconnected(port) ||
+                   !tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_UNATTACHED, 0);
+               break;
+       case SNK_UNATTACHED:
+               if (tcpm_port_is_sink(port))
+                       tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
+               break;
+       case SNK_ATTACH_WAIT:
+               if ((port->cc1 == TYPEC_CC_OPEN &&
+                    port->cc2 != TYPEC_CC_OPEN) ||
+                   (port->cc1 != TYPEC_CC_OPEN &&
+                    port->cc2 == TYPEC_CC_OPEN))
+                       new_state = SNK_DEBOUNCED;
+               else if (tcpm_port_is_disconnected(port))
+                       new_state = SNK_UNATTACHED;
+               else
+                       break;
+               if (new_state != port->delayed_state)
+                       tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
+               break;
+       case SNK_DEBOUNCED:
+               if (tcpm_port_is_disconnected(port))
+                       new_state = SNK_UNATTACHED;
+               else if (port->vbus_present)
+                       new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
+               else
+                       new_state = SNK_UNATTACHED;
+               if (new_state != port->delayed_state)
+                       tcpm_set_state(port, SNK_DEBOUNCED, 0);
+               break;
+       case SNK_READY:
+               if (tcpm_port_is_disconnected(port))
+                       tcpm_set_state(port, unattached_state(port), 0);
+               else if (!port->pd_capable &&
+                        (cc1 != old_cc1 || cc2 != old_cc2))
+                       tcpm_set_current_limit(port,
+                                              tcpm_get_current_limit(port),
+                                              5000);
+               break;
+
+       case AUDIO_ACC_ATTACHED:
+               if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
+                       tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
+               break;
+       case AUDIO_ACC_DEBOUNCE:
+               if (tcpm_port_is_audio(port))
+                       tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
+               break;
+
+       case DEBUG_ACC_ATTACHED:
+               if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
+                       tcpm_set_state(port, ACC_UNATTACHED, 0);
+               break;
+
+       case SNK_TRY:
+               /* Do nothing, waiting for timeout */
+               break;
+
+       case SNK_DISCOVERY:
+               /* CC line is unstable, wait for debounce */
+               if (tcpm_port_is_disconnected(port))
+                       tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
+               break;
+       case SNK_DISCOVERY_DEBOUNCE:
+               break;
+
+       case SRC_TRYWAIT:
+               /* Hand over to state machine if needed */
+               if (!port->vbus_present && tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
+               break;
+       case SRC_TRYWAIT_DEBOUNCE:
+               if (port->vbus_present || !tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_TRYWAIT, 0);
+               break;
+       case SNK_TRY_WAIT_DEBOUNCE:
+               if (!tcpm_port_is_sink(port)) {
+                       port->max_wait = 0;
+                       tcpm_set_state(port, SRC_TRYWAIT, 0);
+               }
+               break;
+       case SRC_TRY_WAIT:
+               if (tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
+               break;
+       case SRC_TRY_DEBOUNCE:
+               tcpm_set_state(port, SRC_TRY_WAIT, 0);
+               break;
+       case SNK_TRYWAIT_DEBOUNCE:
+               if (tcpm_port_is_sink(port))
+                       tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
+               break;
+       case SNK_TRYWAIT_VBUS:
+               if (!tcpm_port_is_sink(port))
+                       tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
+               break;
+       case SNK_TRYWAIT:
+               /* Do nothing, waiting for tCCDebounce */
+               break;
+       case PR_SWAP_SNK_SRC_SINK_OFF:
+       case PR_SWAP_SRC_SNK_TRANSITION_OFF:
+       case PR_SWAP_SRC_SNK_SOURCE_OFF:
+       case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
+       case PR_SWAP_SNK_SRC_SOURCE_ON:
+               /*
+                * CC state change is expected in PR_SWAP
+                * Ignore it.
+                */
+               break;
+
+       default:
+               if (tcpm_port_is_disconnected(port))
+                       tcpm_set_state(port, unattached_state(port), 0);
+               break;
+       }
+}
+
+static void _tcpm_pd_vbus_on(struct tcpm_port *port)
+{
+       tcpm_log_force(port, "VBUS on");
+       port->vbus_present = true;
+       switch (port->state) {
+       case SNK_TRANSITION_SINK_VBUS:
+               port->explicit_contract = true;
+               tcpm_set_state(port, SNK_READY, 0);
+               break;
+       case SNK_DISCOVERY:
+               tcpm_set_state(port, SNK_DISCOVERY, 0);
+               break;
+
+       case SNK_DEBOUNCED:
+               tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
+                                                       : SNK_ATTACHED,
+                                      0);
+               break;
+       case SNK_HARD_RESET_WAIT_VBUS:
+               tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
+               break;
+       case SRC_ATTACHED:
+               tcpm_set_state(port, SRC_STARTUP, 0);
+               break;
+       case SRC_HARD_RESET_VBUS_ON:
+               tcpm_set_state(port, SRC_STARTUP, 0);
+               break;
+
+       case SNK_TRY:
+               /* Do nothing, waiting for timeout */
+               break;
+       case SRC_TRYWAIT:
+               /* Do nothing, Waiting for Rd to be detected */
+               break;
+       case SRC_TRYWAIT_DEBOUNCE:
+               tcpm_set_state(port, SRC_TRYWAIT, 0);
+               break;
+       case SNK_TRY_WAIT_DEBOUNCE:
+               /* Do nothing, waiting for PD_DEBOUNCE to do be done */
+               break;
+       case SNK_TRYWAIT:
+               /* Do nothing, waiting for tCCDebounce */
+               break;
+       case SNK_TRYWAIT_VBUS:
+               if (tcpm_port_is_sink(port))
+                       tcpm_set_state(port, SNK_ATTACHED, 0);
+               break;
+       case SNK_TRYWAIT_DEBOUNCE:
+               /* Do nothing, waiting for Rp */
+               break;
+       case SRC_TRY_WAIT:
+       case SRC_TRY_DEBOUNCE:
+               /* Do nothing, waiting for sink detection */
+               break;
+       default:
+               break;
+       }
+}
+
+static void _tcpm_pd_vbus_off(struct tcpm_port *port)
+{
+       tcpm_log_force(port, "VBUS off");
+       port->vbus_present = false;
+       port->vbus_never_low = false;
+       switch (port->state) {
+       case SNK_HARD_RESET_SINK_OFF:
+               tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
+               break;
+       case SRC_HARD_RESET_VBUS_OFF:
+               tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0);
+               break;
+       case HARD_RESET_SEND:
+               break;
+
+       case SNK_TRY:
+               /* Do nothing, waiting for timeout */
+               break;
+       case SRC_TRYWAIT:
+               /* Hand over to state machine if needed */
+               if (tcpm_port_is_source(port))
+                       tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
+               break;
+       case SNK_TRY_WAIT_DEBOUNCE:
+               /* Do nothing, waiting for PD_DEBOUNCE to do be done */
+               break;
+       case SNK_TRYWAIT:
+       case SNK_TRYWAIT_VBUS:
+       case SNK_TRYWAIT_DEBOUNCE:
+               break;
+       case SNK_ATTACH_WAIT:
+               tcpm_set_state(port, SNK_UNATTACHED, 0);
+               break;
+
+       case SNK_NEGOTIATE_CAPABILITIES:
+               break;
+
+       case PR_SWAP_SRC_SNK_TRANSITION_OFF:
+               tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
+               break;
+
+       case PR_SWAP_SNK_SRC_SINK_OFF:
+               /* Do nothing, expected */
+               break;
+
+       case PORT_RESET_WAIT_OFF:
+               tcpm_set_state(port, tcpm_default_state(port), 0);
+               break;
+       case SRC_TRY_WAIT:
+       case SRC_TRY_DEBOUNCE:
+               /* Do nothing, waiting for sink detection */
+               break;
+       default:
+               if (port->pwr_role == TYPEC_SINK &&
+                   port->attached)
+                       tcpm_set_state(port, SNK_UNATTACHED, 0);
+               break;
+       }
+}
+
+static void _tcpm_pd_hard_reset(struct tcpm_port *port)
+{
+       tcpm_log_force(port, "Received hard reset");
+       /*
+        * If we keep receiving hard reset requests, executing the hard reset
+        * must have failed. Revert to error recovery if that happens.
+        */
+       tcpm_set_state(port,
+                      port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
+                               HARD_RESET_START : ERROR_RECOVERY,
+                      0);
+}
+
+static void tcpm_pd_event_handler(struct work_struct *work)
+{
+       struct tcpm_port *port = container_of(work, struct tcpm_port,
+                                             event_work);
+       u32 events;
+
+       mutex_lock(&port->lock);
+
+       spin_lock(&port->pd_event_lock);
+       while (port->pd_events) {
+               events = port->pd_events;
+               port->pd_events = 0;
+               spin_unlock(&port->pd_event_lock);
+               if (events & TCPM_RESET_EVENT)
+                       _tcpm_pd_hard_reset(port);
+               if (events & TCPM_VBUS_EVENT) {
+                       bool vbus;
+
+                       vbus = port->tcpc->get_vbus(port->tcpc);
+                       if (vbus)
+                               _tcpm_pd_vbus_on(port);
+                       else
+                               _tcpm_pd_vbus_off(port);
+               }
+               if (events & TCPM_CC_EVENT) {
+                       enum typec_cc_status cc1, cc2;
+
+                       if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
+                               _tcpm_cc_change(port, cc1, cc2);
+               }
+               spin_lock(&port->pd_event_lock);
+       }
+       spin_unlock(&port->pd_event_lock);
+       mutex_unlock(&port->lock);
+}
+
+void tcpm_cc_change(struct tcpm_port *port)
+{
+       spin_lock(&port->pd_event_lock);
+       port->pd_events |= TCPM_CC_EVENT;
+       spin_unlock(&port->pd_event_lock);
+       queue_work(port->wq, &port->event_work);
+}
+EXPORT_SYMBOL_GPL(tcpm_cc_change);
+
+void tcpm_vbus_change(struct tcpm_port *port)
+{
+       spin_lock(&port->pd_event_lock);
+       port->pd_events |= TCPM_VBUS_EVENT;
+       spin_unlock(&port->pd_event_lock);
+       queue_work(port->wq, &port->event_work);
+}
+EXPORT_SYMBOL_GPL(tcpm_vbus_change);
+
+void tcpm_pd_hard_reset(struct tcpm_port *port)
+{
+       spin_lock(&port->pd_event_lock);
+       port->pd_events = TCPM_RESET_EVENT;
+       spin_unlock(&port->pd_event_lock);
+       queue_work(port->wq, &port->event_work);
+}
+EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
+
+static int tcpm_dr_set(const struct typec_capability *cap,
+                      enum typec_data_role data)
+{
+       struct tcpm_port *port = typec_cap_to_tcpm(cap);
+       int ret;
+
+       mutex_lock(&port->swap_lock);
+       mutex_lock(&port->lock);
+
+       if (port->port_type != TYPEC_PORT_DRP) {
+               ret = -EINVAL;
+               goto port_unlock;
+       }
+       if (port->state != SRC_READY && port->state != SNK_READY) {
+               ret = -EAGAIN;
+               goto port_unlock;
+       }
+
+       if (port->data_role == data) {
+               ret = 0;
+               goto port_unlock;
+       }
+
+       /*
+        * XXX
+        * 6.3.9: If an alternate mode is active, a request to swap
+        * alternate modes shall trigger a port reset.
+        * Reject data role swap request in this case.
+        */
+
+       if (!port->pd_capable) {
+               /*
+                * If the partner is not PD capable, reset the port to
+                * trigger a role change. This can only work if a preferred
+                * role is configured, and if it matches the requested role.
+                */
+               if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
+                   port->try_role == port->pwr_role) {
+                       ret = -EINVAL;
+                       goto port_unlock;
+               }
+               port->non_pd_role_swap = true;
+               tcpm_set_state(port, PORT_RESET, 0);
+       } else {
+               tcpm_set_state(port, DR_SWAP_SEND, 0);
+       }
+
+       port->swap_status = 0;
+       port->swap_pending = true;
+       reinit_completion(&port->swap_complete);
+       mutex_unlock(&port->lock);
+
+       if (!wait_for_completion_timeout(&port->swap_complete,
+                               msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
+               ret = -ETIMEDOUT;
+       else
+               ret = port->swap_status;
+
+       port->non_pd_role_swap = false;
+       goto swap_unlock;
+
+port_unlock:
+       mutex_unlock(&port->lock);
+swap_unlock:
+       mutex_unlock(&port->swap_lock);
+       return ret;
+}
+
+static int tcpm_pr_set(const struct typec_capability *cap,
+                      enum typec_role role)
+{
+       struct tcpm_port *port = typec_cap_to_tcpm(cap);
+       int ret;
+
+       mutex_lock(&port->swap_lock);
+       mutex_lock(&port->lock);
+
+       if (port->port_type != TYPEC_PORT_DRP) {
+               ret = -EINVAL;
+               goto port_unlock;
+       }
+       if (port->state != SRC_READY && port->state != SNK_READY) {
+               ret = -EAGAIN;
+               goto port_unlock;
+       }
+
+       if (role == port->pwr_role) {
+               ret = 0;
+               goto port_unlock;
+       }
+
+       port->swap_status = 0;
+       port->swap_pending = true;
+       reinit_completion(&port->swap_complete);
+       tcpm_set_state(port, PR_SWAP_SEND, 0);
+       mutex_unlock(&port->lock);
+
+       if (!wait_for_completion_timeout(&port->swap_complete,
+                               msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
+               ret = -ETIMEDOUT;
+       else
+               ret = port->swap_status;
+
+       goto swap_unlock;
+
+port_unlock:
+       mutex_unlock(&port->lock);
+swap_unlock:
+       mutex_unlock(&port->swap_lock);
+       return ret;
+}
+
+static int tcpm_vconn_set(const struct typec_capability *cap,
+                         enum typec_role role)
+{
+       struct tcpm_port *port = typec_cap_to_tcpm(cap);
+       int ret;
+
+       mutex_lock(&port->swap_lock);
+       mutex_lock(&port->lock);
+
+       if (port->state != SRC_READY && port->state != SNK_READY) {
+               ret = -EAGAIN;
+               goto port_unlock;
+       }
+
+       if (role == port->vconn_role) {
+               ret = 0;
+               goto port_unlock;
+       }
+
+       port->swap_status = 0;
+       port->swap_pending = true;
+       reinit_completion(&port->swap_complete);
+       tcpm_set_state(port, VCONN_SWAP_SEND, 0);
+       mutex_unlock(&port->lock);
+
+       if (!wait_for_completion_timeout(&port->swap_complete,
+                               msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
+               ret = -ETIMEDOUT;
+       else
+               ret = port->swap_status;
+
+       goto swap_unlock;
+
+port_unlock:
+       mutex_unlock(&port->lock);
+swap_unlock:
+       mutex_unlock(&port->swap_lock);
+       return ret;
+}
+
+static int tcpm_try_role(const struct typec_capability *cap, int role)
+{
+       struct tcpm_port *port = typec_cap_to_tcpm(cap);
+       struct tcpc_dev *tcpc = port->tcpc;
+       int ret = 0;
+
+       mutex_lock(&port->lock);
+       if (tcpc->try_role)
+               ret = tcpc->try_role(tcpc, role);
+       if (!ret && (!tcpc->config || !tcpc->config->try_role_hw))
+               port->try_role = role;
+       port->try_src_count = 0;
+       port->try_snk_count = 0;
+       mutex_unlock(&port->lock);
+
+       return ret;
+}
+
+static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr)
+{
+       unsigned int target_mw;
+       int ret;
+
+       mutex_lock(&port->swap_lock);
+       mutex_lock(&port->lock);
+
+       if (!port->pps_data.active) {
+               ret = -EOPNOTSUPP;
+               goto port_unlock;
+       }
+
+       if (port->state != SNK_READY) {
+               ret = -EAGAIN;
+               goto port_unlock;
+       }
+
+       if (op_curr > port->pps_data.max_curr) {
+               ret = -EINVAL;
+               goto port_unlock;
+       }
+
+       target_mw = (op_curr * port->pps_data.out_volt) / 1000;
+       if (target_mw < port->operating_snk_mw) {
+               ret = -EINVAL;
+               goto port_unlock;
+       }
+
+       /* Round down operating current to align with PPS valid steps */
+       op_curr = op_curr - (op_curr % RDO_PROG_CURR_MA_STEP);
+
+       reinit_completion(&port->pps_complete);
+       port->pps_data.op_curr = op_curr;
+       port->pps_status = 0;
+       port->pps_pending = true;
+       tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
+       mutex_unlock(&port->lock);
+
+       if (!wait_for_completion_timeout(&port->pps_complete,
+                               msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
+               ret = -ETIMEDOUT;
+       else
+               ret = port->pps_status;
+
+       goto swap_unlock;
+
+port_unlock:
+       mutex_unlock(&port->lock);
+swap_unlock:
+       mutex_unlock(&port->swap_lock);
+
+       return ret;
+}
+
+static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt)
+{
+       unsigned int target_mw;
+       int ret;
+
+       mutex_lock(&port->swap_lock);
+       mutex_lock(&port->lock);
+
+       if (!port->pps_data.active) {
+               ret = -EOPNOTSUPP;
+               goto port_unlock;
+       }
+
+       if (port->state != SNK_READY) {
+               ret = -EAGAIN;
+               goto port_unlock;
+       }
+
+       if (out_volt < port->pps_data.min_volt ||
+           out_volt > port->pps_data.max_volt) {
+               ret = -EINVAL;
+               goto port_unlock;
+       }
+
+       target_mw = (port->pps_data.op_curr * out_volt) / 1000;
+       if (target_mw < port->operating_snk_mw) {
+               ret = -EINVAL;
+               goto port_unlock;
+       }
+
+       /* Round down output voltage to align with PPS valid steps */
+       out_volt = out_volt - (out_volt % RDO_PROG_VOLT_MV_STEP);
+
+       reinit_completion(&port->pps_complete);
+       port->pps_data.out_volt = out_volt;
+       port->pps_status = 0;
+       port->pps_pending = true;
+       tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
+       mutex_unlock(&port->lock);
+
+       if (!wait_for_completion_timeout(&port->pps_complete,
+                               msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
+               ret = -ETIMEDOUT;
+       else
+               ret = port->pps_status;
+
+       goto swap_unlock;
+
+port_unlock:
+       mutex_unlock(&port->lock);
+swap_unlock:
+       mutex_unlock(&port->swap_lock);
+
+       return ret;
+}
+
+static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
+{
+       int ret = 0;
+
+       mutex_lock(&port->swap_lock);
+       mutex_lock(&port->lock);
+
+       if (!port->pps_data.supported) {
+               ret = -EOPNOTSUPP;
+               goto port_unlock;
+       }
+
+       /* Trying to deactivate PPS when already deactivated so just bail */
+       if (!port->pps_data.active && !activate)
+               goto port_unlock;
+
+       if (port->state != SNK_READY) {
+               ret = -EAGAIN;
+               goto port_unlock;
+       }
+
+       reinit_completion(&port->pps_complete);
+       port->pps_status = 0;
+       port->pps_pending = true;
+
+       /* Trigger PPS request or move back to standard PDO contract */
+       if (activate) {
+               port->pps_data.out_volt = port->supply_voltage;
+               port->pps_data.op_curr = port->current_limit;
+               tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
+       } else {
+               tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
+       }
+       mutex_unlock(&port->lock);
+
+       if (!wait_for_completion_timeout(&port->pps_complete,
+                               msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
+               ret = -ETIMEDOUT;
+       else
+               ret = port->pps_status;
+
+       goto swap_unlock;
+
+port_unlock:
+       mutex_unlock(&port->lock);
+swap_unlock:
+       mutex_unlock(&port->swap_lock);
+
+       return ret;
+}
+
+static void tcpm_init(struct tcpm_port *port)
+{
+       enum typec_cc_status cc1, cc2;
+
+       port->tcpc->init(port->tcpc);
+
+       tcpm_reset_port(port);
+
+       /*
+        * XXX
+        * Should possibly wait for VBUS to settle if it was enabled locally
+        * since tcpm_reset_port() will disable VBUS.
+        */
+       port->vbus_present = port->tcpc->get_vbus(port->tcpc);
+       if (port->vbus_present)
+               port->vbus_never_low = true;
+
+       tcpm_set_state(port, tcpm_default_state(port), 0);
+
+       if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
+               _tcpm_cc_change(port, cc1, cc2);
+
+       /*
+        * Some adapters need a clean slate at startup, and won't recover
+        * otherwise. So do not try to be fancy and force a clean disconnect.
+        */
+       tcpm_set_state(port, PORT_RESET, 0);
+}
+
+static int tcpm_port_type_set(const struct typec_capability *cap,
+                             enum typec_port_type type)
+{
+       struct tcpm_port *port = typec_cap_to_tcpm(cap);
+
+       mutex_lock(&port->lock);
+       if (type == port->port_type)
+               goto port_unlock;
+
+       port->port_type = type;
+
+       if (!port->connected) {
+               tcpm_set_state(port, PORT_RESET, 0);
+       } else if (type == TYPEC_PORT_SNK) {
+               if (!(port->pwr_role == TYPEC_SINK &&
+                     port->data_role == TYPEC_DEVICE))
+                       tcpm_set_state(port, PORT_RESET, 0);
+       } else if (type == TYPEC_PORT_SRC) {
+               if (!(port->pwr_role == TYPEC_SOURCE &&
+                     port->data_role == TYPEC_HOST))
+                       tcpm_set_state(port, PORT_RESET, 0);
+       }
+
+port_unlock:
+       mutex_unlock(&port->lock);
+       return 0;
+}
+
+void tcpm_tcpc_reset(struct tcpm_port *port)
+{
+       mutex_lock(&port->lock);
+       /* XXX: Maintain PD connection if possible? */
+       tcpm_init(port);
+       mutex_unlock(&port->lock);
+}
+EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
+
+static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo,
+                         unsigned int nr_pdo)
+{
+       unsigned int i;
+
+       if (nr_pdo > PDO_MAX_OBJECTS)
+               nr_pdo = PDO_MAX_OBJECTS;
+
+       for (i = 0; i < nr_pdo; i++)
+               dest_pdo[i] = src_pdo[i];
+
+       return nr_pdo;
+}
+
+static int tcpm_copy_vdos(u32 *dest_vdo, const u32 *src_vdo,
+                         unsigned int nr_vdo)
+{
+       unsigned int i;
+
+       if (nr_vdo > VDO_MAX_OBJECTS)
+               nr_vdo = VDO_MAX_OBJECTS;
+
+       for (i = 0; i < nr_vdo; i++)
+               dest_vdo[i] = src_vdo[i];
+
+       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;
+
+       /* VCONN capability is true by default */
+       port->typec_caps.no_vconn = fwnode_property_read_bool(fwnode,
+                                                             "no-vconn");
+
+       /* USB data support is optional */
+       ret = fwnode_property_read_string(fwnode, "data-role", &cap_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", &cap_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", &cap_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", &mw) < 0)
+               return -EINVAL;
+       port->operating_snk_mw = mw / 1000;
+
+       port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
+
+       return 0;
+}
+
+int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
+                                   unsigned int nr_pdo)
+{
+       if (tcpm_validate_caps(port, pdo, nr_pdo))
+               return -EINVAL;
+
+       mutex_lock(&port->lock);
+       port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo);
+       switch (port->state) {
+       case SRC_UNATTACHED:
+       case SRC_ATTACH_WAIT:
+       case SRC_TRYWAIT:
+               tcpm_set_cc(port, tcpm_rp_cc(port));
+               break;
+       case SRC_SEND_CAPABILITIES:
+       case SRC_NEGOTIATE_CAPABILITIES:
+       case SRC_READY:
+       case SRC_WAIT_NEW_CAPABILITIES:
+               tcpm_set_cc(port, tcpm_rp_cc(port));
+               tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
+               break;
+       default:
+               break;
+       }
+       mutex_unlock(&port->lock);
+       return 0;
+}
+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 operating_snk_mw)
+{
+       if (tcpm_validate_caps(port, pdo, nr_pdo))
+               return -EINVAL;
+
+       mutex_lock(&port->lock);
+       port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
+       port->operating_snk_mw = operating_snk_mw;
+       port->update_sink_caps = true;
+
+       switch (port->state) {
+       case SNK_NEGOTIATE_CAPABILITIES:
+       case SNK_NEGOTIATE_PPS_CAPABILITIES:
+       case SNK_READY:
+       case SNK_TRANSITION_SINK:
+       case SNK_TRANSITION_SINK_VBUS:
+               if (port->pps_data.active)
+                       tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
+               else
+                       tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
+               break;
+       default:
+               break;
+       }
+       mutex_unlock(&port->lock);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
+
+/* Power Supply access to expose source power information */
+enum tcpm_psy_online_states {
+       TCPM_PSY_OFFLINE = 0,
+       TCPM_PSY_FIXED_ONLINE,
+       TCPM_PSY_PROG_ONLINE,
+};
+
+static enum power_supply_property tcpm_psy_props[] = {
+       POWER_SUPPLY_PROP_USB_TYPE,
+       POWER_SUPPLY_PROP_ONLINE,
+       POWER_SUPPLY_PROP_VOLTAGE_MIN,
+       POWER_SUPPLY_PROP_VOLTAGE_MAX,
+       POWER_SUPPLY_PROP_VOLTAGE_NOW,
+       POWER_SUPPLY_PROP_CURRENT_MAX,
+       POWER_SUPPLY_PROP_CURRENT_NOW,
+};
+
+static int tcpm_psy_get_online(struct tcpm_port *port,
+                              union power_supply_propval *val)
+{
+       if (port->vbus_charge) {
+               if (port->pps_data.active)
+                       val->intval = TCPM_PSY_PROG_ONLINE;
+               else
+                       val->intval = TCPM_PSY_FIXED_ONLINE;
+       } else {
+               val->intval = TCPM_PSY_OFFLINE;
+       }
+
+       return 0;
+}
+
+static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
+                                   union power_supply_propval *val)
+{
+       if (port->pps_data.active)
+               val->intval = port->pps_data.min_volt * 1000;
+       else
+               val->intval = port->supply_voltage * 1000;
+
+       return 0;
+}
+
+static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
+                                   union power_supply_propval *val)
+{
+       if (port->pps_data.active)
+               val->intval = port->pps_data.max_volt * 1000;
+       else
+               val->intval = port->supply_voltage * 1000;
+
+       return 0;
+}
+
+static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
+                                   union power_supply_propval *val)
+{
+       val->intval = port->supply_voltage * 1000;
+
+       return 0;
+}
+
+static int tcpm_psy_get_current_max(struct tcpm_port *port,
+                                   union power_supply_propval *val)
+{
+       if (port->pps_data.active)
+               val->intval = port->pps_data.max_curr * 1000;
+       else
+               val->intval = max(tcpm_get_max_fixed_current(port),
+                                 port->current_limit) * 1000;
+
+       return 0;
+}
+
+static int tcpm_psy_get_current_now(struct tcpm_port *port,
+                                   union power_supply_propval *val)
+{
+       val->intval = port->current_limit * 1000;
+
+       return 0;
+}
+
+static int tcpm_psy_get_prop(struct power_supply *psy,
+                            enum power_supply_property psp,
+                            union power_supply_propval *val)
+{
+       struct tcpm_port *port = power_supply_get_drvdata(psy);
+       int ret = 0;
+
+       switch (psp) {
+       case POWER_SUPPLY_PROP_USB_TYPE:
+               val->intval = port->usb_type;
+               break;
+       case POWER_SUPPLY_PROP_ONLINE:
+               ret = tcpm_psy_get_online(port, val);
+               break;
+       case POWER_SUPPLY_PROP_VOLTAGE_MIN:
+               ret = tcpm_psy_get_voltage_min(port, val);
+               break;
+       case POWER_SUPPLY_PROP_VOLTAGE_MAX:
+               ret = tcpm_psy_get_voltage_max(port, val);
+               break;
+       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+               ret = tcpm_psy_get_voltage_now(port, val);
+               break;
+       case POWER_SUPPLY_PROP_CURRENT_MAX:
+               ret = tcpm_psy_get_current_max(port, val);
+               break;
+       case POWER_SUPPLY_PROP_CURRENT_NOW:
+               ret = tcpm_psy_get_current_now(port, val);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
+static int tcpm_psy_set_online(struct tcpm_port *port,
+                              const union power_supply_propval *val)
+{
+       int ret;
+
+       switch (val->intval) {
+       case TCPM_PSY_FIXED_ONLINE:
+               ret = tcpm_pps_activate(port, false);
+               break;
+       case TCPM_PSY_PROG_ONLINE:
+               ret = tcpm_pps_activate(port, true);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
+static int tcpm_psy_set_prop(struct power_supply *psy,
+                            enum power_supply_property psp,
+                            const union power_supply_propval *val)
+{
+       struct tcpm_port *port = power_supply_get_drvdata(psy);
+       int ret;
+
+       switch (psp) {
+       case POWER_SUPPLY_PROP_ONLINE:
+               ret = tcpm_psy_set_online(port, val);
+               break;
+       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+               if (val->intval < port->pps_data.min_volt * 1000 ||
+                   val->intval > port->pps_data.max_volt * 1000)
+                       ret = -EINVAL;
+               else
+                       ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
+               break;
+       case POWER_SUPPLY_PROP_CURRENT_NOW:
+               if (val->intval > port->pps_data.max_curr * 1000)
+                       ret = -EINVAL;
+               else
+                       ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
+static int tcpm_psy_prop_writeable(struct power_supply *psy,
+                                  enum power_supply_property psp)
+{
+       switch (psp) {
+       case POWER_SUPPLY_PROP_ONLINE:
+       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+       case POWER_SUPPLY_PROP_CURRENT_NOW:
+               return 1;
+       default:
+               return 0;
+       }
+}
+
+static enum power_supply_usb_type tcpm_psy_usb_types[] = {
+       POWER_SUPPLY_USB_TYPE_C,
+       POWER_SUPPLY_USB_TYPE_PD,
+       POWER_SUPPLY_USB_TYPE_PD_PPS,
+};
+
+static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
+
+static int devm_tcpm_psy_register(struct tcpm_port *port)
+{
+       struct power_supply_config psy_cfg = {};
+       const char *port_dev_name = dev_name(port->dev);
+       size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
+                                    strlen(port_dev_name) + 1;
+       char *psy_name;
+
+       psy_cfg.drv_data = port;
+       psy_cfg.fwnode = dev_fwnode(port->dev);
+       psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
+       if (!psy_name)
+               return -ENOMEM;
+
+       snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
+                port_dev_name);
+       port->psy_desc.name = psy_name;
+       port->psy_desc.type = POWER_SUPPLY_TYPE_USB,
+       port->psy_desc.usb_types = tcpm_psy_usb_types;
+       port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
+       port->psy_desc.properties = tcpm_psy_props,
+       port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props),
+       port->psy_desc.get_property = tcpm_psy_get_prop,
+       port->psy_desc.set_property = tcpm_psy_set_prop,
+       port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable,
+
+       port->usb_type = POWER_SUPPLY_USB_TYPE_C;
+
+       port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
+                                              &psy_cfg);
+
+       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;
+       port->self_powered = tcfg->self_powered;
+
+       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->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
+           !tcpc->set_polarity || !tcpc->set_vconn  ||
+           !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
+               return ERR_PTR(-EINVAL);
+
+       port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
+       if (!port)
+               return ERR_PTR(-ENOMEM);
+
+       port->dev = dev;
+       port->tcpc = tcpc;
+
+       mutex_init(&port->lock);
+       mutex_init(&port->swap_lock);
+
+       port->wq = create_singlethread_workqueue(dev_name(dev));
+       if (!port->wq)
+               return ERR_PTR(-ENOMEM);
+       INIT_DELAYED_WORK(&port->state_machine, tcpm_state_machine_work);
+       INIT_DELAYED_WORK(&port->vdm_state_machine, vdm_state_machine_work);
+       INIT_WORK(&port->event_work, tcpm_pd_event_handler);
+
+       spin_lock_init(&port->pd_event_lock);
+
+       init_completion(&port->tx_complete);
+       init_completion(&port->swap_complete);
+       init_completion(&port->pps_complete);
+       tcpm_debugfs_init(port);
+
+       err = tcpm_fw_get_caps(port, tcpc->fwnode);
+       if ((err < 0) && tcpc->config)
+               err = tcpm_copy_caps(port, tcpc->config);
+       if (err < 0)
+               goto out_destroy_wq;
+
+       if (!tcpc->config || !tcpc->config->try_role_hw)
+               port->try_role = port->typec_caps.prefer_role;
+       else
+               port->try_role = TYPEC_NO_PREFERRED_ROLE;
+
+       port->typec_caps.fwnode = tcpc->fwnode;
+       port->typec_caps.revision = 0x0120;     /* Type-C spec release 1.2 */
+       port->typec_caps.pd_revision = 0x0300;  /* USB-PD spec release 3.0 */
+       port->typec_caps.dr_set = tcpm_dr_set;
+       port->typec_caps.pr_set = tcpm_pr_set;
+       port->typec_caps.vconn_set = tcpm_vconn_set;
+       port->typec_caps.try_role = tcpm_try_role;
+       port->typec_caps.port_type_set = tcpm_port_type_set;
+
+       port->partner_desc.identity = &port->partner_ident;
+       port->port_type = port->typec_caps.type;
+
+       port->role_sw = usb_role_switch_get(port->dev);
+       if (IS_ERR(port->role_sw)) {
+               err = PTR_ERR(port->role_sw);
+               goto out_destroy_wq;
+       }
+
+       err = devm_tcpm_psy_register(port);
+       if (err)
+               goto out_destroy_wq;
+
+       port->typec_port = typec_register_port(port->dev, &port->typec_caps);
+       if (IS_ERR(port->typec_port)) {
+               err = PTR_ERR(port->typec_port);
+               goto out_destroy_wq;
+       }
+
+       if (tcpc->config && tcpc->config->alt_modes) {
+               const struct typec_altmode_desc *paltmode = tcpc->config->alt_modes;
+
+               i = 0;
+               while (paltmode->svid && i < ARRAY_SIZE(port->port_altmode)) {
+                       struct typec_altmode *alt;
+
+                       alt = typec_port_register_altmode(port->typec_port,
+                                                         paltmode);
+                       if (IS_ERR(alt)) {
+                               tcpm_log(port,
+                                        "%s: failed to register port alternate mode 0x%x",
+                                        dev_name(dev), paltmode->svid);
+                               break;
+                       }
+                       typec_altmode_set_drvdata(alt, port);
+                       alt->ops = &tcpm_altmode_ops;
+                       port->port_altmode[i] = alt;
+                       i++;
+                       paltmode++;
+               }
+       }
+
+       mutex_lock(&port->lock);
+       tcpm_init(port);
+       mutex_unlock(&port->lock);
+
+       tcpm_log(port, "%s: registered", dev_name(dev));
+       return port;
+
+out_destroy_wq:
+       usb_role_switch_put(port->role_sw);
+       destroy_workqueue(port->wq);
+       return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(tcpm_register_port);
+
+void tcpm_unregister_port(struct tcpm_port *port)
+{
+       int i;
+
+       tcpm_reset_port(port);
+       for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
+               typec_unregister_altmode(port->port_altmode[i]);
+       typec_unregister_port(port->typec_port);
+       usb_role_switch_put(port->role_sw);
+       tcpm_debugfs_exit(port);
+       destroy_workqueue(port->wq);
+}
+EXPORT_SYMBOL_GPL(tcpm_unregister_port);
+
+MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
+MODULE_DESCRIPTION("USB Type-C Port Manager");
+MODULE_LICENSE("GPL");
diff --git a/include/dt-bindings/usb/pd.h b/include/dt-bindings/usb/pd.h
new file mode 100644 (file)
index 0000000..7b7a92f
--- /dev/null
@@ -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))
+
+#define VSAFE5V 5000 /* mv units */
+
+#define PDO_BATT_MAX_VOLT_SHIFT        20      /* 50mV units */
+#define PDO_BATT_MIN_VOLT_SHIFT        10      /* 50mV units */
+#define PDO_BATT_MAX_PWR_SHIFT 0       /* 250mW units */
+
+#define PDO_BATT_MIN_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_BATT_MIN_VOLT_SHIFT)
+#define PDO_BATT_MAX_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_BATT_MAX_VOLT_SHIFT)
+#define PDO_BATT_MAX_POWER(mw) ((((mw) / 250) & PDO_PWR_MASK) << PDO_BATT_MAX_PWR_SHIFT)
+
+#define PDO_BATT(min_mv, max_mv, max_mw)                       \
+       (PDO_TYPE(PDO_TYPE_BATT) | PDO_BATT_MIN_VOLT(min_mv) |  \
+        PDO_BATT_MAX_VOLT(max_mv) | PDO_BATT_MAX_POWER(max_mw))
+
+#define PDO_VAR_MAX_VOLT_SHIFT 20      /* 50mV units */
+#define PDO_VAR_MIN_VOLT_SHIFT 10      /* 50mV units */
+#define PDO_VAR_MAX_CURR_SHIFT 0       /* 10mA units */
+
+#define PDO_VAR_MIN_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_VAR_MIN_VOLT_SHIFT)
+#define PDO_VAR_MAX_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_VAR_MAX_VOLT_SHIFT)
+#define PDO_VAR_MAX_CURR(ma) ((((ma) / 10) & PDO_CURR_MASK) << PDO_VAR_MAX_CURR_SHIFT)
+
+#define PDO_VAR(min_mv, max_mv, max_ma)                                \
+       (PDO_TYPE(PDO_TYPE_VAR) | PDO_VAR_MIN_VOLT(min_mv) |    \
+        PDO_VAR_MAX_VOLT(max_mv) | PDO_VAR_MAX_CURR(max_ma))
+
+ #endif /* __DT_POWER_DELIVERY_H */
index df841a72b804a79a8b4cb9c015e5ce52f32867fc..d3fea084a8fe73343908b86b0ec89f407b7c4a44 100644 (file)
@@ -679,4 +679,16 @@ struct fsl_mc_device_id {
 };
 
 
+#define TYPEC_ANY_MODE 0x7
+/**
+ * struct typec_device_id - USB Type-C alternate mode identifiers
+ * @svid: Standard or Vendor ID
+ * @mode: Mode index
+ * @driver_data: Driver specific data
+ */
+struct typec_device_id {
+       __u16 svid;
+       __u8 mode;
+       kernel_ulong_t driver_data;
+};
 #endif /* LINUX_MOD_DEVICETABLE_H */
index 29fc01af3b37f32bdf3696ad869248bbb8c6419e..1173fcba4ef22d9126ebc8660b8a5360deebaba3 100644 (file)
@@ -146,6 +146,7 @@ enum power_supply_property {
        POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
        POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
        POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
+       POWER_SUPPLY_PROP_USB_TYPE,
        POWER_SUPPLY_PROP_SCOPE,
        POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
        POWER_SUPPLY_PROP_CALIBRATE,
@@ -175,6 +176,18 @@ enum power_supply_type {
        POWER_SUPPLY_TYPE_USB_PD_DRP,   /* PD Dual Role Port */
 };
 
+enum power_supply_usb_type {
+       POWER_SUPPLY_USB_TYPE_UNKNOWN = 0,
+       POWER_SUPPLY_USB_TYPE_SDP,              /* Standard Downstream Port */
+       POWER_SUPPLY_USB_TYPE_DCP,              /* Dedicated Charging Port */
+       POWER_SUPPLY_USB_TYPE_CDP,              /* Charging Downstream Port */
+       POWER_SUPPLY_USB_TYPE_ACA,              /* Accessory Charger Adapters */
+       POWER_SUPPLY_USB_TYPE_C,                /* Type C Port */
+       POWER_SUPPLY_USB_TYPE_PD,               /* Power Delivery Port */
+       POWER_SUPPLY_USB_TYPE_PD_DRP,           /* PD Dual Role Port */
+       POWER_SUPPLY_USB_TYPE_PD_PPS,           /* PD Programmable Power Supply */
+       POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,   /* Apple Charging Method */
+};
 enum power_supply_notifier_events {
        PSY_EVENT_PROP_CHANGED,
 };
@@ -191,6 +204,8 @@ struct power_supply;
 /* Run-time specific power supply configuration */
 struct power_supply_config {
        struct device_node *of_node;
+       struct fwnode_handle *fwnode;
+
        /* Driver private data */
        void *drv_data;
 
@@ -202,6 +217,8 @@ struct power_supply_config {
 struct power_supply_desc {
        const char *name;
        enum power_supply_type type;
+       enum power_supply_usb_type *usb_types;
+       size_t num_usb_types;
        enum power_supply_property *properties;
        size_t num_properties;
 
diff --git a/include/linux/usb/pd.h b/include/linux/usb/pd.h
new file mode 100644 (file)
index 0000000..f2162e0
--- /dev/null
@@ -0,0 +1,464 @@
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __LINUX_USB_PD_H
+#define __LINUX_USB_PD_H
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/usb/typec.h>
+
+/* USB PD Messages */
+enum pd_ctrl_msg_type {
+       /* 0 Reserved */
+       PD_CTRL_GOOD_CRC = 1,
+       PD_CTRL_GOTO_MIN = 2,
+       PD_CTRL_ACCEPT = 3,
+       PD_CTRL_REJECT = 4,
+       PD_CTRL_PING = 5,
+       PD_CTRL_PS_RDY = 6,
+       PD_CTRL_GET_SOURCE_CAP = 7,
+       PD_CTRL_GET_SINK_CAP = 8,
+       PD_CTRL_DR_SWAP = 9,
+       PD_CTRL_PR_SWAP = 10,
+       PD_CTRL_VCONN_SWAP = 11,
+       PD_CTRL_WAIT = 12,
+       PD_CTRL_SOFT_RESET = 13,
+       /* 14-15 Reserved */
+       PD_CTRL_NOT_SUPP = 16,
+       PD_CTRL_GET_SOURCE_CAP_EXT = 17,
+       PD_CTRL_GET_STATUS = 18,
+       PD_CTRL_FR_SWAP = 19,
+       PD_CTRL_GET_PPS_STATUS = 20,
+       PD_CTRL_GET_COUNTRY_CODES = 21,
+       /* 22-31 Reserved */
+};
+
+enum pd_data_msg_type {
+       /* 0 Reserved */
+       PD_DATA_SOURCE_CAP = 1,
+       PD_DATA_REQUEST = 2,
+       PD_DATA_BIST = 3,
+       PD_DATA_SINK_CAP = 4,
+       PD_DATA_BATT_STATUS = 5,
+       PD_DATA_ALERT = 6,
+       PD_DATA_GET_COUNTRY_INFO = 7,
+       /* 8-14 Reserved */
+       PD_DATA_VENDOR_DEF = 15,
+       /* 16-31 Reserved */
+};
+
+enum pd_ext_msg_type {
+       /* 0 Reserved */
+       PD_EXT_SOURCE_CAP_EXT = 1,
+       PD_EXT_STATUS = 2,
+       PD_EXT_GET_BATT_CAP = 3,
+       PD_EXT_GET_BATT_STATUS = 4,
+       PD_EXT_BATT_CAP = 5,
+       PD_EXT_GET_MANUFACTURER_INFO = 6,
+       PD_EXT_MANUFACTURER_INFO = 7,
+       PD_EXT_SECURITY_REQUEST = 8,
+       PD_EXT_SECURITY_RESPONSE = 9,
+       PD_EXT_FW_UPDATE_REQUEST = 10,
+       PD_EXT_FW_UPDATE_RESPONSE = 11,
+       PD_EXT_PPS_STATUS = 12,
+       PD_EXT_COUNTRY_INFO = 13,
+       PD_EXT_COUNTRY_CODES = 14,
+       /* 15-31 Reserved */
+};
+
+#define PD_REV10       0x0
+#define PD_REV20       0x1
+#define PD_REV30       0x2
+#define PD_MAX_REV     PD_REV30
+
+#define PD_HEADER_EXT_HDR      BIT(15)
+#define PD_HEADER_CNT_SHIFT    12
+#define PD_HEADER_CNT_MASK     0x7
+#define PD_HEADER_ID_SHIFT     9
+#define PD_HEADER_ID_MASK      0x7
+#define PD_HEADER_PWR_ROLE     BIT(8)
+#define PD_HEADER_REV_SHIFT    6
+#define PD_HEADER_REV_MASK     0x3
+#define PD_HEADER_DATA_ROLE    BIT(5)
+#define PD_HEADER_TYPE_SHIFT   0
+#define PD_HEADER_TYPE_MASK    0x1f
+
+#define PD_HEADER(type, pwr, data, rev, id, cnt, ext_hdr)              \
+       ((((type) & PD_HEADER_TYPE_MASK) << PD_HEADER_TYPE_SHIFT) |     \
+        ((pwr) == TYPEC_SOURCE ? PD_HEADER_PWR_ROLE : 0) |             \
+        ((data) == TYPEC_HOST ? PD_HEADER_DATA_ROLE : 0) |             \
+        (rev << PD_HEADER_REV_SHIFT) |                                 \
+        (((id) & PD_HEADER_ID_MASK) << PD_HEADER_ID_SHIFT) |           \
+        (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT) |        \
+        ((ext_hdr) ? PD_HEADER_EXT_HDR : 0))
+
+#define PD_HEADER_LE(type, pwr, data, rev, id, cnt) \
+       cpu_to_le16(PD_HEADER((type), (pwr), (data), (rev), (id), (cnt), (0)))
+
+static inline unsigned int pd_header_cnt(u16 header)
+{
+       return (header >> PD_HEADER_CNT_SHIFT) & PD_HEADER_CNT_MASK;
+}
+
+static inline unsigned int pd_header_cnt_le(__le16 header)
+{
+       return pd_header_cnt(le16_to_cpu(header));
+}
+
+static inline unsigned int pd_header_type(u16 header)
+{
+       return (header >> PD_HEADER_TYPE_SHIFT) & PD_HEADER_TYPE_MASK;
+}
+
+static inline unsigned int pd_header_type_le(__le16 header)
+{
+       return pd_header_type(le16_to_cpu(header));
+}
+
+static inline unsigned int pd_header_msgid(u16 header)
+{
+       return (header >> PD_HEADER_ID_SHIFT) & PD_HEADER_ID_MASK;
+}
+
+static inline unsigned int pd_header_msgid_le(__le16 header)
+{
+       return pd_header_msgid(le16_to_cpu(header));
+}
+
+static inline unsigned int pd_header_rev(u16 header)
+{
+       return (header >> PD_HEADER_REV_SHIFT) & PD_HEADER_REV_MASK;
+}
+
+static inline unsigned int pd_header_rev_le(__le16 header)
+{
+       return pd_header_rev(le16_to_cpu(header));
+}
+
+#define PD_EXT_HDR_CHUNKED             BIT(15)
+#define PD_EXT_HDR_CHUNK_NUM_SHIFT     11
+#define PD_EXT_HDR_CHUNK_NUM_MASK      0xf
+#define PD_EXT_HDR_REQ_CHUNK           BIT(10)
+#define PD_EXT_HDR_DATA_SIZE_SHIFT     0
+#define PD_EXT_HDR_DATA_SIZE_MASK      0x1ff
+
+#define PD_EXT_HDR(data_size, req_chunk, chunk_num, chunked)                           \
+       ((((data_size) & PD_EXT_HDR_DATA_SIZE_MASK) << PD_EXT_HDR_DATA_SIZE_SHIFT) |    \
+        ((req_chunk) ? PD_EXT_HDR_REQ_CHUNK : 0) |                                     \
+        (((chunk_num) & PD_EXT_HDR_CHUNK_NUM_MASK) << PD_EXT_HDR_CHUNK_NUM_SHIFT) |    \
+        ((chunked) ? PD_EXT_HDR_CHUNKED : 0))
+
+#define PD_EXT_HDR_LE(data_size, req_chunk, chunk_num, chunked) \
+       cpu_to_le16(PD_EXT_HDR((data_size), (req_chunk), (chunk_num), (chunked)))
+
+static inline unsigned int pd_ext_header_chunk_num(u16 ext_header)
+{
+       return (ext_header >> PD_EXT_HDR_CHUNK_NUM_SHIFT) &
+               PD_EXT_HDR_CHUNK_NUM_MASK;
+}
+
+static inline unsigned int pd_ext_header_data_size(u16 ext_header)
+{
+       return (ext_header >> PD_EXT_HDR_DATA_SIZE_SHIFT) &
+               PD_EXT_HDR_DATA_SIZE_MASK;
+}
+
+static inline unsigned int pd_ext_header_data_size_le(__le16 ext_header)
+{
+       return pd_ext_header_data_size(le16_to_cpu(ext_header));
+}
+
+#define PD_MAX_PAYLOAD         7
+#define PD_EXT_MAX_CHUNK_DATA  26
+
+/**
+  * struct pd_chunked_ext_message_data - PD chunked extended message data as
+  *                                     seen on wire
+  * @header:    PD extended message header
+  * @data:      PD extended message data
+  */
+struct pd_chunked_ext_message_data {
+       __le16 header;
+       u8 data[PD_EXT_MAX_CHUNK_DATA];
+} __packed;
+
+/**
+  * struct pd_message - PD message as seen on wire
+  * @header:    PD message header
+  * @payload:   PD message payload
+  * @ext_msg:   PD message chunked extended message data
+  */
+struct pd_message {
+       __le16 header;
+       union {
+               __le32 payload[PD_MAX_PAYLOAD];
+               struct pd_chunked_ext_message_data ext_msg;
+       };
+} __packed;
+
+/* PDO: Power Data Object */
+#define PDO_MAX_OBJECTS                7
+
+enum pd_pdo_type {
+       PDO_TYPE_FIXED = 0,
+       PDO_TYPE_BATT = 1,
+       PDO_TYPE_VAR = 2,
+       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    BIT(29) /* Power role swap supported */
+#define PDO_FIXED_SUSPEND      BIT(28) /* USB Suspend supported (Source) */
+#define PDO_FIXED_HIGHER_CAP   BIT(28) /* Requires more than vSafe5V (Sink) */
+#define PDO_FIXED_EXTPOWER     BIT(27) /* Externally powered */
+#define PDO_FIXED_USB_COMM     BIT(26) /* USB communications capable */
+#define PDO_FIXED_DATA_SWAP    BIT(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))
+
+#define VSAFE5V 5000 /* mv units */
+
+#define PDO_BATT_MAX_VOLT_SHIFT        20      /* 50mV units */
+#define PDO_BATT_MIN_VOLT_SHIFT        10      /* 50mV units */
+#define PDO_BATT_MAX_PWR_SHIFT 0       /* 250mW units */
+
+#define PDO_BATT_MIN_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_BATT_MIN_VOLT_SHIFT)
+#define PDO_BATT_MAX_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_BATT_MAX_VOLT_SHIFT)
+#define PDO_BATT_MAX_POWER(mw) ((((mw) / 250) & PDO_PWR_MASK) << PDO_BATT_MAX_PWR_SHIFT)
+
+#define PDO_BATT(min_mv, max_mv, max_mw)                       \
+       (PDO_TYPE(PDO_TYPE_BATT) | PDO_BATT_MIN_VOLT(min_mv) |  \
+        PDO_BATT_MAX_VOLT(max_mv) | PDO_BATT_MAX_POWER(max_mw))
+
+#define PDO_VAR_MAX_VOLT_SHIFT 20      /* 50mV units */
+#define PDO_VAR_MIN_VOLT_SHIFT 10      /* 50mV units */
+#define PDO_VAR_MAX_CURR_SHIFT 0       /* 10mA units */
+
+#define PDO_VAR_MIN_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_VAR_MIN_VOLT_SHIFT)
+#define PDO_VAR_MAX_VOLT(mv) ((((mv) / 50) & PDO_VOLT_MASK) << PDO_VAR_MAX_VOLT_SHIFT)
+#define PDO_VAR_MAX_CURR(ma) ((((ma) / 10) & PDO_CURR_MASK) << PDO_VAR_MAX_CURR_SHIFT)
+
+#define PDO_VAR(min_mv, max_mv, max_ma)                                \
+       (PDO_TYPE(PDO_TYPE_VAR) | PDO_VAR_MIN_VOLT(min_mv) |    \
+        PDO_VAR_MAX_VOLT(max_mv) | PDO_VAR_MAX_CURR(max_ma))
+
+enum pd_apdo_type {
+       APDO_TYPE_PPS = 0,
+};
+
+#define PDO_APDO_TYPE_SHIFT    28      /* Only valid value currently is 0x0 - PPS */
+#define PDO_APDO_TYPE_MASK     0x3
+
+#define PDO_APDO_TYPE(t)       ((t) << PDO_APDO_TYPE_SHIFT)
+
+#define PDO_PPS_APDO_MAX_VOLT_SHIFT    17      /* 100mV units */
+#define PDO_PPS_APDO_MIN_VOLT_SHIFT    8       /* 100mV units */
+#define PDO_PPS_APDO_MAX_CURR_SHIFT    0       /* 50mA units */
+
+#define PDO_PPS_APDO_VOLT_MASK 0xff
+#define PDO_PPS_APDO_CURR_MASK 0x7f
+
+#define PDO_PPS_APDO_MIN_VOLT(mv)      \
+       ((((mv) / 100) & PDO_PPS_APDO_VOLT_MASK) << PDO_PPS_APDO_MIN_VOLT_SHIFT)
+#define PDO_PPS_APDO_MAX_VOLT(mv)      \
+       ((((mv) / 100) & PDO_PPS_APDO_VOLT_MASK) << PDO_PPS_APDO_MAX_VOLT_SHIFT)
+#define PDO_PPS_APDO_MAX_CURR(ma)      \
+       ((((ma) / 50) & PDO_PPS_APDO_CURR_MASK) << PDO_PPS_APDO_MAX_CURR_SHIFT)
+
+#define PDO_PPS_APDO(min_mv, max_mv, max_ma)                           \
+       (PDO_TYPE(PDO_TYPE_APDO) | PDO_APDO_TYPE(APDO_TYPE_PPS) |       \
+       PDO_PPS_APDO_MIN_VOLT(min_mv) | PDO_PPS_APDO_MAX_VOLT(max_mv) | \
+       PDO_PPS_APDO_MAX_CURR(max_ma))
+
+static inline enum pd_pdo_type pdo_type(u32 pdo)
+{
+       return (pdo >> PDO_TYPE_SHIFT) & PDO_TYPE_MASK;
+}
+
+static inline unsigned int pdo_fixed_voltage(u32 pdo)
+{
+       return ((pdo >> PDO_FIXED_VOLT_SHIFT) & PDO_VOLT_MASK) * 50;
+}
+
+static inline unsigned int pdo_min_voltage(u32 pdo)
+{
+       return ((pdo >> PDO_VAR_MIN_VOLT_SHIFT) & PDO_VOLT_MASK) * 50;
+}
+
+static inline unsigned int pdo_max_voltage(u32 pdo)
+{
+       return ((pdo >> PDO_VAR_MAX_VOLT_SHIFT) & PDO_VOLT_MASK) * 50;
+}
+
+static inline unsigned int pdo_max_current(u32 pdo)
+{
+       return ((pdo >> PDO_VAR_MAX_CURR_SHIFT) & PDO_CURR_MASK) * 10;
+}
+
+static inline unsigned int pdo_max_power(u32 pdo)
+{
+       return ((pdo >> PDO_BATT_MAX_PWR_SHIFT) & PDO_PWR_MASK) * 250;
+}
+
+static inline enum pd_apdo_type pdo_apdo_type(u32 pdo)
+{
+       return (pdo >> PDO_APDO_TYPE_SHIFT) & PDO_APDO_TYPE_MASK;
+}
+
+static inline unsigned int pdo_pps_apdo_min_voltage(u32 pdo)
+{
+       return ((pdo >> PDO_PPS_APDO_MIN_VOLT_SHIFT) &
+               PDO_PPS_APDO_VOLT_MASK) * 100;
+}
+
+static inline unsigned int pdo_pps_apdo_max_voltage(u32 pdo)
+{
+       return ((pdo >> PDO_PPS_APDO_MAX_VOLT_SHIFT) &
+               PDO_PPS_APDO_VOLT_MASK) * 100;
+}
+
+static inline unsigned int pdo_pps_apdo_max_current(u32 pdo)
+{
+       return ((pdo >> PDO_PPS_APDO_MAX_CURR_SHIFT) &
+               PDO_PPS_APDO_CURR_MASK) * 50;
+}
+
+/* RDO: Request Data Object */
+#define RDO_OBJ_POS_SHIFT      28
+#define RDO_OBJ_POS_MASK       0x7
+#define RDO_GIVE_BACK          BIT(27) /* Supports reduced operating current */
+#define RDO_CAP_MISMATCH       BIT(26) /* Not satisfied by source caps */
+#define RDO_USB_COMM           BIT(25) /* USB communications capable */
+#define RDO_NO_SUSPEND         BIT(24) /* USB Suspend not supported */
+
+#define RDO_PWR_MASK                   0x3ff
+#define RDO_CURR_MASK                  0x3ff
+
+#define RDO_FIXED_OP_CURR_SHIFT                10
+#define RDO_FIXED_MAX_CURR_SHIFT       0
+
+#define RDO_OBJ(idx) (((idx) & RDO_OBJ_POS_MASK) << RDO_OBJ_POS_SHIFT)
+
+#define PDO_FIXED_OP_CURR(ma) ((((ma) / 10) & RDO_CURR_MASK) << RDO_FIXED_OP_CURR_SHIFT)
+#define PDO_FIXED_MAX_CURR(ma) ((((ma) / 10) & RDO_CURR_MASK) << RDO_FIXED_MAX_CURR_SHIFT)
+
+#define RDO_FIXED(idx, op_ma, max_ma, flags)                   \
+       (RDO_OBJ(idx) | (flags) |                               \
+        PDO_FIXED_OP_CURR(op_ma) | PDO_FIXED_MAX_CURR(max_ma))
+
+#define RDO_BATT_OP_PWR_SHIFT          10      /* 250mW units */
+#define RDO_BATT_MAX_PWR_SHIFT         0       /* 250mW units */
+
+#define RDO_BATT_OP_PWR(mw) ((((mw) / 250) & RDO_PWR_MASK) << RDO_BATT_OP_PWR_SHIFT)
+#define RDO_BATT_MAX_PWR(mw) ((((mw) / 250) & RDO_PWR_MASK) << RDO_BATT_MAX_PWR_SHIFT)
+
+#define RDO_BATT(idx, op_mw, max_mw, flags)                    \
+       (RDO_OBJ(idx) | (flags) |                               \
+        RDO_BATT_OP_PWR(op_mw) | RDO_BATT_MAX_PWR(max_mw))
+
+#define RDO_PROG_VOLT_MASK     0x7ff
+#define RDO_PROG_CURR_MASK     0x7f
+
+#define RDO_PROG_VOLT_SHIFT    9
+#define RDO_PROG_CURR_SHIFT    0
+
+#define RDO_PROG_VOLT_MV_STEP  20
+#define RDO_PROG_CURR_MA_STEP  50
+
+#define PDO_PROG_OUT_VOLT(mv)  \
+       ((((mv) / RDO_PROG_VOLT_MV_STEP) & RDO_PROG_VOLT_MASK) << RDO_PROG_VOLT_SHIFT)
+#define PDO_PROG_OP_CURR(ma)   \
+       ((((ma) / RDO_PROG_CURR_MA_STEP) & RDO_PROG_CURR_MASK) << RDO_PROG_CURR_SHIFT)
+
+#define RDO_PROG(idx, out_mv, op_ma, flags)                    \
+       (RDO_OBJ(idx) | (flags) |                               \
+        PDO_PROG_OUT_VOLT(out_mv) | PDO_PROG_OP_CURR(op_ma))
+
+static inline unsigned int rdo_index(u32 rdo)
+{
+       return (rdo >> RDO_OBJ_POS_SHIFT) & RDO_OBJ_POS_MASK;
+}
+
+static inline unsigned int rdo_op_current(u32 rdo)
+{
+       return ((rdo >> RDO_FIXED_OP_CURR_SHIFT) & RDO_CURR_MASK) * 10;
+}
+
+static inline unsigned int rdo_max_current(u32 rdo)
+{
+       return ((rdo >> RDO_FIXED_MAX_CURR_SHIFT) &
+               RDO_CURR_MASK) * 10;
+}
+
+static inline unsigned int rdo_op_power(u32 rdo)
+{
+       return ((rdo >> RDO_BATT_OP_PWR_SHIFT) & RDO_PWR_MASK) * 250;
+}
+
+static inline unsigned int rdo_max_power(u32 rdo)
+{
+       return ((rdo >> RDO_BATT_MAX_PWR_SHIFT) & RDO_PWR_MASK) * 250;
+}
+
+/* USB PD timers and counters */
+#define PD_T_NO_RESPONSE       5000    /* 4.5 - 5.5 seconds */
+#define PD_T_DB_DETECT         10000   /* 10 - 15 seconds */
+#define PD_T_SEND_SOURCE_CAP   150     /* 100 - 200 ms */
+#define PD_T_SENDER_RESPONSE   60      /* 24 - 30 ms, relaxed */
+#define PD_T_SOURCE_ACTIVITY   45
+#define PD_T_SINK_ACTIVITY     135
+#define PD_T_SINK_WAIT_CAP     240
+#define PD_T_PS_TRANSITION     500
+#define PD_T_SRC_TRANSITION    35
+#define PD_T_DRP_SNK           40
+#define PD_T_DRP_SRC           30
+#define PD_T_PS_SOURCE_OFF     920
+#define PD_T_PS_SOURCE_ON      480
+#define PD_T_PS_HARD_RESET     30
+#define PD_T_SRC_RECOVER       760
+#define PD_T_SRC_RECOVER_MAX   1000
+#define PD_T_SRC_TURN_ON       275
+#define PD_T_SAFE_0V           650
+#define PD_T_VCONN_SOURCE_ON   100
+#define PD_T_SINK_REQUEST      100     /* 100 ms minimum */
+#define PD_T_ERROR_RECOVERY    100     /* minimum 25 is insufficient */
+#define PD_T_SRCSWAPSTDBY      625     /* Maximum of 650ms */
+#define PD_T_NEWSRC            250     /* Maximum of 275ms */
+
+#define PD_T_DRP_TRY           100     /* 75 - 150 ms */
+#define PD_T_DRP_TRYWAIT       600     /* 400 - 800 ms */
+
+#define PD_T_CC_DEBOUNCE       200     /* 100 - 200 ms */
+#define PD_T_PD_DEBOUNCE       20      /* 10 - 20 ms */
+
+#define PD_N_CAPS_COUNT                (PD_T_NO_RESPONSE / PD_T_SEND_SOURCE_CAP)
+#define PD_N_HARD_RESET_COUNT  2
+
+#endif /* __LINUX_USB_PD_H */
diff --git a/include/linux/usb/pd_ado.h b/include/linux/usb/pd_ado.h
new file mode 100644 (file)
index 0000000..9aa1cf3
--- /dev/null
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2017 Dialog Semiconductor
+ *
+ * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
+ */
+
+#ifndef __LINUX_USB_PD_ADO_H
+#define __LINUX_USB_PD_ADO_H
+
+/* ADO : Alert Data Object */
+#define USB_PD_ADO_TYPE_SHIFT                  24
+#define USB_PD_ADO_TYPE_MASK                   0xff
+#define USB_PD_ADO_FIXED_BATT_SHIFT            20
+#define USB_PD_ADO_FIXED_BATT_MASK             0xf
+#define USB_PD_ADO_HOT_SWAP_BATT_SHIFT         16
+#define USB_PD_ADO_HOT_SWAP_BATT_MASK          0xf
+
+#define USB_PD_ADO_TYPE_BATT_STATUS_CHANGE     BIT(1)
+#define USB_PD_ADO_TYPE_OCP                    BIT(2)
+#define USB_PD_ADO_TYPE_OTP                    BIT(3)
+#define USB_PD_ADO_TYPE_OP_COND_CHANGE         BIT(4)
+#define USB_PD_ADO_TYPE_SRC_INPUT_CHANGE       BIT(5)
+#define USB_PD_ADO_TYPE_OVP                    BIT(6)
+
+static inline unsigned int usb_pd_ado_type(u32 ado)
+{
+       return (ado >> USB_PD_ADO_TYPE_SHIFT) & USB_PD_ADO_TYPE_MASK;
+}
+
+static inline unsigned int usb_pd_ado_fixed_batt(u32 ado)
+{
+       return (ado >> USB_PD_ADO_FIXED_BATT_SHIFT) &
+              USB_PD_ADO_FIXED_BATT_MASK;
+}
+
+static inline unsigned int usb_pd_ado_hot_swap_batt(u32 ado)
+{
+       return (ado >> USB_PD_ADO_HOT_SWAP_BATT_SHIFT) &
+              USB_PD_ADO_HOT_SWAP_BATT_MASK;
+}
+#endif /* __LINUX_USB_PD_ADO_H */
diff --git a/include/linux/usb/pd_bdo.h b/include/linux/usb/pd_bdo.h
new file mode 100644 (file)
index 0000000..90b94d9
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __LINUX_USB_PD_BDO_H
+#define __LINUX_USB_PD_BDO_H
+
+/* BDO : BIST Data Object */
+#define BDO_MODE_RECV          (0 << 28)
+#define BDO_MODE_TRANSMIT      (1 << 28)
+#define BDO_MODE_COUNTERS      (2 << 28)
+#define BDO_MODE_CARRIER0      (3 << 28)
+#define BDO_MODE_CARRIER1      (4 << 28)
+#define BDO_MODE_CARRIER2      (5 << 28)
+#define BDO_MODE_CARRIER3      (6 << 28)
+#define BDO_MODE_EYE           (7 << 28)
+#define BDO_MODE_TESTDATA      (8 << 28)
+
+#define BDO_MODE_MASK(mode)    ((mode) & 0xf0000000)
+
+#endif
diff --git a/include/linux/usb/pd_ext_sdb.h b/include/linux/usb/pd_ext_sdb.h
new file mode 100644 (file)
index 0000000..0eb83ce
--- /dev/null
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2017 Dialog Semiconductor
+ *
+ * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
+ */
+
+#ifndef __LINUX_USB_PD_EXT_SDB_H
+#define __LINUX_USB_PD_EXT_SDB_H
+
+/* SDB : Status Data Block */
+enum usb_pd_ext_sdb_fields {
+       USB_PD_EXT_SDB_INTERNAL_TEMP = 0,
+       USB_PD_EXT_SDB_PRESENT_INPUT,
+       USB_PD_EXT_SDB_PRESENT_BATT_INPUT,
+       USB_PD_EXT_SDB_EVENT_FLAGS,
+       USB_PD_EXT_SDB_TEMP_STATUS,
+       USB_PD_EXT_SDB_DATA_SIZE,
+};
+
+/* Event Flags */
+#define USB_PD_EXT_SDB_EVENT_OCP               BIT(1)
+#define USB_PD_EXT_SDB_EVENT_OTP               BIT(2)
+#define USB_PD_EXT_SDB_EVENT_OVP               BIT(3)
+#define USB_PD_EXT_SDB_EVENT_CF_CV_MODE                BIT(4)
+
+#define USB_PD_EXT_SDB_PPS_EVENTS      (USB_PD_EXT_SDB_EVENT_OCP |     \
+                                        USB_PD_EXT_SDB_EVENT_OTP |     \
+                                        USB_PD_EXT_SDB_EVENT_OVP)
+
+#endif /* __LINUX_USB_PD_EXT_SDB_H */
diff --git a/include/linux/usb/pd_vdo.h b/include/linux/usb/pd_vdo.h
new file mode 100644 (file)
index 0000000..2b64d23
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __LINUX_USB_PD_VDO_H
+#define __LINUX_USB_PD_VDO_H
+
+#include "pd.h"
+
+/*
+ * VDO : Vendor Defined Message Object
+ * VDM object is minimum of VDM header + 6 additional data objects.
+ */
+
+#define VDO_MAX_OBJECTS                6
+#define VDO_MAX_SIZE           (VDO_MAX_OBJECTS + 1)
+
+/*
+ * VDM header
+ * ----------
+ * <31:16>  :: SVID
+ * <15>     :: VDM type ( 1b == structured, 0b == unstructured )
+ * <14:13>  :: Structured VDM version (can only be 00 == 1.0 currently)
+ * <12:11>  :: reserved
+ * <10:8>   :: object position (1-7 valid ... used for enter/exit mode only)
+ * <7:6>    :: command type (SVDM only?)
+ * <5>      :: reserved (SVDM), command type (UVDM)
+ * <4:0>    :: command
+ */
+#define VDO(vid, type, custom)                         \
+       (((vid) << 16) |                                \
+        ((type) << 15) |                               \
+        ((custom) & 0x7FFF))
+
+#define VDO_SVDM_TYPE          (1 << 15)
+#define VDO_SVDM_VERS(x)       ((x) << 13)
+#define VDO_OPOS(x)            ((x) << 8)
+#define VDO_CMDT(x)            ((x) << 6)
+#define VDO_OPOS_MASK          VDO_OPOS(0x7)
+#define VDO_CMDT_MASK          VDO_CMDT(0x3)
+
+#define CMDT_INIT              0
+#define CMDT_RSP_ACK           1
+#define CMDT_RSP_NAK           2
+#define CMDT_RSP_BUSY          3
+
+/* reserved for SVDM ... for Google UVDM */
+#define VDO_SRC_INITIATOR      (0 << 5)
+#define VDO_SRC_RESPONDER      (1 << 5)
+
+#define CMD_DISCOVER_IDENT     1
+#define CMD_DISCOVER_SVID      2
+#define CMD_DISCOVER_MODES     3
+#define CMD_ENTER_MODE         4
+#define CMD_EXIT_MODE          5
+#define CMD_ATTENTION          6
+
+#define VDO_CMD_VENDOR(x)    (((0x10 + (x)) & 0x1f))
+
+/* ChromeOS specific commands */
+#define VDO_CMD_VERSION                VDO_CMD_VENDOR(0)
+#define VDO_CMD_SEND_INFO      VDO_CMD_VENDOR(1)
+#define VDO_CMD_READ_INFO      VDO_CMD_VENDOR(2)
+#define VDO_CMD_REBOOT         VDO_CMD_VENDOR(5)
+#define VDO_CMD_FLASH_ERASE    VDO_CMD_VENDOR(6)
+#define VDO_CMD_FLASH_WRITE    VDO_CMD_VENDOR(7)
+#define VDO_CMD_ERASE_SIG      VDO_CMD_VENDOR(8)
+#define VDO_CMD_PING_ENABLE    VDO_CMD_VENDOR(10)
+#define VDO_CMD_CURRENT                VDO_CMD_VENDOR(11)
+#define VDO_CMD_FLIP           VDO_CMD_VENDOR(12)
+#define VDO_CMD_GET_LOG                VDO_CMD_VENDOR(13)
+#define VDO_CMD_CCD_EN         VDO_CMD_VENDOR(14)
+
+#define PD_VDO_VID(vdo)                ((vdo) >> 16)
+#define PD_VDO_SVDM(vdo)       (((vdo) >> 15) & 1)
+#define PD_VDO_OPOS(vdo)       (((vdo) >> 8) & 0x7)
+#define PD_VDO_CMD(vdo)                ((vdo) & 0x1f)
+#define PD_VDO_CMDT(vdo)       (((vdo) >> 6) & 0x3)
+
+/*
+ * SVDM Identity request -> response
+ *
+ * Request is simply properly formatted SVDM header
+ *
+ * Response is 4 data objects:
+ * [0] :: SVDM header
+ * [1] :: Identitiy header
+ * [2] :: Cert Stat VDO
+ * [3] :: (Product | Cable) VDO
+ * [4] :: AMA VDO
+ *
+ */
+#define VDO_INDEX_HDR          0
+#define VDO_INDEX_IDH          1
+#define VDO_INDEX_CSTAT                2
+#define VDO_INDEX_CABLE                3
+#define VDO_INDEX_PRODUCT      3
+#define VDO_INDEX_AMA          4
+
+/*
+ * SVDM Identity Header
+ * --------------------
+ * <31>     :: data capable as a USB host
+ * <30>     :: data capable as a USB device
+ * <29:27>  :: product type
+ * <26>     :: modal operation supported (1b == yes)
+ * <25:16>  :: Reserved, Shall be set to zero
+ * <15:0>   :: USB-IF assigned VID for this cable vendor
+ */
+#define IDH_PTYPE_UNDEF                0
+#define IDH_PTYPE_HUB          1
+#define IDH_PTYPE_PERIPH       2
+#define IDH_PTYPE_PCABLE       3
+#define IDH_PTYPE_ACABLE       4
+#define IDH_PTYPE_AMA          5
+
+#define VDO_IDH(usbh, usbd, ptype, is_modal, vid)              \
+       ((usbh) << 31 | (usbd) << 30 | ((ptype) & 0x7) << 27    \
+        | (is_modal) << 26 | ((vid) & 0xffff))
+
+#define PD_IDH_PTYPE(vdo)      (((vdo) >> 27) & 0x7)
+#define PD_IDH_VID(vdo)                ((vdo) & 0xffff)
+#define PD_IDH_MODAL_SUPP(vdo) ((vdo) & (1 << 26))
+
+/*
+ * Cert Stat VDO
+ * -------------
+ * <31:0>  : USB-IF assigned XID for this cable
+ */
+#define PD_CSTAT_XID(vdo)      (vdo)
+
+/*
+ * Product VDO
+ * -----------
+ * <31:16> : USB Product ID
+ * <15:0>  : USB bcdDevice
+ */
+#define VDO_PRODUCT(pid, bcd)  (((pid) & 0xffff) << 16 | ((bcd) & 0xffff))
+#define PD_PRODUCT_PID(vdo)    (((vdo) >> 16) & 0xffff)
+
+/*
+ * Cable VDO
+ * ---------
+ * <31:28> :: Cable HW version
+ * <27:24> :: Cable FW version
+ * <23:20> :: Reserved, Shall be set to zero
+ * <19:18> :: type-C to Type-A/B/C (00b == A, 01 == B, 10 == C)
+ * <17>    :: Type-C to Plug/Receptacle (0b == plug, 1b == receptacle)
+ * <16:13> :: cable latency (0001 == <10ns(~1m length))
+ * <12:11> :: cable termination type (11b == both ends active VCONN req)
+ * <10>    :: SSTX1 Directionality support (0b == fixed, 1b == cfgable)
+ * <9>     :: SSTX2 Directionality support
+ * <8>     :: SSRX1 Directionality support
+ * <7>     :: SSRX2 Directionality support
+ * <6:5>   :: Vbus current handling capability
+ * <4>     :: Vbus through cable (0b == no, 1b == yes)
+ * <3>     :: SOP" controller present? (0b == no, 1b == yes)
+ * <2:0>   :: USB SS Signaling support
+ */
+#define CABLE_ATYPE            0
+#define CABLE_BTYPE            1
+#define CABLE_CTYPE            2
+#define CABLE_PLUG             0
+#define CABLE_RECEPTACLE       1
+#define CABLE_CURR_1A5         0
+#define CABLE_CURR_3A          1
+#define CABLE_CURR_5A          2
+#define CABLE_USBSS_U2_ONLY    0
+#define CABLE_USBSS_U31_GEN1   1
+#define CABLE_USBSS_U31_GEN2   2
+#define VDO_CABLE(hw, fw, cbl, gdr, lat, term, tx1d, tx2d, rx1d, rx2d, cur,\
+                 vps, sopp, usbss) \
+       (((hw) & 0x7) << 28 | ((fw) & 0x7) << 24 | ((cbl) & 0x3) << 18  \
+        | (gdr) << 17 | ((lat) & 0x7) << 13 | ((term) & 0x3) << 11     \
+        | (tx1d) << 10 | (tx2d) << 9 | (rx1d) << 8 | (rx2d) << 7       \
+        | ((cur) & 0x3) << 5 | (vps) << 4 | (sopp) << 3                \
+        | ((usbss) & 0x7))
+
+/*
+ * AMA VDO
+ * ---------
+ * <31:28> :: Cable HW version
+ * <27:24> :: Cable FW version
+ * <23:12> :: Reserved, Shall be set to zero
+ * <11>    :: SSTX1 Directionality support (0b == fixed, 1b == cfgable)
+ * <10>    :: SSTX2 Directionality support
+ * <9>     :: SSRX1 Directionality support
+ * <8>     :: SSRX2 Directionality support
+ * <7:5>   :: Vconn power
+ * <4>     :: Vconn power required
+ * <3>     :: Vbus power required
+ * <2:0>   :: USB SS Signaling support
+ */
+#define VDO_AMA(hw, fw, tx1d, tx2d, rx1d, rx2d, vcpwr, vcr, vbr, usbss) \
+       (((hw) & 0x7) << 28 | ((fw) & 0x7) << 24                        \
+        | (tx1d) << 11 | (tx2d) << 10 | (rx1d) << 9 | (rx2d) << 8      \
+        | ((vcpwr) & 0x7) << 5 | (vcr) << 4 | (vbr) << 3               \
+        | ((usbss) & 0x7))
+
+#define PD_VDO_AMA_VCONN_REQ(vdo)      (((vdo) >> 4) & 1)
+#define PD_VDO_AMA_VBUS_REQ(vdo)       (((vdo) >> 3) & 1)
+
+#define AMA_VCONN_PWR_1W       0
+#define AMA_VCONN_PWR_1W5      1
+#define AMA_VCONN_PWR_2W       2
+#define AMA_VCONN_PWR_3W       3
+#define AMA_VCONN_PWR_4W       4
+#define AMA_VCONN_PWR_5W       5
+#define AMA_VCONN_PWR_6W       6
+#define AMA_USBSS_U2_ONLY      0
+#define AMA_USBSS_U31_GEN1     1
+#define AMA_USBSS_U31_GEN2     2
+#define AMA_USBSS_BBONLY       3
+
+/*
+ * SVDM Discover SVIDs request -> response
+ *
+ * Request is properly formatted VDM Header with discover SVIDs command.
+ * Response is a set of SVIDs of all all supported SVIDs with all zero's to
+ * mark the end of SVIDs.  If more than 12 SVIDs are supported command SHOULD be
+ * repeated.
+ */
+#define VDO_SVID(svid0, svid1) (((svid0) & 0xffff) << 16 | ((svid1) & 0xffff))
+#define PD_VDO_SVID_SVID0(vdo) ((vdo) >> 16)
+#define PD_VDO_SVID_SVID1(vdo) ((vdo) & 0xffff)
+
+/* USB-IF SIDs */
+#define USB_SID_PD             0xff00 /* power delivery */
+#define USB_SID_DISPLAYPORT    0xff01
+#define USB_SID_MHL            0xff02  /* Mobile High-Definition Link */
+
+/* VDM command timeouts (in ms) */
+
+#define PD_T_VDM_UNSTRUCTURED  500
+#define PD_T_VDM_BUSY          100
+#define PD_T_VDM_WAIT_MODE_E   100
+#define PD_T_VDM_SNDR_RSP      30
+#define PD_T_VDM_E_MODE                25
+#define PD_T_VDM_RCVR_RSP      15
+
+#endif /* __LINUX_USB_PD_VDO_H */
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
new file mode 100644 (file)
index 0000000..87109b0
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2015-2017 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __LINUX_USB_TCPM_H
+#define __LINUX_USB_TCPM_H
+
+#include <linux/bitops.h>
+#include <linux/usb/typec.h>
+#include "pd.h"
+
+enum typec_cc_status {
+       TYPEC_CC_OPEN,
+       TYPEC_CC_RA,
+       TYPEC_CC_RD,
+       TYPEC_CC_RP_DEF,
+       TYPEC_CC_RP_1_5,
+       TYPEC_CC_RP_3_0,
+};
+
+enum typec_cc_polarity {
+       TYPEC_POLARITY_CC1,
+       TYPEC_POLARITY_CC2,
+};
+
+/* Time to wait for TCPC to complete transmit */
+#define PD_T_TCPC_TX_TIMEOUT   100             /* in ms        */
+#define PD_ROLE_SWAP_TIMEOUT   (MSEC_PER_SEC * 10)
+#define PD_PPS_CTRL_TIMEOUT    (MSEC_PER_SEC * 10)
+
+enum tcpm_transmit_status {
+       TCPC_TX_SUCCESS = 0,
+       TCPC_TX_DISCARDED = 1,
+       TCPC_TX_FAILED = 2,
+};
+
+enum tcpm_transmit_type {
+       TCPC_TX_SOP = 0,
+       TCPC_TX_SOP_PRIME = 1,
+       TCPC_TX_SOP_PRIME_PRIME = 2,
+       TCPC_TX_SOP_DEBUG_PRIME = 3,
+       TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
+       TCPC_TX_HARD_RESET = 5,
+       TCPC_TX_CABLE_RESET = 6,
+       TCPC_TX_BIST_MODE_2 = 7
+};
+
+/**
+ * struct tcpc_config - Port configuration
+ * @src_pdo:   PDO parameters sent to port partner as response to
+ *             PD_CTRL_GET_SOURCE_CAP message
+ * @nr_src_pdo:        Number of entries in @src_pdo
+ * @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
+ * @operating_snk_mw:
+ *             Required operating sink power in mW
+ * @type:      Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
+ *             TYPEC_PORT_DRP)
+ * @default_role:
+ *             Default port role (TYPEC_SINK or TYPEC_SOURCE).
+ *             Set to TYPEC_NO_PREFERRED_ROLE if no default role.
+ * @try_role_hw:True if try.{Src,Snk} is implemented in hardware
+ * @alt_modes: List of supported alternate modes
+ */
+struct tcpc_config {
+       const u32 *src_pdo;
+       unsigned int nr_src_pdo;
+
+       const u32 *snk_pdo;
+       unsigned int nr_snk_pdo;
+
+       const u32 *snk_vdo;
+       unsigned int nr_snk_vdo;
+
+       unsigned int operating_snk_mw;
+
+       enum typec_port_type type;
+       enum typec_port_data data;
+       enum typec_role default_role;
+       bool try_role_hw;       /* try.{src,snk} implemented in hardware */
+       bool self_powered;      /* port belongs to a self powered device */
+
+       const struct typec_altmode_desc *alt_modes;
+};
+
+/* Mux state attributes */
+#define TCPC_MUX_USB_ENABLED           BIT(0)  /* USB enabled */
+#define TCPC_MUX_DP_ENABLED            BIT(1)  /* DP enabled */
+#define TCPC_MUX_POLARITY_INVERTED     BIT(2)  /* Polarity inverted */
+
+/**
+ * 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
+ *             and cc=Rp-def. This allows the tcpm to provide a fallback
+ *             current-limit detection method for the cc=Rp-def case.
+ *             For example, some tcpcs may include BC1.2 charger detection
+ *             and use that in this case.
+ * @set_cc:    Called to set value of CC pins
+ * @get_cc:    Called to read current CC pin values
+ * @set_bist:
+ *             Called to set hardware bist mode
+ * @set_polarity:
+ *             Called to set polarity
+ * @set_vconn: Called to enable or disable VCONN
+ * @set_vbus:  Called to enable or disable VBUS
+ * @set_current_limit:
+ *             Optional; called to set current limit as negotiated
+ *             with partner.
+ * @set_pd_rx: Called to enable or disable reception of PD messages
+ * @set_roles: Called to set power and data roles
+ * @start_drp_toggling:
+ *             Optional; if supported by hardware, called to start DRP
+ *             toggling. DRP toggling is stopped automatically if
+ *             a connection is established.
+ * @try_role:  Optional; called to set a preferred role
+ * @pd_transmit:Called to transmit PD message
+ * @mux:       Pointer to multiplexer data
+ */
+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);
+       int (*get_current_limit)(struct tcpc_dev *dev);
+       int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
+       int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
+                     enum typec_cc_status *cc2);
+       int (*set_bist)(struct tcpc_dev *dev, bool on);
+       int (*set_polarity)(struct tcpc_dev *dev,
+                           enum typec_cc_polarity polarity);
+       int (*set_vconn)(struct tcpc_dev *dev, bool on);
+       int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
+       int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
+       int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
+       int (*set_roles)(struct tcpc_dev *dev, bool attached,
+                        enum typec_role role, enum typec_data_role data);
+       int (*start_drp_toggling)(struct tcpc_dev *dev,
+                                 enum typec_cc_status cc);
+       int (*try_role)(struct tcpc_dev *dev, int role);
+       int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
+                          const struct pd_message *msg);
+};
+
+struct tcpm_port;
+
+struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
+void tcpm_unregister_port(struct tcpm_port *port);
+
+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 operating_snk_mw);
+
+void tcpm_vbus_change(struct tcpm_port *port);
+void tcpm_cc_change(struct tcpm_port *port);
+void tcpm_pd_receive(struct tcpm_port *port,
+                    const struct pd_message *msg);
+void tcpm_pd_transmit_complete(struct tcpm_port *port,
+                              enum tcpm_transmit_status status);
+void tcpm_pd_hard_reset(struct tcpm_port *port);
+void tcpm_tcpc_reset(struct tcpm_port *port);
+
+#endif /* __LINUX_USB_TCPM_H */
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
new file mode 100644 (file)
index 0000000..5def262
--- /dev/null
@@ -0,0 +1,249 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __LINUX_USB_TYPEC_H
+#define __LINUX_USB_TYPEC_H
+
+#include <linux/types.h>
+
+/* USB Type-C Specification releases */
+#define USB_TYPEC_REV_1_0      0x100 /* 1.0 */
+#define USB_TYPEC_REV_1_1      0x110 /* 1.1 */
+#define USB_TYPEC_REV_1_2      0x120 /* 1.2 */
+
+struct typec_partner;
+struct typec_cable;
+struct typec_plug;
+struct typec_port;
+
+struct fwnode_handle;
+struct device;
+
+enum typec_port_type {
+       TYPEC_PORT_SRC,
+       TYPEC_PORT_SNK,
+       TYPEC_PORT_DRP,
+};
+
+enum typec_port_data {
+       TYPEC_PORT_DFP,
+       TYPEC_PORT_UFP,
+       TYPEC_PORT_DRD,
+};
+
+enum typec_plug_type {
+       USB_PLUG_NONE,
+       USB_PLUG_TYPE_A,
+       USB_PLUG_TYPE_B,
+       USB_PLUG_TYPE_C,
+       USB_PLUG_CAPTIVE,
+};
+
+enum typec_data_role {
+       TYPEC_DEVICE,
+       TYPEC_HOST,
+};
+
+enum typec_role {
+       TYPEC_SINK,
+       TYPEC_SOURCE,
+};
+
+enum typec_pwr_opmode {
+       TYPEC_PWR_MODE_USB,
+       TYPEC_PWR_MODE_1_5A,
+       TYPEC_PWR_MODE_3_0A,
+       TYPEC_PWR_MODE_PD,
+};
+
+enum typec_accessory {
+       TYPEC_ACCESSORY_NONE,
+       TYPEC_ACCESSORY_AUDIO,
+       TYPEC_ACCESSORY_DEBUG,
+};
+
+#define TYPEC_MAX_ACCESSORY    3
+
+enum typec_orientation {
+       TYPEC_ORIENTATION_NONE,
+       TYPEC_ORIENTATION_NORMAL,
+       TYPEC_ORIENTATION_REVERSE,
+};
+
+/*
+ * struct usb_pd_identity - USB Power Delivery identity data
+ * @id_header: ID Header VDO
+ * @cert_stat: Cert Stat VDO
+ * @product: Product VDO
+ *
+ * USB power delivery Discover Identity command response data.
+ *
+ * REVISIT: This is USB Power Delivery specific information, so this structure
+ * probable belongs to USB Power Delivery header file once we have them.
+ */
+struct usb_pd_identity {
+       u32                     id_header;
+       u32                     cert_stat;
+       u32                     product;
+};
+
+int typec_partner_set_identity(struct typec_partner *partner);
+int typec_cable_set_identity(struct typec_cable *cable);
+
+/*
+ * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
+ * @svid: Standard or Vendor ID
+ * @mode: Index of the Mode
+ * @vdo: VDO returned by Discover Modes USB PD command
+ * @roles: Only for ports. DRP if the mode is available in both roles
+ *
+ * Description of an Alternate Mode which a connector, cable plug or partner
+ * supports.
+ */
+struct typec_altmode_desc {
+       u16                     svid;
+       u8                      mode;
+       u32                     vdo;
+       /* Only used with ports */
+       enum typec_port_data    roles;
+};
+
+struct typec_altmode
+*typec_partner_register_altmode(struct typec_partner *partner,
+                               const struct typec_altmode_desc *desc);
+struct typec_altmode
+*typec_plug_register_altmode(struct typec_plug *plug,
+                            const struct typec_altmode_desc *desc);
+struct typec_altmode
+*typec_port_register_altmode(struct typec_port *port,
+                            const struct typec_altmode_desc *desc);
+void typec_unregister_altmode(struct typec_altmode *altmode);
+
+struct typec_port *typec_altmode2port(struct typec_altmode *alt);
+
+void typec_altmode_update_active(struct typec_altmode *alt, bool active);
+
+enum typec_plug_index {
+       TYPEC_PLUG_SOP_P,
+       TYPEC_PLUG_SOP_PP,
+};
+
+/*
+ * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
+ * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
+ *         plug connected to UFP
+ *
+ * Represents USB Type-C Cable Plug.
+ */
+struct typec_plug_desc {
+       enum typec_plug_index   index;
+};
+
+/*
+ * struct typec_cable_desc - USB Type-C Cable Descriptor
+ * @type: The plug type from USB PD Cable VDO
+ * @active: Is the cable active or passive
+ * @identity: Result of Discover Identity command
+ *
+ * Represents USB Type-C Cable attached to USB Type-C port.
+ */
+struct typec_cable_desc {
+       enum typec_plug_type    type;
+       unsigned int            active:1;
+       struct usb_pd_identity  *identity;
+};
+
+/*
+ * struct typec_partner_desc - USB Type-C Partner Descriptor
+ * @usb_pd: USB Power Delivery support
+ * @accessory: Audio, Debug or none.
+ * @identity: Discover Identity command data
+ *
+ * Details about a partner that is attached to USB Type-C port. If @identity
+ * member exists when partner is registered, a directory named "identity" is
+ * created to sysfs for the partner device.
+ */
+struct typec_partner_desc {
+       unsigned int            usb_pd:1;
+       enum typec_accessory    accessory;
+       struct usb_pd_identity  *identity;
+};
+
+/*
+ * struct typec_capability - USB Type-C Port Capabilities
+ * @type: Supported power role of the port
+ * @data: Supported data role of the port
+ * @no_vconn: The port does not support vconn
+ * @revision: USB Type-C Specification release. Binary coded decimal
+ * @pd_revision: USB Power Delivery Specification revision if supported
+ * @prefer_role: Initial role preference (DRP ports).
+ * @accessory: Supported Accessory Modes
+ * @sw: Cable plug orientation switch
+ * @mux: Multiplexer switch for Alternate/Accessory Modes
+ * @fwnode: Optional fwnode of the port
+ * @try_role: Set data role preference for DRP port
+ * @dr_set: Set Data Role
+ * @pr_set: Set Power Role
+ * @vconn_set: Set VCONN Role
+ * @port_type_set: Set port type
+ *
+ * Static capabilities of a single USB Type-C port.
+ */
+struct typec_capability {
+       enum typec_port_type    type;
+       enum typec_port_data    data;
+       bool                    no_vconn;
+       u16                     revision; /* 0120H = "1.2" */
+       u16                     pd_revision; /* 0300H = "3.0" */
+       int                     prefer_role;
+       enum typec_accessory    accessory[TYPEC_MAX_ACCESSORY];
+
+       struct typec_switch     *sw;
+       struct typec_mux        *mux;
+       struct fwnode_handle    *fwnode;
+
+       int             (*try_role)(const struct typec_capability *,
+                                   int role);
+
+       int             (*dr_set)(const struct typec_capability *,
+                                 enum typec_data_role);
+       int             (*pr_set)(const struct typec_capability *,
+                                 enum typec_role);
+       int             (*vconn_set)(const struct typec_capability *,
+                                    enum typec_role);
+       int             (*port_type_set)(const struct typec_capability *,
+                                        enum typec_port_type);
+};
+
+/* Specific to try_role(). Indicates the user want's to clear the preference. */
+#define TYPEC_NO_PREFERRED_ROLE        (-1)
+
+struct typec_port *typec_register_port(struct device *parent,
+                                      const struct typec_capability *cap);
+void typec_unregister_port(struct typec_port *port);
+
+struct typec_partner *typec_register_partner(struct typec_port *port,
+                                            struct typec_partner_desc *desc);
+void typec_unregister_partner(struct typec_partner *partner);
+
+struct typec_cable *typec_register_cable(struct typec_port *port,
+                                        struct typec_cable_desc *desc);
+void typec_unregister_cable(struct typec_cable *cable);
+
+struct typec_plug *typec_register_plug(struct typec_cable *cable,
+                                      struct typec_plug_desc *desc);
+void typec_unregister_plug(struct typec_plug *plug);
+
+void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
+void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
+void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
+void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
+
+int typec_set_orientation(struct typec_port *port,
+                         enum typec_orientation orientation);
+enum typec_orientation typec_get_orientation(struct typec_port *port);
+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 */
diff --git a/include/linux/usb/typec_altmode.h b/include/linux/usb/typec_altmode.h
new file mode 100644 (file)
index 0000000..9a88c74
--- /dev/null
@@ -0,0 +1,160 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __USB_TYPEC_ALTMODE_H
+#define __USB_TYPEC_ALTMODE_H
+
+#include <linux/mod_devicetable.h>
+#include <linux/usb/typec.h>
+#include <linux/device.h>
+
+#define MODE_DISCOVERY_MAX     6
+
+struct typec_altmode_ops;
+
+/**
+ * struct typec_altmode - USB Type-C alternate mode device
+ * @dev: Driver model's view of this device
+ * @svid: Standard or Vendor ID (SVID) of the alternate mode
+ * @mode: Index of the Mode
+ * @vdo: VDO returned by Discover Modes USB PD command
+ * @active: Tells has the mode been entered or not
+ * @desc: Optional human readable description of the mode
+ * @ops: Operations vector from the driver
+ */
+struct typec_altmode {
+       struct device                   dev;
+       u16                             svid;
+       int                             mode;
+       u32                             vdo;
+       unsigned int                    active:1;
+
+       char                            *desc;
+       const struct typec_altmode_ops  *ops;
+};
+
+#define to_typec_altmode(d) container_of(d, struct typec_altmode, dev)
+
+static inline void typec_altmode_set_drvdata(struct typec_altmode *altmode,
+                                            void *data)
+{
+       dev_set_drvdata(&altmode->dev, data);
+}
+
+static inline void *typec_altmode_get_drvdata(struct typec_altmode *altmode)
+{
+       return dev_get_drvdata(&altmode->dev);
+}
+
+/**
+ * struct typec_altmode_ops - Alternate mode specific operations vector
+ * @enter: Operations to be executed with Enter Mode Command
+ * @exit: Operations to be executed with Exit Mode Command
+ * @attention: Callback for Attention Command
+ * @vdm: Callback for SVID specific commands
+ * @notify: Communication channel for platform and the alternate mode
+ * @activate: User callback for Enter/Exit Mode
+ */
+struct typec_altmode_ops {
+       int (*enter)(struct typec_altmode *altmode);
+       int (*exit)(struct typec_altmode *altmode);
+       void (*attention)(struct typec_altmode *altmode, u32 vdo);
+       int (*vdm)(struct typec_altmode *altmode, const u32 hdr,
+                  const u32 *vdo, int cnt);
+       int (*notify)(struct typec_altmode *altmode, unsigned long conf,
+                     void *data);
+       int (*activate)(struct typec_altmode *altmode, int activate);
+};
+
+int typec_altmode_enter(struct typec_altmode *altmode);
+int typec_altmode_exit(struct typec_altmode *altmode);
+void typec_altmode_attention(struct typec_altmode *altmode, u32 vdo);
+int typec_altmode_vdm(struct typec_altmode *altmode,
+                     const u32 header, const u32 *vdo, int count);
+int typec_altmode_notify(struct typec_altmode *altmode, unsigned long conf,
+                        void *data);
+const struct typec_altmode *
+typec_altmode_get_partner(struct typec_altmode *altmode);
+
+/*
+ * These are the connector states (USB, Safe and Alt Mode) defined in USB Type-C
+ * Specification. SVID specific connector states are expected to follow and
+ * start from the value TYPEC_STATE_MODAL.
+ */
+enum {
+       TYPEC_STATE_SAFE,       /* USB Safe State */
+       TYPEC_STATE_USB,        /* USB Operation */
+       TYPEC_STATE_MODAL,      /* Alternate Modes */
+};
+
+/*
+ * For the muxes there is no difference between Accessory Modes and Alternate
+ * Modes, so the Accessory Modes are supplied with specific modal state values
+ * here. Unlike with Alternate Modes, where the mux will be linked with the
+ * alternate mode device, the mux for Accessory Modes will be linked with the
+ * port device instead.
+ *
+ * Port drivers can use TYPEC_MODE_AUDIO and TYPEC_MODE_DEBUG as the mode
+ * value for typec_set_mode() when accessory modes are supported.
+ */
+enum {
+       TYPEC_MODE_AUDIO = TYPEC_STATE_MODAL,   /* Audio Accessory */
+       TYPEC_MODE_DEBUG,                       /* Debug Accessory */
+};
+
+#define TYPEC_MODAL_STATE(_state_)     ((_state_) + TYPEC_STATE_MODAL)
+
+struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *altmode,
+                                            enum typec_plug_index index);
+void typec_altmode_put_plug(struct typec_altmode *plug);
+
+struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
+                                         size_t n, u16 svid, u8 mode);
+
+struct typec_altmode *
+typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
+                               struct notifier_block *nb);
+
+void typec_altmode_unregister_notifier(struct typec_altmode *adev,
+                                      struct notifier_block *nb);
+
+/**
+ * typec_altmode_get_orientation - Get cable plug orientation
+ * altmode: Handle to the alternate mode
+ */
+static inline enum typec_orientation
+typec_altmode_get_orientation(struct typec_altmode *altmode)
+{
+       return typec_get_orientation(typec_altmode2port(altmode));
+}
+
+/**
+ * struct typec_altmode_driver - USB Type-C alternate mode device driver
+ * @id_table: Null terminated array of SVIDs
+ * @probe: Callback for device binding
+ * @remove: Callback for device unbinding
+ * @driver: Device driver model driver
+ *
+ * These drivers will be bind to the partner alternate mode devices. They will
+ * handle all SVID specific communication.
+ */
+struct typec_altmode_driver {
+       const struct typec_device_id *id_table;
+       int (*probe)(struct typec_altmode *altmode);
+       void (*remove)(struct typec_altmode *altmode);
+       struct device_driver driver;
+};
+
+#define to_altmode_driver(d) container_of(d, struct typec_altmode_driver, \
+                                         driver)
+
+#define typec_altmode_register_driver(drv) \
+               __typec_altmode_register_driver(drv, THIS_MODULE)
+int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
+                                   struct module *module);
+void typec_altmode_unregister_driver(struct typec_altmode_driver *drv);
+
+#define module_typec_altmode_driver(__typec_altmode_driver) \
+       module_driver(__typec_altmode_driver, typec_altmode_register_driver, \
+                     typec_altmode_unregister_driver)
+
+#endif /* __USB_TYPEC_ALTMODE_H */
diff --git a/include/linux/usb/typec_mux.h b/include/linux/usb/typec_mux.h
new file mode 100644 (file)
index 0000000..79293f6
--- /dev/null
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#ifndef __USB_TYPEC_MUX
+#define __USB_TYPEC_MUX
+
+#include <linux/list.h>
+#include <linux/usb/typec.h>
+
+struct device;
+
+/**
+ * struct typec_switch - USB Type-C cable orientation switch
+ * @dev: Switch device
+ * @entry: List entry
+ * @set: Callback to the driver for setting the orientation
+ *
+ * USB Type-C pin flipper switch routing the correct data pairs from the
+ * connector to the USB controller depending on the orientation of the cable
+ * plug.
+ */
+struct typec_switch {
+       struct device *dev;
+       struct list_head entry;
+
+       int (*set)(struct typec_switch *sw, enum typec_orientation orientation);
+};
+
+/**
+ * struct typec_switch - USB Type-C connector pin mux
+ * @dev: Mux device
+ * @entry: List entry
+ * @set: Callback to the driver for setting the state of the mux
+ *
+ * Pin Multiplexer/DeMultiplexer switch routing the USB Type-C connector pins to
+ * different components depending on the requested mode of operation. Used with
+ * Accessory/Alternate modes.
+ */
+struct typec_mux {
+       struct device *dev;
+       struct list_head entry;
+
+       int (*set)(struct typec_mux *mux, int state);
+};
+
+struct typec_switch *typec_switch_get(struct device *dev);
+void typec_switch_put(struct typec_switch *sw);
+int typec_switch_register(struct typec_switch *sw);
+void typec_switch_unregister(struct typec_switch *sw);
+
+struct typec_mux *typec_mux_get(struct device *dev, const char *name);
+void typec_mux_put(struct typec_mux *mux);
+int typec_mux_register(struct typec_mux *mux);
+void typec_mux_unregister(struct typec_mux *mux);
+
+#endif /* __USB_TYPEC_MUX */