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>
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 fwnode_handle *fwnode, const char *id,
42 if (id && !fwnode_property_present(fwnode, id))
45 dev = class_find_device(&typec_mux_class, NULL, 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_switch_put - 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 fwnode_handle *fwnode, const char *id,
189 const struct typec_altmode_desc *desc = data;
198 * Check has the identifier already been "consumed". If it
199 * has, no need to do any extra connection identification.
205 /* Accessory Mode muxes */
207 match = fwnode_property_present(fwnode, "accessory");
213 /* Alternate Mode muxes */
214 nval = fwnode_property_count_u16(fwnode, "svid");
218 val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
220 return ERR_PTR(-ENOMEM);
222 ret = fwnode_property_read_u16_array(fwnode, "svid", val, nval);
228 for (i = 0; i < nval; i++) {
229 match = val[i] == desc->svid;
239 dev = class_find_device(&typec_mux_class, NULL, fwnode,
242 return dev ? to_typec_mux(dev) : ERR_PTR(-EPROBE_DEFER);
246 * fwnode_typec_mux_get - Find USB Type-C Multiplexer
247 * @fwnode: The caller device node
248 * @desc: Alt Mode description
250 * Finds a mux linked to the caller. This function is primarily meant for the
251 * Type-C drivers. Returns a reference to the mux on success, NULL if no
252 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
253 * was found but the mux has not been enumerated yet.
255 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
256 const struct typec_altmode_desc *desc)
258 struct typec_mux *mux;
260 mux = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc,
262 if (!IS_ERR_OR_NULL(mux))
263 WARN_ON(!try_module_get(mux->dev.parent->driver->owner));
267 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get);
270 * typec_mux_put - Release handle to a Multiplexer
271 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
273 * Decrements reference count for @mux.
275 void typec_mux_put(struct typec_mux *mux)
277 if (!IS_ERR_OR_NULL(mux)) {
278 module_put(mux->dev.parent->driver->owner);
279 put_device(&mux->dev);
282 EXPORT_SYMBOL_GPL(typec_mux_put);
284 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
286 if (IS_ERR_OR_NULL(mux))
289 return mux->set(mux, state);
291 EXPORT_SYMBOL_GPL(typec_mux_set);
293 static void typec_mux_release(struct device *dev)
295 kfree(to_typec_mux(dev));
298 static const struct device_type typec_mux_dev_type = {
299 .name = "mode_switch",
300 .release = typec_mux_release,
304 * typec_mux_register - Register Multiplexer routing USB Type-C pins
305 * @parent: Parent device
306 * @desc: Multiplexer description
308 * USB Type-C connectors can be used for alternate modes of operation besides
309 * USB when Accessory/Alternate Modes are supported. With some of those modes,
310 * the pins on the connector need to be reconfigured. This function registers
311 * multiplexer switches routing the pins on the connector.
314 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
316 struct typec_mux *mux;
319 if (!desc || !desc->set)
320 return ERR_PTR(-EINVAL);
322 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
324 return ERR_PTR(-ENOMEM);
326 mux->set = desc->set;
328 device_initialize(&mux->dev);
329 mux->dev.parent = parent;
330 mux->dev.fwnode = desc->fwnode;
331 mux->dev.class = &typec_mux_class;
332 mux->dev.type = &typec_mux_dev_type;
333 mux->dev.driver_data = desc->drvdata;
334 dev_set_name(&mux->dev, "%s-mux",
335 desc->name ? desc->name : dev_name(parent));
337 ret = device_add(&mux->dev);
339 dev_err(parent, "failed to register mux (%d)\n", ret);
340 put_device(&mux->dev);
346 EXPORT_SYMBOL_GPL(typec_mux_register);
349 * typec_mux_unregister - Unregister Multiplexer Switch
350 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
352 * Unregister mux that was registered with typec_mux_register().
354 void typec_mux_unregister(struct typec_mux *mux)
356 if (!IS_ERR_OR_NULL(mux))
357 device_unregister(&mux->dev);
359 EXPORT_SYMBOL_GPL(typec_mux_unregister);
361 void typec_mux_set_drvdata(struct typec_mux *mux, void *data)
363 dev_set_drvdata(&mux->dev, data);
365 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
367 void *typec_mux_get_drvdata(struct typec_mux *mux)
369 return dev_get_drvdata(&mux->dev);
371 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
373 struct class typec_mux_class = {
375 .owner = THIS_MODULE,