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
64 #define PMC_USB_ALTMODE_ORI_AUX_SHIFT 4
65 #define PMC_USB_ALTMODE_ORI_HSL_SHIFT 5
67 /* DP specific Mode Data bits */
68 #define PMC_USB_ALTMODE_DP_MODE_SHIFT 8
70 /* TBT specific Mode Data bits */
71 #define PMC_USB_ALTMODE_HPD_HIGH BIT(14)
72 #define PMC_USB_ALTMODE_TBT_TYPE BIT(17)
73 #define PMC_USB_ALTMODE_CABLE_TYPE BIT(18)
74 #define PMC_USB_ALTMODE_ACTIVE_LINK BIT(20)
75 #define PMC_USB_ALTMODE_FORCE_LSR BIT(23)
76 #define PMC_USB_ALTMODE_CABLE_SPD(_s_) (((_s_) & GENMASK(2, 0)) << 25)
77 #define PMC_USB_ALTMODE_CABLE_USB31 1
78 #define PMC_USB_ALTMODE_CABLE_10GPS 2
79 #define PMC_USB_ALTMODE_CABLE_20GPS 3
80 #define PMC_USB_ALTMODE_TBT_GEN(_g_) (((_g_) & GENMASK(1, 0)) << 28)
82 /* Display HPD Request bits */
83 #define PMC_USB_DP_HPD_LVL BIT(4)
84 #define PMC_USB_DP_HPD_IRQ BIT(5)
91 struct typec_mux *typec_mux;
92 struct typec_switch *typec_sw;
93 struct usb_role_switch *usb_sw;
95 enum typec_orientation orientation;
101 enum typec_orientation sbu_orientation;
102 enum typec_orientation hsl_orientation;
108 struct intel_scu_ipc_dev *ipc;
109 struct pmc_usb_port *port;
112 static int sbu_orientation(struct pmc_usb_port *port)
114 if (port->sbu_orientation)
115 return port->sbu_orientation - 1;
117 return port->orientation - 1;
120 static int hsl_orientation(struct pmc_usb_port *port)
122 if (port->hsl_orientation)
123 return port->hsl_orientation - 1;
125 return port->orientation - 1;
128 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
133 * Error bit will always be 0 with the USBC command.
134 * Status can be checked from the response message.
136 intel_scu_ipc_dev_command(port->pmc->ipc, PMC_USBC_CMD, 0, msg, len,
137 response, sizeof(response));
138 if (response[2] & PMC_USB_RESP_STATUS_FAILURE) {
139 if (response[2] & PMC_USB_RESP_STATUS_FATAL)
148 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_mux_state *state)
150 struct typec_displayport_data *data = state->data;
153 msg[0] = PMC_USB_DP_HPD;
154 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
156 if (data->status & DP_STATUS_IRQ_HPD)
157 msg[1] = PMC_USB_DP_HPD_IRQ;
159 if (data->status & DP_STATUS_HPD_STATE)
160 msg[1] |= PMC_USB_DP_HPD_LVL;
162 return pmc_usb_command(port, msg, sizeof(msg));
166 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
168 struct typec_displayport_data *data = state->data;
169 struct altmode_req req = { };
172 if (data->status & DP_STATUS_IRQ_HPD)
173 return pmc_usb_mux_dp_hpd(port, state);
175 req.usage = PMC_USB_ALT_MODE;
176 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
177 req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
179 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
180 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
182 req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
183 req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
185 req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
186 PMC_USB_ALTMODE_DP_MODE_SHIFT;
188 if (data->status & DP_STATUS_HPD_STATE)
189 req.mode_data |= PMC_USB_ALTMODE_HPD_HIGH;
191 ret = pmc_usb_command(port, (void *)&req, sizeof(req));
195 if (data->status & DP_STATUS_HPD_STATE)
196 return pmc_usb_mux_dp_hpd(port, state);
202 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
204 struct typec_thunderbolt_data *data = state->data;
205 u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
206 struct altmode_req req = { };
208 req.usage = PMC_USB_ALT_MODE;
209 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
210 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
212 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
213 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
215 req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
216 req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
218 if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
219 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
221 if (data->cable_mode & TBT_CABLE_OPTICAL)
222 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
224 if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
225 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
227 if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
228 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
230 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
232 return pmc_usb_command(port, (void *)&req, sizeof(req));
236 pmc_usb_mux_usb4(struct pmc_usb_port *port, struct typec_mux_state *state)
238 struct enter_usb_data *data = state->data;
239 struct altmode_req req = { };
242 req.usage = PMC_USB_ALT_MODE;
243 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
244 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
247 req.mode_data = PMC_USB_ALTMODE_FORCE_LSR;
249 if (data->active_link_training)
250 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
252 req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
253 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
255 switch ((data->eudo & EUDO_CABLE_TYPE_MASK) >> EUDO_CABLE_TYPE_SHIFT) {
256 case EUDO_CABLE_TYPE_PASSIVE:
258 case EUDO_CABLE_TYPE_OPTICAL:
259 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
262 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
266 cable_speed = (data->eudo & EUDO_CABLE_SPEED_MASK) >> EUDO_CABLE_SPEED_SHIFT;
267 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
269 return pmc_usb_command(port, (void *)&req, sizeof(req));
272 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port)
276 msg = PMC_USB_SAFE_MODE;
277 msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
279 return pmc_usb_command(port, &msg, sizeof(msg));
282 static int pmc_usb_connect(struct pmc_usb_port *port)
286 msg[0] = PMC_USB_CONNECT;
287 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
289 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
290 msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
291 msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
293 return pmc_usb_command(port, msg, sizeof(msg));
296 static int pmc_usb_disconnect(struct pmc_usb_port *port)
300 msg[0] = PMC_USB_DISCONNECT;
301 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
303 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
305 return pmc_usb_command(port, msg, sizeof(msg));
309 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
311 struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
313 if (port->orientation == TYPEC_ORIENTATION_NONE || port->role == USB_ROLE_NONE)
316 if (state->mode == TYPEC_STATE_SAFE)
317 return pmc_usb_mux_safe_state(port);
318 if (state->mode == TYPEC_STATE_USB)
319 return pmc_usb_connect(port);
322 switch (state->alt->svid) {
323 case USB_TYPEC_TBT_SID:
324 return pmc_usb_mux_tbt(port, state);
325 case USB_TYPEC_DP_SID:
326 return pmc_usb_mux_dp(port, state);
329 switch (state->mode) {
330 case TYPEC_MODE_USB2:
331 /* REVISIT: Try with usb3_port set to 0? */
333 case TYPEC_MODE_USB3:
334 return pmc_usb_connect(port);
335 case TYPEC_MODE_USB4:
336 return pmc_usb_mux_usb4(port, state);
343 static int pmc_usb_set_orientation(struct typec_switch *sw,
344 enum typec_orientation orientation)
346 struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
348 if (port->orientation == orientation)
351 port->orientation = orientation;
354 if (orientation == TYPEC_ORIENTATION_NONE)
355 return pmc_usb_disconnect(port);
357 return pmc_usb_connect(port);
363 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
365 struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
367 if (port->role == role)
372 if (port->orientation) {
373 if (role == USB_ROLE_NONE)
374 return pmc_usb_disconnect(port);
376 return pmc_usb_connect(port);
382 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
383 struct fwnode_handle *fwnode)
385 struct pmc_usb_port *port = &pmc->port[index];
386 struct usb_role_switch_desc desc = { };
387 struct typec_switch_desc sw_desc = { };
388 struct typec_mux_desc mux_desc = { };
392 ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
396 ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
400 ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
402 port->sbu_orientation = typec_find_orientation(str);
404 ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
406 port->hsl_orientation = typec_find_orientation(str);
411 sw_desc.fwnode = fwnode;
412 sw_desc.drvdata = port;
413 sw_desc.name = fwnode_get_name(fwnode);
414 sw_desc.set = pmc_usb_set_orientation;
416 port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
417 if (IS_ERR(port->typec_sw))
418 return PTR_ERR(port->typec_sw);
420 mux_desc.fwnode = fwnode;
421 mux_desc.drvdata = port;
422 mux_desc.name = fwnode_get_name(fwnode);
423 mux_desc.set = pmc_usb_mux_set;
425 port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
426 if (IS_ERR(port->typec_mux)) {
427 ret = PTR_ERR(port->typec_mux);
428 goto err_unregister_switch;
431 desc.fwnode = fwnode;
432 desc.driver_data = port;
433 desc.name = fwnode_get_name(fwnode);
434 desc.set = pmc_usb_set_role;
436 port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
437 if (IS_ERR(port->usb_sw)) {
438 ret = PTR_ERR(port->usb_sw);
439 goto err_unregister_mux;
445 typec_mux_unregister(port->typec_mux);
447 err_unregister_switch:
448 typec_switch_unregister(port->typec_sw);
453 static int pmc_usb_probe(struct platform_device *pdev)
455 struct fwnode_handle *fwnode = NULL;
460 pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
464 device_for_each_child_node(&pdev->dev, fwnode)
467 pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
468 sizeof(struct pmc_usb_port), GFP_KERNEL);
472 pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
476 pmc->dev = &pdev->dev;
479 * For every physical USB connector (USB2 and USB3 combo) there is a
480 * child ACPI device node under the PMC mux ACPI device object.
482 for (i = 0; i < pmc->num_ports; i++) {
483 fwnode = device_get_next_child_node(pmc->dev, fwnode);
487 ret = pmc_usb_register_port(pmc, i, fwnode);
489 goto err_remove_ports;
492 platform_set_drvdata(pdev, pmc);
497 for (i = 0; i < pmc->num_ports; i++) {
498 typec_switch_unregister(pmc->port[i].typec_sw);
499 typec_mux_unregister(pmc->port[i].typec_mux);
505 static int pmc_usb_remove(struct platform_device *pdev)
507 struct pmc_usb *pmc = platform_get_drvdata(pdev);
510 for (i = 0; i < pmc->num_ports; i++) {
511 typec_switch_unregister(pmc->port[i].typec_sw);
512 typec_mux_unregister(pmc->port[i].typec_mux);
518 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
522 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
524 static struct platform_driver pmc_usb_driver = {
526 .name = "intel_pmc_usb",
527 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
529 .probe = pmc_usb_probe,
530 .remove = pmc_usb_remove,
533 module_platform_driver(pmc_usb_driver);
535 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
536 MODULE_LICENSE("GPL v2");
537 MODULE_DESCRIPTION("Intel PMC USB mux control");