net: rtl8192cu: change config name as vendor driver
[platform/kernel/linux-rpi.git] / drivers / usb / typec / mux.c
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 static int switch_fwnode_match(struct device *dev, const void *fwnode)
21 {
22         if (!is_typec_switch(dev))
23                 return 0;
24
25         return dev_fwnode(dev) == fwnode;
26 }
27
28 static void *typec_switch_match(struct fwnode_handle *fwnode, const char *id,
29                                 void *data)
30 {
31         struct device *dev;
32
33         /*
34          * Device graph (OF graph) does not give any means to identify the
35          * device type or the device class of the remote port parent that @fwnode
36          * represents, so in order to identify the type or the class of @fwnode
37          * an additional device property is needed. With typec switches the
38          * property is named "orientation-switch" (@id). The value of the device
39          * property is ignored.
40          */
41         if (id && !fwnode_property_present(fwnode, id))
42                 return NULL;
43
44         /*
45          * At this point we are sure that @fwnode is a typec switch in all
46          * cases. If the switch hasn't yet been registered for some reason, the
47          * function "defers probe" for now.
48          */
49         dev = class_find_device(&typec_mux_class, NULL, fwnode,
50                                 switch_fwnode_match);
51
52         return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
53 }
54
55 /**
56  * fwnode_typec_switch_get - Find USB Type-C orientation switch
57  * @fwnode: The caller device node
58  *
59  * Finds a switch linked with @dev. Returns a reference to the switch on
60  * success, NULL if no matching connection was found, or
61  * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch
62  * has not been enumerated yet.
63  */
64 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode)
65 {
66         struct typec_switch *sw;
67
68         sw = fwnode_connection_find_match(fwnode, "orientation-switch", NULL,
69                                           typec_switch_match);
70         if (!IS_ERR_OR_NULL(sw))
71                 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
72
73         return sw;
74 }
75 EXPORT_SYMBOL_GPL(fwnode_typec_switch_get);
76
77 /**
78  * typec_switch_put - Release USB Type-C orientation switch
79  * @sw: USB Type-C orientation switch
80  *
81  * Decrement reference count for @sw.
82  */
83 void typec_switch_put(struct typec_switch *sw)
84 {
85         if (!IS_ERR_OR_NULL(sw)) {
86                 module_put(sw->dev.parent->driver->owner);
87                 put_device(&sw->dev);
88         }
89 }
90 EXPORT_SYMBOL_GPL(typec_switch_put);
91
92 static void typec_switch_release(struct device *dev)
93 {
94         kfree(to_typec_switch(dev));
95 }
96
97 const struct device_type typec_switch_dev_type = {
98         .name = "orientation_switch",
99         .release = typec_switch_release,
100 };
101
102 /**
103  * typec_switch_register - Register USB Type-C orientation switch
104  * @parent: Parent device
105  * @desc: Orientation switch description
106  *
107  * This function registers a switch that can be used for routing the correct
108  * data pairs depending on the cable plug orientation from the USB Type-C
109  * connector to the USB controllers. USB Type-C plugs can be inserted
110  * right-side-up or upside-down.
111  */
112 struct typec_switch *
113 typec_switch_register(struct device *parent,
114                       const struct typec_switch_desc *desc)
115 {
116         struct typec_switch *sw;
117         int ret;
118
119         if (!desc || !desc->set)
120                 return ERR_PTR(-EINVAL);
121
122         sw = kzalloc(sizeof(*sw), GFP_KERNEL);
123         if (!sw)
124                 return ERR_PTR(-ENOMEM);
125
126         sw->set = desc->set;
127
128         device_initialize(&sw->dev);
129         sw->dev.parent = parent;
130         sw->dev.fwnode = desc->fwnode;
131         sw->dev.class = &typec_mux_class;
132         sw->dev.type = &typec_switch_dev_type;
133         sw->dev.driver_data = desc->drvdata;
134         ret = dev_set_name(&sw->dev, "%s-switch", desc->name ? desc->name : dev_name(parent));
135         if (ret) {
136                 put_device(&sw->dev);
137                 return ERR_PTR(ret);
138         }
139
140         ret = device_add(&sw->dev);
141         if (ret) {
142                 dev_err(parent, "failed to register switch (%d)\n", ret);
143                 put_device(&sw->dev);
144                 return ERR_PTR(ret);
145         }
146
147         return sw;
148 }
149 EXPORT_SYMBOL_GPL(typec_switch_register);
150
151 int typec_switch_set(struct typec_switch *sw,
152                      enum typec_orientation orientation)
153 {
154         if (IS_ERR_OR_NULL(sw))
155                 return 0;
156
157         return sw->set(sw, orientation);
158 }
159 EXPORT_SYMBOL_GPL(typec_switch_set);
160
161 /**
162  * typec_switch_unregister - Unregister USB Type-C orientation switch
163  * @sw: USB Type-C orientation switch
164  *
165  * Unregister switch that was registered with typec_switch_register().
166  */
167 void typec_switch_unregister(struct typec_switch *sw)
168 {
169         if (!IS_ERR_OR_NULL(sw))
170                 device_unregister(&sw->dev);
171 }
172 EXPORT_SYMBOL_GPL(typec_switch_unregister);
173
174 void typec_switch_set_drvdata(struct typec_switch *sw, void *data)
175 {
176         dev_set_drvdata(&sw->dev, data);
177 }
178 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
179
180 void *typec_switch_get_drvdata(struct typec_switch *sw)
181 {
182         return dev_get_drvdata(&sw->dev);
183 }
184 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
185
186 /* ------------------------------------------------------------------------- */
187
188 static int mux_fwnode_match(struct device *dev, const void *fwnode)
189 {
190         if (!is_typec_mux(dev))
191                 return 0;
192
193         return dev_fwnode(dev) == fwnode;
194 }
195
196 static void *typec_mux_match(struct fwnode_handle *fwnode, const char *id,
197                              void *data)
198 {
199         const struct typec_altmode_desc *desc = data;
200         struct device *dev;
201         bool match;
202         int nval;
203         u16 *val;
204         int ret;
205         int i;
206
207         /*
208          * Check has the identifier already been "consumed". If it
209          * has, no need to do any extra connection identification.
210          */
211         match = !id;
212         if (match)
213                 goto find_mux;
214
215         /* Accessory Mode muxes */
216         if (!desc) {
217                 match = fwnode_property_present(fwnode, "accessory");
218                 if (match)
219                         goto find_mux;
220                 return NULL;
221         }
222
223         /* Alternate Mode muxes */
224         nval = fwnode_property_count_u16(fwnode, "svid");
225         if (nval <= 0)
226                 return NULL;
227
228         val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
229         if (!val)
230                 return ERR_PTR(-ENOMEM);
231
232         ret = fwnode_property_read_u16_array(fwnode, "svid", val, nval);
233         if (ret < 0) {
234                 kfree(val);
235                 return ERR_PTR(ret);
236         }
237
238         for (i = 0; i < nval; i++) {
239                 match = val[i] == desc->svid;
240                 if (match) {
241                         kfree(val);
242                         goto find_mux;
243                 }
244         }
245         kfree(val);
246         return NULL;
247
248 find_mux:
249         dev = class_find_device(&typec_mux_class, NULL, fwnode,
250                                 mux_fwnode_match);
251
252         return dev ? to_typec_mux(dev) : ERR_PTR(-EPROBE_DEFER);
253 }
254
255 /**
256  * fwnode_typec_mux_get - Find USB Type-C Multiplexer
257  * @fwnode: The caller device node
258  * @desc: Alt Mode description
259  *
260  * Finds a mux linked to the caller. This function is primarily meant for the
261  * Type-C drivers. Returns a reference to the mux on success, NULL if no
262  * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
263  * was found but the mux has not been enumerated yet.
264  */
265 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
266                                        const struct typec_altmode_desc *desc)
267 {
268         struct typec_mux *mux;
269
270         mux = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc,
271                                            typec_mux_match);
272         if (!IS_ERR_OR_NULL(mux))
273                 WARN_ON(!try_module_get(mux->dev.parent->driver->owner));
274
275         return mux;
276 }
277 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get);
278
279 /**
280  * typec_mux_put - Release handle to a Multiplexer
281  * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
282  *
283  * Decrements reference count for @mux.
284  */
285 void typec_mux_put(struct typec_mux *mux)
286 {
287         if (!IS_ERR_OR_NULL(mux)) {
288                 module_put(mux->dev.parent->driver->owner);
289                 put_device(&mux->dev);
290         }
291 }
292 EXPORT_SYMBOL_GPL(typec_mux_put);
293
294 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
295 {
296         if (IS_ERR_OR_NULL(mux))
297                 return 0;
298
299         return mux->set(mux, state);
300 }
301 EXPORT_SYMBOL_GPL(typec_mux_set);
302
303 static void typec_mux_release(struct device *dev)
304 {
305         kfree(to_typec_mux(dev));
306 }
307
308 const struct device_type typec_mux_dev_type = {
309         .name = "mode_switch",
310         .release = typec_mux_release,
311 };
312
313 /**
314  * typec_mux_register - Register Multiplexer routing USB Type-C pins
315  * @parent: Parent device
316  * @desc: Multiplexer description
317  *
318  * USB Type-C connectors can be used for alternate modes of operation besides
319  * USB when Accessory/Alternate Modes are supported. With some of those modes,
320  * the pins on the connector need to be reconfigured. This function registers
321  * multiplexer switches routing the pins on the connector.
322  */
323 struct typec_mux *
324 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
325 {
326         struct typec_mux *mux;
327         int ret;
328
329         if (!desc || !desc->set)
330                 return ERR_PTR(-EINVAL);
331
332         mux = kzalloc(sizeof(*mux), GFP_KERNEL);
333         if (!mux)
334                 return ERR_PTR(-ENOMEM);
335
336         mux->set = desc->set;
337
338         device_initialize(&mux->dev);
339         mux->dev.parent = parent;
340         mux->dev.fwnode = desc->fwnode;
341         mux->dev.class = &typec_mux_class;
342         mux->dev.type = &typec_mux_dev_type;
343         mux->dev.driver_data = desc->drvdata;
344         ret = dev_set_name(&mux->dev, "%s-mux", desc->name ? desc->name : dev_name(parent));
345         if (ret) {
346                 put_device(&mux->dev);
347                 return ERR_PTR(ret);
348         }
349
350         ret = device_add(&mux->dev);
351         if (ret) {
352                 dev_err(parent, "failed to register mux (%d)\n", ret);
353                 put_device(&mux->dev);
354                 return ERR_PTR(ret);
355         }
356
357         return mux;
358 }
359 EXPORT_SYMBOL_GPL(typec_mux_register);
360
361 /**
362  * typec_mux_unregister - Unregister Multiplexer Switch
363  * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
364  *
365  * Unregister mux that was registered with typec_mux_register().
366  */
367 void typec_mux_unregister(struct typec_mux *mux)
368 {
369         if (!IS_ERR_OR_NULL(mux))
370                 device_unregister(&mux->dev);
371 }
372 EXPORT_SYMBOL_GPL(typec_mux_unregister);
373
374 void typec_mux_set_drvdata(struct typec_mux *mux, void *data)
375 {
376         dev_set_drvdata(&mux->dev, data);
377 }
378 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
379
380 void *typec_mux_get_drvdata(struct typec_mux *mux)
381 {
382         return dev_get_drvdata(&mux->dev);
383 }
384 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
385
386 struct class typec_mux_class = {
387         .name = "typec_mux",
388         .owner = THIS_MODULE,
389 };