1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Type-C Multiplexer/DeMultiplexer Switch support
4 *
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * Hans de Goede <hdegoede@redhat.com>
8 */
9
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>
17
18 #include "bus.h"
19
dev_name_ends_with(struct device * dev,const char * suffix)20 static bool dev_name_ends_with(struct device *dev, const char *suffix)
21 {
22 const char *name = dev_name(dev);
23 const int name_len = strlen(name);
24 const int suffix_len = strlen(suffix);
25
26 if (suffix_len > name_len)
27 return false;
28
29 return strcmp(name + (name_len - suffix_len), suffix) == 0;
30 }
31
switch_fwnode_match(struct device * dev,const void * fwnode)32 static int switch_fwnode_match(struct device *dev, const void *fwnode)
33 {
34 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-switch");
35 }
36
typec_switch_match(struct fwnode_handle * fwnode,const char * id,void * data)37 static void *typec_switch_match(struct fwnode_handle *fwnode, const char *id,
38 void *data)
39 {
40 struct device *dev;
41
42 if (id && !fwnode_property_present(fwnode, id))
43 return NULL;
44
45 dev = class_find_device(&typec_mux_class, NULL, fwnode,
46 switch_fwnode_match);
47
48 return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
49 }
50
51 /**
52 * fwnode_typec_switch_get - Find USB Type-C orientation switch
53 * @fwnode: The caller device node
54 *
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.
59 */
fwnode_typec_switch_get(struct fwnode_handle * fwnode)60 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode)
61 {
62 struct typec_switch *sw;
63
64 sw = fwnode_connection_find_match(fwnode, "orientation-switch", NULL,
65 typec_switch_match);
66 if (!IS_ERR_OR_NULL(sw))
67 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
68
69 return sw;
70 }
71 EXPORT_SYMBOL_GPL(fwnode_typec_switch_get);
72
73 /**
74 * typec_switch_put - Release USB Type-C orientation switch
75 * @sw: USB Type-C orientation switch
76 *
77 * Decrement reference count for @sw.
78 */
typec_switch_put(struct typec_switch * sw)79 void typec_switch_put(struct typec_switch *sw)
80 {
81 if (!IS_ERR_OR_NULL(sw)) {
82 module_put(sw->dev.parent->driver->owner);
83 put_device(&sw->dev);
84 }
85 }
86 EXPORT_SYMBOL_GPL(typec_switch_put);
87
typec_switch_release(struct device * dev)88 static void typec_switch_release(struct device *dev)
89 {
90 kfree(to_typec_switch(dev));
91 }
92
93 static const struct device_type typec_switch_dev_type = {
94 .name = "orientation_switch",
95 .release = typec_switch_release,
96 };
97
98 /**
99 * typec_switch_register - Register USB Type-C orientation switch
100 * @parent: Parent device
101 * @desc: Orientation switch description
102 *
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.
107 */
108 struct typec_switch *
typec_switch_register(struct device * parent,const struct typec_switch_desc * desc)109 typec_switch_register(struct device *parent,
110 const struct typec_switch_desc *desc)
111 {
112 struct typec_switch *sw;
113 int ret;
114
115 if (!desc || !desc->set)
116 return ERR_PTR(-EINVAL);
117
118 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
119 if (!sw)
120 return ERR_PTR(-ENOMEM);
121
122 sw->set = desc->set;
123
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));
132
133 ret = device_add(&sw->dev);
134 if (ret) {
135 dev_err(parent, "failed to register switch (%d)\n", ret);
136 put_device(&sw->dev);
137 return ERR_PTR(ret);
138 }
139
140 return sw;
141 }
142 EXPORT_SYMBOL_GPL(typec_switch_register);
143
typec_switch_set(struct typec_switch * sw,enum typec_orientation orientation)144 int typec_switch_set(struct typec_switch *sw,
145 enum typec_orientation orientation)
146 {
147 if (IS_ERR_OR_NULL(sw))
148 return 0;
149
150 return sw->set(sw, orientation);
151 }
152 EXPORT_SYMBOL_GPL(typec_switch_set);
153
154 /**
155 * typec_switch_unregister - Unregister USB Type-C orientation switch
156 * @sw: USB Type-C orientation switch
157 *
158 * Unregister switch that was registered with typec_switch_register().
159 */
typec_switch_unregister(struct typec_switch * sw)160 void typec_switch_unregister(struct typec_switch *sw)
161 {
162 if (!IS_ERR_OR_NULL(sw))
163 device_unregister(&sw->dev);
164 }
165 EXPORT_SYMBOL_GPL(typec_switch_unregister);
166
typec_switch_set_drvdata(struct typec_switch * sw,void * data)167 void typec_switch_set_drvdata(struct typec_switch *sw, void *data)
168 {
169 dev_set_drvdata(&sw->dev, data);
170 }
171 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
172
typec_switch_get_drvdata(struct typec_switch * sw)173 void *typec_switch_get_drvdata(struct typec_switch *sw)
174 {
175 return dev_get_drvdata(&sw->dev);
176 }
177 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
178
179 /* ------------------------------------------------------------------------- */
180
mux_fwnode_match(struct device * dev,const void * fwnode)181 static int mux_fwnode_match(struct device *dev, const void *fwnode)
182 {
183 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-mux");
184 }
185
typec_mux_match(struct fwnode_handle * fwnode,const char * id,void * data)186 static void *typec_mux_match(struct fwnode_handle *fwnode, const char *id,
187 void *data)
188 {
189 const struct typec_altmode_desc *desc = data;
190 struct device *dev;
191 bool match;
192 int nval;
193 u16 *val;
194 int i;
195
196 /*
197 * Check has the identifier already been "consumed". If it
198 * has, no need to do any extra connection identification.
199 */
200 match = !id;
201 if (match)
202 goto find_mux;
203
204 /* Accessory Mode muxes */
205 if (!desc) {
206 match = fwnode_property_present(fwnode, "accessory");
207 if (match)
208 goto find_mux;
209 return NULL;
210 }
211
212 /* Alternate Mode muxes */
213 nval = fwnode_property_count_u16(fwnode, "svid");
214 if (nval <= 0)
215 return NULL;
216
217 val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
218 if (!val)
219 return ERR_PTR(-ENOMEM);
220
221 nval = fwnode_property_read_u16_array(fwnode, "svid", val, nval);
222 if (nval < 0) {
223 kfree(val);
224 return ERR_PTR(nval);
225 }
226
227 for (i = 0; i < nval; i++) {
228 match = val[i] == desc->svid;
229 if (match) {
230 kfree(val);
231 goto find_mux;
232 }
233 }
234 kfree(val);
235 return NULL;
236
237 find_mux:
238 dev = class_find_device(&typec_mux_class, NULL, fwnode,
239 mux_fwnode_match);
240
241 return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
242 }
243
244 /**
245 * fwnode_typec_mux_get - Find USB Type-C Multiplexer
246 * @fwnode: The caller device node
247 * @desc: Alt Mode description
248 *
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.
253 */
fwnode_typec_mux_get(struct fwnode_handle * fwnode,const struct typec_altmode_desc * desc)254 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
255 const struct typec_altmode_desc *desc)
256 {
257 struct typec_mux *mux;
258
259 mux = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc,
260 typec_mux_match);
261 if (!IS_ERR_OR_NULL(mux))
262 WARN_ON(!try_module_get(mux->dev.parent->driver->owner));
263
264 return mux;
265 }
266 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get);
267
268 /**
269 * typec_mux_put - Release handle to a Multiplexer
270 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
271 *
272 * Decrements reference count for @mux.
273 */
typec_mux_put(struct typec_mux * mux)274 void typec_mux_put(struct typec_mux *mux)
275 {
276 if (!IS_ERR_OR_NULL(mux)) {
277 module_put(mux->dev.parent->driver->owner);
278 put_device(&mux->dev);
279 }
280 }
281 EXPORT_SYMBOL_GPL(typec_mux_put);
282
typec_mux_set(struct typec_mux * mux,struct typec_mux_state * state)283 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
284 {
285 if (IS_ERR_OR_NULL(mux))
286 return 0;
287
288 return mux->set(mux, state);
289 }
290 EXPORT_SYMBOL_GPL(typec_mux_set);
291
typec_mux_release(struct device * dev)292 static void typec_mux_release(struct device *dev)
293 {
294 kfree(to_typec_mux(dev));
295 }
296
297 static const struct device_type typec_mux_dev_type = {
298 .name = "mode_switch",
299 .release = typec_mux_release,
300 };
301
302 /**
303 * typec_mux_register - Register Multiplexer routing USB Type-C pins
304 * @parent: Parent device
305 * @desc: Multiplexer description
306 *
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.
311 */
312 struct typec_mux *
typec_mux_register(struct device * parent,const struct typec_mux_desc * desc)313 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
314 {
315 struct typec_mux *mux;
316 int ret;
317
318 if (!desc || !desc->set)
319 return ERR_PTR(-EINVAL);
320
321 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
322 if (!mux)
323 return ERR_PTR(-ENOMEM);
324
325 mux->set = desc->set;
326
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));
335
336 ret = device_add(&mux->dev);
337 if (ret) {
338 dev_err(parent, "failed to register mux (%d)\n", ret);
339 put_device(&mux->dev);
340 return ERR_PTR(ret);
341 }
342
343 return mux;
344 }
345 EXPORT_SYMBOL_GPL(typec_mux_register);
346
347 /**
348 * typec_mux_unregister - Unregister Multiplexer Switch
349 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
350 *
351 * Unregister mux that was registered with typec_mux_register().
352 */
typec_mux_unregister(struct typec_mux * mux)353 void typec_mux_unregister(struct typec_mux *mux)
354 {
355 if (!IS_ERR_OR_NULL(mux))
356 device_unregister(&mux->dev);
357 }
358 EXPORT_SYMBOL_GPL(typec_mux_unregister);
359
typec_mux_set_drvdata(struct typec_mux * mux,void * data)360 void typec_mux_set_drvdata(struct typec_mux *mux, void *data)
361 {
362 dev_set_drvdata(&mux->dev, data);
363 }
364 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
365
typec_mux_get_drvdata(struct typec_mux * mux)366 void *typec_mux_get_drvdata(struct typec_mux *mux)
367 {
368 return dev_get_drvdata(&mux->dev);
369 }
370 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
371
372 struct class typec_mux_class = {
373 .name = "typec_mux",
374 .owner = THIS_MODULE,
375 };
376