1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Intel PMC USB mux control
5 * Copyright (C) 2020 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/usb/role.h>
14 #include <linux/usb/typec_mux.h>
15 #include <linux/usb/typec_dp.h>
16 #include <linux/usb/typec_tbt.h>
18 #include <asm/intel_pmc_ipc.h>
20 #define PMC_USBC_CMD 0xa7
22 /* "Usage" OOB Message field values */
31 #define PMC_USB_MSG_USB2_PORT_SHIFT 0
32 #define PMC_USB_MSG_USB3_PORT_SHIFT 4
33 #define PMC_USB_MSG_UFP_SHIFT 4
34 #define PMC_USB_MSG_ORI_HSL_SHIFT 5
35 #define PMC_USB_MSG_ORI_AUX_SHIFT 6
37 /* Alt Mode Request */
46 #define PMC_USB_MODE_TYPE_SHIFT 4
49 PMC_USB_MODE_TYPE_USB,
51 PMC_USB_MODE_TYPE_TBT,
54 /* Common Mode Data bits */
55 #define PMC_USB_ALTMODE_ACTIVE_CABLE BIT(2)
57 #define PMC_USB_ALTMODE_ORI_SHIFT 1
58 #define PMC_USB_ALTMODE_UFP_SHIFT 3
59 #define PMC_USB_ALTMODE_ORI_AUX_SHIFT 4
60 #define PMC_USB_ALTMODE_ORI_HSL_SHIFT 5
62 /* DP specific Mode Data bits */
63 #define PMC_USB_ALTMODE_DP_MODE_SHIFT 8
65 /* TBT specific Mode Data bits */
66 #define PMC_USB_ALTMODE_HPD_HIGH BIT(14)
67 #define PMC_USB_ALTMODE_TBT_TYPE BIT(17)
68 #define PMC_USB_ALTMODE_CABLE_TYPE BIT(18)
69 #define PMC_USB_ALTMODE_ACTIVE_LINK BIT(20)
70 #define PMC_USB_ALTMODE_FORCE_LSR BIT(23)
71 #define PMC_USB_ALTMODE_CABLE_SPD(_s_) (((_s_) & GENMASK(2, 0)) << 25)
72 #define PMC_USB_ALTMODE_CABLE_USB31 1
73 #define PMC_USB_ALTMODE_CABLE_10GPS 2
74 #define PMC_USB_ALTMODE_CABLE_20GPS 3
75 #define PMC_USB_ALTMODE_TBT_GEN(_g_) (((_g_) & GENMASK(1, 0)) << 28)
77 /* Display HPD Request bits */
78 #define PMC_USB_DP_HPD_LVL BIT(4)
79 #define PMC_USB_DP_HPD_IRQ BIT(5)
86 struct typec_mux *typec_mux;
87 struct typec_switch *typec_sw;
88 struct usb_role_switch *usb_sw;
90 enum typec_orientation orientation;
96 enum typec_orientation sbu_orientation;
97 enum typec_orientation hsl_orientation;
103 struct pmc_usb_port *port;
106 static int sbu_orientation(struct pmc_usb_port *port)
108 if (port->sbu_orientation)
109 return port->sbu_orientation - 1;
111 return port->orientation - 1;
114 static int hsl_orientation(struct pmc_usb_port *port)
116 if (port->hsl_orientation)
117 return port->hsl_orientation - 1;
119 return port->orientation - 1;
122 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
127 * Error bit will always be 0 with the USBC command.
128 * Status can be checked from the response message.
130 intel_pmc_ipc_command(PMC_USBC_CMD, 0, msg, len,
131 (void *)response, 1);
134 if (response[2] & BIT(1))
143 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_mux_state *state)
145 struct typec_displayport_data *data = state->data;
148 msg[0] = PMC_USB_DP_HPD;
149 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
151 msg[1] = PMC_USB_DP_HPD_IRQ;
153 if (data->status & DP_STATUS_HPD_STATE)
154 msg[1] |= PMC_USB_DP_HPD_LVL;
156 return pmc_usb_command(port, msg, sizeof(msg));
160 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
162 struct typec_displayport_data *data = state->data;
163 struct altmode_req req = { };
165 if (data->status & DP_STATUS_IRQ_HPD)
166 return pmc_usb_mux_dp_hpd(port, state);
168 req.usage = PMC_USB_ALT_MODE;
169 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
170 req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
172 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
173 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
175 req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
176 req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
178 req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
179 PMC_USB_ALTMODE_DP_MODE_SHIFT;
181 if (data->status & DP_STATUS_HPD_STATE)
182 req.mode_data |= PMC_USB_ALTMODE_HPD_HIGH;
184 return pmc_usb_command(port, (void *)&req, sizeof(req));
188 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
190 struct typec_thunderbolt_data *data = state->data;
191 u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
192 struct altmode_req req = { };
194 req.usage = PMC_USB_ALT_MODE;
195 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
196 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
198 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
199 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
201 req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
202 req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
204 if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
205 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
207 if (data->cable_mode & TBT_CABLE_OPTICAL)
208 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
210 if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
211 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
213 if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
214 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
216 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
218 return pmc_usb_command(port, (void *)&req, sizeof(req));
221 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port)
225 msg = PMC_USB_SAFE_MODE;
226 msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
228 return pmc_usb_command(port, &msg, sizeof(msg));
231 static int pmc_usb_connect(struct pmc_usb_port *port)
235 msg[0] = PMC_USB_CONNECT;
236 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
238 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
239 msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
240 msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
242 return pmc_usb_command(port, msg, sizeof(msg));
245 static int pmc_usb_disconnect(struct pmc_usb_port *port)
249 msg[0] = PMC_USB_DISCONNECT;
250 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
252 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
254 return pmc_usb_command(port, msg, sizeof(msg));
258 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
260 struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
265 if (state->mode == TYPEC_STATE_SAFE)
266 return pmc_usb_mux_safe_state(port);
268 switch (state->alt->svid) {
269 case USB_TYPEC_TBT_SID:
270 return pmc_usb_mux_tbt(port, state);
271 case USB_TYPEC_DP_SID:
272 return pmc_usb_mux_dp(port, state);
278 static int pmc_usb_set_orientation(struct typec_switch *sw,
279 enum typec_orientation orientation)
281 struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
283 if (port->orientation == orientation)
286 port->orientation = orientation;
289 if (orientation == TYPEC_ORIENTATION_NONE)
290 return pmc_usb_disconnect(port);
292 return pmc_usb_connect(port);
298 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
300 struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
302 if (port->role == role)
307 if (port->orientation) {
308 if (role == USB_ROLE_NONE)
309 return pmc_usb_disconnect(port);
311 return pmc_usb_connect(port);
317 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
318 struct fwnode_handle *fwnode)
320 struct pmc_usb_port *port = &pmc->port[index];
321 struct usb_role_switch_desc desc = { };
322 struct typec_switch_desc sw_desc = { };
323 struct typec_mux_desc mux_desc = { };
327 ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
331 ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
335 ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
337 port->sbu_orientation = typec_find_orientation(str);
339 ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
341 port->hsl_orientation = typec_find_orientation(str);
346 sw_desc.fwnode = fwnode;
347 sw_desc.drvdata = port;
348 sw_desc.name = fwnode_get_name(fwnode);
349 sw_desc.set = pmc_usb_set_orientation;
351 port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
352 if (IS_ERR(port->typec_sw))
353 return PTR_ERR(port->typec_sw);
355 mux_desc.fwnode = fwnode;
356 mux_desc.drvdata = port;
357 mux_desc.name = fwnode_get_name(fwnode);
358 mux_desc.set = pmc_usb_mux_set;
360 port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
361 if (IS_ERR(port->typec_mux)) {
362 ret = PTR_ERR(port->typec_mux);
363 goto err_unregister_switch;
366 desc.fwnode = fwnode;
367 desc.driver_data = port;
368 desc.name = fwnode_get_name(fwnode);
369 desc.set = pmc_usb_set_role;
371 port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
372 if (IS_ERR(port->usb_sw)) {
373 ret = PTR_ERR(port->usb_sw);
374 goto err_unregister_mux;
380 typec_mux_unregister(port->typec_mux);
382 err_unregister_switch:
383 typec_switch_unregister(port->typec_sw);
388 static int pmc_usb_probe(struct platform_device *pdev)
390 struct fwnode_handle *fwnode = NULL;
395 pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
399 device_for_each_child_node(&pdev->dev, fwnode)
402 pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
403 sizeof(struct pmc_usb_port), GFP_KERNEL);
407 pmc->dev = &pdev->dev;
410 * For every physical USB connector (USB2 and USB3 combo) there is a
411 * child ACPI device node under the PMC mux ACPI device object.
413 for (i = 0; i < pmc->num_ports; i++) {
414 fwnode = device_get_next_child_node(pmc->dev, fwnode);
418 ret = pmc_usb_register_port(pmc, i, fwnode);
420 goto err_remove_ports;
423 platform_set_drvdata(pdev, pmc);
428 for (i = 0; i < pmc->num_ports; i++) {
429 typec_switch_unregister(pmc->port[i].typec_sw);
430 typec_mux_unregister(pmc->port[i].typec_mux);
436 static int pmc_usb_remove(struct platform_device *pdev)
438 struct pmc_usb *pmc = platform_get_drvdata(pdev);
441 for (i = 0; i < pmc->num_ports; i++) {
442 typec_switch_unregister(pmc->port[i].typec_sw);
443 typec_mux_unregister(pmc->port[i].typec_mux);
449 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
453 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
455 static struct platform_driver pmc_usb_driver = {
457 .name = "intel_pmc_usb",
458 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
460 .probe = pmc_usb_probe,
461 .remove = pmc_usb_remove,
464 module_platform_driver(pmc_usb_driver);
466 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
467 MODULE_LICENSE("GPL v2");
468 MODULE_DESCRIPTION("Intel PMC USB mux control");