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