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