]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
usb: typec: Add typec_port_register_altmodes()
authorHans de Goede <hdegoede@redhat.com>
Fri, 9 Apr 2021 13:40:31 +0000 (15:40 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 9 Apr 2021 14:07:41 +0000 (16:07 +0200)
This can be used by Type-C controller drivers which use a standard
usb-connector fwnode, with altmodes sub-node, to describe the available
altmodes.

Note there are is no devicetree bindings documentation for the altmodes
node, this is deliberate. ATM the fwnodes used to register the altmodes
are only used internally to pass platform info from a drivers/platform/x86
driver to the type-c subsystem.

When a devicetree user of this functionally comes up and the dt-bindings
have been hashed out the internal use can be adjusted to match the
dt-bindings.

Currently the typec_port_register_altmodes() function expects
an "altmodes" child fwnode on port->dev with this "altmodes" fwnode having
child fwnodes itself with each child containing 2 integer properties:

1. A "svid" property, which sets the id of the altmode, e.g. displayport
altmode has a svid of 0xff01.

2. A "vdo" property, typically used as a bitmask describing the
capabilities of the altmode, the bits in the vdo are specified in the
specification of the altmode.

Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Link: https://lore.kernel.org/r/20210409134033.105834-2-hdegoede@redhat.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/typec/class.c
include/linux/usb/typec.h

index f1c2d823c6509ed51ad749bd3d9bc2d75ad3173c..b9429c9f65f6c27eb14d671559aa7a913d97c681 100644 (file)
@@ -1923,6 +1923,60 @@ typec_port_register_altmode(struct typec_port *port,
 }
 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
 
+void typec_port_register_altmodes(struct typec_port *port,
+       const struct typec_altmode_ops *ops, void *drvdata,
+       struct typec_altmode **altmodes, size_t n)
+{
+       struct fwnode_handle *altmodes_node, *child;
+       struct typec_altmode_desc desc;
+       struct typec_altmode *alt;
+       size_t index = 0;
+       u32 svid, vdo;
+       int ret;
+
+       altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
+       if (!altmodes_node)
+               return; /* No altmodes specified */
+
+       fwnode_for_each_child_node(altmodes_node, child) {
+               ret = fwnode_property_read_u32(child, "svid", &svid);
+               if (ret) {
+                       dev_err(&port->dev, "Error reading svid for altmode %s\n",
+                               fwnode_get_name(child));
+                       continue;
+               }
+
+               ret = fwnode_property_read_u32(child, "vdo", &vdo);
+               if (ret) {
+                       dev_err(&port->dev, "Error reading vdo for altmode %s\n",
+                               fwnode_get_name(child));
+                       continue;
+               }
+
+               if (index >= n) {
+                       dev_err(&port->dev, "Error not enough space for altmode %s\n",
+                               fwnode_get_name(child));
+                       continue;
+               }
+
+               desc.svid = svid;
+               desc.vdo = vdo;
+               desc.mode = index + 1;
+               alt = typec_port_register_altmode(port, &desc);
+               if (IS_ERR(alt)) {
+                       dev_err(&port->dev, "Error registering altmode %s\n",
+                               fwnode_get_name(child));
+                       continue;
+               }
+
+               alt->ops = ops;
+               typec_altmode_set_drvdata(alt, drvdata);
+               altmodes[index] = alt;
+               index++;
+       }
+}
+EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
+
 /**
  * typec_register_port - Register a USB Type-C Port
  * @parent: Parent device
index e2714722b0c95330f1acb8d10e1c9cbe63b3ac1b..e2e44bb1dad856789cfd6f6a663f5136afa6ba14 100644 (file)
@@ -17,6 +17,7 @@ struct typec_partner;
 struct typec_cable;
 struct typec_plug;
 struct typec_port;
+struct typec_altmode_ops;
 
 struct fwnode_handle;
 struct device;
@@ -138,6 +139,11 @@ struct typec_altmode
 struct typec_altmode
 *typec_port_register_altmode(struct typec_port *port,
                             const struct typec_altmode_desc *desc);
+
+void typec_port_register_altmodes(struct typec_port *port,
+       const struct typec_altmode_ops *ops, void *drvdata,
+       struct typec_altmode **altmodes, size_t n);
+
 void typec_unregister_altmode(struct typec_altmode *altmode);
 
 struct typec_port *typec_altmode2port(struct typec_altmode *alt);