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