• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2022 Google LLC
4  *
5  * USB Type-C Retimer support.
6  * Author: Prashant Malani <pmalani@chromium.org>
7  *
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 "retimer.h"
19 
retimer_fwnode_match(struct device * dev,const void * fwnode)20 static int retimer_fwnode_match(struct device *dev, const void *fwnode)
21 {
22 	return is_typec_retimer(dev) && device_match_fwnode(dev, fwnode);
23 }
24 
typec_retimer_match(const struct fwnode_handle * fwnode,const char * id,void * data)25 static void *typec_retimer_match(const struct fwnode_handle *fwnode, const char *id, void *data)
26 {
27 	struct device *dev;
28 
29 	if (id && !fwnode_property_present(fwnode, id))
30 		return NULL;
31 
32 	dev = class_find_device(&retimer_class, NULL, fwnode,
33 				retimer_fwnode_match);
34 
35 	return dev ? to_typec_retimer(dev) : ERR_PTR(-EPROBE_DEFER);
36 }
37 
38 /**
39  * fwnode_typec_retimer_get - Find USB Type-C retimer.
40  * @fwnode: The caller device node.
41  *
42  * Finds a retimer linked to the caller. This function is primarily meant for the
43  * Type-C drivers. Returns a reference to the retimer on success, NULL if no
44  * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection
45  * was found but the retimer has not been enumerated yet.
46  */
fwnode_typec_retimer_get(struct fwnode_handle * fwnode)47 struct typec_retimer *fwnode_typec_retimer_get(struct fwnode_handle *fwnode)
48 {
49 	struct typec_retimer *retimer;
50 
51 	retimer = fwnode_connection_find_match(fwnode, "retimer-switch", NULL, typec_retimer_match);
52 	if (!IS_ERR_OR_NULL(retimer))
53 		WARN_ON(!try_module_get(retimer->dev.parent->driver->owner));
54 
55 	return retimer;
56 }
57 EXPORT_SYMBOL_GPL(fwnode_typec_retimer_get);
58 
59 /**
60  * typec_retimer_put - Release handle to a retimer.
61  * @retimer: USB Type-C Connector Retimer.
62  *
63  * Decrements reference count for @retimer.
64  */
typec_retimer_put(struct typec_retimer * retimer)65 void typec_retimer_put(struct typec_retimer *retimer)
66 {
67 	if (!IS_ERR_OR_NULL(retimer)) {
68 		module_put(retimer->dev.parent->driver->owner);
69 		put_device(&retimer->dev);
70 	}
71 }
72 EXPORT_SYMBOL_GPL(typec_retimer_put);
73 
typec_retimer_set(struct typec_retimer * retimer,struct typec_retimer_state * state)74 int typec_retimer_set(struct typec_retimer *retimer, struct typec_retimer_state *state)
75 {
76 	if (IS_ERR_OR_NULL(retimer))
77 		return 0;
78 
79 	return retimer->set(retimer, state);
80 }
81 EXPORT_SYMBOL_GPL(typec_retimer_set);
82 
typec_retimer_release(struct device * dev)83 static void typec_retimer_release(struct device *dev)
84 {
85 	kfree(to_typec_retimer(dev));
86 }
87 
88 const struct device_type typec_retimer_dev_type = {
89 	.name = "typec_retimer",
90 	.release = typec_retimer_release,
91 };
92 
93 /**
94  * typec_retimer_register - Register a retimer device.
95  * @parent: Parent device.
96  * @desc: Retimer description.
97  *
98  * Some USB Type-C connectors have their physical lines routed through retimers before they
99  * reach muxes or host controllers. In some cases (for example: using alternate modes)
100  * these retimers need to be reconfigured appropriately. This function registers retimer
101  * switches which route and potentially modify the signals on the Type C physical lines
102  * enroute to the host controllers.
103  */
104 struct typec_retimer *
typec_retimer_register(struct device * parent,const struct typec_retimer_desc * desc)105 typec_retimer_register(struct device *parent, const struct typec_retimer_desc *desc)
106 {
107 	struct typec_retimer *retimer;
108 	int ret;
109 
110 	if (!desc || !desc->set)
111 		return ERR_PTR(-EINVAL);
112 
113 	retimer = kzalloc(sizeof(*retimer), GFP_KERNEL);
114 	if (!retimer)
115 		return ERR_PTR(-ENOMEM);
116 
117 	retimer->set = desc->set;
118 
119 	device_initialize(&retimer->dev);
120 	retimer->dev.parent = parent;
121 	retimer->dev.fwnode = desc->fwnode;
122 	retimer->dev.class = &retimer_class;
123 	retimer->dev.type = &typec_retimer_dev_type;
124 	retimer->dev.driver_data = desc->drvdata;
125 	dev_set_name(&retimer->dev, "%s-retimer",
126 		     desc->name ? desc->name : dev_name(parent));
127 
128 	ret = device_add(&retimer->dev);
129 	if (ret) {
130 		dev_err(parent, "failed to register retimer (%d)\n", ret);
131 		put_device(&retimer->dev);
132 		return ERR_PTR(ret);
133 	}
134 
135 	return retimer;
136 }
137 EXPORT_SYMBOL_GPL(typec_retimer_register);
138 
139 /**
140  * typec_retimer_unregister - Unregister retimer device.
141  * @retimer: USB Type-C Connector retimer.
142  *
143  * Unregister retimer that was registered with typec_retimer_register().
144  */
typec_retimer_unregister(struct typec_retimer * retimer)145 void typec_retimer_unregister(struct typec_retimer *retimer)
146 {
147 	if (!IS_ERR_OR_NULL(retimer))
148 		device_unregister(&retimer->dev);
149 }
150 EXPORT_SYMBOL_GPL(typec_retimer_unregister);
151 
typec_retimer_get_drvdata(struct typec_retimer * retimer)152 void *typec_retimer_get_drvdata(struct typec_retimer *retimer)
153 {
154 	return dev_get_drvdata(&retimer->dev);
155 }
156 EXPORT_SYMBOL_GPL(typec_retimer_get_drvdata);
157 
158 struct class retimer_class = {
159 	.name = "retimer",
160 };
161