Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: typec: Add typec_port_register_altmodes()

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>

authored by

Hans de Goede and committed by
Greg Kroah-Hartman
7b458a4c 3a2a91a2

+60
+54
drivers/usb/typec/class.c
··· 1923 1923 } 1924 1924 EXPORT_SYMBOL_GPL(typec_port_register_altmode); 1925 1925 1926 + void typec_port_register_altmodes(struct typec_port *port, 1927 + const struct typec_altmode_ops *ops, void *drvdata, 1928 + struct typec_altmode **altmodes, size_t n) 1929 + { 1930 + struct fwnode_handle *altmodes_node, *child; 1931 + struct typec_altmode_desc desc; 1932 + struct typec_altmode *alt; 1933 + size_t index = 0; 1934 + u32 svid, vdo; 1935 + int ret; 1936 + 1937 + altmodes_node = device_get_named_child_node(&port->dev, "altmodes"); 1938 + if (!altmodes_node) 1939 + return; /* No altmodes specified */ 1940 + 1941 + fwnode_for_each_child_node(altmodes_node, child) { 1942 + ret = fwnode_property_read_u32(child, "svid", &svid); 1943 + if (ret) { 1944 + dev_err(&port->dev, "Error reading svid for altmode %s\n", 1945 + fwnode_get_name(child)); 1946 + continue; 1947 + } 1948 + 1949 + ret = fwnode_property_read_u32(child, "vdo", &vdo); 1950 + if (ret) { 1951 + dev_err(&port->dev, "Error reading vdo for altmode %s\n", 1952 + fwnode_get_name(child)); 1953 + continue; 1954 + } 1955 + 1956 + if (index >= n) { 1957 + dev_err(&port->dev, "Error not enough space for altmode %s\n", 1958 + fwnode_get_name(child)); 1959 + continue; 1960 + } 1961 + 1962 + desc.svid = svid; 1963 + desc.vdo = vdo; 1964 + desc.mode = index + 1; 1965 + alt = typec_port_register_altmode(port, &desc); 1966 + if (IS_ERR(alt)) { 1967 + dev_err(&port->dev, "Error registering altmode %s\n", 1968 + fwnode_get_name(child)); 1969 + continue; 1970 + } 1971 + 1972 + alt->ops = ops; 1973 + typec_altmode_set_drvdata(alt, drvdata); 1974 + altmodes[index] = alt; 1975 + index++; 1976 + } 1977 + } 1978 + EXPORT_SYMBOL_GPL(typec_port_register_altmodes); 1979 + 1926 1980 /** 1927 1981 * typec_register_port - Register a USB Type-C Port 1928 1982 * @parent: Parent device
+6
include/linux/usb/typec.h
··· 17 17 struct typec_cable; 18 18 struct typec_plug; 19 19 struct typec_port; 20 + struct typec_altmode_ops; 20 21 21 22 struct fwnode_handle; 22 23 struct device; ··· 139 138 struct typec_altmode 140 139 *typec_port_register_altmode(struct typec_port *port, 141 140 const struct typec_altmode_desc *desc); 141 + 142 + void typec_port_register_altmodes(struct typec_port *port, 143 + const struct typec_altmode_ops *ops, void *drvdata, 144 + struct typec_altmode **altmodes, size_t n); 145 + 142 146 void typec_unregister_altmode(struct typec_altmode *altmode); 143 147 144 148 struct typec_port *typec_altmode2port(struct typec_altmode *alt);