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 #define TYPEC_MUX_MAX_DEVS 3
23 struct typec_switch_dev *sw_devs[TYPEC_MUX_MAX_DEVS];
24 unsigned int num_sw_devs;
27 static int switch_fwnode_match(struct device *dev, const void *fwnode)
29 if (!is_typec_switch_dev(dev))
32 return dev_fwnode(dev) == fwnode;
35 static void *typec_switch_match(struct fwnode_handle *fwnode, const char *id,
41 * Device graph (OF graph) does not give any means to identify the
42 * device type or the device class of the remote port parent that @fwnode
43 * represents, so in order to identify the type or the class of @fwnode
44 * an additional device property is needed. With typec switches the
45 * property is named "orientation-switch" (@id). The value of the device
46 * property is ignored.
48 if (id && !fwnode_property_present(fwnode, id))
52 * At this point we are sure that @fwnode is a typec switch in all
53 * cases. If the switch hasn't yet been registered for some reason, the
54 * function "defers probe" for now.
56 dev = class_find_device(&typec_mux_class, NULL, fwnode,
59 return dev ? to_typec_switch_dev(dev) : ERR_PTR(-EPROBE_DEFER);
63 * fwnode_typec_switch_get - Find USB Type-C orientation switch
64 * @fwnode: The caller device node
66 * Finds a switch linked with @dev. Returns a reference to the switch on
67 * success, NULL if no matching connection was found, or
68 * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch
69 * has not been enumerated yet.
71 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode)
73 struct typec_switch_dev *sw_devs[TYPEC_MUX_MAX_DEVS];
74 struct typec_switch *sw;
79 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
81 return ERR_PTR(-ENOMEM);
83 count = fwnode_connection_find_matches(fwnode, "orientation-switch", NULL,
92 for (i = 0; i < count; i++) {
93 if (IS_ERR(sw_devs[i])) {
94 err = PTR_ERR(sw_devs[i]);
99 for (i = 0; i < count; i++) {
100 WARN_ON(!try_module_get(sw_devs[i]->dev.parent->driver->owner));
101 sw->sw_devs[i] = sw_devs[i];
104 sw->num_sw_devs = count;
109 for (i = 0; i < count; i++) {
110 if (!IS_ERR(sw_devs[i]))
111 put_device(&sw_devs[i]->dev);
118 EXPORT_SYMBOL_GPL(fwnode_typec_switch_get);
121 * typec_switch_put - Release USB Type-C orientation switch
122 * @sw: USB Type-C orientation switch
124 * Decrement reference count for @sw.
126 void typec_switch_put(struct typec_switch *sw)
128 struct typec_switch_dev *sw_dev;
131 if (IS_ERR_OR_NULL(sw))
134 for (i = 0; i < sw->num_sw_devs; i++) {
135 sw_dev = sw->sw_devs[i];
137 module_put(sw_dev->dev.parent->driver->owner);
138 put_device(&sw_dev->dev);
142 EXPORT_SYMBOL_GPL(typec_switch_put);
144 static void typec_switch_release(struct device *dev)
146 kfree(to_typec_switch_dev(dev));
149 const struct device_type typec_switch_dev_type = {
150 .name = "orientation_switch",
151 .release = typec_switch_release,
155 * typec_switch_register - Register USB Type-C orientation switch
156 * @parent: Parent device
157 * @desc: Orientation switch description
159 * This function registers a switch that can be used for routing the correct
160 * data pairs depending on the cable plug orientation from the USB Type-C
161 * connector to the USB controllers. USB Type-C plugs can be inserted
162 * right-side-up or upside-down.
164 struct typec_switch_dev *
165 typec_switch_register(struct device *parent,
166 const struct typec_switch_desc *desc)
168 struct typec_switch_dev *sw_dev;
171 if (!desc || !desc->set)
172 return ERR_PTR(-EINVAL);
174 sw_dev = kzalloc(sizeof(*sw_dev), GFP_KERNEL);
176 return ERR_PTR(-ENOMEM);
178 sw_dev->set = desc->set;
180 device_initialize(&sw_dev->dev);
181 sw_dev->dev.parent = parent;
182 sw_dev->dev.fwnode = desc->fwnode;
183 sw_dev->dev.class = &typec_mux_class;
184 sw_dev->dev.type = &typec_switch_dev_type;
185 sw_dev->dev.driver_data = desc->drvdata;
186 ret = dev_set_name(&sw_dev->dev, "%s-switch", desc->name ? desc->name : dev_name(parent));
188 put_device(&sw_dev->dev);
192 ret = device_add(&sw_dev->dev);
194 dev_err(parent, "failed to register switch (%d)\n", ret);
195 put_device(&sw_dev->dev);
201 EXPORT_SYMBOL_GPL(typec_switch_register);
203 int typec_switch_set(struct typec_switch *sw,
204 enum typec_orientation orientation)
206 struct typec_switch_dev *sw_dev;
210 if (IS_ERR_OR_NULL(sw))
213 for (i = 0; i < sw->num_sw_devs; i++) {
214 sw_dev = sw->sw_devs[i];
216 ret = sw_dev->set(sw_dev, orientation);
223 EXPORT_SYMBOL_GPL(typec_switch_set);
226 * typec_switch_unregister - Unregister USB Type-C orientation switch
227 * @sw_dev: USB Type-C orientation switch
229 * Unregister switch that was registered with typec_switch_register().
231 void typec_switch_unregister(struct typec_switch_dev *sw_dev)
233 if (!IS_ERR_OR_NULL(sw_dev))
234 device_unregister(&sw_dev->dev);
236 EXPORT_SYMBOL_GPL(typec_switch_unregister);
238 void typec_switch_set_drvdata(struct typec_switch_dev *sw_dev, void *data)
240 dev_set_drvdata(&sw_dev->dev, data);
242 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
244 void *typec_switch_get_drvdata(struct typec_switch_dev *sw_dev)
246 return dev_get_drvdata(&sw_dev->dev);
248 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
250 /* ------------------------------------------------------------------------- */
253 struct typec_mux_dev *mux_devs[TYPEC_MUX_MAX_DEVS];
254 unsigned int num_mux_devs;
257 static int mux_fwnode_match(struct device *dev, const void *fwnode)
259 if (!is_typec_mux_dev(dev))
262 return dev_fwnode(dev) == fwnode;
265 static void *typec_mux_match(struct fwnode_handle *fwnode, const char *id,
268 const struct typec_altmode_desc *desc = data;
277 * Check has the identifier already been "consumed". If it
278 * has, no need to do any extra connection identification.
284 /* Accessory Mode muxes */
286 match = fwnode_property_present(fwnode, "accessory");
292 /* Alternate Mode muxes */
293 nval = fwnode_property_count_u16(fwnode, "svid");
297 val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
299 return ERR_PTR(-ENOMEM);
301 ret = fwnode_property_read_u16_array(fwnode, "svid", val, nval);
307 for (i = 0; i < nval; i++) {
308 match = val[i] == desc->svid;
318 dev = class_find_device(&typec_mux_class, NULL, fwnode,
321 return dev ? to_typec_mux_dev(dev) : ERR_PTR(-EPROBE_DEFER);
325 * fwnode_typec_mux_get - Find USB Type-C Multiplexer
326 * @fwnode: The caller device node
327 * @desc: Alt Mode description
329 * Finds a mux linked to the caller. This function is primarily meant for the
330 * Type-C drivers. Returns a reference to the mux on success, NULL if no
331 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
332 * was found but the mux has not been enumerated yet.
334 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
335 const struct typec_altmode_desc *desc)
337 struct typec_mux_dev *mux_devs[TYPEC_MUX_MAX_DEVS];
338 struct typec_mux *mux;
343 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
345 return ERR_PTR(-ENOMEM);
347 count = fwnode_connection_find_matches(fwnode, "mode-switch",
348 (void *)desc, typec_mux_match,
350 ARRAY_SIZE(mux_devs));
356 for (i = 0; i < count; i++) {
357 if (IS_ERR(mux_devs[i])) {
358 err = PTR_ERR(mux_devs[i]);
363 for (i = 0; i < count; i++) {
364 WARN_ON(!try_module_get(mux_devs[i]->dev.parent->driver->owner));
365 mux->mux_devs[i] = mux_devs[i];
368 mux->num_mux_devs = count;
373 for (i = 0; i < count; i++) {
374 if (!IS_ERR(mux_devs[i]))
375 put_device(&mux_devs[i]->dev);
382 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get);
385 * typec_mux_put - Release handle to a Multiplexer
386 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
388 * Decrements reference count for @mux.
390 void typec_mux_put(struct typec_mux *mux)
392 struct typec_mux_dev *mux_dev;
395 if (IS_ERR_OR_NULL(mux))
398 for (i = 0; i < mux->num_mux_devs; i++) {
399 mux_dev = mux->mux_devs[i];
400 module_put(mux_dev->dev.parent->driver->owner);
401 put_device(&mux_dev->dev);
405 EXPORT_SYMBOL_GPL(typec_mux_put);
407 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
409 struct typec_mux_dev *mux_dev;
413 if (IS_ERR_OR_NULL(mux))
416 for (i = 0; i < mux->num_mux_devs; i++) {
417 mux_dev = mux->mux_devs[i];
419 ret = mux_dev->set(mux_dev, state);
426 EXPORT_SYMBOL_GPL(typec_mux_set);
428 static void typec_mux_release(struct device *dev)
430 kfree(to_typec_mux_dev(dev));
433 const struct device_type typec_mux_dev_type = {
434 .name = "mode_switch",
435 .release = typec_mux_release,
439 * typec_mux_register - Register Multiplexer routing USB Type-C pins
440 * @parent: Parent device
441 * @desc: Multiplexer description
443 * USB Type-C connectors can be used for alternate modes of operation besides
444 * USB when Accessory/Alternate Modes are supported. With some of those modes,
445 * the pins on the connector need to be reconfigured. This function registers
446 * multiplexer switches routing the pins on the connector.
448 struct typec_mux_dev *
449 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
451 struct typec_mux_dev *mux_dev;
454 if (!desc || !desc->set)
455 return ERR_PTR(-EINVAL);
457 mux_dev = kzalloc(sizeof(*mux_dev), GFP_KERNEL);
459 return ERR_PTR(-ENOMEM);
461 mux_dev->set = desc->set;
463 device_initialize(&mux_dev->dev);
464 mux_dev->dev.parent = parent;
465 mux_dev->dev.fwnode = desc->fwnode;
466 mux_dev->dev.class = &typec_mux_class;
467 mux_dev->dev.type = &typec_mux_dev_type;
468 mux_dev->dev.driver_data = desc->drvdata;
469 ret = dev_set_name(&mux_dev->dev, "%s-mux", desc->name ? desc->name : dev_name(parent));
471 put_device(&mux_dev->dev);
475 ret = device_add(&mux_dev->dev);
477 dev_err(parent, "failed to register mux (%d)\n", ret);
478 put_device(&mux_dev->dev);
484 EXPORT_SYMBOL_GPL(typec_mux_register);
487 * typec_mux_unregister - Unregister Multiplexer Switch
488 * @mux_dev: USB Type-C Connector Multiplexer/DeMultiplexer
490 * Unregister mux that was registered with typec_mux_register().
492 void typec_mux_unregister(struct typec_mux_dev *mux_dev)
494 if (!IS_ERR_OR_NULL(mux_dev))
495 device_unregister(&mux_dev->dev);
497 EXPORT_SYMBOL_GPL(typec_mux_unregister);
499 void typec_mux_set_drvdata(struct typec_mux_dev *mux_dev, void *data)
501 dev_set_drvdata(&mux_dev->dev, data);
503 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
505 void *typec_mux_get_drvdata(struct typec_mux_dev *mux_dev)
507 return dev_get_drvdata(&mux_dev->dev);
509 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
511 struct class typec_mux_class = {
513 .owner = THIS_MODULE,