OSDN Git Service

Merge 5.6-rc3 into usb-next
[tomoyo/tomoyo-test1.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 #include <linux/usb/typec_mux.h>
17
18 #include "bus.h"
19
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
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
37 static void *typec_switch_match(struct device_connection *con, int ep,
38                                 void *data)
39 {
40         struct device *dev;
41
42         if (con->id && !fwnode_property_present(con->fwnode, con->id))
43                 return NULL;
44
45         dev = class_find_device(&typec_mux_class, NULL, con->fwnode,
46                                 switch_fwnode_match);
47
48         return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
49 }
50
51 /**
52  * typec_switch_get - Find USB Type-C orientation switch
53  * @dev: The caller device
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  */
60 struct typec_switch *typec_switch_get(struct device *dev)
61 {
62         struct typec_switch *sw;
63
64         sw = device_connection_find_match(dev, "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(typec_switch_get);
72
73 /**
74  * typec_put_switch - Release USB Type-C orientation switch
75  * @sw: USB Type-C orientation switch
76  *
77  * Decrement reference count for @sw.
78  */
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
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 *
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", dev_name(parent));
131
132         ret = device_add(&sw->dev);
133         if (ret) {
134                 dev_err(parent, "failed to register switch (%d)\n", ret);
135                 put_device(&sw->dev);
136                 return ERR_PTR(ret);
137         }
138
139         return sw;
140 }
141 EXPORT_SYMBOL_GPL(typec_switch_register);
142
143 /**
144  * typec_switch_unregister - Unregister USB Type-C orientation switch
145  * @sw: USB Type-C orientation switch
146  *
147  * Unregister switch that was registered with typec_switch_register().
148  */
149 void typec_switch_unregister(struct typec_switch *sw)
150 {
151         if (!IS_ERR_OR_NULL(sw))
152                 device_unregister(&sw->dev);
153 }
154 EXPORT_SYMBOL_GPL(typec_switch_unregister);
155
156 void typec_switch_set_drvdata(struct typec_switch *sw, void *data)
157 {
158         dev_set_drvdata(&sw->dev, data);
159 }
160 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata);
161
162 void *typec_switch_get_drvdata(struct typec_switch *sw)
163 {
164         return dev_get_drvdata(&sw->dev);
165 }
166 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
167
168 /* ------------------------------------------------------------------------- */
169
170 static int mux_fwnode_match(struct device *dev, const void *fwnode)
171 {
172         return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-mux");
173 }
174
175 static void *typec_mux_match(struct device_connection *con, int ep, void *data)
176 {
177         const struct typec_altmode_desc *desc = data;
178         struct device *dev;
179         bool match;
180         int nval;
181         u16 *val;
182         int i;
183
184         /*
185          * Check has the identifier already been "consumed". If it
186          * has, no need to do any extra connection identification.
187          */
188         match = !con->id;
189         if (match)
190                 goto find_mux;
191
192         /* Accessory Mode muxes */
193         if (!desc) {
194                 match = fwnode_property_present(con->fwnode, "accessory");
195                 if (match)
196                         goto find_mux;
197                 return NULL;
198         }
199
200         /* Alternate Mode muxes */
201         nval = fwnode_property_count_u16(con->fwnode, "svid");
202         if (nval <= 0)
203                 return NULL;
204
205         val = kcalloc(nval, sizeof(*val), GFP_KERNEL);
206         if (!val)
207                 return ERR_PTR(-ENOMEM);
208
209         nval = fwnode_property_read_u16_array(con->fwnode, "svid", val, nval);
210         if (nval < 0) {
211                 kfree(val);
212                 return ERR_PTR(nval);
213         }
214
215         for (i = 0; i < nval; i++) {
216                 match = val[i] == desc->svid;
217                 if (match) {
218                         kfree(val);
219                         goto find_mux;
220                 }
221         }
222         kfree(val);
223         return NULL;
224
225 find_mux:
226         dev = class_find_device(&typec_mux_class, NULL, con->fwnode,
227                                 mux_fwnode_match);
228
229         return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER);
230 }
231
232 /**
233  * typec_mux_get - Find USB Type-C Multiplexer
234  * @dev: The caller device
235  * @desc: Alt Mode description
236  *
237  * Finds a mux linked to the caller. This function is primarily meant for the
238  * Type-C drivers. Returns a reference to the mux on success, NULL if no
239  * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
240  * was found but the mux has not been enumerated yet.
241  */
242 struct typec_mux *typec_mux_get(struct device *dev,
243                                 const struct typec_altmode_desc *desc)
244 {
245         struct typec_mux *mux;
246
247         mux = device_connection_find_match(dev, "mode-switch", (void *)desc,
248                                            typec_mux_match);
249         if (!IS_ERR_OR_NULL(mux))
250                 WARN_ON(!try_module_get(mux->dev.parent->driver->owner));
251
252         return mux;
253 }
254 EXPORT_SYMBOL_GPL(typec_mux_get);
255
256 /**
257  * typec_mux_put - Release handle to a Multiplexer
258  * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
259  *
260  * Decrements reference count for @mux.
261  */
262 void typec_mux_put(struct typec_mux *mux)
263 {
264         if (!IS_ERR_OR_NULL(mux)) {
265                 module_put(mux->dev.parent->driver->owner);
266                 put_device(&mux->dev);
267         }
268 }
269 EXPORT_SYMBOL_GPL(typec_mux_put);
270
271 static void typec_mux_release(struct device *dev)
272 {
273         kfree(to_typec_mux(dev));
274 }
275
276 static const struct device_type typec_mux_dev_type = {
277         .name = "mode_switch",
278         .release = typec_mux_release,
279 };
280
281 /**
282  * typec_mux_register - Register Multiplexer routing USB Type-C pins
283  * @parent: Parent device
284  * @desc: Multiplexer description
285  *
286  * USB Type-C connectors can be used for alternate modes of operation besides
287  * USB when Accessory/Alternate Modes are supported. With some of those modes,
288  * the pins on the connector need to be reconfigured. This function registers
289  * multiplexer switches routing the pins on the connector.
290  */
291 struct typec_mux *
292 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
293 {
294         struct typec_mux *mux;
295         int ret;
296
297         if (!desc || !desc->set)
298                 return ERR_PTR(-EINVAL);
299
300         mux = kzalloc(sizeof(*mux), GFP_KERNEL);
301         if (!mux)
302                 return ERR_PTR(-ENOMEM);
303
304         mux->set = desc->set;
305
306         device_initialize(&mux->dev);
307         mux->dev.parent = parent;
308         mux->dev.fwnode = desc->fwnode;
309         mux->dev.class = &typec_mux_class;
310         mux->dev.type = &typec_mux_dev_type;
311         mux->dev.driver_data = desc->drvdata;
312         dev_set_name(&mux->dev, "%s-mux", dev_name(parent));
313
314         ret = device_add(&mux->dev);
315         if (ret) {
316                 dev_err(parent, "failed to register mux (%d)\n", ret);
317                 put_device(&mux->dev);
318                 return ERR_PTR(ret);
319         }
320
321         return mux;
322 }
323 EXPORT_SYMBOL_GPL(typec_mux_register);
324
325 /**
326  * typec_mux_unregister - Unregister Multiplexer Switch
327  * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
328  *
329  * Unregister mux that was registered with typec_mux_register().
330  */
331 void typec_mux_unregister(struct typec_mux *mux)
332 {
333         if (!IS_ERR_OR_NULL(mux))
334                 device_unregister(&mux->dev);
335 }
336 EXPORT_SYMBOL_GPL(typec_mux_unregister);
337
338 void typec_mux_set_drvdata(struct typec_mux *mux, void *data)
339 {
340         dev_set_drvdata(&mux->dev, data);
341 }
342 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata);
343
344 void *typec_mux_get_drvdata(struct typec_mux *mux)
345 {
346         return dev_get_drvdata(&mux->dev);
347 }
348 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata);
349
350 struct class typec_mux_class = {
351         .name = "typec_mux",
352         .owner = THIS_MODULE,
353 };