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 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
;
197 * Check has the identifier already been "consumed". If it
198 * has, no need to do any extra connection identification.
204 /* Accessory Mode muxes */
206 match
= fwnode_property_present(fwnode
, "accessory");
212 /* Alternate Mode muxes */
213 nval
= fwnode_property_count_u16(fwnode
, "svid");
217 val
= kcalloc(nval
, sizeof(*val
), GFP_KERNEL
);
219 return ERR_PTR(-ENOMEM
);
221 nval
= fwnode_property_read_u16_array(fwnode
, "svid", val
, nval
);
224 return ERR_PTR(nval
);
227 for (i
= 0; i
< nval
; i
++) {
228 match
= val
[i
] == desc
->svid
;
238 dev
= class_find_device(&typec_mux_class
, NULL
, fwnode
,
241 return dev
? to_typec_switch(dev
) : ERR_PTR(-EPROBE_DEFER
);
245 * fwnode_typec_mux_get - Find USB Type-C Multiplexer
246 * @fwnode: The caller device node
247 * @desc: Alt Mode description
249 * Finds a mux linked to the caller. This function is primarily meant for the
250 * Type-C drivers. Returns a reference to the mux on success, NULL if no
251 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
252 * was found but the mux has not been enumerated yet.
254 struct typec_mux
*fwnode_typec_mux_get(struct fwnode_handle
*fwnode
,
255 const struct typec_altmode_desc
*desc
)
257 struct typec_mux
*mux
;
259 mux
= fwnode_connection_find_match(fwnode
, "mode-switch", (void *)desc
,
261 if (!IS_ERR_OR_NULL(mux
))
262 WARN_ON(!try_module_get(mux
->dev
.parent
->driver
->owner
));
266 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get
);
269 * typec_mux_put - Release handle to a Multiplexer
270 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
272 * Decrements reference count for @mux.
274 void typec_mux_put(struct typec_mux
*mux
)
276 if (!IS_ERR_OR_NULL(mux
)) {
277 module_put(mux
->dev
.parent
->driver
->owner
);
278 put_device(&mux
->dev
);
281 EXPORT_SYMBOL_GPL(typec_mux_put
);
283 int typec_mux_set(struct typec_mux
*mux
, struct typec_mux_state
*state
)
285 if (IS_ERR_OR_NULL(mux
))
288 return mux
->set(mux
, state
);
290 EXPORT_SYMBOL_GPL(typec_mux_set
);
292 static void typec_mux_release(struct device
*dev
)
294 kfree(to_typec_mux(dev
));
297 static const struct device_type typec_mux_dev_type
= {
298 .name
= "mode_switch",
299 .release
= typec_mux_release
,
303 * typec_mux_register - Register Multiplexer routing USB Type-C pins
304 * @parent: Parent device
305 * @desc: Multiplexer description
307 * USB Type-C connectors can be used for alternate modes of operation besides
308 * USB when Accessory/Alternate Modes are supported. With some of those modes,
309 * the pins on the connector need to be reconfigured. This function registers
310 * multiplexer switches routing the pins on the connector.
313 typec_mux_register(struct device
*parent
, const struct typec_mux_desc
*desc
)
315 struct typec_mux
*mux
;
318 if (!desc
|| !desc
->set
)
319 return ERR_PTR(-EINVAL
);
321 mux
= kzalloc(sizeof(*mux
), GFP_KERNEL
);
323 return ERR_PTR(-ENOMEM
);
325 mux
->set
= desc
->set
;
327 device_initialize(&mux
->dev
);
328 mux
->dev
.parent
= parent
;
329 mux
->dev
.fwnode
= desc
->fwnode
;
330 mux
->dev
.class = &typec_mux_class
;
331 mux
->dev
.type
= &typec_mux_dev_type
;
332 mux
->dev
.driver_data
= desc
->drvdata
;
333 dev_set_name(&mux
->dev
, "%s-mux",
334 desc
->name
? desc
->name
: dev_name(parent
));
336 ret
= device_add(&mux
->dev
);
338 dev_err(parent
, "failed to register mux (%d)\n", ret
);
339 put_device(&mux
->dev
);
345 EXPORT_SYMBOL_GPL(typec_mux_register
);
348 * typec_mux_unregister - Unregister Multiplexer Switch
349 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
351 * Unregister mux that was registered with typec_mux_register().
353 void typec_mux_unregister(struct typec_mux
*mux
)
355 if (!IS_ERR_OR_NULL(mux
))
356 device_unregister(&mux
->dev
);
358 EXPORT_SYMBOL_GPL(typec_mux_unregister
);
360 void typec_mux_set_drvdata(struct typec_mux
*mux
, void *data
)
362 dev_set_drvdata(&mux
->dev
, data
);
364 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata
);
366 void *typec_mux_get_drvdata(struct typec_mux
*mux
)
368 return dev_get_drvdata(&mux
->dev
);
370 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata
);
372 struct class typec_mux_class
= {
374 .owner
= THIS_MODULE
,