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
17 #include "class.h"
18 #include "mux.h"
19
20 #define TYPEC_MUX_MAX_DEVS 3
21
22 struct typec_switch {
23 struct typec_switch_dev *sw_devs[TYPEC_MUX_MAX_DEVS];
24 unsigned int num_sw_devs;
25 };
26
switch_fwnode_match(struct device * dev,const void * fwnode)27 static int switch_fwnode_match(struct device *dev, const void *fwnode)
28 {
29 if (!is_typec_switch_dev(dev))
30 return 0;
31
32 return device_match_fwnode(dev, fwnode);
33 }
34
typec_switch_match(const struct fwnode_handle * fwnode,const char * id,void * data)35 static void *typec_switch_match(const struct fwnode_handle *fwnode,
36 const char *id, void *data)
37 {
38 struct typec_switch_dev **sw_devs = data;
39 struct device *dev;
40 int i;
41
42 /*
43 * Device graph (OF graph) does not give any means to identify the
44 * device type or the device class of the remote port parent that @fwnode
45 * represents, so in order to identify the type or the class of @fwnode
46 * an additional device property is needed. With typec switches the
47 * property is named "orientation-switch" (@id). The value of the device
48 * property is ignored.
49 */
50 if (id && !fwnode_property_present(fwnode, id))
51 return NULL;
52
53 /*
54 * At this point we are sure that @fwnode is a typec switch in all
55 * cases. If the switch hasn't yet been registered for some reason, the
56 * function "defers probe" for now.
57 */
58 dev = class_find_device(&typec_mux_class, NULL, fwnode,
59 switch_fwnode_match);
60 if (!dev)
61 return ERR_PTR(-EPROBE_DEFER);
62
63 /* Skip duplicates */
64 for (i = 0; i < TYPEC_MUX_MAX_DEVS; i++)
65 if (to_typec_switch_dev(dev) == sw_devs[i]) {
66 put_device(dev);
67 return NULL;
68 }
69
70 return to_typec_switch_dev(dev);
71 }
72
73 /**
74 * fwnode_typec_switch_get - Find USB Type-C orientation switch
75 * @fwnode: The caller device node
76 *
77 * Finds a switch linked with @dev. Returns a reference to the switch on
78 * success, NULL if no matching connection was found, or
79 * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch
80 * has not been enumerated yet.
81 */
fwnode_typec_switch_get(struct fwnode_handle * fwnode)82 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode)
83 {
84 struct typec_switch_dev *sw_devs[TYPEC_MUX_MAX_DEVS];
85 struct typec_switch *sw;
86 int count;
87 int err;
88 int i;
89
90 sw = kzalloc_obj(*sw);
91 if (!sw)
92 return ERR_PTR(-ENOMEM);
93
94 count = fwnode_connection_find_matches(fwnode, "orientation-switch",
95 (void **)sw_devs,
96 typec_switch_match,
97 (void **)sw_devs,
98 ARRAY_SIZE(sw_devs));
99 if (count <= 0) {
100 kfree(sw);
101 return NULL;
102 }
103
104 for (i = 0; i < count; i++) {
105 if (IS_ERR(sw_devs[i])) {
106 err = PTR_ERR(sw_devs[i]);
107 goto put_sw_devs;
108 }
109 }
110
111 for (i = 0; i < count; i++) {
112 WARN_ON(!try_module_get(sw_devs[i]->dev.parent->driver->owner));
113 sw->sw_devs[i] = sw_devs[i];
114 }
115
116 sw->num_sw_devs = count;
117
118 return sw;
119
120 put_sw_devs:
121 for (i = 0; i < count; i++) {
122 if (!IS_ERR(sw_devs[i]))
123 put_device(&sw_devs[i]->dev);
124 }
125
126 kfree(sw);
127
128 return ERR_PTR(err);
129 }
130 EXPORT_SYMBOL_GPL(fwnode_typec_switch_get);
131
132 /**
133 * typec_switch_put - Release USB Type-C orientation switch
134 * @sw: USB Type-C orientation switch
135 *
136 * Decrement reference count for @sw.
137 */
typec_switch_put(struct typec_switch * sw)138 void typec_switch_put(struct typec_switch *sw)
139 {
140 struct typec_switch_dev *sw_dev;
141 unsigned int i;
142
143 if (IS_ERR_OR_NULL(sw))
144 return;
145
146 for (i = 0; i < sw->num_sw_devs; i++) {
147 sw_dev = sw->sw_devs[i];
148
149 module_put(sw_dev->dev.parent->driver->owner);
150 put_device(&sw_dev->dev);
151 }
152 kfree(sw);
153 }
154 EXPORT_SYMBOL_GPL(typec_switch_put);
155
typec_switch_release(struct device * dev)156 static void typec_switch_release(struct device *dev)
157 {
158 kfree(to_typec_switch_dev(dev));
159 }
160
161 const struct device_type typec_switch_dev_type = {
162 .name = "orientation_switch",
163 .release = typec_switch_release,
164 };
165
166 /**
167 * typec_switch_register - Register USB Type-C orientation switch
168 * @parent: Parent device
169 * @desc: Orientation switch description
170 *
171 * This function registers a switch that can be used for routing the correct
172 * data pairs depending on the cable plug orientation from the USB Type-C
173 * connector to the USB controllers. USB Type-C plugs can be inserted
174 * right-side-up or upside-down.
175 */
176 struct typec_switch_dev *
typec_switch_register(struct device * parent,const struct typec_switch_desc * desc)177 typec_switch_register(struct device *parent,
178 const struct typec_switch_desc *desc)
179 {
180 struct typec_switch_dev *sw_dev;
181 int ret;
182
183 if (!desc || !desc->set)
184 return ERR_PTR(-EINVAL);
185
186 sw_dev = kzalloc_obj(*sw_dev);
187 if (!sw_dev)
188 return ERR_PTR(-ENOMEM);
189
190 sw_dev->set = desc->set;
191
192 device_initialize(&sw_dev->dev);
193 sw_dev->dev.parent = parent;
194 sw_dev->dev.fwnode = desc->fwnode;
195 sw_dev->dev.class = &typec_mux_class;
196 sw_dev->dev.type = &typec_switch_dev_type;
197 sw_dev->dev.driver_data = desc->drvdata;
198 ret = dev_set_name(&sw_dev->dev, "%s-switch", desc->name ? desc->name : dev_name(parent));
199 if (ret) {
200 put_device(&sw_dev->dev);
201 return ERR_PTR(ret);
202 }
203
204 ret = device_add(&sw_dev->dev);
205 if (ret) {
206 dev_err(parent, "failed to register switch (%d)\n", ret);
207 put_device(&sw_dev->dev);
208 return ERR_PTR(ret);
209 }
210
211 return sw_dev;
212 }
213 EXPORT_SYMBOL_GPL(typec_switch_register);
214
typec_switch_set(struct typec_switch * sw,enum typec_orientation orientation)215 int typec_switch_set(struct typec_switch *sw,
216 enum typec_orientation orientation)
217 {
218 struct typec_switch_dev *sw_dev;
219 unsigned int i;
220 int ret;
221
222 if (IS_ERR_OR_NULL(sw))
223 return 0;
224
225 for (i = 0; i < sw->num_sw_devs; i++) {
226 sw_dev = sw->sw_devs[i];
227
228 ret = sw_dev->set(sw_dev, orientation);
229 if (ret && ret != -EOPNOTSUPP)
230 return ret;
231 }
232
233 return 0;
234 }
235 EXPORT_SYMBOL_GPL(typec_switch_set);
236
237 /**
238 * typec_switch_unregister - Unregister USB Type-C orientation switch
239 * @sw_dev: USB Type-C orientation switch
240 *
241 * Unregister switch that was registered with typec_switch_register().
242 */
typec_switch_unregister(struct typec_switch_dev * sw_dev)243 void typec_switch_unregister(struct typec_switch_dev *sw_dev)
244 {
245 if (!IS_ERR_OR_NULL(sw_dev))
246 device_unregister(&sw_dev->dev);
247 }
248 EXPORT_SYMBOL_GPL(typec_switch_unregister);
249
typec_switch_set_drvdata(struct typec_switch_dev * sw_dev,void * data)250 void typec_switch_set_drvdata(struct typec_switch_dev *sw_dev, void *data)
251 {
252 dev_set_drvdata(&sw_dev->dev, data);
253 }
254 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
255
typec_switch_get_drvdata(struct typec_switch_dev * sw_dev)256 void *typec_switch_get_drvdata(struct typec_switch_dev *sw_dev)
257 {
258 return dev_get_drvdata(&sw_dev->dev);
259 }
260 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
261
262 /* ------------------------------------------------------------------------- */
263
264 struct typec_mux {
265 struct typec_mux_dev *mux_devs[TYPEC_MUX_MAX_DEVS];
266 unsigned int num_mux_devs;
267 };
268
mux_fwnode_match(struct device * dev,const void * fwnode)269 static int mux_fwnode_match(struct device *dev, const void *fwnode)
270 {
271 if (!is_typec_mux_dev(dev))
272 return 0;
273
274 return device_match_fwnode(dev, fwnode);
275 }
276
typec_mux_match(const struct fwnode_handle * fwnode,const char * id,void * data)277 static void *typec_mux_match(const struct fwnode_handle *fwnode,
278 const char *id, void *data)
279 {
280 struct typec_mux_dev **mux_devs = data;
281 struct device *dev;
282 int i;
283
284 /*
285 * Device graph (OF graph) does not give any means to identify the
286 * device type or the device class of the remote port parent that @fwnode
287 * represents, so in order to identify the type or the class of @fwnode
288 * an additional device property is needed. With typec muxes the
289 * property is named "mode-switch" (@id). The value of the device
290 * property is ignored.
291 */
292 if (id && !fwnode_property_present(fwnode, id))
293 return NULL;
294
295 dev = class_find_device(&typec_mux_class, NULL, fwnode,
296 mux_fwnode_match);
297 if (!dev)
298 return ERR_PTR(-EPROBE_DEFER);
299
300 /* Skip duplicates */
301 for (i = 0; i < TYPEC_MUX_MAX_DEVS; i++)
302 if (to_typec_mux_dev(dev) == mux_devs[i]) {
303 put_device(dev);
304 return NULL;
305 }
306
307
308 return to_typec_mux_dev(dev);
309 }
310
311 /**
312 * fwnode_typec_mux_get - Find USB Type-C Multiplexer
313 * @fwnode: The caller device node
314 *
315 * Finds a mux linked to the caller. This function is primarily meant for the
316 * Type-C drivers. Returns a reference to the mux on success, NULL if no
317 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
318 * was found but the mux has not been enumerated yet.
319 */
fwnode_typec_mux_get(struct fwnode_handle * fwnode)320 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode)
321 {
322 struct typec_mux_dev *mux_devs[TYPEC_MUX_MAX_DEVS];
323 struct typec_mux *mux;
324 int count;
325 int err;
326 int i;
327
328 mux = kzalloc_obj(*mux);
329 if (!mux)
330 return ERR_PTR(-ENOMEM);
331
332 count = fwnode_connection_find_matches(fwnode, "mode-switch",
333 (void **)mux_devs,
334 typec_mux_match,
335 (void **)mux_devs,
336 ARRAY_SIZE(mux_devs));
337 if (count <= 0) {
338 kfree(mux);
339 return NULL;
340 }
341
342 for (i = 0; i < count; i++) {
343 if (IS_ERR(mux_devs[i])) {
344 err = PTR_ERR(mux_devs[i]);
345 goto put_mux_devs;
346 }
347 }
348
349 for (i = 0; i < count; i++) {
350 WARN_ON(!try_module_get(mux_devs[i]->dev.parent->driver->owner));
351 mux->mux_devs[i] = mux_devs[i];
352 }
353
354 mux->num_mux_devs = count;
355
356 return mux;
357
358 put_mux_devs:
359 for (i = 0; i < count; i++) {
360 if (!IS_ERR(mux_devs[i]))
361 put_device(&mux_devs[i]->dev);
362 }
363
364 kfree(mux);
365
366 return ERR_PTR(err);
367 }
368 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get);
369
370 /**
371 * typec_mux_put - Release handle to a Multiplexer
372 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
373 *
374 * Decrements reference count for @mux.
375 */
typec_mux_put(struct typec_mux * mux)376 void typec_mux_put(struct typec_mux *mux)
377 {
378 struct typec_mux_dev *mux_dev;
379 unsigned int i;
380
381 if (IS_ERR_OR_NULL(mux))
382 return;
383
384 for (i = 0; i < mux->num_mux_devs; i++) {
385 mux_dev = mux->mux_devs[i];
386 module_put(mux_dev->dev.parent->driver->owner);
387 put_device(&mux_dev->dev);
388 }
389 kfree(mux);
390 }
391 EXPORT_SYMBOL_GPL(typec_mux_put);
392
typec_mux_set(struct typec_mux * mux,struct typec_mux_state * state)393 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
394 {
395 struct typec_mux_dev *mux_dev;
396 unsigned int i;
397 int ret;
398
399 if (IS_ERR_OR_NULL(mux))
400 return 0;
401
402 for (i = 0; i < mux->num_mux_devs; i++) {
403 mux_dev = mux->mux_devs[i];
404
405 ret = mux_dev->set(mux_dev, state);
406 if (ret && ret != -EOPNOTSUPP)
407 return ret;
408 }
409
410 return 0;
411 }
412 EXPORT_SYMBOL_GPL(typec_mux_set);
413
typec_mux_release(struct device * dev)414 static void typec_mux_release(struct device *dev)
415 {
416 kfree(to_typec_mux_dev(dev));
417 }
418
419 const struct device_type typec_mux_dev_type = {
420 .name = "mode_switch",
421 .release = typec_mux_release,
422 };
423
424 /**
425 * typec_mux_register - Register Multiplexer routing USB Type-C pins
426 * @parent: Parent device
427 * @desc: Multiplexer description
428 *
429 * USB Type-C connectors can be used for alternate modes of operation besides
430 * USB when Accessory/Alternate Modes are supported. With some of those modes,
431 * the pins on the connector need to be reconfigured. This function registers
432 * multiplexer switches routing the pins on the connector.
433 */
434 struct typec_mux_dev *
typec_mux_register(struct device * parent,const struct typec_mux_desc * desc)435 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
436 {
437 struct typec_mux_dev *mux_dev;
438 int ret;
439
440 if (!desc || !desc->set)
441 return ERR_PTR(-EINVAL);
442
443 mux_dev = kzalloc_obj(*mux_dev);
444 if (!mux_dev)
445 return ERR_PTR(-ENOMEM);
446
447 mux_dev->set = desc->set;
448
449 device_initialize(&mux_dev->dev);
450 mux_dev->dev.parent = parent;
451 mux_dev->dev.fwnode = desc->fwnode;
452 mux_dev->dev.class = &typec_mux_class;
453 mux_dev->dev.type = &typec_mux_dev_type;
454 mux_dev->dev.driver_data = desc->drvdata;
455 ret = dev_set_name(&mux_dev->dev, "%s-mux", desc->name ? desc->name : dev_name(parent));
456 if (ret) {
457 put_device(&mux_dev->dev);
458 return ERR_PTR(ret);
459 }
460
461 ret = device_add(&mux_dev->dev);
462 if (ret) {
463 dev_err(parent, "failed to register mux (%d)\n", ret);
464 put_device(&mux_dev->dev);
465 return ERR_PTR(ret);
466 }
467
468 return mux_dev;
469 }
470 EXPORT_SYMBOL_GPL(typec_mux_register);
471
472 /**
473 * typec_mux_unregister - Unregister Multiplexer Switch
474 * @mux_dev: USB Type-C Connector Multiplexer/DeMultiplexer
475 *
476 * Unregister mux that was registered with typec_mux_register().
477 */
typec_mux_unregister(struct typec_mux_dev * mux_dev)478 void typec_mux_unregister(struct typec_mux_dev *mux_dev)
479 {
480 if (!IS_ERR_OR_NULL(mux_dev))
481 device_unregister(&mux_dev->dev);
482 }
483 EXPORT_SYMBOL_GPL(typec_mux_unregister);
484
typec_mux_set_drvdata(struct typec_mux_dev * mux_dev,void * data)485 void typec_mux_set_drvdata(struct typec_mux_dev *mux_dev, void *data)
486 {
487 dev_set_drvdata(&mux_dev->dev, data);
488 }
489 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
490
typec_mux_get_drvdata(struct typec_mux_dev * mux_dev)491 void *typec_mux_get_drvdata(struct typec_mux_dev *mux_dev)
492 {
493 return dev_get_drvdata(&mux_dev->dev);
494 }
495 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
496
497 const struct class typec_mux_class = {
498 .name = "typec_mux",
499 };
500