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