1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for Intel PMC USB mux control 4 * 5 * Copyright (C) 2020 Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 */ 8 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> 18 #include <linux/debugfs.h> 19 #include <linux/usb.h> 20 21 #include <asm/intel_scu_ipc.h> 22 23 #define PMC_USBC_CMD 0xa7 24 25 /* Response status bits */ 26 #define PMC_USB_RESP_STATUS_FAILURE BIT(0) 27 #define PMC_USB_RESP_STATUS_FATAL BIT(1) 28 29 /* "Usage" OOB Message field values */ 30 enum { 31 PMC_USB_CONNECT, 32 PMC_USB_DISCONNECT, 33 PMC_USB_SAFE_MODE, 34 PMC_USB_ALT_MODE, 35 PMC_USB_DP_HPD, 36 }; 37 38 #define PMC_USB_MSG_USB2_PORT_SHIFT 0 39 #define PMC_USB_MSG_USB3_PORT_SHIFT 4 40 #define PMC_USB_MSG_UFP_SHIFT 4 41 #define PMC_USB_MSG_ORI_HSL_SHIFT 5 42 #define PMC_USB_MSG_ORI_AUX_SHIFT 6 43 44 /* Alt Mode Request */ 45 struct altmode_req { 46 u8 usage; 47 u8 mode_type; 48 u8 mode_id; 49 u8 reserved; 50 u32 mode_data; 51 } __packed; 52 53 #define PMC_USB_MODE_TYPE_SHIFT 4 54 55 enum { 56 PMC_USB_MODE_TYPE_USB, 57 PMC_USB_MODE_TYPE_DP, 58 PMC_USB_MODE_TYPE_TBT, 59 }; 60 61 /* Common Mode Data bits */ 62 #define PMC_USB_ALTMODE_RETIMER_CABLE BIT(2) 63 64 #define PMC_USB_ALTMODE_ORI_SHIFT 1 65 #define PMC_USB_ALTMODE_UFP_SHIFT 3 66 67 /* DP specific Mode Data bits */ 68 #define PMC_USB_ALTMODE_DP_MODE_SHIFT 8 69 70 /* TBT specific Mode Data bits */ 71 #define PMC_USB_ALTMODE_TBT_TYPE BIT(17) 72 #define PMC_USB_ALTMODE_CABLE_TYPE BIT(18) 73 #define PMC_USB_ALTMODE_ACTIVE_LINK BIT(20) 74 #define PMC_USB_ALTMODE_ACTIVE_CABLE BIT(22) 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) 81 82 /* Display HPD Request bits */ 83 #define PMC_USB_DP_HPD_LVL BIT(4) 84 #define PMC_USB_DP_HPD_IRQ BIT(5) 85 86 /* 87 * Input Output Manager (IOM) PORT STATUS 88 */ 89 #define IOM_PORT_STATUS_ACTIVITY_TYPE_MASK GENMASK(9, 6) 90 #define IOM_PORT_STATUS_ACTIVITY_TYPE_SHIFT 6 91 #define IOM_PORT_STATUS_ACTIVITY_TYPE_USB 0x03 92 /* activity type: Safe Mode */ 93 #define IOM_PORT_STATUS_ACTIVITY_TYPE_SAFE_MODE 0x04 94 /* activity type: Display Port */ 95 #define IOM_PORT_STATUS_ACTIVITY_TYPE_DP 0x05 96 /* activity type: Display Port Multi Function Device */ 97 #define IOM_PORT_STATUS_ACTIVITY_TYPE_DP_MFD 0x06 98 /* activity type: Thunderbolt */ 99 #define IOM_PORT_STATUS_ACTIVITY_TYPE_TBT 0x07 100 #define IOM_PORT_STATUS_ACTIVITY_TYPE_ALT_MODE_USB 0x0c 101 #define IOM_PORT_STATUS_ACTIVITY_TYPE_ALT_MODE_TBT_USB 0x0d 102 /* Upstream Facing Port Information */ 103 #define IOM_PORT_STATUS_UFP BIT(10) 104 /* Display Port Hot Plug Detect status */ 105 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_MASK GENMASK(13, 12) 106 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_SHIFT 12 107 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_ASSERT 0x01 108 #define IOM_PORT_STATUS_DHPD_HPD_SOURCE_TBT BIT(14) 109 #define IOM_PORT_STATUS_CONNECTED BIT(31) 110 111 #define IOM_PORT_ACTIVITY_IS(_status_, _type_) \ 112 ((((_status_) & IOM_PORT_STATUS_ACTIVITY_TYPE_MASK) >> \ 113 IOM_PORT_STATUS_ACTIVITY_TYPE_SHIFT) == \ 114 (IOM_PORT_STATUS_ACTIVITY_TYPE_##_type_)) 115 116 #define IOM_PORT_HPD_ASSERTED(_status_) \ 117 ((((_status_) & IOM_PORT_STATUS_DHPD_HPD_STATUS_MASK) >> \ 118 IOM_PORT_STATUS_DHPD_HPD_STATUS_SHIFT) & \ 119 IOM_PORT_STATUS_DHPD_HPD_STATUS_ASSERT) 120 121 /* IOM port status register */ 122 #define IOM_PORT_STATUS_REGS(_offset_, _size_) ((_offset_) | (_size_)) 123 #define IOM_PORT_STATUS_REGS_SZ_MASK BIT(0) 124 #define IOM_PORT_STATUS_REGS_SZ_4 0 125 #define IOM_PORT_STATUS_REGS_SZ_8 1 126 #define IOM_PORT_STATUS_REGS_OFFSET(_d_) \ 127 ((_d_) & ~IOM_PORT_STATUS_REGS_SZ_MASK) 128 #define IOM_PORT_STATUS_REGS_SIZE(_d_) \ 129 (4 << ((_d_) & IOM_PORT_STATUS_REGS_SZ_MASK)) 130 131 struct pmc_usb; 132 133 struct pmc_usb_port { 134 int num; 135 u32 iom_status; 136 struct pmc_usb *pmc; 137 struct typec_mux_dev *typec_mux; 138 struct typec_switch_dev *typec_sw; 139 struct usb_role_switch *usb_sw; 140 141 enum typec_orientation orientation; 142 enum usb_role role; 143 144 u8 usb2_port; 145 u8 usb3_port; 146 147 enum typec_orientation sbu_orientation; 148 enum typec_orientation hsl_orientation; 149 }; 150 151 struct pmc_usb { 152 u8 num_ports; 153 struct device *dev; 154 struct intel_scu_ipc_dev *ipc; 155 struct pmc_usb_port *port; 156 struct acpi_device *iom_adev; 157 void __iomem *iom_base; 158 u32 iom_port_status_offset; 159 u8 iom_port_status_size; 160 161 struct dentry *dentry; 162 }; 163 164 static struct dentry *pmc_mux_debugfs_root; 165 166 static void update_port_status(struct pmc_usb_port *port) 167 { 168 u8 port_num; 169 170 /* SoC expects the USB Type-C port numbers to start with 0 */ 171 port_num = port->usb3_port - 1; 172 173 port->iom_status = readl(port->pmc->iom_base + 174 port->pmc->iom_port_status_offset + 175 port_num * port->pmc->iom_port_status_size); 176 } 177 178 static int sbu_orientation(struct pmc_usb_port *port) 179 { 180 if (port->sbu_orientation) 181 return port->sbu_orientation - 1; 182 183 return port->orientation - 1; 184 } 185 186 static int hsl_orientation(struct pmc_usb_port *port) 187 { 188 if (port->hsl_orientation) 189 return port->hsl_orientation - 1; 190 191 return port->orientation - 1; 192 } 193 194 static int pmc_usb_send_command(struct intel_scu_ipc_dev *ipc, u8 *msg, u32 len) 195 { 196 u8 response[4]; 197 u8 status_res; 198 int ret; 199 200 /* 201 * Error bit will always be 0 with the USBC command. 202 * Status can be checked from the response message if the 203 * function intel_scu_ipc_dev_command succeeds. 204 */ 205 ret = intel_scu_ipc_dev_command(ipc, PMC_USBC_CMD, 0, msg, 206 len, response, sizeof(response)); 207 208 if (ret) 209 return ret; 210 211 status_res = (msg[0] & 0xf) < PMC_USB_SAFE_MODE ? 212 response[2] : response[1]; 213 214 if (status_res & PMC_USB_RESP_STATUS_FAILURE) { 215 if (status_res & PMC_USB_RESP_STATUS_FATAL) 216 return -EIO; 217 218 return -EBUSY; 219 } 220 221 return 0; 222 } 223 224 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len) 225 { 226 int retry_count = 3; 227 int ret; 228 229 /* 230 * If PMC is busy then retry the command once again 231 */ 232 while (retry_count--) { 233 ret = pmc_usb_send_command(port->pmc->ipc, msg, len); 234 if (ret != -EBUSY) 235 break; 236 } 237 238 return ret; 239 } 240 241 static int 242 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_displayport_data *dp) 243 { 244 u8 msg[2] = { }; 245 int ret; 246 247 msg[0] = PMC_USB_DP_HPD; 248 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 249 250 /* Configure HPD first if HPD,IRQ comes together */ 251 if (!IOM_PORT_HPD_ASSERTED(port->iom_status) && 252 dp->status & DP_STATUS_IRQ_HPD && 253 dp->status & DP_STATUS_HPD_STATE) { 254 msg[1] = PMC_USB_DP_HPD_LVL; 255 ret = pmc_usb_command(port, msg, sizeof(msg)); 256 if (ret) 257 return ret; 258 } 259 260 if (dp->status & DP_STATUS_IRQ_HPD) 261 msg[1] = PMC_USB_DP_HPD_IRQ; 262 263 if (dp->status & DP_STATUS_HPD_STATE) 264 msg[1] |= PMC_USB_DP_HPD_LVL; 265 266 return pmc_usb_command(port, msg, sizeof(msg)); 267 } 268 269 static int 270 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state) 271 { 272 struct typec_displayport_data *data = state->data; 273 struct altmode_req req = { }; 274 int ret; 275 276 if (IOM_PORT_ACTIVITY_IS(port->iom_status, DP) || 277 IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) { 278 if (IOM_PORT_HPD_ASSERTED(port->iom_status) && 279 (!(data->status & DP_STATUS_IRQ_HPD) && 280 data->status & DP_STATUS_HPD_STATE)) 281 return 0; 282 283 return pmc_usb_mux_dp_hpd(port, state->data); 284 } 285 286 req.usage = PMC_USB_ALT_MODE; 287 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 288 req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT; 289 290 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT; 291 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT; 292 293 req.mode_data |= (state->mode - TYPEC_STATE_MODAL) << 294 PMC_USB_ALTMODE_DP_MODE_SHIFT; 295 296 ret = pmc_usb_command(port, (void *)&req, sizeof(req)); 297 if (ret) 298 return ret; 299 300 if (data->status & (DP_STATUS_IRQ_HPD | DP_STATUS_HPD_STATE)) 301 return pmc_usb_mux_dp_hpd(port, state->data); 302 303 return 0; 304 } 305 306 static int 307 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state) 308 { 309 struct typec_thunderbolt_data *data = state->data; 310 u8 cable_rounded = TBT_CABLE_ROUNDED_SUPPORT(data->cable_mode); 311 u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode); 312 struct altmode_req req = { }; 313 314 if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) || 315 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB)) 316 return 0; 317 318 req.usage = PMC_USB_ALT_MODE; 319 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 320 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT; 321 322 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT; 323 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT; 324 325 if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3) 326 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE; 327 328 if (data->cable_mode & TBT_CABLE_OPTICAL) 329 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE; 330 331 if (data->cable_mode & TBT_CABLE_LINK_TRAINING) 332 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK; 333 334 if (acpi_dev_hid_uid_match(port->pmc->iom_adev, "INTC1072", NULL) || 335 acpi_dev_hid_uid_match(port->pmc->iom_adev, "INTC1079", NULL)) { 336 if ((data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE) || 337 (data->cable_mode & TBT_CABLE_RETIMER)) 338 req.mode_data |= PMC_USB_ALTMODE_RETIMER_CABLE; 339 } else { 340 if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE) 341 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE; 342 343 if (data->cable_mode & TBT_CABLE_RETIMER) 344 req.mode_data |= PMC_USB_ALTMODE_RETIMER_CABLE; 345 } 346 347 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed); 348 349 req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(cable_rounded); 350 351 return pmc_usb_command(port, (void *)&req, sizeof(req)); 352 } 353 354 static int 355 pmc_usb_mux_usb4(struct pmc_usb_port *port, struct typec_mux_state *state) 356 { 357 struct enter_usb_data *data = state->data; 358 struct altmode_req req = { }; 359 u8 cable_speed; 360 361 if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) || 362 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB)) 363 return 0; 364 365 req.usage = PMC_USB_ALT_MODE; 366 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 367 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT; 368 369 /* USB4 Mode */ 370 req.mode_data = PMC_USB_ALTMODE_FORCE_LSR; 371 372 if (data->active_link_training) 373 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK; 374 375 req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT; 376 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT; 377 378 switch ((data->eudo & EUDO_CABLE_TYPE_MASK) >> EUDO_CABLE_TYPE_SHIFT) { 379 case EUDO_CABLE_TYPE_PASSIVE: 380 break; 381 case EUDO_CABLE_TYPE_OPTICAL: 382 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE; 383 fallthrough; 384 case EUDO_CABLE_TYPE_RE_TIMER: 385 if (!acpi_dev_hid_uid_match(port->pmc->iom_adev, "INTC1072", NULL) || 386 !acpi_dev_hid_uid_match(port->pmc->iom_adev, "INTC1079", NULL)) 387 req.mode_data |= PMC_USB_ALTMODE_RETIMER_CABLE; 388 fallthrough; 389 default: 390 if (acpi_dev_hid_uid_match(port->pmc->iom_adev, "INTC1072", NULL) || 391 acpi_dev_hid_uid_match(port->pmc->iom_adev, "INTC1079", NULL)) 392 req.mode_data |= PMC_USB_ALTMODE_RETIMER_CABLE; 393 else 394 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE; 395 396 /* Configure data rate to rounded in the case of Active TBT3 397 * and USB4 cables. 398 */ 399 req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(1); 400 break; 401 } 402 403 cable_speed = (data->eudo & EUDO_CABLE_SPEED_MASK) >> EUDO_CABLE_SPEED_SHIFT; 404 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed); 405 406 return pmc_usb_command(port, (void *)&req, sizeof(req)); 407 } 408 409 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port, 410 struct typec_mux_state *state) 411 { 412 u8 msg; 413 414 if (IOM_PORT_ACTIVITY_IS(port->iom_status, SAFE_MODE)) 415 return 0; 416 417 if ((IOM_PORT_ACTIVITY_IS(port->iom_status, DP) || 418 IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) && 419 state->alt && state->alt->svid == USB_TYPEC_DP_SID) 420 return 0; 421 422 if ((IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) || 423 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB)) && 424 state->alt && state->alt->svid == USB_TYPEC_TBT_SID) 425 return 0; 426 427 msg = PMC_USB_SAFE_MODE; 428 msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 429 430 return pmc_usb_command(port, &msg, sizeof(msg)); 431 } 432 433 static int pmc_usb_disconnect(struct pmc_usb_port *port) 434 { 435 struct typec_displayport_data data = { }; 436 u8 msg[2]; 437 438 if (!(port->iom_status & IOM_PORT_STATUS_CONNECTED)) 439 return 0; 440 441 /* Clear DisplayPort HPD if it's still asserted. */ 442 if (IOM_PORT_HPD_ASSERTED(port->iom_status)) 443 pmc_usb_mux_dp_hpd(port, &data); 444 445 msg[0] = PMC_USB_DISCONNECT; 446 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 447 448 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT; 449 450 return pmc_usb_command(port, msg, sizeof(msg)); 451 } 452 453 static int pmc_usb_connect(struct pmc_usb_port *port, enum usb_role role) 454 { 455 u8 ufp = role == USB_ROLE_DEVICE ? 1 : 0; 456 u8 msg[2]; 457 int ret; 458 459 if (port->orientation == TYPEC_ORIENTATION_NONE) 460 return -EINVAL; 461 462 if (port->iom_status & IOM_PORT_STATUS_CONNECTED) { 463 if (port->role == role || port->role == USB_ROLE_NONE) 464 return 0; 465 466 /* Role swap */ 467 ret = pmc_usb_disconnect(port); 468 if (ret) 469 return ret; 470 } 471 472 msg[0] = PMC_USB_CONNECT; 473 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT; 474 475 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT; 476 msg[1] |= ufp << PMC_USB_MSG_UFP_SHIFT; 477 msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT; 478 msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT; 479 480 return pmc_usb_command(port, msg, sizeof(msg)); 481 } 482 483 static int 484 pmc_usb_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state) 485 { 486 struct pmc_usb_port *port = typec_mux_get_drvdata(mux); 487 488 update_port_status(port); 489 490 if (port->orientation == TYPEC_ORIENTATION_NONE || port->role == USB_ROLE_NONE) 491 return 0; 492 493 if (state->mode == TYPEC_STATE_SAFE) 494 return pmc_usb_mux_safe_state(port, state); 495 if (state->mode == TYPEC_STATE_USB) 496 return pmc_usb_connect(port, port->role); 497 498 if (state->alt) { 499 switch (state->alt->svid) { 500 case USB_TYPEC_TBT_SID: 501 return pmc_usb_mux_tbt(port, state); 502 case USB_TYPEC_DP_SID: 503 return pmc_usb_mux_dp(port, state); 504 } 505 } else { 506 switch (state->mode) { 507 case TYPEC_MODE_USB2: 508 /* REVISIT: Try with usb3_port set to 0? */ 509 break; 510 case TYPEC_MODE_USB3: 511 return pmc_usb_connect(port, port->role); 512 case TYPEC_MODE_USB4: 513 return pmc_usb_mux_usb4(port, state); 514 } 515 } 516 517 return -EOPNOTSUPP; 518 } 519 520 static int pmc_usb_set_orientation(struct typec_switch_dev *sw, 521 enum typec_orientation orientation) 522 { 523 struct pmc_usb_port *port = typec_switch_get_drvdata(sw); 524 525 update_port_status(port); 526 527 port->orientation = orientation; 528 529 return 0; 530 } 531 532 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role) 533 { 534 struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw); 535 int ret; 536 537 update_port_status(port); 538 539 if (role == USB_ROLE_NONE) 540 ret = pmc_usb_disconnect(port); 541 else 542 ret = pmc_usb_connect(port, role); 543 544 port->role = role; 545 546 return ret; 547 } 548 549 static int pmc_usb_register_port(struct pmc_usb *pmc, int index, 550 struct fwnode_handle *fwnode) 551 { 552 struct pmc_usb_port *port = &pmc->port[index]; 553 struct usb_role_switch_desc desc = { }; 554 struct typec_switch_desc sw_desc = { }; 555 struct typec_mux_desc mux_desc = { }; 556 const char *str; 557 int ret; 558 559 ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port); 560 if (ret) 561 return ret; 562 563 ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port); 564 if (ret) 565 return ret; 566 567 ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str); 568 if (!ret) 569 port->sbu_orientation = typec_find_orientation(str); 570 571 ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str); 572 if (!ret) 573 port->hsl_orientation = typec_find_orientation(str); 574 575 port->num = index; 576 port->pmc = pmc; 577 578 sw_desc.fwnode = fwnode; 579 sw_desc.drvdata = port; 580 sw_desc.name = fwnode_get_name(fwnode); 581 sw_desc.set = pmc_usb_set_orientation; 582 583 port->typec_sw = typec_switch_register(pmc->dev, &sw_desc); 584 if (IS_ERR(port->typec_sw)) 585 return PTR_ERR(port->typec_sw); 586 587 mux_desc.fwnode = fwnode; 588 mux_desc.drvdata = port; 589 mux_desc.name = fwnode_get_name(fwnode); 590 mux_desc.set = pmc_usb_mux_set; 591 592 port->typec_mux = typec_mux_register(pmc->dev, &mux_desc); 593 if (IS_ERR(port->typec_mux)) { 594 ret = PTR_ERR(port->typec_mux); 595 goto err_unregister_switch; 596 } 597 598 desc.fwnode = fwnode; 599 desc.driver_data = port; 600 desc.name = fwnode_get_name(fwnode); 601 desc.set = pmc_usb_set_role; 602 603 port->usb_sw = usb_role_switch_register(pmc->dev, &desc); 604 if (IS_ERR(port->usb_sw)) { 605 ret = PTR_ERR(port->usb_sw); 606 goto err_unregister_mux; 607 } 608 609 return 0; 610 611 err_unregister_mux: 612 typec_mux_unregister(port->typec_mux); 613 614 err_unregister_switch: 615 typec_switch_unregister(port->typec_sw); 616 617 return ret; 618 } 619 620 /* IOM ACPI IDs and IOM_PORT_STATUS_OFFSET */ 621 static const struct acpi_device_id iom_acpi_ids[] = { 622 /* TigerLake */ 623 { "INTC1072", IOM_PORT_STATUS_REGS(0x560, IOM_PORT_STATUS_REGS_SZ_4) }, 624 625 /* AlderLake */ 626 { "INTC1079", IOM_PORT_STATUS_REGS(0x160, IOM_PORT_STATUS_REGS_SZ_4) }, 627 628 /* Meteor Lake */ 629 { "INTC107A", IOM_PORT_STATUS_REGS(0x160, IOM_PORT_STATUS_REGS_SZ_4) }, 630 631 /* Lunar Lake */ 632 { "INTC10EA", IOM_PORT_STATUS_REGS(0x150, IOM_PORT_STATUS_REGS_SZ_8) }, 633 {} 634 }; 635 636 static int pmc_usb_probe_iom(struct pmc_usb *pmc) 637 { 638 struct list_head resource_list; 639 struct resource_entry *rentry; 640 static const struct acpi_device_id *dev_id; 641 struct acpi_device *adev = NULL; 642 int ret; 643 644 for (dev_id = &iom_acpi_ids[0]; dev_id->id[0]; dev_id++) { 645 adev = acpi_dev_get_first_match_dev(dev_id->id, NULL, -1); 646 if (adev) 647 break; 648 } 649 if (!adev) 650 return -ENODEV; 651 652 pmc->iom_port_status_offset = IOM_PORT_STATUS_REGS_OFFSET(dev_id->driver_data); 653 pmc->iom_port_status_size = IOM_PORT_STATUS_REGS_SIZE(dev_id->driver_data); 654 655 INIT_LIST_HEAD(&resource_list); 656 ret = acpi_dev_get_memory_resources(adev, &resource_list); 657 if (ret < 0) { 658 acpi_dev_put(adev); 659 return ret; 660 } 661 662 rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node); 663 if (rentry) 664 pmc->iom_base = devm_ioremap_resource(pmc->dev, rentry->res); 665 666 acpi_dev_free_resource_list(&resource_list); 667 668 if (!pmc->iom_base) { 669 acpi_dev_put(adev); 670 return -ENOMEM; 671 } 672 673 if (IS_ERR(pmc->iom_base)) { 674 acpi_dev_put(adev); 675 return PTR_ERR(pmc->iom_base); 676 } 677 678 pmc->iom_adev = adev; 679 680 return 0; 681 } 682 683 static int port_iom_status_show(struct seq_file *s, void *unused) 684 { 685 struct pmc_usb_port *port = s->private; 686 687 update_port_status(port); 688 seq_printf(s, "0x%08x\n", port->iom_status); 689 690 return 0; 691 } 692 DEFINE_SHOW_ATTRIBUTE(port_iom_status); 693 694 static void pmc_mux_port_debugfs_init(struct pmc_usb_port *port) 695 { 696 struct dentry *debugfs_dir; 697 char name[6]; 698 699 snprintf(name, sizeof(name), "port%d", port->usb3_port - 1); 700 701 debugfs_dir = debugfs_create_dir(name, port->pmc->dentry); 702 debugfs_create_file("iom_status", 0400, debugfs_dir, port, 703 &port_iom_status_fops); 704 } 705 706 static int pmc_usb_probe(struct platform_device *pdev) 707 { 708 struct fwnode_handle *fwnode = NULL; 709 struct pmc_usb *pmc; 710 int i = 0; 711 int ret; 712 713 pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL); 714 if (!pmc) 715 return -ENOMEM; 716 717 device_for_each_child_node(&pdev->dev, fwnode) 718 pmc->num_ports++; 719 720 /* The IOM microcontroller has a limitation of max 4 ports. */ 721 if (pmc->num_ports > 4) { 722 dev_err(&pdev->dev, "driver limited to 4 ports\n"); 723 return -ERANGE; 724 } 725 726 pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports, 727 sizeof(struct pmc_usb_port), GFP_KERNEL); 728 if (!pmc->port) 729 return -ENOMEM; 730 731 pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev); 732 if (!pmc->ipc) 733 return -ENODEV; 734 735 pmc->dev = &pdev->dev; 736 737 ret = pmc_usb_probe_iom(pmc); 738 if (ret) 739 return ret; 740 741 pmc->dentry = debugfs_create_dir(dev_name(pmc->dev), pmc_mux_debugfs_root); 742 743 /* 744 * For every physical USB connector (USB2 and USB3 combo) there is a 745 * child ACPI device node under the PMC mux ACPI device object. 746 */ 747 for (i = 0; i < pmc->num_ports; i++) { 748 fwnode = device_get_next_child_node(pmc->dev, fwnode); 749 if (!fwnode) 750 break; 751 752 ret = pmc_usb_register_port(pmc, i, fwnode); 753 if (ret) { 754 fwnode_handle_put(fwnode); 755 goto err_remove_ports; 756 } 757 758 pmc_mux_port_debugfs_init(&pmc->port[i]); 759 } 760 761 platform_set_drvdata(pdev, pmc); 762 763 return 0; 764 765 err_remove_ports: 766 for (i = 0; i < pmc->num_ports; i++) { 767 typec_switch_unregister(pmc->port[i].typec_sw); 768 typec_mux_unregister(pmc->port[i].typec_mux); 769 usb_role_switch_unregister(pmc->port[i].usb_sw); 770 } 771 772 acpi_dev_put(pmc->iom_adev); 773 774 debugfs_remove(pmc->dentry); 775 776 return ret; 777 } 778 779 static void pmc_usb_remove(struct platform_device *pdev) 780 { 781 struct pmc_usb *pmc = platform_get_drvdata(pdev); 782 int i; 783 784 for (i = 0; i < pmc->num_ports; i++) { 785 typec_switch_unregister(pmc->port[i].typec_sw); 786 typec_mux_unregister(pmc->port[i].typec_mux); 787 usb_role_switch_unregister(pmc->port[i].usb_sw); 788 } 789 790 acpi_dev_put(pmc->iom_adev); 791 792 debugfs_remove(pmc->dentry); 793 } 794 795 static const struct acpi_device_id pmc_usb_acpi_ids[] = { 796 { "INTC105C", }, 797 { } 798 }; 799 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids); 800 801 static struct platform_driver pmc_usb_driver = { 802 .driver = { 803 .name = "intel_pmc_usb", 804 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids), 805 }, 806 .probe = pmc_usb_probe, 807 .remove_new = pmc_usb_remove, 808 }; 809 810 static int __init pmc_usb_init(void) 811 { 812 pmc_mux_debugfs_root = debugfs_create_dir("intel_pmc_mux", usb_debug_root); 813 814 return platform_driver_register(&pmc_usb_driver); 815 } 816 module_init(pmc_usb_init); 817 818 static void __exit pmc_usb_exit(void) 819 { 820 platform_driver_unregister(&pmc_usb_driver); 821 debugfs_remove(pmc_mux_debugfs_root); 822 } 823 module_exit(pmc_usb_exit); 824 825 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 826 MODULE_LICENSE("GPL v2"); 827 MODULE_DESCRIPTION("Intel PMC USB mux control"); 828