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