| @@ -14,9 +14,21 @@ · 13 unmodified lines |
| 14 | 14 | #include <linux/platform_data/cros_usbpd_notify.h> |
| 15 | 15 | #include <linux/platform_device.h> |
| 16 | 16 | #include <linux/usb/typec.h> |
| 17 | +#include <linux/usb/typec_altmode.h> |
| 18 | +#include <linux/usb/typec_dp.h> |
| 19 | +#include <linux/usb/typec_mux.h> |
| 20 | +#include <linux/usb/typec_tbt.h> |
| 21 | +#include <linux/usb/role.h> |
| 17 | 22 | |
| 18 | 23 | #define DRV_NAME "cros-ec-typec" |
| 19 | 24 | |
| 25 | +/* Supported alt modes. */ |
| 26 | +enum { |
| 27 | + CROS_EC_ALTMODE_DP = 0, |
| 28 | + CROS_EC_ALTMODE_TBT, |
| 29 | + CROS_EC_ALTMODE_MAX, |
| 30 | +}; |
| 31 | + |
| 20 | 32 | /* Per port data. */ |
| 21 | 33 | struct cros_typec_port { |
| 22 | 34 | struct typec_port *port; |
| @@ -25,6 +37,16 @@ struct cros_typec_port { · 2 unmodified lines |
| 25 | 37 | struct typec_partner *partner; |
| 26 | 38 | /* Port partner PD identity info. */ |
| 27 | 39 | struct usb_pd_identity p_identity; |
| 40 | + struct typec_switch *ori_sw; |
| 41 | + struct typec_mux *mux; |
| 42 | + struct usb_role_switch *role_sw; |
| 43 | + |
| 44 | + /* Variables keeping track of switch state. */ |
| 45 | + struct typec_mux_state state; |
| 46 | + uint8_t mux_flags; |
| 47 | + |
| 48 | + /* Port alt modes. */ |
| 49 | + struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX]; |
| 28 | 50 | }; |
| 29 | 51 | |
| 30 | 52 | /* Platform-specific data for the Chrome OS EC Type C controller. */ |
| @@ -32,10 +54,11 @@ struct cros_typec_data { · 1 unmodified line |
| 32 | 54 | struct device *dev; |
| 33 | 55 | struct cros_ec_device *ec; |
| 34 | 56 | int num_ports; |
| 35 | | − unsigned int cmd_ver; |
| 57 | + unsigned int pd_ctrl_ver; |
| 36 | 58 | /* Array of ports, indexed by port number. */ |
| 37 | 59 | struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS]; |
| 38 | 60 | struct notifier_block nb; |
| 61 | + struct work_struct port_work; |
| 39 | 62 | }; |
| 40 | 63 | |
| 41 | 64 | static int cros_typec_parse_port_props(struct typec_capability *cap, |
| @@ -84,6 +107,81 @@ static int cros_typec_parse_port_props(struct typec_capability *cap, · 42 unmodified lines |
| 84 | 107 | return 0; |
| 85 | 108 | } |
| 86 | 109 | |
| 110 | +static int cros_typec_get_switch_handles(struct cros_typec_port *port, |
| 111 | + struct fwnode_handle *fwnode, |
| 112 | + struct device *dev) |
| 113 | +{ |
| 114 | + port->mux = fwnode_typec_mux_get(fwnode, NULL); |
| 115 | + if (IS_ERR(port->mux)) { |
| 116 | + dev_dbg(dev, "Mux handle not found.\n"); |
| 117 | + goto mux_err; |
| 118 | + } |
| 119 | + |
| 120 | + port->ori_sw = fwnode_typec_switch_get(fwnode); |
| 121 | + if (IS_ERR(port->ori_sw)) { |
| 122 | + dev_dbg(dev, "Orientation switch handle not found.\n"); |
| 123 | + goto ori_sw_err; |
| 124 | + } |
| 125 | + |
| 126 | + port->role_sw = fwnode_usb_role_switch_get(fwnode); |
| 127 | + if (IS_ERR(port->role_sw)) { |
| 128 | + dev_dbg(dev, "USB role switch handle not found.\n"); |
| 129 | + goto role_sw_err; |
| 130 | + } |
| 131 | + |
| 132 | + return 0; |
| 133 | + |
| 134 | +role_sw_err: |
| 135 | + usb_role_switch_put(port->role_sw); |
| 136 | +ori_sw_err: |
| 137 | + typec_switch_put(port->ori_sw); |
| 138 | +mux_err: |
| 139 | + typec_mux_put(port->mux); |
| 140 | + |
| 141 | + return -ENODEV; |
| 142 | +} |
| 143 | + |
| 144 | +static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num, |
| 145 | + bool pd_en) |
| 146 | +{ |
| 147 | + struct cros_typec_port *port = typec->ports[port_num]; |
| 148 | + struct typec_partner_desc p_desc = { |
| 149 | + .usb_pd = pd_en, |
| 150 | + }; |
| 151 | + int ret = 0; |
| 152 | + |
| 153 | + /* |
| 154 | + * Fill an initial PD identity, which will then be updated with info |
| 155 | + * from the EC. |
| 156 | + */ |
| 157 | + p_desc.identity = &port->p_identity; |
| 158 | + |
| 159 | + port->partner = typec_register_partner(port->port, &p_desc); |
| 160 | + if (IS_ERR(port->partner)) { |
| 161 | + ret = PTR_ERR(port->partner); |
| 162 | + port->partner = NULL; |
| 163 | + } |
| 164 | + |
| 165 | + return ret; |
| 166 | +} |
| 167 | + |
| 168 | +static void cros_typec_remove_partner(struct cros_typec_data *typec, |
| 169 | + int port_num) |
| 170 | +{ |
| 171 | + struct cros_typec_port *port = typec->ports[port_num]; |
| 172 | + |
| 173 | + port->state.alt = NULL; |
| 174 | + port->state.mode = TYPEC_STATE_USB; |
| 175 | + port->state.data = NULL; |
| 176 | + |
| 177 | + usb_role_switch_set_role(port->role_sw, USB_ROLE_NONE); |
| 178 | + typec_switch_set(port->ori_sw, TYPEC_ORIENTATION_NONE); |
| 179 | + typec_mux_set(port->mux, &port->state); |
| 180 | + |
| 181 | + typec_unregister_partner(port->partner); |
| 182 | + port->partner = NULL; |
| 183 | +} |
| 184 | + |
| 87 | 185 | static void cros_unregister_ports(struct cros_typec_data *typec) |
| 88 | 186 | { |
| 89 | 187 | int i; |
| @@ -91,10 +189,40 @@ static void cros_unregister_ports(struct cros_typec_data *typec) · 1 unmodified line |
| 91 | 189 | for (i = 0; i < typec->num_ports; i++) { |
| 92 | 190 | if (!typec->ports[i]) |
| 93 | 191 | continue; |
| 192 | + cros_typec_remove_partner(typec, i); |
| 193 | + usb_role_switch_put(typec->ports[i]->role_sw); |
| 194 | + typec_switch_put(typec->ports[i]->ori_sw); |
| 195 | + typec_mux_put(typec->ports[i]->mux); |
| 94 | 196 | typec_unregister_port(typec->ports[i]->port); |
| 95 | 197 | } |
| 96 | 198 | } |
| 97 | 199 | |
| 200 | +/* |
| 201 | + * Fake the alt mode structs until we actually start registering Type C port |
| 202 | + * and partner alt modes. |
| 203 | + */ |
| 204 | +static void cros_typec_register_port_altmodes(struct cros_typec_data *typec, |
| 205 | + int port_num) |
| 206 | +{ |
| 207 | + struct cros_typec_port *port = typec->ports[port_num]; |
| 208 | + |
| 209 | + /* All PD capable CrOS devices are assumed to support DP altmode. */ |
| 210 | + port->p_altmode[CROS_EC_ALTMODE_DP].svid = USB_TYPEC_DP_SID; |
| 211 | + port->p_altmode[CROS_EC_ALTMODE_DP].mode = USB_TYPEC_DP_MODE; |
| 212 | + |
| 213 | + /* |
| 214 | + * Register TBT compatibility alt mode. The EC will not enter the mode |
| 215 | + * if it doesn't support it, so it's safe to register it unconditionally |
| 216 | + * here for now. |
| 217 | + */ |
| 218 | + port->p_altmode[CROS_EC_ALTMODE_TBT].svid = USB_TYPEC_TBT_SID; |
| 219 | + port->p_altmode[CROS_EC_ALTMODE_TBT].mode = TYPEC_ANY_MODE; |
| 220 | + |
| 221 | + port->state.alt = NULL; |
| 222 | + port->state.mode = TYPEC_STATE_USB; |
| 223 | + port->state.data = NULL; |
| 224 | +} |
| 225 | + |
| 98 | 226 | static int cros_typec_init_ports(struct cros_typec_data *typec) |
| 99 | 227 | { |
| 100 | 228 | struct device *dev = typec->dev; |
| @@ -153,6 +281,13 @@ static int cros_typec_init_ports(struct cros_typec_data *typec) · 52 unmodified lines |
| 153 | 281 | ret = PTR_ERR(cros_port->port); |
| 154 | 282 | goto unregister_ports; |
| 155 | 283 | } |
| 284 | + |
| 285 | + ret = cros_typec_get_switch_handles(cros_port, fwnode, dev); |
| 286 | + if (ret) |
| 287 | + dev_dbg(dev, "No switch control for port %d\n", |
| 288 | + port_num); |
| 289 | + |
| 290 | + cros_typec_register_port_altmodes(typec, port_num); |
| 156 | 291 | } |
| 157 | 292 | |
| 158 | 293 | return 0; |
| @@ -193,30 +328,6 @@ static int cros_typec_ec_command(struct cros_typec_data *typec, · 34 unmodified lines |
| 193 | 328 | return ret; |
| 194 | 329 | } |
| 195 | 330 | |
| 196 | | −static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num, |
| 197 | | − bool pd_en) |
| 198 | | −{ |
| 199 | | − struct cros_typec_port *port = typec->ports[port_num]; |
| 200 | | − struct typec_partner_desc p_desc = { |
| 201 | | − .usb_pd = pd_en, |
| 202 | | − }; |
| 203 | | − int ret = 0; |
| 204 | | − |
| 205 | | − /* |
| 206 | | − * Fill an initial PD identity, which will then be updated with info |
| 207 | | − * from the EC. |
| 208 | | − */ |
| 209 | | − p_desc.identity = &port->p_identity; |
| 210 | | − |
| 211 | | − port->partner = typec_register_partner(port->port, &p_desc); |
| 212 | | − if (IS_ERR(port->partner)) { |
| 213 | | − ret = PTR_ERR(port->partner); |
| 214 | | − port->partner = NULL; |
| 215 | | − } |
| 216 | | − |
| 217 | | − return ret; |
| 218 | | −} |
| 219 | | − |
| 220 | 331 | static void cros_typec_set_port_params_v0(struct cros_typec_data *typec, |
| 221 | 332 | int port_num, struct ec_response_usb_pd_control *resp) |
| 222 | 333 | { |
| @@ -270,16 +381,166 @@ static void cros_typec_set_port_params_v1(struct cros_typec_data *typec, · 47 unmodified lines |
| 270 | 381 | } else { |
| 271 | 382 | if (!typec->ports[port_num]->partner) |
| 272 | 383 | return; |
| 384 | + cros_typec_remove_partner(typec, port_num); |
| 385 | + } |
| 386 | +} |
| 273 | 387 | |
| 274 | | − typec_unregister_partner(typec->ports[port_num]->partner); |
| 275 | | − typec->ports[port_num]->partner = NULL; |
| 388 | +static int cros_typec_get_mux_info(struct cros_typec_data *typec, int port_num, |
| 389 | + struct ec_response_usb_pd_mux_info *resp) |
| 390 | +{ |
| 391 | + struct ec_params_usb_pd_mux_info req = { |
| 392 | + .port = port_num, |
| 393 | + }; |
| 394 | + |
| 395 | + return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req, |
| 396 | + sizeof(req), resp, sizeof(*resp)); |
| 397 | +} |
| 398 | + |
| 399 | +static int cros_typec_usb_safe_state(struct cros_typec_port *port) |
| 400 | +{ |
| 401 | + port->state.mode = TYPEC_STATE_SAFE; |
| 402 | + |
| 403 | + return typec_mux_set(port->mux, &port->state); |
| 404 | +} |
| 405 | + |
| 406 | +/* |
| 407 | + * Spoof the VDOs that were likely communicated by the partner for TBT alt |
| 408 | + * mode. |
| 409 | + */ |
| 410 | +static int cros_typec_enable_tbt(struct cros_typec_data *typec, |
| 411 | + int port_num, |
| 412 | + struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 413 | +{ |
| 414 | + struct cros_typec_port *port = typec->ports[port_num]; |
| 415 | + struct typec_thunderbolt_data data; |
| 416 | + int ret; |
| 417 | + |
| 418 | + if (typec->pd_ctrl_ver < 2) { |
| 419 | + dev_err(typec->dev, |
| 420 | + "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver); |
| 421 | + return -ENOTSUPP; |
| 422 | + } |
| 423 | + |
| 424 | + /* Device Discover Mode VDO */ |
| 425 | + data.device_mode = TBT_MODE; |
| 426 | + |
| 427 | + if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER) |
| 428 | + data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3); |
| 429 | + |
| 430 | + /* Cable Discover Mode VDO */ |
| 431 | + data.cable_mode = TBT_MODE; |
| 432 | + data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed); |
| 433 | + |
| 434 | + if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE) |
| 435 | + data.cable_mode |= TBT_CABLE_OPTICAL; |
| 436 | + |
| 437 | + if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR) |
| 438 | + data.cable_mode |= TBT_CABLE_LINK_TRAINING; |
| 439 | + |
| 440 | + if (pd_ctrl->cable_gen) |
| 441 | + data.cable_mode |= TBT_CABLE_ROUNDED; |
| 442 | + |
| 443 | + /* Enter Mode VDO */ |
| 444 | + data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed); |
| 445 | + |
| 446 | + if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE) |
| 447 | + data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE; |
| 448 | + |
| 449 | + if (!port->state.alt) { |
| 450 | + port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT]; |
| 451 | + ret = cros_typec_usb_safe_state(port); |
| 452 | + if (ret) |
| 453 | + return ret; |
| 454 | + } |
| 455 | + |
| 456 | + port->state.data = &data; |
| 457 | + port->state.mode = TYPEC_TBT_MODE; |
| 458 | + |
| 459 | + return typec_mux_set(port->mux, &port->state); |
| 460 | +} |
| 461 | + |
| 462 | +/* Spoof the VDOs that were likely communicated by the partner. */ |
| 463 | +static int cros_typec_enable_dp(struct cros_typec_data *typec, |
| 464 | + int port_num, |
| 465 | + struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 466 | +{ |
| 467 | + struct cros_typec_port *port = typec->ports[port_num]; |
| 468 | + struct typec_displayport_data dp_data; |
| 469 | + int ret; |
| 470 | + |
| 471 | + if (typec->pd_ctrl_ver < 2) { |
| 472 | + dev_err(typec->dev, |
| 473 | + "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver); |
| 474 | + return -ENOTSUPP; |
| 475 | + } |
| 476 | + |
| 477 | + /* Status VDO. */ |
| 478 | + dp_data.status = DP_STATUS_ENABLED; |
| 479 | + if (port->mux_flags & USB_PD_MUX_HPD_IRQ) |
| 480 | + dp_data.status |= DP_STATUS_IRQ_HPD; |
| 481 | + if (port->mux_flags & USB_PD_MUX_HPD_LVL) |
| 482 | + dp_data.status |= DP_STATUS_HPD_STATE; |
| 483 | + |
| 484 | + /* Configuration VDO. */ |
| 485 | + dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode); |
| 486 | + if (!port->state.alt) { |
| 487 | + port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP]; |
| 488 | + ret = cros_typec_usb_safe_state(port); |
| 489 | + if (ret) |
| 490 | + return ret; |
| 276 | 491 | } |
| 492 | + |
| 493 | + port->state.data = &dp_data; |
| 494 | + port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode)); |
| 495 | + |
| 496 | + return typec_mux_set(port->mux, &port->state); |
| 497 | +} |
| 498 | + |
| 499 | +static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num, |
| 500 | + uint8_t mux_flags, |
| 501 | + struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 502 | +{ |
| 503 | + struct cros_typec_port *port = typec->ports[port_num]; |
| 504 | + enum typec_orientation orientation; |
| 505 | + int ret; |
| 506 | + |
| 507 | + if (!port->partner) |
| 508 | + return 0; |
| 509 | + |
| 510 | + if (mux_flags & USB_PD_MUX_POLARITY_INVERTED) |
| 511 | + orientation = TYPEC_ORIENTATION_REVERSE; |
| 512 | + else |
| 513 | + orientation = TYPEC_ORIENTATION_NORMAL; |
| 514 | + |
| 515 | + ret = typec_switch_set(port->ori_sw, orientation); |
| 516 | + if (ret) |
| 517 | + return ret; |
| 518 | + |
| 519 | + if (mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) { |
| 520 | + ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl); |
| 521 | + } else if (mux_flags & USB_PD_MUX_DP_ENABLED) { |
| 522 | + ret = cros_typec_enable_dp(typec, port_num, pd_ctrl); |
| 523 | + } else if (mux_flags & USB_PD_MUX_SAFE_MODE) { |
| 524 | + ret = cros_typec_usb_safe_state(port); |
| 525 | + } else if (mux_flags & USB_PD_MUX_USB_ENABLED) { |
| 526 | + port->state.alt = NULL; |
| 527 | + port->state.mode = TYPEC_STATE_USB; |
| 528 | + ret = typec_mux_set(port->mux, &port->state); |
| 529 | + } else { |
| 530 | + dev_info(typec->dev, |
| 531 | + "Unsupported mode requested, mux flags: %x\n", |
| 532 | + mux_flags); |
| 533 | + ret = -ENOTSUPP; |
| 534 | + } |
| 535 | + |
| 536 | + return ret; |
| 277 | 537 | } |
| 278 | 538 | |
| 279 | 539 | static int cros_typec_port_update(struct cros_typec_data *typec, int port_num) |
| 280 | 540 | { |
| 281 | 541 | struct ec_params_usb_pd_control req; |
| 282 | | − struct ec_response_usb_pd_control_v1 resp; |
| 542 | + struct ec_response_usb_pd_control_v2 resp; |
| 543 | + struct ec_response_usb_pd_mux_info mux_resp; |
| 283 | 544 | int ret; |
| 284 | 545 | |
| 285 | 546 | if (port_num < 0 || port_num >= typec->num_ports) { |
| @@ -293,7 +554,7 @@ static int cros_typec_port_update(struct cros_typec_data *typec, int port_num) · 7 unmodified lines |
| 293 | 554 | req.mux = USB_PD_CTRL_MUX_NO_CHANGE; |
| 294 | 555 | req.swap = USB_PD_CTRL_SWAP_NONE; |
| 295 | 556 | |
| 296 | | − ret = cros_typec_ec_command(typec, typec->cmd_ver, |
| 557 | + ret = cros_typec_ec_command(typec, typec->pd_ctrl_ver, |
| 297 | 558 | EC_CMD_USB_PD_CONTROL, &req, sizeof(req), |
| 298 | 559 | &resp, sizeof(resp)); |
| 299 | 560 | if (ret < 0) |
| @@ -304,13 +565,33 @@ static int cros_typec_port_update(struct cros_typec_data *typec, int port_num) · 4 unmodified lines |
| 304 | 565 | dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity); |
| 305 | 566 | dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state); |
| 306 | 567 | |
| 307 | | − if (typec->cmd_ver == 1) |
| 308 | | − cros_typec_set_port_params_v1(typec, port_num, &resp); |
| 568 | + if (typec->pd_ctrl_ver != 0) |
| 569 | + cros_typec_set_port_params_v1(typec, port_num, |
| 570 | + (struct ec_response_usb_pd_control_v1 *)&resp); |
| 309 | 571 | else |
| 310 | 572 | cros_typec_set_port_params_v0(typec, port_num, |
| 311 | 573 | (struct ec_response_usb_pd_control *) &resp); |
| 312 | 574 | |
| 313 | | − return 0; |
| 575 | + /* Update the switches if they exist, according to requested state */ |
| 576 | + ret = cros_typec_get_mux_info(typec, port_num, &mux_resp); |
| 577 | + if (ret < 0) { |
| 578 | + dev_warn(typec->dev, |
| 579 | + "Failed to get mux info for port: %d, err = %d\n", |
| 580 | + port_num, ret); |
| 581 | + return 0; |
| 582 | + } |
| 583 | + |
| 584 | + /* No change needs to be made, let's exit early. */ |
| 585 | + if (typec->ports[port_num]->mux_flags == mux_resp.flags) |
| 586 | + return 0; |
| 587 | + |
| 588 | + typec->ports[port_num]->mux_flags = mux_resp.flags; |
| 589 | + ret = cros_typec_configure_mux(typec, port_num, mux_resp.flags, &resp); |
| 590 | + if (ret) |
| 591 | + dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret); |
| 592 | + |
| 593 | + return usb_role_switch_set_role(typec->ports[port_num]->role_sw, |
| 594 | + !!(resp.role & PD_CTRL_RESP_ROLE_DATA)); |
| 314 | 595 | } |
| 315 | 596 | |
| 316 | 597 | static int cros_typec_get_cmd_version(struct cros_typec_data *typec) |
| @@ -327,22 +608,22 @@ static int cros_typec_get_cmd_version(struct cros_typec_data *typec) · 10 unmodified lines |
| 327 | 608 | if (ret < 0) |
| 328 | 609 | return ret; |
| 329 | 610 | |
| 330 | | − if (resp.version_mask & EC_VER_MASK(1)) |
| 331 | | − typec->cmd_ver = 1; |
| 611 | + if (resp.version_mask & EC_VER_MASK(2)) |
| 612 | + typec->pd_ctrl_ver = 2; |
| 613 | + else if (resp.version_mask & EC_VER_MASK(1)) |
| 614 | + typec->pd_ctrl_ver = 1; |
| 332 | 615 | else |
| 333 | | − typec->cmd_ver = 0; |
| 616 | + typec->pd_ctrl_ver = 0; |
| 334 | 617 | |
| 335 | 618 | dev_dbg(typec->dev, "PD Control has version mask 0x%hhx\n", |
| 336 | | − typec->cmd_ver); |
| 619 | + typec->pd_ctrl_ver); |
| 337 | 620 | |
| 338 | 621 | return 0; |
| 339 | 622 | } |
| 340 | 623 | |
| 341 | | −static int cros_ec_typec_event(struct notifier_block *nb, |
| 342 | | − unsigned long host_event, void *_notify) |
| 624 | +static void cros_typec_port_work(struct work_struct *work) |
| 343 | 625 | { |
| 344 | | − struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, |
| 345 | | − nb); |
| 626 | + struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work); |
| 346 | 627 | int ret, i; |
| 347 | 628 | |
| 348 | 629 | for (i = 0; i < typec->num_ports; i++) { |
| @@ -350,6 +631,14 @@ static int cros_ec_typec_event(struct notifier_block *nb, · 1 unmodified line |
| 350 | 631 | if (ret < 0) |
| 351 | 632 | dev_warn(typec->dev, "Update failed for port: %d\n", i); |
| 352 | 633 | } |
| 634 | +} |
| 635 | + |
| 636 | +static int cros_ec_typec_event(struct notifier_block *nb, |
| 637 | + unsigned long host_event, void *_notify) |
| 638 | +{ |
| 639 | + struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb); |
| 640 | + |
| 641 | + schedule_work(&typec->port_work); |
| 353 | 642 | |
| 354 | 643 | return NOTIFY_OK; |
| 355 | 644 | } |
| @@ -408,6 +697,12 @@ static int cros_typec_probe(struct platform_device *pdev) · 52 unmodified lines |
| 408 | 697 | if (ret < 0) |
| 409 | 698 | return ret; |
| 410 | 699 | |
| 700 | + INIT_WORK(&typec->port_work, cros_typec_port_work); |
| 701 | + |
| 702 | + /* |
| 703 | + * Safe to call port update here, since we haven't registered the |
| 704 | + * PD notifier yet. |
| 705 | + */ |
| 411 | 706 | for (i = 0; i < typec->num_ports; i++) { |
| 412 | 707 | ret = cros_typec_port_update(typec, i); |
| 413 | 708 | if (ret < 0) |
| @@ -426,11 +721,35 @@ unregister_ports: · 12 unmodified lines |
| 426 | 721 | return ret; |
| 427 | 722 | } |
| 428 | 723 | |
| 724 | +static int __maybe_unused cros_typec_suspend(struct device *dev) |
| 725 | +{ |
| 726 | + struct cros_typec_data *typec = dev_get_drvdata(dev); |
| 727 | + |
| 728 | + cancel_work_sync(&typec->port_work); |
| 729 | + |
| 730 | + return 0; |
| 731 | +} |
| 732 | + |
| 733 | +static int __maybe_unused cros_typec_resume(struct device *dev) |
| 734 | +{ |
| 735 | + struct cros_typec_data *typec = dev_get_drvdata(dev); |
| 736 | + |
| 737 | + /* Refresh port state. */ |
| 738 | + schedule_work(&typec->port_work); |
| 739 | + |
| 740 | + return 0; |
| 741 | +} |
| 742 | + |
| 743 | +static const struct dev_pm_ops cros_typec_pm_ops = { |
| 744 | + SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume) |
| 745 | +}; |
| 746 | + |
| 429 | 747 | static struct platform_driver cros_typec_driver = { |
| 430 | 748 | .driver = { |
| 431 | 749 | .name = DRV_NAME, |
| 432 | 750 | .acpi_match_table = ACPI_PTR(cros_typec_acpi_id), |
| 433 | 751 | .of_match_table = of_match_ptr(cros_typec_of_match), |
| 752 | + .pm = &cros_typec_pm_ops, |
| 434 | 753 | }, |
| 435 | 754 | .probe = cros_typec_probe, |
| 436 | 755 | }; |