1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2021, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
9 #include <linux/pm_runtime.h>
10 #include <linux/component.h>
11 #include <linux/property.h>
15 static int connector_bind(struct device *dev, struct device *connector, void *data)
19 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
23 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
25 sysfs_remove_link(&dev->kobj, "connector");
30 static void connector_unbind(struct device *dev, struct device *connector, void *data)
32 sysfs_remove_link(&connector->kobj, dev_name(dev));
33 sysfs_remove_link(&dev->kobj, "connector");
36 static const struct component_ops connector_ops = {
37 .bind = connector_bind,
38 .unbind = connector_unbind,
41 static ssize_t link_show(struct device *dev, struct device_attribute *attr,
44 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
45 struct tb_port *port = usb4->port;
46 struct tb *tb = port->sw->tb;
49 if (mutex_lock_interruptible(&tb->lock))
52 if (tb_is_upstream_port(port))
53 link = port->sw->link_usb4 ? "usb4" : "tbt";
54 else if (tb_port_has_remote(port))
55 link = port->remote->sw->link_usb4 ? "usb4" : "tbt";
56 else if (port->xdomain)
57 link = port->xdomain->link_usb4 ? "usb4" : "tbt";
61 mutex_unlock(&tb->lock);
63 return sysfs_emit(buf, "%s\n", link);
65 static DEVICE_ATTR_RO(link);
67 static struct attribute *common_attrs[] = {
72 static const struct attribute_group common_group = {
73 .attrs = common_attrs,
76 static int usb4_port_offline(struct usb4_port *usb4)
78 struct tb_port *port = usb4->port;
81 ret = tb_acpi_power_on_retimers(port);
85 ret = usb4_port_router_offline(port);
87 tb_acpi_power_off_retimers(port);
91 ret = tb_retimer_scan(port, false);
93 usb4_port_router_online(port);
94 tb_acpi_power_off_retimers(port);
100 static void usb4_port_online(struct usb4_port *usb4)
102 struct tb_port *port = usb4->port;
104 usb4_port_router_online(port);
105 tb_acpi_power_off_retimers(port);
108 static ssize_t offline_show(struct device *dev,
109 struct device_attribute *attr, char *buf)
111 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
113 return sysfs_emit(buf, "%d\n", usb4->offline);
116 static ssize_t offline_store(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t count)
119 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
120 struct tb_port *port = usb4->port;
121 struct tb *tb = port->sw->tb;
125 ret = kstrtobool(buf, &val);
129 pm_runtime_get_sync(&usb4->dev);
131 if (mutex_lock_interruptible(&tb->lock)) {
136 if (val == usb4->offline)
139 /* Offline mode works only for ports that are not connected */
140 if (tb_port_has_remote(port)) {
146 ret = usb4_port_offline(usb4);
150 usb4_port_online(usb4);
151 tb_retimer_remove_all(port);
155 tb_port_dbg(port, "%s offline mode\n", val ? "enter" : "exit");
158 mutex_unlock(&tb->lock);
160 pm_runtime_mark_last_busy(&usb4->dev);
161 pm_runtime_put_autosuspend(&usb4->dev);
163 return ret ? ret : count;
165 static DEVICE_ATTR_RW(offline);
167 static ssize_t rescan_store(struct device *dev,
168 struct device_attribute *attr, const char *buf, size_t count)
170 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
171 struct tb_port *port = usb4->port;
172 struct tb *tb = port->sw->tb;
176 ret = kstrtobool(buf, &val);
183 pm_runtime_get_sync(&usb4->dev);
185 if (mutex_lock_interruptible(&tb->lock)) {
190 /* Must be in offline mode already */
191 if (!usb4->offline) {
196 tb_retimer_remove_all(port);
197 ret = tb_retimer_scan(port, true);
200 mutex_unlock(&tb->lock);
202 pm_runtime_mark_last_busy(&usb4->dev);
203 pm_runtime_put_autosuspend(&usb4->dev);
205 return ret ? ret : count;
207 static DEVICE_ATTR_WO(rescan);
209 static struct attribute *service_attrs[] = {
210 &dev_attr_offline.attr,
211 &dev_attr_rescan.attr,
215 static umode_t service_attr_is_visible(struct kobject *kobj,
216 struct attribute *attr, int n)
218 struct device *dev = kobj_to_dev(kobj);
219 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
222 * Always need some platform help to cycle the modes so that
223 * retimers can be accessed through the sideband.
225 return usb4->can_offline ? attr->mode : 0;
228 static const struct attribute_group service_group = {
229 .attrs = service_attrs,
230 .is_visible = service_attr_is_visible,
233 static const struct attribute_group *usb4_port_device_groups[] = {
239 static void usb4_port_device_release(struct device *dev)
241 struct usb4_port *usb4 = container_of(dev, struct usb4_port, dev);
246 struct device_type usb4_port_device_type = {
248 .groups = usb4_port_device_groups,
249 .release = usb4_port_device_release,
253 * usb4_port_device_add() - Add USB4 port device
254 * @port: Lane 0 adapter port to add the USB4 port
256 * Creates and registers a USB4 port device for @port. Returns the new
257 * USB4 port device pointer or ERR_PTR() in case of error.
259 struct usb4_port *usb4_port_device_add(struct tb_port *port)
261 struct usb4_port *usb4;
264 usb4 = kzalloc(sizeof(*usb4), GFP_KERNEL);
266 return ERR_PTR(-ENOMEM);
269 usb4->dev.type = &usb4_port_device_type;
270 usb4->dev.parent = &port->sw->dev;
271 dev_set_name(&usb4->dev, "usb4_port%d", port->port);
273 ret = device_register(&usb4->dev);
275 put_device(&usb4->dev);
279 if (dev_fwnode(&usb4->dev)) {
280 ret = component_add(&usb4->dev, &connector_ops);
282 dev_err(&usb4->dev, "failed to add component\n");
283 device_unregister(&usb4->dev);
287 if (!tb_is_upstream_port(port))
288 device_set_wakeup_capable(&usb4->dev, true);
290 pm_runtime_no_callbacks(&usb4->dev);
291 pm_runtime_set_active(&usb4->dev);
292 pm_runtime_enable(&usb4->dev);
293 pm_runtime_set_autosuspend_delay(&usb4->dev, TB_AUTOSUSPEND_DELAY);
294 pm_runtime_mark_last_busy(&usb4->dev);
295 pm_runtime_use_autosuspend(&usb4->dev);
301 * usb4_port_device_remove() - Removes USB4 port device
302 * @usb4: USB4 port device
304 * Unregisters the USB4 port device from the system. The device will be
305 * released when the last reference is dropped.
307 void usb4_port_device_remove(struct usb4_port *usb4)
309 if (dev_fwnode(&usb4->dev))
310 component_del(&usb4->dev, &connector_ops);
311 device_unregister(&usb4->dev);
315 * usb4_port_device_resume() - Resumes USB4 port device
316 * @usb4: USB4 port device
318 * Used to resume USB4 port device after sleep state.
320 int usb4_port_device_resume(struct usb4_port *usb4)
322 return usb4->offline ? usb4_port_offline(usb4) : 0;