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/pd.h>
14 #include <linux/usb/role.h>
15 #include <linux/usb/typec_mux.h>
16 #include <linux/usb/typec_dp.h>
17 #include <linux/usb/typec_tbt.h>
19 #include <asm/intel_scu_ipc.h>
21 #define PMC_USBC_CMD 0xa7
23 /* Response status bits */
24 #define PMC_USB_RESP_STATUS_FAILURE BIT(0)
25 #define PMC_USB_RESP_STATUS_FATAL BIT(1)
27 /* "Usage" OOB Message field values */
36 #define PMC_USB_MSG_USB2_PORT_SHIFT 0
37 #define PMC_USB_MSG_USB3_PORT_SHIFT 4
38 #define PMC_USB_MSG_UFP_SHIFT 4
39 #define PMC_USB_MSG_ORI_HSL_SHIFT 5
40 #define PMC_USB_MSG_ORI_AUX_SHIFT 6
42 /* Alt Mode Request */
51 #define PMC_USB_MODE_TYPE_SHIFT 4
54 PMC_USB_MODE_TYPE_USB,
56 PMC_USB_MODE_TYPE_TBT,
59 /* Common Mode Data bits */
60 #define PMC_USB_ALTMODE_ACTIVE_CABLE BIT(2)
62 #define PMC_USB_ALTMODE_ORI_SHIFT 1
63 #define PMC_USB_ALTMODE_UFP_SHIFT 3
65 /* DP specific Mode Data bits */
66 #define PMC_USB_ALTMODE_DP_MODE_SHIFT 8
68 /* TBT specific Mode Data bits */
69 #define PMC_USB_ALTMODE_TBT_TYPE BIT(17)
70 #define PMC_USB_ALTMODE_CABLE_TYPE BIT(18)
71 #define PMC_USB_ALTMODE_ACTIVE_LINK BIT(20)
72 #define PMC_USB_ALTMODE_FORCE_LSR BIT(23)
73 #define PMC_USB_ALTMODE_CABLE_SPD(_s_) (((_s_) & GENMASK(2, 0)) << 25)
74 #define PMC_USB_ALTMODE_CABLE_USB31 1
75 #define PMC_USB_ALTMODE_CABLE_10GPS 2
76 #define PMC_USB_ALTMODE_CABLE_20GPS 3
77 #define PMC_USB_ALTMODE_TBT_GEN(_g_) (((_g_) & GENMASK(1, 0)) << 28)
79 /* Display HPD Request bits */
80 #define PMC_USB_DP_HPD_LVL BIT(4)
81 #define PMC_USB_DP_HPD_IRQ BIT(5)
84 * Input Output Manager (IOM) PORT STATUS
86 #define IOM_PORT_STATUS_OFFSET 0x560
88 #define IOM_PORT_STATUS_ACTIVITY_TYPE_MASK GENMASK(9, 6)
89 #define IOM_PORT_STATUS_ACTIVITY_TYPE_SHIFT 6
90 #define IOM_PORT_STATUS_ACTIVITY_TYPE_USB 0x03
91 /* activity type: Safe Mode */
92 #define IOM_PORT_STATUS_ACTIVITY_TYPE_SAFE_MODE 0x04
93 /* activity type: Display Port */
94 #define IOM_PORT_STATUS_ACTIVITY_TYPE_DP 0x05
95 /* activity type: Display Port Multi Function Device */
96 #define IOM_PORT_STATUS_ACTIVITY_TYPE_DP_MFD 0x06
97 /* activity type: Thunderbolt */
98 #define IOM_PORT_STATUS_ACTIVITY_TYPE_TBT 0x07
99 #define IOM_PORT_STATUS_ACTIVITY_TYPE_ALT_MODE_USB 0x0c
100 #define IOM_PORT_STATUS_ACTIVITY_TYPE_ALT_MODE_TBT_USB 0x0d
101 /* Upstream Facing Port Information */
102 #define IOM_PORT_STATUS_UFP BIT(10)
103 /* Display Port Hot Plug Detect status */
104 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_MASK GENMASK(13, 12)
105 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_SHIFT 12
106 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_ASSERT 0x01
107 #define IOM_PORT_STATUS_DHPD_HPD_SOURCE_TBT BIT(14)
108 #define IOM_PORT_STATUS_CONNECTED BIT(31)
110 #define IOM_PORT_ACTIVITY_IS(_status_, _type_) \
111 ((((_status_) & IOM_PORT_STATUS_ACTIVITY_TYPE_MASK) >> \
112 IOM_PORT_STATUS_ACTIVITY_TYPE_SHIFT) == \
113 (IOM_PORT_STATUS_ACTIVITY_TYPE_##_type_))
115 #define IOM_PORT_HPD_ASSERTED(_status_) \
116 ((((_status_) & IOM_PORT_STATUS_DHPD_HPD_STATUS_MASK) >> \
117 IOM_PORT_STATUS_DHPD_HPD_STATUS_SHIFT) & \
118 IOM_PORT_STATUS_DHPD_HPD_STATUS_ASSERT)
122 struct pmc_usb_port {
126 struct typec_mux *typec_mux;
127 struct typec_switch *typec_sw;
128 struct usb_role_switch *usb_sw;
130 enum typec_orientation orientation;
136 enum typec_orientation sbu_orientation;
137 enum typec_orientation hsl_orientation;
143 struct intel_scu_ipc_dev *ipc;
144 struct pmc_usb_port *port;
145 struct acpi_device *iom_adev;
146 void __iomem *iom_base;
149 static void update_port_status(struct pmc_usb_port *port)
153 /* SoC expects the USB Type-C port numbers to start with 0 */
154 port_num = port->usb3_port - 1;
156 port->iom_status = readl(port->pmc->iom_base + IOM_PORT_STATUS_OFFSET +
157 port_num * sizeof(u32));
160 static int sbu_orientation(struct pmc_usb_port *port)
162 if (port->sbu_orientation)
163 return port->sbu_orientation - 1;
165 return port->orientation - 1;
168 static int hsl_orientation(struct pmc_usb_port *port)
170 if (port->hsl_orientation)
171 return port->hsl_orientation - 1;
173 return port->orientation - 1;
176 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
183 * Error bit will always be 0 with the USBC command.
184 * Status can be checked from the response message if the
185 * function intel_scu_ipc_dev_command succeeds.
187 ret = intel_scu_ipc_dev_command(port->pmc->ipc, PMC_USBC_CMD, 0, msg,
188 len, response, sizeof(response));
193 status_res = (msg[0] & 0xf) < PMC_USB_SAFE_MODE ?
194 response[2] : response[1];
196 if (status_res & PMC_USB_RESP_STATUS_FAILURE) {
197 if (status_res & PMC_USB_RESP_STATUS_FATAL)
207 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_displayport_data *dp)
212 msg[0] = PMC_USB_DP_HPD;
213 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
215 /* Configure HPD first if HPD,IRQ comes together */
216 if (!IOM_PORT_HPD_ASSERTED(port->iom_status) &&
217 dp->status & DP_STATUS_IRQ_HPD &&
218 dp->status & DP_STATUS_HPD_STATE) {
219 msg[1] = PMC_USB_DP_HPD_LVL;
220 ret = pmc_usb_command(port, msg, sizeof(msg));
225 if (dp->status & DP_STATUS_IRQ_HPD)
226 msg[1] = PMC_USB_DP_HPD_IRQ;
228 if (dp->status & DP_STATUS_HPD_STATE)
229 msg[1] |= PMC_USB_DP_HPD_LVL;
231 return pmc_usb_command(port, msg, sizeof(msg));
235 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
237 struct typec_displayport_data *data = state->data;
238 struct altmode_req req = { };
241 if (IOM_PORT_ACTIVITY_IS(port->iom_status, DP) ||
242 IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) {
243 if (IOM_PORT_HPD_ASSERTED(port->iom_status) &&
244 (!(data->status & DP_STATUS_IRQ_HPD) &&
245 data->status & DP_STATUS_HPD_STATE))
248 return pmc_usb_mux_dp_hpd(port, state->data);
251 req.usage = PMC_USB_ALT_MODE;
252 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
253 req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
255 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
256 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
258 req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
259 PMC_USB_ALTMODE_DP_MODE_SHIFT;
261 ret = pmc_usb_command(port, (void *)&req, sizeof(req));
265 if (data->status & (DP_STATUS_IRQ_HPD | DP_STATUS_HPD_STATE))
266 return pmc_usb_mux_dp_hpd(port, state->data);
272 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
274 struct typec_thunderbolt_data *data = state->data;
275 u8 cable_rounded = TBT_CABLE_ROUNDED_SUPPORT(data->cable_mode);
276 u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
277 struct altmode_req req = { };
279 if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
280 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB))
283 req.usage = PMC_USB_ALT_MODE;
284 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
285 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
287 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
288 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
290 if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
291 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
293 if (data->cable_mode & TBT_CABLE_OPTICAL)
294 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
296 if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
297 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
299 if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
300 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
302 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
304 req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(cable_rounded);
306 return pmc_usb_command(port, (void *)&req, sizeof(req));
310 pmc_usb_mux_usb4(struct pmc_usb_port *port, struct typec_mux_state *state)
312 struct enter_usb_data *data = state->data;
313 struct altmode_req req = { };
316 if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
317 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB))
320 req.usage = PMC_USB_ALT_MODE;
321 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
322 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
325 req.mode_data = PMC_USB_ALTMODE_FORCE_LSR;
327 if (data->active_link_training)
328 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
330 req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
331 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
333 switch ((data->eudo & EUDO_CABLE_TYPE_MASK) >> EUDO_CABLE_TYPE_SHIFT) {
334 case EUDO_CABLE_TYPE_PASSIVE:
336 case EUDO_CABLE_TYPE_OPTICAL:
337 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
340 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
342 /* Configure data rate to rounded in the case of Active TBT3
345 req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(1);
349 cable_speed = (data->eudo & EUDO_CABLE_SPEED_MASK) >> EUDO_CABLE_SPEED_SHIFT;
350 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
352 return pmc_usb_command(port, (void *)&req, sizeof(req));
355 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port)
359 if (IOM_PORT_ACTIVITY_IS(port->iom_status, SAFE_MODE))
362 msg = PMC_USB_SAFE_MODE;
363 msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
365 return pmc_usb_command(port, &msg, sizeof(msg));
368 static int pmc_usb_disconnect(struct pmc_usb_port *port)
370 struct typec_displayport_data data = { };
373 if (!(port->iom_status & IOM_PORT_STATUS_CONNECTED))
376 /* Clear DisplayPort HPD if it's still asserted. */
377 if (IOM_PORT_HPD_ASSERTED(port->iom_status))
378 pmc_usb_mux_dp_hpd(port, &data);
380 msg[0] = PMC_USB_DISCONNECT;
381 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
383 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
385 return pmc_usb_command(port, msg, sizeof(msg));
388 static int pmc_usb_connect(struct pmc_usb_port *port, enum usb_role role)
390 u8 ufp = role == USB_ROLE_DEVICE ? 1 : 0;
394 if (port->orientation == TYPEC_ORIENTATION_NONE)
397 if (port->iom_status & IOM_PORT_STATUS_CONNECTED) {
398 if (port->role == role || port->role == USB_ROLE_NONE)
402 ret = pmc_usb_disconnect(port);
407 msg[0] = PMC_USB_CONNECT;
408 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
410 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
411 msg[1] |= ufp << PMC_USB_MSG_UFP_SHIFT;
412 msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
413 msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
415 return pmc_usb_command(port, msg, sizeof(msg));
419 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
421 struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
423 update_port_status(port);
425 if (port->orientation == TYPEC_ORIENTATION_NONE || port->role == USB_ROLE_NONE)
428 if (state->mode == TYPEC_STATE_SAFE)
429 return pmc_usb_mux_safe_state(port);
430 if (state->mode == TYPEC_STATE_USB)
431 return pmc_usb_connect(port, port->role);
434 switch (state->alt->svid) {
435 case USB_TYPEC_TBT_SID:
436 return pmc_usb_mux_tbt(port, state);
437 case USB_TYPEC_DP_SID:
438 return pmc_usb_mux_dp(port, state);
441 switch (state->mode) {
442 case TYPEC_MODE_USB2:
443 /* REVISIT: Try with usb3_port set to 0? */
445 case TYPEC_MODE_USB3:
446 return pmc_usb_connect(port, port->role);
447 case TYPEC_MODE_USB4:
448 return pmc_usb_mux_usb4(port, state);
455 static int pmc_usb_set_orientation(struct typec_switch *sw,
456 enum typec_orientation orientation)
458 struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
460 update_port_status(port);
462 port->orientation = orientation;
467 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
469 struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
472 update_port_status(port);
474 if (role == USB_ROLE_NONE)
475 ret = pmc_usb_disconnect(port);
477 ret = pmc_usb_connect(port, role);
484 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
485 struct fwnode_handle *fwnode)
487 struct pmc_usb_port *port = &pmc->port[index];
488 struct usb_role_switch_desc desc = { };
489 struct typec_switch_desc sw_desc = { };
490 struct typec_mux_desc mux_desc = { };
494 ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
498 ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
502 ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
504 port->sbu_orientation = typec_find_orientation(str);
506 ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
508 port->hsl_orientation = typec_find_orientation(str);
513 sw_desc.fwnode = fwnode;
514 sw_desc.drvdata = port;
515 sw_desc.name = fwnode_get_name(fwnode);
516 sw_desc.set = pmc_usb_set_orientation;
518 port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
519 if (IS_ERR(port->typec_sw))
520 return PTR_ERR(port->typec_sw);
522 mux_desc.fwnode = fwnode;
523 mux_desc.drvdata = port;
524 mux_desc.name = fwnode_get_name(fwnode);
525 mux_desc.set = pmc_usb_mux_set;
527 port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
528 if (IS_ERR(port->typec_mux)) {
529 ret = PTR_ERR(port->typec_mux);
530 goto err_unregister_switch;
533 desc.fwnode = fwnode;
534 desc.driver_data = port;
535 desc.name = fwnode_get_name(fwnode);
536 desc.set = pmc_usb_set_role;
538 port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
539 if (IS_ERR(port->usb_sw)) {
540 ret = PTR_ERR(port->usb_sw);
541 goto err_unregister_mux;
547 typec_mux_unregister(port->typec_mux);
549 err_unregister_switch:
550 typec_switch_unregister(port->typec_sw);
555 static int is_memory(struct acpi_resource *res, void *data)
559 return !acpi_dev_resource_memory(res, &r);
562 static int pmc_usb_probe_iom(struct pmc_usb *pmc)
564 struct list_head resource_list;
565 struct resource_entry *rentry;
566 struct acpi_device *adev;
569 adev = acpi_dev_get_first_match_dev("INTC1072", NULL, -1);
573 INIT_LIST_HEAD(&resource_list);
574 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
578 rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
580 pmc->iom_base = devm_ioremap_resource(pmc->dev, rentry->res);
582 acpi_dev_free_resource_list(&resource_list);
584 if (!pmc->iom_base) {
589 if (IS_ERR(pmc->iom_base)) {
591 return PTR_ERR(pmc->iom_base);
594 pmc->iom_adev = adev;
599 static int pmc_usb_probe(struct platform_device *pdev)
601 struct fwnode_handle *fwnode = NULL;
606 pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
610 device_for_each_child_node(&pdev->dev, fwnode)
613 /* The IOM microcontroller has a limitation of max 4 ports. */
614 if (pmc->num_ports > 4) {
615 dev_err(&pdev->dev, "driver limited to 4 ports\n");
619 pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
620 sizeof(struct pmc_usb_port), GFP_KERNEL);
624 pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
628 pmc->dev = &pdev->dev;
630 ret = pmc_usb_probe_iom(pmc);
635 * For every physical USB connector (USB2 and USB3 combo) there is a
636 * child ACPI device node under the PMC mux ACPI device object.
638 for (i = 0; i < pmc->num_ports; i++) {
639 fwnode = device_get_next_child_node(pmc->dev, fwnode);
643 ret = pmc_usb_register_port(pmc, i, fwnode);
645 fwnode_handle_put(fwnode);
646 goto err_remove_ports;
650 platform_set_drvdata(pdev, pmc);
655 for (i = 0; i < pmc->num_ports; i++) {
656 typec_switch_unregister(pmc->port[i].typec_sw);
657 typec_mux_unregister(pmc->port[i].typec_mux);
658 usb_role_switch_unregister(pmc->port[i].usb_sw);
661 acpi_dev_put(pmc->iom_adev);
666 static int pmc_usb_remove(struct platform_device *pdev)
668 struct pmc_usb *pmc = platform_get_drvdata(pdev);
671 for (i = 0; i < pmc->num_ports; i++) {
672 typec_switch_unregister(pmc->port[i].typec_sw);
673 typec_mux_unregister(pmc->port[i].typec_mux);
674 usb_role_switch_unregister(pmc->port[i].usb_sw);
677 acpi_dev_put(pmc->iom_adev);
682 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
686 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
688 static struct platform_driver pmc_usb_driver = {
690 .name = "intel_pmc_usb",
691 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
693 .probe = pmc_usb_probe,
694 .remove = pmc_usb_remove,
697 module_platform_driver(pmc_usb_driver);
699 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
700 MODULE_LICENSE("GPL v2");
701 MODULE_DESCRIPTION("Intel PMC USB mux control");