Skip to content

Commit

Permalink
usb: typec: API for controlling USB Type-C Multiplexers
Browse files Browse the repository at this point in the history
USB Type-C connectors consist of various muxes and switches
that route the pins on the connector to the right locations.
The USB Type-C drivers need to be able to control the muxes,
as they are the ones that know things like the cable plug
orientation, and the current mode that was negotiated with
the partner.

This introduces a small API for registering and controlling
cable plug orientation switches, and separate small API for
registering and controlling pin multiplexer/demultiplexer
switches that are needed with Accessory/Alternate Modes.

Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Heikki Krogerus authored and Greg Kroah-Hartman committed Mar 22, 2018
1 parent f2d9b66 commit bdecb33
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 11 deletions.
73 changes: 62 additions & 11 deletions Documentation/driver-api/usb/typec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Registering the ports
The port drivers will describe every Type-C port they control with struct
typec_capability data structure, and register them with the following API:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_register_port typec_unregister_port

When registering the ports, the prefer_role member in struct typec_capability
Expand All @@ -80,7 +80,7 @@ typec_partner_desc. The class copies the details of the partner during
registration. The class offers the following API for registering/unregistering
partners.

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_register_partner typec_unregister_partner

The class will provide a handle to struct typec_partner if the registration was
Expand All @@ -92,7 +92,7 @@ should include handle to struct usb_pd_identity instance. The class will then
create a sysfs directory for the identity under the partner device. The result
of Discover Identity command can then be reported with the following API:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_partner_set_identity

Registering Cables
Expand All @@ -113,7 +113,7 @@ typec_cable_desc and about a plug in struct typec_plug_desc. The class copies
the details during registration. The class offers the following API for
registering/unregistering cables and their plugs:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_register_cable typec_unregister_cable typec_register_plug typec_unregister_plug

The class will provide a handle to struct typec_cable and struct typec_plug if
Expand All @@ -125,7 +125,7 @@ include handle to struct usb_pd_identity instance. The class will then create a
sysfs directory for the identity under the cable device. The result of Discover
Identity command can then be reported with the following API:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_cable_set_identity

Notifications
Expand All @@ -135,7 +135,7 @@ When the partner has executed a role change, or when the default roles change
during connection of a partner or cable, the port driver must use the following
APIs to report it to the class:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_set_data_role typec_set_pwr_role typec_set_vconn_role typec_set_pwr_opmode

Alternate Modes
Expand All @@ -150,7 +150,7 @@ and struct typec_altmode_desc which is a container for all the supported modes.
Ports that support Alternate Modes need to register each SVID they support with
the following API:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_port_register_altmode

If a partner or cable plug provides a list of SVIDs as response to USB Power
Expand All @@ -159,24 +159,75 @@ registered.

API for the partners:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_partner_register_altmode

API for the Cable Plugs:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_plug_register_altmode

So ports, partners and cable plugs will register the alternate modes with their
own functions, but the registration will always return a handle to struct
typec_altmode on success, or NULL. The unregistration will happen with the same
function:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_unregister_altmode

If a partner or cable plug enters or exits a mode, the port driver needs to
notify the class with the following API:

.. kernel-doc:: drivers/usb/typec/typec.c
.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_altmode_update_active

Multiplexer/DeMultiplexer Switches
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

USB Type-C connectors may have one or more mux/demux switches behind them. Since
the plugs can be inserted right-side-up or upside-down, a switch is needed to
route the correct data pairs from the connector to the USB controllers. If
Alternate or Accessory Modes are supported, another switch is needed that can
route the pins on the connector to some other component besides USB. USB Type-C
Connector Class supplies an API for registering those switches.

.. kernel-doc:: drivers/usb/typec/mux.c
:functions: typec_switch_register typec_switch_unregister typec_mux_register typec_mux_unregister

In most cases the same physical mux will handle both the orientation and mode.
However, as the port drivers will be responsible for the orientation, and the
alternate mode drivers for the mode, the two are always separated into their
own logical components: "mux" for the mode and "switch" for the orientation.

When a port is registered, USB Type-C Connector Class requests both the mux and
the switch for the port. The drivers can then use the following API for
controlling them:

.. kernel-doc:: drivers/usb/typec/class.c
:functions: typec_set_orientation typec_set_mode

If the connector is dual-role capable, there may also be a switch for the data
role. USB Type-C Connector Class does not supply separate API for them. The
port drivers can use USB Role Class API with those.

Illustration of the muxes behind a connector that supports an alternate mode:

------------------------
| Connector |
------------------------
| |
------------------------
\ Orientation /
--------------------
|
--------------------
/ Mode \
------------------------
/ \
------------------------ --------------------
| Alt Mode | / USB Role \
------------------------ ------------------------
/ \
------------------------ ------------------------
| USB Host | | USB Device |
------------------------ ------------------------
1 change: 1 addition & 0 deletions drivers/usb/typec/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_TYPEC) += typec.o
typec-y := class.o mux.o
obj-$(CONFIG_TYPEC_TCPM) += tcpm.o
obj-y += fusb302/
obj-$(CONFIG_TYPEC_WCOVE) += typec_wcove.o
Expand Down
70 changes: 70 additions & 0 deletions drivers/usb/typec/typec.c → drivers/usb/typec/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/usb/typec.h>
#include <linux/usb/typec_mux.h>

struct typec_mode {
int index;
Expand Down Expand Up @@ -70,6 +71,10 @@ struct typec_port {
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;
};

Expand All @@ -92,6 +97,7 @@ static const struct device_type typec_port_dev_type;
static DEFINE_IDA(typec_index_ida);
static struct class *typec_class;

/* ------------------------------------------------------------------------- */
/* Common attributes */

static const char * const typec_accessory_modes[] = {
Expand Down Expand Up @@ -1137,6 +1143,8 @@ static void typec_release(struct device *dev)
struct typec_port *port = to_typec_port(dev);

ida_simple_remove(&typec_index_ida, port->id);
typec_switch_put(port->sw);
typec_mux_put(port->mux);
kfree(port);
}

Expand Down Expand Up @@ -1246,6 +1254,47 @@ void typec_set_pwr_opmode(struct typec_port *port,
}
EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);

/* ------------------------------------------ */
/* 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_set_mode - Set mode of operation for USB Type-C connector
* @port: USB Type-C port for the connector
* @mode: Operation mode for the connector
*
* Set mode @mode for @port. This function will configure the muxes needed to
* enter @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);

/* --------------------------------------- */

/**
Expand Down Expand Up @@ -1293,6 +1342,18 @@ struct typec_port *typec_register_port(struct device *parent,
return ERR_PTR(id);
}

port->sw = typec_switch_get(cap->fwnode ? &port->dev : parent);
if (IS_ERR(port->sw)) {
ret = PTR_ERR(port->sw);
goto err_switch;
}

port->mux = typec_mux_get(cap->fwnode ? &port->dev : parent);
if (IS_ERR(port->mux)) {
ret = PTR_ERR(port->mux);
goto err_mux;
}

if (cap->type == TYPEC_PORT_DFP)
role = TYPEC_SOURCE;
else if (cap->type == TYPEC_PORT_UFP)
Expand Down Expand Up @@ -1330,6 +1391,15 @@ struct typec_port *typec_register_port(struct device *parent,
}

return port;

err_mux:
typec_switch_put(port->sw);

err_switch:
ida_simple_remove(&typec_index_ida, port->id);
kfree(port);

return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(typec_register_port);

Expand Down
Loading

0 comments on commit bdecb33

Please sign in to comment.