1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Multiplexer/DeMultiplexer Switch support
5 * Copyright (C) 2018 Intel Corporation
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 * fwnode_typec_switch_get - Find USB Type-C orientation switch
53 * @fwnode: The caller device node
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 *fwnode_typec_switch_get(struct fwnode_handle *fwnode)
62 struct typec_switch *sw;
64 sw = fwnode_connection_find_match(fwnode, "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(fwnode_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",
131 desc->name ? desc->name : dev_name(parent));
133 ret = device_add(&sw->dev);
135 dev_err(parent, "failed to register switch (%d)\n", ret);
136 put_device(&sw->dev);
142 EXPORT_SYMBOL_GPL(typec_switch_register);
144 int typec_switch_set(struct typec_switch *sw,
145 enum typec_orientation orientation)
147 if (IS_ERR_OR_NULL(sw))
150 return sw->set(sw, orientation);
152 EXPORT_SYMBOL_GPL(typec_switch_set);
155 * typec_switch_unregister - Unregister USB Type-C orientation switch
156 * @sw: USB Type-C orientation switch
158 * Unregister switch that was registered with typec_switch_register().
160 void typec_switch_unregister(struct typec_switch *sw)
162 if (!IS_ERR_OR_NULL(sw))
163 device_unregister(&sw->dev);
165 EXPORT_SYMBOL_GPL(typec_switch_unregister);
167 void typec_switch_set_drvdata(struct typec_switch *sw, void *data)
169 dev_set_drvdata(&sw->dev, data);
171 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
173 void *typec_switch_get_drvdata(struct typec_switch *sw)
175 return dev_get_drvdata(&sw->dev);
177 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
179 /* ------------------------------------------------------------------------- */
181 static int mux_fwnode_match(struct device *dev, const void *fwnode)
183 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-mux");
186 static void *typec_mux_match(struct device_connection *con, int ep, void *data)
188 const struct typec_altmode_desc *desc = data;
196 * Check has the identifier already been "consumed". If it
197 * has, no need to do any extra connection identification.
203 /* Accessory Mode muxes */
205 match = fwnode_property_present(con->fwnode, "accessory");
211 /* Alternate Mode muxes */
212 nval = fwnode_property_count_u16(con->fwnode, "svid");
216 val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
218 return ERR_PTR(-ENOMEM);
220 nval = fwnode_property_read_u16_array(con->fwnode, "svid", val, nval);
223 return ERR_PTR(nval);
226 for (i = 0; i < nval; i++) {
227 match = val[i] == desc->svid;
237 dev = class_find_device(&typec_mux_class, NULL, con->fwnode,
240 return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
244 * fwnode_typec_mux_get - Find USB Type-C Multiplexer
245 * @fwnode: The caller device node
246 * @desc: Alt Mode description
248 * Finds a mux linked to the caller. This function is primarily meant for the
249 * Type-C drivers. Returns a reference to the mux on success, NULL if no
250 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
251 * was found but the mux has not been enumerated yet.
253 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
254 const struct typec_altmode_desc *desc)
256 struct typec_mux *mux;
258 mux = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc,
260 if (!IS_ERR_OR_NULL(mux))
261 WARN_ON(!try_module_get(mux->dev.parent->driver->owner));
265 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get);
268 * typec_mux_put - Release handle to a Multiplexer
269 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
271 * Decrements reference count for @mux.
273 void typec_mux_put(struct typec_mux *mux)
275 if (!IS_ERR_OR_NULL(mux)) {
276 module_put(mux->dev.parent->driver->owner);
277 put_device(&mux->dev);
280 EXPORT_SYMBOL_GPL(typec_mux_put);
282 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
284 if (IS_ERR_OR_NULL(mux))
287 return mux->set(mux, state);
289 EXPORT_SYMBOL_GPL(typec_mux_set);
291 static void typec_mux_release(struct device *dev)
293 kfree(to_typec_mux(dev));
296 static const struct device_type typec_mux_dev_type = {
297 .name = "mode_switch",
298 .release = typec_mux_release,
302 * typec_mux_register - Register Multiplexer routing USB Type-C pins
303 * @parent: Parent device
304 * @desc: Multiplexer description
306 * USB Type-C connectors can be used for alternate modes of operation besides
307 * USB when Accessory/Alternate Modes are supported. With some of those modes,
308 * the pins on the connector need to be reconfigured. This function registers
309 * multiplexer switches routing the pins on the connector.
312 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
314 struct typec_mux *mux;
317 if (!desc || !desc->set)
318 return ERR_PTR(-EINVAL);
320 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
322 return ERR_PTR(-ENOMEM);
324 mux->set = desc->set;
326 device_initialize(&mux->dev);
327 mux->dev.parent = parent;
328 mux->dev.fwnode = desc->fwnode;
329 mux->dev.class = &typec_mux_class;
330 mux->dev.type = &typec_mux_dev_type;
331 mux->dev.driver_data = desc->drvdata;
332 dev_set_name(&mux->dev, "%s-mux",
333 desc->name ? desc->name : dev_name(parent));
335 ret = device_add(&mux->dev);
337 dev_err(parent, "failed to register mux (%d)\n", ret);
338 put_device(&mux->dev);
344 EXPORT_SYMBOL_GPL(typec_mux_register);
347 * typec_mux_unregister - Unregister Multiplexer Switch
348 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
350 * Unregister mux that was registered with typec_mux_register().
352 void typec_mux_unregister(struct typec_mux *mux)
354 if (!IS_ERR_OR_NULL(mux))
355 device_unregister(&mux->dev);
357 EXPORT_SYMBOL_GPL(typec_mux_unregister);
359 void typec_mux_set_drvdata(struct typec_mux *mux, void *data)
361 dev_set_drvdata(&mux->dev, data);
363 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
365 void *typec_mux_get_drvdata(struct typec_mux *mux)
367 return dev_get_drvdata(&mux->dev);
369 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
371 struct class typec_mux_class = {
373 .owner = THIS_MODULE,