1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Multiplexer/DeMultiplexer Switch support
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * Hans de Goede <hdegoede@redhat.com>
10 #include <linux/device.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/property.h>
15 #include <linux/slab.h>
16 #include <linux/usb/typec_mux.h>
20 static bool dev_name_ends_with(struct device *dev, const char *suffix)
22 const char *name = dev_name(dev);
23 const int name_len = strlen(name);
24 const int suffix_len = strlen(suffix);
26 if (suffix_len > name_len)
29 return strcmp(name + (name_len - suffix_len), suffix) == 0;
32 static int switch_fwnode_match(struct device *dev, const void *fwnode)
34 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-switch");
37 static void *typec_switch_match(struct device_connection *con, int ep,
42 if (con->id && !fwnode_property_present(con->fwnode, con->id))
45 dev = class_find_device(&typec_mux_class, NULL, con->fwnode,
48 return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
52 * typec_switch_get - Find USB Type-C orientation switch
53 * @dev: The caller device
55 * Finds a switch linked with @dev. Returns a reference to the switch on
56 * success, NULL if no matching connection was found, or
57 * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch
58 * has not been enumerated yet.
60 struct typec_switch *typec_switch_get(struct device *dev)
62 struct typec_switch *sw;
64 sw = device_connection_find_match(dev, "orientation-switch", NULL,
66 if (!IS_ERR_OR_NULL(sw))
67 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
71 EXPORT_SYMBOL_GPL(typec_switch_get);
74 * typec_put_switch - Release USB Type-C orientation switch
75 * @sw: USB Type-C orientation switch
77 * Decrement reference count for @sw.
79 void typec_switch_put(struct typec_switch *sw)
81 if (!IS_ERR_OR_NULL(sw)) {
82 module_put(sw->dev.parent->driver->owner);
86 EXPORT_SYMBOL_GPL(typec_switch_put);
88 static void typec_switch_release(struct device *dev)
90 kfree(to_typec_switch(dev));
93 static const struct device_type typec_switch_dev_type = {
94 .name = "orientation_switch",
95 .release = typec_switch_release,
99 * typec_switch_register - Register USB Type-C orientation switch
100 * @parent: Parent device
101 * @desc: Orientation switch description
103 * This function registers a switch that can be used for routing the correct
104 * data pairs depending on the cable plug orientation from the USB Type-C
105 * connector to the USB controllers. USB Type-C plugs can be inserted
106 * right-side-up or upside-down.
108 struct typec_switch *
109 typec_switch_register(struct device *parent,
110 const struct typec_switch_desc *desc)
112 struct typec_switch *sw;
115 if (!desc || !desc->set)
116 return ERR_PTR(-EINVAL);
118 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
120 return ERR_PTR(-ENOMEM);
124 device_initialize(&sw->dev);
125 sw->dev.parent = parent;
126 sw->dev.fwnode = desc->fwnode;
127 sw->dev.class = &typec_mux_class;
128 sw->dev.type = &typec_switch_dev_type;
129 sw->dev.driver_data = desc->drvdata;
130 dev_set_name(&sw->dev, "%s-switch", dev_name(parent));
132 ret = device_add(&sw->dev);
134 dev_err(parent, "failed to register switch (%d)\n", ret);
135 put_device(&sw->dev);
141 EXPORT_SYMBOL_GPL(typec_switch_register);
144 * typec_switch_unregister - Unregister USB Type-C orientation switch
145 * @sw: USB Type-C orientation switch
147 * Unregister switch that was registered with typec_switch_register().
149 void typec_switch_unregister(struct typec_switch *sw)
151 if (!IS_ERR_OR_NULL(sw))
152 device_unregister(&sw->dev);
154 EXPORT_SYMBOL_GPL(typec_switch_unregister);
156 void typec_switch_set_drvdata(struct typec_switch *sw, void *data)
158 dev_set_drvdata(&sw->dev, data);
160 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
162 void *typec_switch_get_drvdata(struct typec_switch *sw)
164 return dev_get_drvdata(&sw->dev);
166 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
168 /* ------------------------------------------------------------------------- */
170 static int mux_fwnode_match(struct device *dev, const void *fwnode)
172 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-mux");
175 static void *typec_mux_match(struct device_connection *con, int ep, void *data)
177 const struct typec_altmode_desc *desc = data;
185 * Check has the identifier already been "consumed". If it
186 * has, no need to do any extra connection identification.
192 /* Accessory Mode muxes */
194 match = fwnode_property_present(con->fwnode, "accessory");
200 /* Alternate Mode muxes */
201 nval = fwnode_property_count_u16(con->fwnode, "svid");
205 val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
207 return ERR_PTR(-ENOMEM);
209 nval = fwnode_property_read_u16_array(con->fwnode, "svid", val, nval);
212 return ERR_PTR(nval);
215 for (i = 0; i < nval; i++) {
216 match = val[i] == desc->svid;
226 dev = class_find_device(&typec_mux_class, NULL, con->fwnode,
229 return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
233 * typec_mux_get - Find USB Type-C Multiplexer
234 * @dev: The caller device
235 * @desc: Alt Mode description
237 * Finds a mux linked to the caller. This function is primarily meant for the
238 * Type-C drivers. Returns a reference to the mux on success, NULL if no
239 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
240 * was found but the mux has not been enumerated yet.
242 struct typec_mux *typec_mux_get(struct device *dev,
243 const struct typec_altmode_desc *desc)
245 struct typec_mux *mux;
247 mux = device_connection_find_match(dev, "mode-switch", (void *)desc,
249 if (!IS_ERR_OR_NULL(mux))
250 WARN_ON(!try_module_get(mux->dev.parent->driver->owner));
254 EXPORT_SYMBOL_GPL(typec_mux_get);
257 * typec_mux_put - Release handle to a Multiplexer
258 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
260 * Decrements reference count for @mux.
262 void typec_mux_put(struct typec_mux *mux)
264 if (!IS_ERR_OR_NULL(mux)) {
265 module_put(mux->dev.parent->driver->owner);
266 put_device(&mux->dev);
269 EXPORT_SYMBOL_GPL(typec_mux_put);
271 static void typec_mux_release(struct device *dev)
273 kfree(to_typec_mux(dev));
276 static const struct device_type typec_mux_dev_type = {
277 .name = "mode_switch",
278 .release = typec_mux_release,
282 * typec_mux_register - Register Multiplexer routing USB Type-C pins
283 * @parent: Parent device
284 * @desc: Multiplexer description
286 * USB Type-C connectors can be used for alternate modes of operation besides
287 * USB when Accessory/Alternate Modes are supported. With some of those modes,
288 * the pins on the connector need to be reconfigured. This function registers
289 * multiplexer switches routing the pins on the connector.
292 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
294 struct typec_mux *mux;
297 if (!desc || !desc->set)
298 return ERR_PTR(-EINVAL);
300 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
302 return ERR_PTR(-ENOMEM);
304 mux->set = desc->set;
306 device_initialize(&mux->dev);
307 mux->dev.parent = parent;
308 mux->dev.fwnode = desc->fwnode;
309 mux->dev.class = &typec_mux_class;
310 mux->dev.type = &typec_mux_dev_type;
311 mux->dev.driver_data = desc->drvdata;
312 dev_set_name(&mux->dev, "%s-mux", dev_name(parent));
314 ret = device_add(&mux->dev);
316 dev_err(parent, "failed to register mux (%d)\n", ret);
317 put_device(&mux->dev);
323 EXPORT_SYMBOL_GPL(typec_mux_register);
326 * typec_mux_unregister - Unregister Multiplexer Switch
327 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
329 * Unregister mux that was registered with typec_mux_register().
331 void typec_mux_unregister(struct typec_mux *mux)
333 if (!IS_ERR_OR_NULL(mux))
334 device_unregister(&mux->dev);
336 EXPORT_SYMBOL_GPL(typec_mux_unregister);
338 void typec_mux_set_drvdata(struct typec_mux *mux, void *data)
340 dev_set_drvdata(&mux->dev, data);
342 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
344 void *typec_mux_get_drvdata(struct typec_mux *mux)
346 return dev_get_drvdata(&mux->dev);
348 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
350 struct class typec_mux_class = {
352 .owner = THIS_MODULE,