| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright 2020 Google LLC |
| 4 | * |
| 5 | * This driver provides the ability to view and manage Type C ports through the |
| 6 | * Chrome OS EC. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/acpi.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/platform_data/cros_ec_commands.h> |
| 13 | #include <linux/platform_data/cros_usbpd_notify.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/usb/pd_vdo.h> |
| 16 | #include <linux/usb/typec_dp.h> |
| 17 | #include <linux/usb/typec_tbt.h> |
| 18 | |
| 19 | #include "cros_ec_typec.h" |
| 20 | #include "cros_typec_vdm.h" |
| 21 | #include "cros_typec_altmode.h" |
| 22 | |
| 23 | #define DRV_NAME "cros-ec-typec" |
| 24 | |
| 25 | #define DP_PORT_VDO (DP_CAP_DFP_D | DP_CAP_RECEPTACLE | \ |
| 26 | DP_CONF_SET_PIN_ASSIGN(BIT(DP_PIN_ASSIGN_C) | \ |
| 27 | BIT(DP_PIN_ASSIGN_D) | \ |
| 28 | BIT(DP_PIN_ASSIGN_E))) |
| 29 | |
| 30 | static void cros_typec_role_switch_quirk(struct fwnode_handle *fwnode) |
| 31 | { |
| 32 | #ifdef CONFIG_ACPI |
| 33 | struct fwnode_handle *switch_fwnode; |
| 34 | |
| 35 | /* Supply the USB role switch with the correct pld_crc if it's missing. */ |
| 36 | switch_fwnode = fwnode_find_reference(fwnode, name: "usb-role-switch" , index: 0); |
| 37 | if (!IS_ERR_OR_NULL(ptr: switch_fwnode)) { |
| 38 | struct acpi_device *adev = to_acpi_device_node(switch_fwnode); |
| 39 | |
| 40 | if (adev && !adev->pld_crc) |
| 41 | adev->pld_crc = to_acpi_device_node(fwnode)->pld_crc; |
| 42 | fwnode_handle_put(fwnode: switch_fwnode); |
| 43 | } |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | static int cros_typec_enter_usb_mode(struct typec_port *tc_port, enum usb_mode mode) |
| 48 | { |
| 49 | struct cros_typec_port *port = typec_get_drvdata(port: tc_port); |
| 50 | struct ec_params_typec_control req = { |
| 51 | .port = port->port_num, |
| 52 | .command = (mode == USB_MODE_USB4) ? |
| 53 | TYPEC_CONTROL_COMMAND_ENTER_MODE : TYPEC_CONTROL_COMMAND_EXIT_MODES, |
| 54 | .mode_to_enter = CROS_EC_ALTMODE_USB4 |
| 55 | }; |
| 56 | |
| 57 | return cros_ec_cmd(ec_dev: port->typec_data->ec, version: 0, EC_CMD_TYPEC_CONTROL, |
| 58 | outdata: &req, outsize: sizeof(req), NULL, insize: 0); |
| 59 | } |
| 60 | |
| 61 | static int cros_typec_perform_role_swap(struct typec_port *tc_port, int target_role, u8 swap_type) |
| 62 | { |
| 63 | struct cros_typec_port *port = typec_get_drvdata(port: tc_port); |
| 64 | struct cros_typec_data *data = port->typec_data; |
| 65 | struct ec_response_usb_pd_control_v2 resp; |
| 66 | struct ec_params_usb_pd_control req; |
| 67 | int role, ret; |
| 68 | |
| 69 | /* Must be at least v1 to support role swap. */ |
| 70 | if (!data->pd_ctrl_ver) |
| 71 | return -EOPNOTSUPP; |
| 72 | |
| 73 | /* First query the state */ |
| 74 | req.port = port->port_num; |
| 75 | req.role = USB_PD_CTRL_ROLE_NO_CHANGE; |
| 76 | req.mux = USB_PD_CTRL_MUX_NO_CHANGE; |
| 77 | req.swap = USB_PD_CTRL_SWAP_NONE; |
| 78 | |
| 79 | ret = cros_ec_cmd(ec_dev: data->ec, version: data->pd_ctrl_ver, EC_CMD_USB_PD_CONTROL, |
| 80 | outdata: &req, outsize: sizeof(req), indata: &resp, insize: sizeof(resp)); |
| 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | |
| 84 | switch (swap_type) { |
| 85 | case USB_PD_CTRL_SWAP_DATA: |
| 86 | role = (resp.role & PD_CTRL_RESP_ROLE_DATA) ? TYPEC_HOST : |
| 87 | TYPEC_DEVICE; |
| 88 | break; |
| 89 | case USB_PD_CTRL_SWAP_POWER: |
| 90 | role = (resp.role & PD_CTRL_RESP_ROLE_POWER) ? TYPEC_SOURCE : |
| 91 | TYPEC_SINK; |
| 92 | break; |
| 93 | default: |
| 94 | dev_warn(data->dev, "Unsupported role swap type %d\n" , swap_type); |
| 95 | return -EOPNOTSUPP; |
| 96 | } |
| 97 | |
| 98 | if (role == target_role) |
| 99 | return 0; |
| 100 | |
| 101 | req.swap = swap_type; |
| 102 | ret = cros_ec_cmd(ec_dev: data->ec, version: data->pd_ctrl_ver, EC_CMD_USB_PD_CONTROL, |
| 103 | outdata: &req, outsize: sizeof(req), indata: &resp, insize: sizeof(resp)); |
| 104 | if (ret < 0) |
| 105 | return ret; |
| 106 | |
| 107 | switch (swap_type) { |
| 108 | case USB_PD_CTRL_SWAP_DATA: |
| 109 | role = resp.role & PD_CTRL_RESP_ROLE_DATA ? TYPEC_HOST : TYPEC_DEVICE; |
| 110 | if (role != target_role) { |
| 111 | dev_err(data->dev, "Data role swap failed despite EC returning success\n" ); |
| 112 | return -EIO; |
| 113 | } |
| 114 | typec_set_data_role(port: tc_port, role: target_role); |
| 115 | break; |
| 116 | case USB_PD_CTRL_SWAP_POWER: |
| 117 | role = resp.role & PD_CTRL_RESP_ROLE_POWER ? TYPEC_SOURCE : TYPEC_SINK; |
| 118 | if (role != target_role) { |
| 119 | dev_err(data->dev, "Power role swap failed despite EC returning success\n" ); |
| 120 | return -EIO; |
| 121 | } |
| 122 | typec_set_pwr_role(port: tc_port, role: target_role); |
| 123 | break; |
| 124 | default: |
| 125 | /* Should never execute */ |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int cros_typec_dr_swap(struct typec_port *port, enum typec_data_role role) |
| 133 | { |
| 134 | return cros_typec_perform_role_swap(tc_port: port, target_role: role, swap_type: USB_PD_CTRL_SWAP_DATA); |
| 135 | } |
| 136 | |
| 137 | static int cros_typec_pr_swap(struct typec_port *port, enum typec_role role) |
| 138 | { |
| 139 | return cros_typec_perform_role_swap(tc_port: port, target_role: role, swap_type: USB_PD_CTRL_SWAP_POWER); |
| 140 | } |
| 141 | |
| 142 | static const struct typec_operations cros_typec_usb_mode_ops = { |
| 143 | .enter_usb_mode = cros_typec_enter_usb_mode, |
| 144 | .dr_set = cros_typec_dr_swap, |
| 145 | .pr_set = cros_typec_pr_swap, |
| 146 | }; |
| 147 | |
| 148 | static int cros_typec_parse_port_props(struct typec_capability *cap, |
| 149 | struct fwnode_handle *fwnode, |
| 150 | struct device *dev) |
| 151 | { |
| 152 | const char *buf; |
| 153 | int ret; |
| 154 | |
| 155 | memset(cap, 0, sizeof(*cap)); |
| 156 | ret = fwnode_property_read_string(fwnode, propname: "power-role" , val: &buf); |
| 157 | if (ret) { |
| 158 | dev_err(dev, "power-role not found: %d\n" , ret); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | ret = typec_find_port_power_role(name: buf); |
| 163 | if (ret < 0) |
| 164 | return ret; |
| 165 | cap->type = ret; |
| 166 | |
| 167 | ret = fwnode_property_read_string(fwnode, propname: "data-role" , val: &buf); |
| 168 | if (ret) { |
| 169 | dev_err(dev, "data-role not found: %d\n" , ret); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | ret = typec_find_port_data_role(name: buf); |
| 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | cap->data = ret; |
| 177 | |
| 178 | /* Try-power-role is optional. */ |
| 179 | ret = fwnode_property_read_string(fwnode, propname: "try-power-role" , val: &buf); |
| 180 | if (ret) { |
| 181 | dev_warn(dev, "try-power-role not found: %d\n" , ret); |
| 182 | cap->prefer_role = TYPEC_NO_PREFERRED_ROLE; |
| 183 | } else { |
| 184 | ret = typec_find_power_role(name: buf); |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | cap->prefer_role = ret; |
| 188 | } |
| 189 | |
| 190 | if (fwnode_property_present(fwnode, propname: "usb2-port" )) |
| 191 | cap->usb_capability |= USB_CAPABILITY_USB2; |
| 192 | if (fwnode_property_present(fwnode, propname: "usb3-port" )) |
| 193 | cap->usb_capability |= USB_CAPABILITY_USB3; |
| 194 | if (fwnode_property_present(fwnode, propname: "usb4-port" )) |
| 195 | cap->usb_capability |= USB_CAPABILITY_USB4; |
| 196 | |
| 197 | cros_typec_role_switch_quirk(fwnode); |
| 198 | |
| 199 | cap->fwnode = fwnode; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int cros_typec_get_switch_handles(struct cros_typec_port *port, |
| 205 | struct fwnode_handle *fwnode, |
| 206 | struct device *dev) |
| 207 | { |
| 208 | int ret = 0; |
| 209 | |
| 210 | port->mux = fwnode_typec_mux_get(fwnode); |
| 211 | if (IS_ERR(ptr: port->mux)) { |
| 212 | ret = PTR_ERR(ptr: port->mux); |
| 213 | dev_err_probe(dev, err: ret, fmt: "Mux handle not found\n" ); |
| 214 | goto mux_err; |
| 215 | } |
| 216 | |
| 217 | port->retimer = fwnode_typec_retimer_get(fwnode); |
| 218 | if (IS_ERR(ptr: port->retimer)) { |
| 219 | ret = PTR_ERR(ptr: port->retimer); |
| 220 | dev_err_probe(dev, err: ret, fmt: "Retimer handle not found\n" ); |
| 221 | goto retimer_sw_err; |
| 222 | } |
| 223 | |
| 224 | port->ori_sw = fwnode_typec_switch_get(fwnode); |
| 225 | if (IS_ERR(ptr: port->ori_sw)) { |
| 226 | ret = PTR_ERR(ptr: port->ori_sw); |
| 227 | dev_err_probe(dev, err: ret, fmt: "Orientation switch handle not found\n" ); |
| 228 | goto ori_sw_err; |
| 229 | } |
| 230 | |
| 231 | port->role_sw = fwnode_usb_role_switch_get(node: fwnode); |
| 232 | if (IS_ERR(ptr: port->role_sw)) { |
| 233 | ret = PTR_ERR(ptr: port->role_sw); |
| 234 | dev_err_probe(dev, err: ret, fmt: "USB role switch handle not found\n" ); |
| 235 | goto role_sw_err; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | |
| 240 | role_sw_err: |
| 241 | typec_switch_put(sw: port->ori_sw); |
| 242 | port->ori_sw = NULL; |
| 243 | ori_sw_err: |
| 244 | typec_retimer_put(retimer: port->retimer); |
| 245 | port->retimer = NULL; |
| 246 | retimer_sw_err: |
| 247 | typec_mux_put(mux: port->mux); |
| 248 | port->mux = NULL; |
| 249 | mux_err: |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num, |
| 254 | bool pd_en) |
| 255 | { |
| 256 | struct cros_typec_port *port = typec->ports[port_num]; |
| 257 | struct typec_partner_desc p_desc = { |
| 258 | .usb_pd = pd_en, |
| 259 | }; |
| 260 | int ret = 0; |
| 261 | |
| 262 | /* |
| 263 | * Fill an initial PD identity, which will then be updated with info |
| 264 | * from the EC. |
| 265 | */ |
| 266 | p_desc.identity = &port->p_identity; |
| 267 | |
| 268 | port->partner = typec_register_partner(port: port->port, desc: &p_desc); |
| 269 | if (IS_ERR(ptr: port->partner)) { |
| 270 | ret = PTR_ERR(ptr: port->partner); |
| 271 | port->partner = NULL; |
| 272 | } |
| 273 | |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | static void cros_typec_unregister_altmodes(struct cros_typec_data *typec, int port_num, |
| 278 | bool is_partner) |
| 279 | { |
| 280 | struct cros_typec_port *port = typec->ports[port_num]; |
| 281 | struct cros_typec_altmode_node *node, *tmp; |
| 282 | struct list_head *head; |
| 283 | |
| 284 | head = is_partner ? &port->partner_mode_list : &port->plug_mode_list; |
| 285 | list_for_each_entry_safe(node, tmp, head, list) { |
| 286 | list_del(entry: &node->list); |
| 287 | typec_unregister_altmode(altmode: node->amode); |
| 288 | devm_kfree(dev: typec->dev, p: node); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Map the Type-C Mux state to retimer state and call the retimer set function. We need this |
| 294 | * because we re-use the Type-C mux state for retimers. |
| 295 | */ |
| 296 | static int cros_typec_retimer_set(struct typec_retimer *retimer, struct typec_mux_state state) |
| 297 | { |
| 298 | struct typec_retimer_state rstate = { |
| 299 | .alt = state.alt, |
| 300 | .mode = state.mode, |
| 301 | .data = state.data, |
| 302 | }; |
| 303 | |
| 304 | return typec_retimer_set(retimer, state: &rstate); |
| 305 | } |
| 306 | |
| 307 | static int cros_typec_usb_disconnect_state(struct cros_typec_port *port) |
| 308 | { |
| 309 | port->state.alt = NULL; |
| 310 | port->state.mode = TYPEC_STATE_USB; |
| 311 | port->state.data = NULL; |
| 312 | |
| 313 | usb_role_switch_set_role(sw: port->role_sw, role: USB_ROLE_NONE); |
| 314 | typec_switch_set(sw: port->ori_sw, orientation: TYPEC_ORIENTATION_NONE); |
| 315 | cros_typec_retimer_set(retimer: port->retimer, state: port->state); |
| 316 | |
| 317 | return typec_mux_set(mux: port->mux, state: &port->state); |
| 318 | } |
| 319 | |
| 320 | static void cros_typec_remove_partner(struct cros_typec_data *typec, |
| 321 | int port_num) |
| 322 | { |
| 323 | struct cros_typec_port *port = typec->ports[port_num]; |
| 324 | |
| 325 | if (!port->partner) |
| 326 | return; |
| 327 | |
| 328 | cros_typec_unregister_altmodes(typec, port_num, is_partner: true); |
| 329 | |
| 330 | typec_partner_set_usb_power_delivery(partner: port->partner, NULL); |
| 331 | usb_power_delivery_unregister_capabilities(cap: port->partner_sink_caps); |
| 332 | port->partner_sink_caps = NULL; |
| 333 | usb_power_delivery_unregister_capabilities(cap: port->partner_src_caps); |
| 334 | port->partner_src_caps = NULL; |
| 335 | usb_power_delivery_unregister(pd: port->partner_pd); |
| 336 | port->partner_pd = NULL; |
| 337 | |
| 338 | cros_typec_usb_disconnect_state(port); |
| 339 | port->mux_flags = USB_PD_MUX_NONE; |
| 340 | |
| 341 | typec_unregister_partner(partner: port->partner); |
| 342 | port->partner = NULL; |
| 343 | memset(&port->p_identity, 0, sizeof(port->p_identity)); |
| 344 | port->sop_disc_done = false; |
| 345 | } |
| 346 | |
| 347 | static void cros_typec_remove_cable(struct cros_typec_data *typec, |
| 348 | int port_num) |
| 349 | { |
| 350 | struct cros_typec_port *port = typec->ports[port_num]; |
| 351 | |
| 352 | if (!port->cable) |
| 353 | return; |
| 354 | |
| 355 | cros_typec_unregister_altmodes(typec, port_num, is_partner: false); |
| 356 | |
| 357 | typec_unregister_plug(plug: port->plug); |
| 358 | port->plug = NULL; |
| 359 | typec_unregister_cable(cable: port->cable); |
| 360 | port->cable = NULL; |
| 361 | memset(&port->c_identity, 0, sizeof(port->c_identity)); |
| 362 | port->sop_prime_disc_done = false; |
| 363 | } |
| 364 | |
| 365 | static void cros_typec_unregister_port_altmodes(struct cros_typec_port *port) |
| 366 | { |
| 367 | int i; |
| 368 | |
| 369 | for (i = 0; i < CROS_EC_ALTMODE_MAX; i++) |
| 370 | typec_unregister_altmode(altmode: port->port_altmode[i]); |
| 371 | } |
| 372 | |
| 373 | static void cros_unregister_ports(struct cros_typec_data *typec) |
| 374 | { |
| 375 | int i; |
| 376 | |
| 377 | for (i = 0; i < typec->num_ports; i++) { |
| 378 | if (!typec->ports[i]) |
| 379 | continue; |
| 380 | |
| 381 | cros_typec_remove_partner(typec, port_num: i); |
| 382 | cros_typec_remove_cable(typec, port_num: i); |
| 383 | |
| 384 | usb_role_switch_put(sw: typec->ports[i]->role_sw); |
| 385 | typec_switch_put(sw: typec->ports[i]->ori_sw); |
| 386 | typec_mux_put(mux: typec->ports[i]->mux); |
| 387 | cros_typec_unregister_port_altmodes(port: typec->ports[i]); |
| 388 | typec_unregister_port(port: typec->ports[i]->port); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Register port alt modes with known values till we start retrieving |
| 394 | * port capabilities from the EC. |
| 395 | */ |
| 396 | static int cros_typec_register_port_altmodes(struct cros_typec_data *typec, |
| 397 | int port_num) |
| 398 | { |
| 399 | struct cros_typec_port *port = typec->ports[port_num]; |
| 400 | struct typec_altmode_desc desc; |
| 401 | struct typec_altmode *amode; |
| 402 | |
| 403 | /* All PD capable CrOS devices are assumed to support DP altmode. */ |
| 404 | memset(&desc, 0, sizeof(desc)); |
| 405 | desc.svid = USB_TYPEC_DP_SID; |
| 406 | desc.mode = USB_TYPEC_DP_MODE; |
| 407 | desc.vdo = DP_PORT_VDO; |
| 408 | amode = cros_typec_register_displayport(port, desc: &desc, |
| 409 | ap_mode_entry: typec->ap_driven_altmode); |
| 410 | if (IS_ERR(ptr: amode)) |
| 411 | return PTR_ERR(ptr: amode); |
| 412 | port->port_altmode[CROS_EC_ALTMODE_DP] = amode; |
| 413 | |
| 414 | /* |
| 415 | * Register TBT compatibility alt mode. The EC will not enter the mode |
| 416 | * if it doesn't support it and it will not enter automatically by |
| 417 | * design so we can use the |ap_driven_altmode| feature to check if we |
| 418 | * should register it. |
| 419 | */ |
| 420 | if (typec->ap_driven_altmode) { |
| 421 | memset(&desc, 0, sizeof(desc)); |
| 422 | desc.svid = USB_TYPEC_TBT_SID; |
| 423 | desc.mode = TBT_MODE; |
| 424 | desc.inactive = true; |
| 425 | amode = cros_typec_register_thunderbolt(port, desc: &desc); |
| 426 | if (IS_ERR(ptr: amode)) |
| 427 | return PTR_ERR(ptr: amode); |
| 428 | port->port_altmode[CROS_EC_ALTMODE_TBT] = amode; |
| 429 | } |
| 430 | |
| 431 | port->state.alt = NULL; |
| 432 | port->state.mode = TYPEC_STATE_USB; |
| 433 | port->state.data = NULL; |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int cros_typec_init_ports(struct cros_typec_data *typec) |
| 439 | { |
| 440 | struct device *dev = typec->dev; |
| 441 | struct typec_capability *cap; |
| 442 | struct fwnode_handle *fwnode; |
| 443 | struct cros_typec_port *cros_port; |
| 444 | const char *port_prop; |
| 445 | int ret; |
| 446 | int nports; |
| 447 | u32 port_num = 0; |
| 448 | |
| 449 | nports = device_get_child_node_count(dev); |
| 450 | if (nports == 0) { |
| 451 | dev_err(dev, "No port entries found.\n" ); |
| 452 | return -ENODEV; |
| 453 | } |
| 454 | |
| 455 | if (nports > typec->num_ports) { |
| 456 | dev_err(dev, "More ports listed than can be supported.\n" ); |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
| 460 | /* DT uses "reg" to specify port number. */ |
| 461 | port_prop = dev->of_node ? "reg" : "port-number" ; |
| 462 | device_for_each_child_node(dev, fwnode) { |
| 463 | if (fwnode_property_read_u32(fwnode, propname: port_prop, val: &port_num)) { |
| 464 | ret = -EINVAL; |
| 465 | dev_err(dev, "No port-number for port, aborting.\n" ); |
| 466 | goto unregister_ports; |
| 467 | } |
| 468 | |
| 469 | if (port_num >= typec->num_ports) { |
| 470 | dev_err(dev, "Invalid port number.\n" ); |
| 471 | ret = -EINVAL; |
| 472 | goto unregister_ports; |
| 473 | } |
| 474 | |
| 475 | dev_dbg(dev, "Registering port %d\n" , port_num); |
| 476 | |
| 477 | cros_port = devm_kzalloc(dev, size: sizeof(*cros_port), GFP_KERNEL); |
| 478 | if (!cros_port) { |
| 479 | ret = -ENOMEM; |
| 480 | goto unregister_ports; |
| 481 | } |
| 482 | |
| 483 | cros_port->port_num = port_num; |
| 484 | cros_port->typec_data = typec; |
| 485 | typec->ports[port_num] = cros_port; |
| 486 | cap = &cros_port->caps; |
| 487 | |
| 488 | ret = cros_typec_parse_port_props(cap, fwnode, dev); |
| 489 | if (ret < 0) |
| 490 | goto unregister_ports; |
| 491 | |
| 492 | cap->driver_data = cros_port; |
| 493 | cap->ops = &cros_typec_usb_mode_ops; |
| 494 | |
| 495 | cros_port->port = typec_register_port(parent: dev, cap); |
| 496 | if (IS_ERR(ptr: cros_port->port)) { |
| 497 | ret = PTR_ERR(ptr: cros_port->port); |
| 498 | dev_err_probe(dev, err: ret, fmt: "Failed to register port %d\n" , port_num); |
| 499 | goto unregister_ports; |
| 500 | } |
| 501 | |
| 502 | ret = cros_typec_get_switch_handles(port: cros_port, fwnode, dev); |
| 503 | if (ret) { |
| 504 | dev_dbg(dev, "No switch control for port %d, err: %d\n" , port_num, ret); |
| 505 | if (ret == -EPROBE_DEFER) |
| 506 | goto unregister_ports; |
| 507 | } |
| 508 | |
| 509 | ret = cros_typec_register_port_altmodes(typec, port_num); |
| 510 | if (ret) { |
| 511 | dev_err(dev, "Failed to register port altmodes\n" ); |
| 512 | goto unregister_ports; |
| 513 | } |
| 514 | |
| 515 | cros_port->disc_data = devm_kzalloc(dev, EC_PROTO2_MAX_RESPONSE_SIZE, GFP_KERNEL); |
| 516 | if (!cros_port->disc_data) { |
| 517 | ret = -ENOMEM; |
| 518 | goto unregister_ports; |
| 519 | } |
| 520 | |
| 521 | INIT_LIST_HEAD(list: &cros_port->partner_mode_list); |
| 522 | INIT_LIST_HEAD(list: &cros_port->plug_mode_list); |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | |
| 527 | unregister_ports: |
| 528 | fwnode_handle_put(fwnode); |
| 529 | cros_unregister_ports(typec); |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | static int cros_typec_usb_safe_state(struct cros_typec_port *port) |
| 534 | { |
| 535 | int ret; |
| 536 | port->state.mode = TYPEC_STATE_SAFE; |
| 537 | |
| 538 | ret = cros_typec_retimer_set(retimer: port->retimer, state: port->state); |
| 539 | if (!ret) |
| 540 | ret = typec_mux_set(mux: port->mux, state: &port->state); |
| 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * cros_typec_get_cable_vdo() - Get Cable VDO of the connected cable |
| 547 | * @port: Type-C port data |
| 548 | * @svid: Standard or Vendor ID to match |
| 549 | * |
| 550 | * Returns the Cable VDO if match is found and returns 0 if match is not found. |
| 551 | */ |
| 552 | static int cros_typec_get_cable_vdo(struct cros_typec_port *port, u16 svid) |
| 553 | { |
| 554 | struct list_head *head = &port->plug_mode_list; |
| 555 | struct cros_typec_altmode_node *node; |
| 556 | u32 ret = 0; |
| 557 | |
| 558 | list_for_each_entry(node, head, list) { |
| 559 | if (node->amode->svid == svid) |
| 560 | return node->amode->vdo; |
| 561 | } |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Spoof the VDOs that were likely communicated by the partner for TBT alt |
| 568 | * mode. |
| 569 | */ |
| 570 | static int cros_typec_enable_tbt(struct cros_typec_data *typec, |
| 571 | int port_num, |
| 572 | struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 573 | { |
| 574 | struct cros_typec_port *port = typec->ports[port_num]; |
| 575 | struct typec_thunderbolt_data data; |
| 576 | int ret; |
| 577 | |
| 578 | if (typec->pd_ctrl_ver < 2) { |
| 579 | dev_err(typec->dev, |
| 580 | "PD_CTRL version too old: %d\n" , typec->pd_ctrl_ver); |
| 581 | return -ENOTSUPP; |
| 582 | } |
| 583 | |
| 584 | /* Device Discover Mode VDO */ |
| 585 | data.device_mode = TBT_MODE; |
| 586 | |
| 587 | if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER) |
| 588 | data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3); |
| 589 | |
| 590 | /* Cable Discover Mode VDO */ |
| 591 | data.cable_mode = TBT_MODE; |
| 592 | |
| 593 | data.cable_mode |= cros_typec_get_cable_vdo(port, USB_TYPEC_TBT_SID); |
| 594 | |
| 595 | data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed); |
| 596 | |
| 597 | if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE) |
| 598 | data.cable_mode |= TBT_CABLE_OPTICAL; |
| 599 | |
| 600 | if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR) |
| 601 | data.cable_mode |= TBT_CABLE_LINK_TRAINING; |
| 602 | |
| 603 | data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen); |
| 604 | |
| 605 | /* Enter Mode VDO */ |
| 606 | data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed); |
| 607 | |
| 608 | if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE) |
| 609 | data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE; |
| 610 | |
| 611 | if (!port->state.alt) { |
| 612 | port->state.alt = port->port_altmode[CROS_EC_ALTMODE_TBT]; |
| 613 | ret = cros_typec_usb_safe_state(port); |
| 614 | if (ret) |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | port->state.data = &data; |
| 619 | port->state.mode = TYPEC_TBT_MODE; |
| 620 | |
| 621 | return typec_mux_set(mux: port->mux, state: &port->state); |
| 622 | } |
| 623 | |
| 624 | /* Spoof the VDOs that were likely communicated by the partner. */ |
| 625 | static int cros_typec_enable_dp(struct cros_typec_data *typec, |
| 626 | int port_num, |
| 627 | struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 628 | { |
| 629 | struct cros_typec_port *port = typec->ports[port_num]; |
| 630 | struct typec_displayport_data dp_data; |
| 631 | u32 cable_tbt_vdo; |
| 632 | u32 cable_dp_vdo; |
| 633 | int ret; |
| 634 | |
| 635 | if (typec->pd_ctrl_ver < 2) { |
| 636 | dev_err(typec->dev, |
| 637 | "PD_CTRL version too old: %d\n" , typec->pd_ctrl_ver); |
| 638 | return -ENOTSUPP; |
| 639 | } |
| 640 | |
| 641 | if (!pd_ctrl->dp_mode) { |
| 642 | dev_err(typec->dev, "No valid DP mode provided.\n" ); |
| 643 | return -EINVAL; |
| 644 | } |
| 645 | |
| 646 | /* Status VDO. */ |
| 647 | dp_data.status = DP_STATUS_ENABLED; |
| 648 | if (port->mux_flags & USB_PD_MUX_HPD_IRQ) |
| 649 | dp_data.status |= DP_STATUS_IRQ_HPD; |
| 650 | if (port->mux_flags & USB_PD_MUX_HPD_LVL) |
| 651 | dp_data.status |= DP_STATUS_HPD_STATE; |
| 652 | |
| 653 | /* Configuration VDO. */ |
| 654 | dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode); |
| 655 | if (!port->state.alt) { |
| 656 | port->state.alt = port->port_altmode[CROS_EC_ALTMODE_DP]; |
| 657 | ret = cros_typec_usb_safe_state(port); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | port->state.data = &dp_data; |
| 663 | port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode)); |
| 664 | |
| 665 | /* Get cable VDO for cables with DPSID to check DPAM2.1 is supported */ |
| 666 | cable_dp_vdo = cros_typec_get_cable_vdo(port, USB_TYPEC_DP_SID); |
| 667 | |
| 668 | /** |
| 669 | * Get cable VDO for thunderbolt cables and cables with DPSID but does not |
| 670 | * support DPAM2.1. |
| 671 | */ |
| 672 | cable_tbt_vdo = cros_typec_get_cable_vdo(port, USB_TYPEC_TBT_SID); |
| 673 | |
| 674 | if (cable_dp_vdo & DP_CAP_DPAM_VERSION) { |
| 675 | dp_data.conf |= cable_dp_vdo; |
| 676 | } else if (cable_tbt_vdo) { |
| 677 | dp_data.conf |= TBT_CABLE_SPEED(cable_tbt_vdo) << DP_CONF_SIGNALLING_SHIFT; |
| 678 | |
| 679 | /* Cable Type */ |
| 680 | if (cable_tbt_vdo & TBT_CABLE_OPTICAL) |
| 681 | dp_data.conf |= DP_CONF_CABLE_TYPE_OPTICAL << DP_CONF_CABLE_TYPE_SHIFT; |
| 682 | else if (cable_tbt_vdo & TBT_CABLE_RETIMER) |
| 683 | dp_data.conf |= DP_CONF_CABLE_TYPE_RE_TIMER << DP_CONF_CABLE_TYPE_SHIFT; |
| 684 | else if (cable_tbt_vdo & TBT_CABLE_ACTIVE_PASSIVE) |
| 685 | dp_data.conf |= DP_CONF_CABLE_TYPE_RE_DRIVER << DP_CONF_CABLE_TYPE_SHIFT; |
| 686 | } else if (PD_IDH_PTYPE(port->c_identity.id_header) == IDH_PTYPE_PCABLE) { |
| 687 | dp_data.conf |= VDO_TYPEC_CABLE_SPEED(port->c_identity.vdo[0]) << |
| 688 | DP_CONF_SIGNALLING_SHIFT; |
| 689 | } |
| 690 | |
| 691 | ret = cros_typec_retimer_set(retimer: port->retimer, state: port->state); |
| 692 | if (!ret) |
| 693 | ret = typec_mux_set(mux: port->mux, state: &port->state); |
| 694 | |
| 695 | if (!ret) |
| 696 | ret = cros_typec_displayport_status_update(altmode: port->state.alt, |
| 697 | data: port->state.data); |
| 698 | |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | static int cros_typec_enable_usb4(struct cros_typec_data *typec, |
| 703 | int port_num, |
| 704 | struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 705 | { |
| 706 | struct cros_typec_port *port = typec->ports[port_num]; |
| 707 | struct enter_usb_data data; |
| 708 | |
| 709 | data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT; |
| 710 | |
| 711 | /* Cable Speed */ |
| 712 | data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT; |
| 713 | |
| 714 | /* Cable Type */ |
| 715 | if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE) |
| 716 | data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT; |
| 717 | else if (cros_typec_get_cable_vdo(port, USB_TYPEC_TBT_SID) & TBT_CABLE_RETIMER) |
| 718 | data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT; |
| 719 | else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE) |
| 720 | data.eudo |= EUDO_CABLE_TYPE_RE_DRIVER << EUDO_CABLE_TYPE_SHIFT; |
| 721 | |
| 722 | data.active_link_training = !!(pd_ctrl->control_flags & |
| 723 | USB_PD_CTRL_ACTIVE_LINK_UNIDIR); |
| 724 | |
| 725 | port->state.alt = NULL; |
| 726 | port->state.data = &data; |
| 727 | port->state.mode = TYPEC_MODE_USB4; |
| 728 | |
| 729 | return typec_mux_set(mux: port->mux, state: &port->state); |
| 730 | } |
| 731 | |
| 732 | static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num, |
| 733 | struct ec_response_usb_pd_control_v2 *pd_ctrl) |
| 734 | { |
| 735 | struct cros_typec_port *port = typec->ports[port_num]; |
| 736 | struct ec_response_usb_pd_mux_info resp; |
| 737 | struct ec_params_usb_pd_mux_info req = { |
| 738 | .port = port_num, |
| 739 | }; |
| 740 | struct ec_params_usb_pd_mux_ack mux_ack; |
| 741 | enum typec_orientation orientation; |
| 742 | struct cros_typec_altmode_node *node; |
| 743 | int ret; |
| 744 | |
| 745 | ret = cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_USB_PD_MUX_INFO, |
| 746 | outdata: &req, outsize: sizeof(req), indata: &resp, insize: sizeof(resp)); |
| 747 | if (ret < 0) { |
| 748 | dev_warn(typec->dev, "Failed to get mux info for port: %d, err = %d\n" , |
| 749 | port_num, ret); |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | /* No change needs to be made, let's exit early. */ |
| 754 | if (port->mux_flags == resp.flags && port->role == pd_ctrl->role) |
| 755 | return 0; |
| 756 | |
| 757 | port->mux_flags = resp.flags; |
| 758 | port->role = pd_ctrl->role; |
| 759 | |
| 760 | if (port->mux_flags == USB_PD_MUX_NONE) { |
| 761 | ret = cros_typec_usb_disconnect_state(port); |
| 762 | goto mux_ack; |
| 763 | } |
| 764 | |
| 765 | if (port->mux_flags & USB_PD_MUX_POLARITY_INVERTED) |
| 766 | orientation = TYPEC_ORIENTATION_REVERSE; |
| 767 | else |
| 768 | orientation = TYPEC_ORIENTATION_NORMAL; |
| 769 | |
| 770 | ret = typec_switch_set(sw: port->ori_sw, orientation); |
| 771 | if (ret) |
| 772 | return ret; |
| 773 | |
| 774 | ret = usb_role_switch_set_role(sw: typec->ports[port_num]->role_sw, |
| 775 | role: pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA |
| 776 | ? USB_ROLE_HOST : USB_ROLE_DEVICE); |
| 777 | if (ret) |
| 778 | return ret; |
| 779 | |
| 780 | if (port->mux_flags & USB_PD_MUX_USB4_ENABLED) { |
| 781 | ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl); |
| 782 | } else if (port->mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) { |
| 783 | ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl); |
| 784 | } else if (port->mux_flags & USB_PD_MUX_DP_ENABLED) { |
| 785 | ret = cros_typec_enable_dp(typec, port_num, pd_ctrl); |
| 786 | } else if (port->mux_flags & USB_PD_MUX_SAFE_MODE) { |
| 787 | ret = cros_typec_usb_safe_state(port); |
| 788 | } else if (port->mux_flags & USB_PD_MUX_USB_ENABLED) { |
| 789 | port->state.alt = NULL; |
| 790 | port->state.mode = TYPEC_STATE_USB; |
| 791 | |
| 792 | ret = cros_typec_retimer_set(retimer: port->retimer, state: port->state); |
| 793 | if (!ret) |
| 794 | ret = typec_mux_set(mux: port->mux, state: &port->state); |
| 795 | } else { |
| 796 | dev_dbg(typec->dev, |
| 797 | "Unrecognized mode requested, mux flags: %x\n" , |
| 798 | port->mux_flags); |
| 799 | } |
| 800 | |
| 801 | /* Iterate all partner alt-modes and set the active alternate mode. */ |
| 802 | list_for_each_entry(node, &port->partner_mode_list, list) { |
| 803 | typec_altmode_update_active( |
| 804 | alt: node->amode, |
| 805 | active: port->state.alt && |
| 806 | node->amode->svid == port->state.alt->svid); |
| 807 | } |
| 808 | |
| 809 | mux_ack: |
| 810 | if (!typec->needs_mux_ack) |
| 811 | return ret; |
| 812 | |
| 813 | /* Sending Acknowledgment to EC */ |
| 814 | mux_ack.port = port_num; |
| 815 | |
| 816 | if (cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_USB_PD_MUX_ACK, outdata: &mux_ack, |
| 817 | outsize: sizeof(mux_ack), NULL, insize: 0) < 0) |
| 818 | dev_warn(typec->dev, |
| 819 | "Failed to send Mux ACK to EC for port: %d\n" , |
| 820 | port_num); |
| 821 | |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | static void cros_typec_set_port_params_v0(struct cros_typec_data *typec, |
| 826 | int port_num, struct ec_response_usb_pd_control *resp) |
| 827 | { |
| 828 | struct typec_port *port = typec->ports[port_num]->port; |
| 829 | enum typec_orientation polarity; |
| 830 | |
| 831 | if (!resp->enabled) |
| 832 | polarity = TYPEC_ORIENTATION_NONE; |
| 833 | else if (!resp->polarity) |
| 834 | polarity = TYPEC_ORIENTATION_NORMAL; |
| 835 | else |
| 836 | polarity = TYPEC_ORIENTATION_REVERSE; |
| 837 | |
| 838 | typec_set_pwr_role(port, role: resp->role ? TYPEC_SOURCE : TYPEC_SINK); |
| 839 | typec_set_orientation(port, orientation: polarity); |
| 840 | } |
| 841 | |
| 842 | static void cros_typec_set_port_params_v1(struct cros_typec_data *typec, |
| 843 | int port_num, struct ec_response_usb_pd_control_v1 *resp) |
| 844 | { |
| 845 | struct typec_port *port = typec->ports[port_num]->port; |
| 846 | enum typec_orientation polarity; |
| 847 | bool pd_en; |
| 848 | int ret; |
| 849 | |
| 850 | if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED)) |
| 851 | polarity = TYPEC_ORIENTATION_NONE; |
| 852 | else if (!resp->polarity) |
| 853 | polarity = TYPEC_ORIENTATION_NORMAL; |
| 854 | else |
| 855 | polarity = TYPEC_ORIENTATION_REVERSE; |
| 856 | typec_set_orientation(port, orientation: polarity); |
| 857 | typec_set_data_role(port, role: resp->role & PD_CTRL_RESP_ROLE_DATA ? |
| 858 | TYPEC_HOST : TYPEC_DEVICE); |
| 859 | typec_set_pwr_role(port, role: resp->role & PD_CTRL_RESP_ROLE_POWER ? |
| 860 | TYPEC_SOURCE : TYPEC_SINK); |
| 861 | typec_set_vconn_role(port, role: resp->role & PD_CTRL_RESP_ROLE_VCONN ? |
| 862 | TYPEC_SOURCE : TYPEC_SINK); |
| 863 | |
| 864 | /* Register/remove partners when a connect/disconnect occurs. */ |
| 865 | if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) { |
| 866 | if (typec->ports[port_num]->partner) |
| 867 | return; |
| 868 | |
| 869 | pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE; |
| 870 | ret = cros_typec_add_partner(typec, port_num, pd_en); |
| 871 | if (ret) |
| 872 | dev_warn(typec->dev, |
| 873 | "Failed to register partner on port: %d\n" , |
| 874 | port_num); |
| 875 | } else { |
| 876 | cros_typec_remove_partner(typec, port_num); |
| 877 | cros_typec_remove_cable(typec, port_num); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Helper function to register partner/plug altmodes. |
| 883 | */ |
| 884 | static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_num, |
| 885 | bool is_partner) |
| 886 | { |
| 887 | struct cros_typec_port *port = typec->ports[port_num]; |
| 888 | struct ec_response_typec_discovery *sop_disc = port->disc_data; |
| 889 | struct cros_typec_altmode_node *node; |
| 890 | struct typec_altmode_desc desc; |
| 891 | struct typec_altmode *amode; |
| 892 | int num_altmodes = 0; |
| 893 | int ret = 0; |
| 894 | int i, j; |
| 895 | |
| 896 | for (i = 0; i < sop_disc->svid_count; i++) { |
| 897 | for (j = 0; j < sop_disc->svids[i].mode_count; j++) { |
| 898 | memset(&desc, 0, sizeof(desc)); |
| 899 | desc.svid = sop_disc->svids[i].svid; |
| 900 | desc.mode = j + 1; |
| 901 | desc.vdo = sop_disc->svids[i].mode_vdo[j]; |
| 902 | |
| 903 | if (is_partner) |
| 904 | amode = typec_partner_register_altmode(partner: port->partner, desc: &desc); |
| 905 | else |
| 906 | amode = typec_plug_register_altmode(plug: port->plug, desc: &desc); |
| 907 | |
| 908 | if (IS_ERR(ptr: amode)) { |
| 909 | ret = PTR_ERR(ptr: amode); |
| 910 | goto err_cleanup; |
| 911 | } |
| 912 | |
| 913 | /* If no memory is available we should unregister and exit. */ |
| 914 | node = devm_kzalloc(dev: typec->dev, size: sizeof(*node), GFP_KERNEL); |
| 915 | if (!node) { |
| 916 | ret = -ENOMEM; |
| 917 | typec_unregister_altmode(altmode: amode); |
| 918 | goto err_cleanup; |
| 919 | } |
| 920 | |
| 921 | node->amode = amode; |
| 922 | |
| 923 | if (is_partner) |
| 924 | list_add_tail(new: &node->list, head: &port->partner_mode_list); |
| 925 | else |
| 926 | list_add_tail(new: &node->list, head: &port->plug_mode_list); |
| 927 | num_altmodes++; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (is_partner) |
| 932 | ret = typec_partner_set_num_altmodes(partner: port->partner, num_altmodes); |
| 933 | else |
| 934 | ret = typec_plug_set_num_altmodes(plug: port->plug, num_altmodes); |
| 935 | |
| 936 | if (ret < 0) { |
| 937 | dev_err(typec->dev, "Unable to set %s num_altmodes for port: %d\n" , |
| 938 | is_partner ? "partner" : "plug" , port_num); |
| 939 | goto err_cleanup; |
| 940 | } |
| 941 | |
| 942 | return 0; |
| 943 | |
| 944 | err_cleanup: |
| 945 | cros_typec_unregister_altmodes(typec, port_num, is_partner); |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Parse the PD identity data from the EC PD discovery responses and copy that to the supplied |
| 951 | * PD identity struct. |
| 952 | */ |
| 953 | static void cros_typec_parse_pd_identity(struct usb_pd_identity *id, |
| 954 | struct ec_response_typec_discovery *disc) |
| 955 | { |
| 956 | int i; |
| 957 | |
| 958 | /* First, update the PD identity VDOs for the partner. */ |
| 959 | if (disc->identity_count > 0) |
| 960 | id->id_header = disc->discovery_vdo[0]; |
| 961 | if (disc->identity_count > 1) |
| 962 | id->cert_stat = disc->discovery_vdo[1]; |
| 963 | if (disc->identity_count > 2) |
| 964 | id->product = disc->discovery_vdo[2]; |
| 965 | |
| 966 | /* Copy the remaining identity VDOs till a maximum of 6. */ |
| 967 | for (i = 3; i < disc->identity_count && i < VDO_MAX_OBJECTS; i++) |
| 968 | id->vdo[i - 3] = disc->discovery_vdo[i]; |
| 969 | } |
| 970 | |
| 971 | static int cros_typec_handle_sop_prime_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision) |
| 972 | { |
| 973 | struct cros_typec_port *port = typec->ports[port_num]; |
| 974 | struct ec_response_typec_discovery *disc = port->disc_data; |
| 975 | struct typec_cable_desc c_desc = {}; |
| 976 | struct typec_plug_desc p_desc; |
| 977 | struct ec_params_typec_discovery req = { |
| 978 | .port = port_num, |
| 979 | .partner_type = TYPEC_PARTNER_SOP_PRIME, |
| 980 | }; |
| 981 | u32 cable_plug_type; |
| 982 | int ret = 0; |
| 983 | |
| 984 | memset(disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE); |
| 985 | ret = cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_TYPEC_DISCOVERY, outdata: &req, outsize: sizeof(req), |
| 986 | indata: disc, EC_PROTO2_MAX_RESPONSE_SIZE); |
| 987 | if (ret < 0) { |
| 988 | dev_err(typec->dev, "Failed to get SOP' discovery data for port: %d\n" , port_num); |
| 989 | goto sop_prime_disc_exit; |
| 990 | } |
| 991 | |
| 992 | /* Parse the PD identity data, even if only 0s were returned. */ |
| 993 | cros_typec_parse_pd_identity(id: &port->c_identity, disc); |
| 994 | |
| 995 | if (disc->identity_count != 0) { |
| 996 | cable_plug_type = VDO_TYPEC_CABLE_TYPE(port->c_identity.vdo[0]); |
| 997 | switch (cable_plug_type) { |
| 998 | case CABLE_ATYPE: |
| 999 | c_desc.type = USB_PLUG_TYPE_A; |
| 1000 | break; |
| 1001 | case CABLE_BTYPE: |
| 1002 | c_desc.type = USB_PLUG_TYPE_B; |
| 1003 | break; |
| 1004 | case CABLE_CTYPE: |
| 1005 | c_desc.type = USB_PLUG_TYPE_C; |
| 1006 | break; |
| 1007 | case CABLE_CAPTIVE: |
| 1008 | c_desc.type = USB_PLUG_CAPTIVE; |
| 1009 | break; |
| 1010 | default: |
| 1011 | c_desc.type = USB_PLUG_NONE; |
| 1012 | } |
| 1013 | c_desc.active = PD_IDH_PTYPE(port->c_identity.id_header) == IDH_PTYPE_ACABLE; |
| 1014 | } |
| 1015 | |
| 1016 | c_desc.identity = &port->c_identity; |
| 1017 | c_desc.pd_revision = pd_revision; |
| 1018 | |
| 1019 | port->cable = typec_register_cable(port: port->port, desc: &c_desc); |
| 1020 | if (IS_ERR(ptr: port->cable)) { |
| 1021 | ret = PTR_ERR(ptr: port->cable); |
| 1022 | port->cable = NULL; |
| 1023 | goto sop_prime_disc_exit; |
| 1024 | } |
| 1025 | |
| 1026 | p_desc.index = TYPEC_PLUG_SOP_P; |
| 1027 | port->plug = typec_register_plug(cable: port->cable, desc: &p_desc); |
| 1028 | if (IS_ERR(ptr: port->plug)) { |
| 1029 | ret = PTR_ERR(ptr: port->plug); |
| 1030 | port->plug = NULL; |
| 1031 | goto sop_prime_disc_exit; |
| 1032 | } |
| 1033 | |
| 1034 | ret = cros_typec_register_altmodes(typec, port_num, is_partner: false); |
| 1035 | if (ret < 0) { |
| 1036 | dev_err(typec->dev, "Failed to register plug altmodes, port: %d\n" , port_num); |
| 1037 | goto sop_prime_disc_exit; |
| 1038 | } |
| 1039 | |
| 1040 | return 0; |
| 1041 | |
| 1042 | sop_prime_disc_exit: |
| 1043 | cros_typec_remove_cable(typec, port_num); |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | static int cros_typec_handle_sop_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision) |
| 1048 | { |
| 1049 | struct cros_typec_port *port = typec->ports[port_num]; |
| 1050 | struct ec_response_typec_discovery *sop_disc = port->disc_data; |
| 1051 | struct ec_params_typec_discovery req = { |
| 1052 | .port = port_num, |
| 1053 | .partner_type = TYPEC_PARTNER_SOP, |
| 1054 | }; |
| 1055 | int ret = 0; |
| 1056 | |
| 1057 | if (!port->partner) { |
| 1058 | dev_err(typec->dev, |
| 1059 | "SOP Discovery received without partner registered, port: %d\n" , |
| 1060 | port_num); |
| 1061 | ret = -EINVAL; |
| 1062 | goto disc_exit; |
| 1063 | } |
| 1064 | |
| 1065 | typec_partner_set_pd_revision(partner: port->partner, pd_revision); |
| 1066 | |
| 1067 | memset(sop_disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE); |
| 1068 | ret = cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_TYPEC_DISCOVERY, outdata: &req, outsize: sizeof(req), |
| 1069 | indata: sop_disc, EC_PROTO2_MAX_RESPONSE_SIZE); |
| 1070 | if (ret < 0) { |
| 1071 | dev_err(typec->dev, "Failed to get SOP discovery data for port: %d\n" , port_num); |
| 1072 | goto disc_exit; |
| 1073 | } |
| 1074 | |
| 1075 | cros_typec_parse_pd_identity(id: &port->p_identity, disc: sop_disc); |
| 1076 | |
| 1077 | ret = typec_partner_set_identity(partner: port->partner); |
| 1078 | if (ret < 0) { |
| 1079 | dev_err(typec->dev, "Failed to update partner PD identity, port: %d\n" , port_num); |
| 1080 | goto disc_exit; |
| 1081 | } |
| 1082 | |
| 1083 | ret = cros_typec_register_altmodes(typec, port_num, is_partner: true); |
| 1084 | if (ret < 0) { |
| 1085 | dev_err(typec->dev, "Failed to register partner altmodes, port: %d\n" , port_num); |
| 1086 | goto disc_exit; |
| 1087 | } |
| 1088 | |
| 1089 | disc_exit: |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | static int cros_typec_send_clear_event(struct cros_typec_data *typec, int port_num, u32 events_mask) |
| 1094 | { |
| 1095 | struct ec_params_typec_control req = { |
| 1096 | .port = port_num, |
| 1097 | .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS, |
| 1098 | .clear_events_mask = events_mask, |
| 1099 | }; |
| 1100 | |
| 1101 | return cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_TYPEC_CONTROL, outdata: &req, |
| 1102 | outsize: sizeof(req), NULL, insize: 0); |
| 1103 | } |
| 1104 | |
| 1105 | static void cros_typec_register_partner_pdos(struct cros_typec_data *typec, |
| 1106 | struct ec_response_typec_status *resp, int port_num) |
| 1107 | { |
| 1108 | struct usb_power_delivery_capabilities_desc caps_desc = {}; |
| 1109 | struct usb_power_delivery_desc desc = { |
| 1110 | .revision = (le16_to_cpu(resp->sop_revision) & 0xff00) >> 4, |
| 1111 | }; |
| 1112 | struct cros_typec_port *port = typec->ports[port_num]; |
| 1113 | |
| 1114 | if (!port->partner || port->partner_pd) |
| 1115 | return; |
| 1116 | |
| 1117 | /* If no caps are available, don't bother creating a device. */ |
| 1118 | if (!resp->source_cap_count && !resp->sink_cap_count) |
| 1119 | return; |
| 1120 | |
| 1121 | port->partner_pd = typec_partner_usb_power_delivery_register(partner: port->partner, desc: &desc); |
| 1122 | if (IS_ERR(ptr: port->partner_pd)) { |
| 1123 | dev_warn(typec->dev, "Failed to register partner PD device, port: %d\n" , port_num); |
| 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | typec_partner_set_usb_power_delivery(partner: port->partner, pd: port->partner_pd); |
| 1128 | |
| 1129 | memcpy(caps_desc.pdo, resp->source_cap_pdos, sizeof(u32) * resp->source_cap_count); |
| 1130 | caps_desc.role = TYPEC_SOURCE; |
| 1131 | port->partner_src_caps = usb_power_delivery_register_capabilities(pd: port->partner_pd, |
| 1132 | desc: &caps_desc); |
| 1133 | if (IS_ERR(ptr: port->partner_src_caps)) |
| 1134 | dev_warn(typec->dev, "Failed to register source caps, port: %d\n" , port_num); |
| 1135 | |
| 1136 | memset(&caps_desc, 0, sizeof(caps_desc)); |
| 1137 | memcpy(caps_desc.pdo, resp->sink_cap_pdos, sizeof(u32) * resp->sink_cap_count); |
| 1138 | caps_desc.role = TYPEC_SINK; |
| 1139 | port->partner_sink_caps = usb_power_delivery_register_capabilities(pd: port->partner_pd, |
| 1140 | desc: &caps_desc); |
| 1141 | if (IS_ERR(ptr: port->partner_sink_caps)) |
| 1142 | dev_warn(typec->dev, "Failed to register sink caps, port: %d\n" , port_num); |
| 1143 | } |
| 1144 | |
| 1145 | static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num) |
| 1146 | { |
| 1147 | struct ec_response_typec_status resp; |
| 1148 | struct ec_params_typec_status req = { |
| 1149 | .port = port_num, |
| 1150 | }; |
| 1151 | int ret; |
| 1152 | |
| 1153 | ret = cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_TYPEC_STATUS, outdata: &req, outsize: sizeof(req), |
| 1154 | indata: &resp, insize: sizeof(resp)); |
| 1155 | if (ret < 0) { |
| 1156 | dev_warn(typec->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n" , port_num); |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | /* If we got a hard reset, unregister everything and return. */ |
| 1161 | if (resp.events & PD_STATUS_EVENT_HARD_RESET) { |
| 1162 | cros_typec_remove_partner(typec, port_num); |
| 1163 | cros_typec_remove_cable(typec, port_num); |
| 1164 | |
| 1165 | ret = cros_typec_send_clear_event(typec, port_num, |
| 1166 | PD_STATUS_EVENT_HARD_RESET); |
| 1167 | if (ret < 0) |
| 1168 | dev_warn(typec->dev, |
| 1169 | "Failed hard reset event clear, port: %d\n" , port_num); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | /* Handle any events appropriately. */ |
| 1174 | if (resp.events & PD_STATUS_EVENT_SOP_DISC_DONE && !typec->ports[port_num]->sop_disc_done) { |
| 1175 | u16 sop_revision; |
| 1176 | |
| 1177 | /* Convert BCD to the format preferred by the TypeC framework */ |
| 1178 | sop_revision = (le16_to_cpu(resp.sop_revision) & 0xff00) >> 4; |
| 1179 | ret = cros_typec_handle_sop_disc(typec, port_num, pd_revision: sop_revision); |
| 1180 | if (ret < 0) |
| 1181 | dev_err(typec->dev, "Couldn't parse SOP Disc data, port: %d\n" , port_num); |
| 1182 | else { |
| 1183 | typec->ports[port_num]->sop_disc_done = true; |
| 1184 | ret = cros_typec_send_clear_event(typec, port_num, |
| 1185 | PD_STATUS_EVENT_SOP_DISC_DONE); |
| 1186 | if (ret < 0) |
| 1187 | dev_warn(typec->dev, |
| 1188 | "Failed SOP Disc event clear, port: %d\n" , port_num); |
| 1189 | } |
| 1190 | if (resp.sop_connected) |
| 1191 | typec_set_pwr_opmode(port: typec->ports[port_num]->port, mode: TYPEC_PWR_MODE_PD); |
| 1192 | |
| 1193 | cros_typec_register_partner_pdos(typec, resp: &resp, port_num); |
| 1194 | } |
| 1195 | |
| 1196 | if (resp.events & PD_STATUS_EVENT_SOP_PRIME_DISC_DONE && |
| 1197 | !typec->ports[port_num]->sop_prime_disc_done) { |
| 1198 | u16 sop_prime_revision; |
| 1199 | |
| 1200 | /* Convert BCD to the format preferred by the TypeC framework */ |
| 1201 | sop_prime_revision = (le16_to_cpu(resp.sop_prime_revision) & 0xff00) >> 4; |
| 1202 | ret = cros_typec_handle_sop_prime_disc(typec, port_num, pd_revision: sop_prime_revision); |
| 1203 | if (ret < 0) |
| 1204 | dev_err(typec->dev, "Couldn't parse SOP' Disc data, port: %d\n" , port_num); |
| 1205 | else { |
| 1206 | typec->ports[port_num]->sop_prime_disc_done = true; |
| 1207 | ret = cros_typec_send_clear_event(typec, port_num, |
| 1208 | PD_STATUS_EVENT_SOP_PRIME_DISC_DONE); |
| 1209 | if (ret < 0) |
| 1210 | dev_warn(typec->dev, |
| 1211 | "Failed SOP Disc event clear, port: %d\n" , port_num); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if (resp.events & PD_STATUS_EVENT_VDM_REQ_REPLY) { |
| 1216 | cros_typec_handle_vdm_response(typec, port_num); |
| 1217 | ret = cros_typec_send_clear_event(typec, port_num, PD_STATUS_EVENT_VDM_REQ_REPLY); |
| 1218 | if (ret < 0) |
| 1219 | dev_warn(typec->dev, "Failed VDM Reply event clear, port: %d\n" , port_num); |
| 1220 | } |
| 1221 | |
| 1222 | if (resp.events & PD_STATUS_EVENT_VDM_ATTENTION) { |
| 1223 | cros_typec_handle_vdm_attention(typec, port_num); |
| 1224 | ret = cros_typec_send_clear_event(typec, port_num, PD_STATUS_EVENT_VDM_ATTENTION); |
| 1225 | if (ret < 0) |
| 1226 | dev_warn(typec->dev, "Failed VDM attention event clear, port: %d\n" , |
| 1227 | port_num); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | static int cros_typec_port_update(struct cros_typec_data *typec, int port_num) |
| 1232 | { |
| 1233 | struct ec_params_usb_pd_control req; |
| 1234 | struct ec_response_usb_pd_control_v2 resp; |
| 1235 | int ret; |
| 1236 | |
| 1237 | if (port_num < 0 || port_num >= typec->num_ports) { |
| 1238 | dev_err(typec->dev, "cannot get status for invalid port %d\n" , |
| 1239 | port_num); |
| 1240 | return -EINVAL; |
| 1241 | } |
| 1242 | |
| 1243 | req.port = port_num; |
| 1244 | req.role = USB_PD_CTRL_ROLE_NO_CHANGE; |
| 1245 | req.mux = USB_PD_CTRL_MUX_NO_CHANGE; |
| 1246 | req.swap = USB_PD_CTRL_SWAP_NONE; |
| 1247 | |
| 1248 | ret = cros_ec_cmd(ec_dev: typec->ec, version: typec->pd_ctrl_ver, |
| 1249 | EC_CMD_USB_PD_CONTROL, outdata: &req, outsize: sizeof(req), |
| 1250 | indata: &resp, insize: sizeof(resp)); |
| 1251 | if (ret < 0) |
| 1252 | return ret; |
| 1253 | |
| 1254 | /* Update the switches if they exist, according to requested state */ |
| 1255 | ret = cros_typec_configure_mux(typec, port_num, pd_ctrl: &resp); |
| 1256 | if (ret) |
| 1257 | dev_warn(typec->dev, "Configure muxes failed, err = %d\n" , ret); |
| 1258 | |
| 1259 | dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n" , port_num, resp.enabled); |
| 1260 | dev_dbg(typec->dev, "Role %d: 0x%hhx\n" , port_num, resp.role); |
| 1261 | dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n" , port_num, resp.polarity); |
| 1262 | dev_dbg(typec->dev, "State %d: %s\n" , port_num, resp.state); |
| 1263 | |
| 1264 | if (typec->pd_ctrl_ver != 0) |
| 1265 | cros_typec_set_port_params_v1(typec, port_num, |
| 1266 | resp: (struct ec_response_usb_pd_control_v1 *)&resp); |
| 1267 | else |
| 1268 | cros_typec_set_port_params_v0(typec, port_num, |
| 1269 | resp: (struct ec_response_usb_pd_control *) &resp); |
| 1270 | |
| 1271 | if (typec->typec_cmd_supported) |
| 1272 | cros_typec_handle_status(typec, port_num); |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int cros_typec_get_cmd_version(struct cros_typec_data *typec) |
| 1278 | { |
| 1279 | struct ec_params_get_cmd_versions_v1 req_v1; |
| 1280 | struct ec_response_get_cmd_versions resp; |
| 1281 | int ret; |
| 1282 | |
| 1283 | /* We're interested in the PD control command version. */ |
| 1284 | req_v1.cmd = EC_CMD_USB_PD_CONTROL; |
| 1285 | ret = cros_ec_cmd(ec_dev: typec->ec, version: 1, EC_CMD_GET_CMD_VERSIONS, |
| 1286 | outdata: &req_v1, outsize: sizeof(req_v1), indata: &resp, insize: sizeof(resp)); |
| 1287 | if (ret < 0) |
| 1288 | return ret; |
| 1289 | |
| 1290 | if (resp.version_mask & EC_VER_MASK(2)) |
| 1291 | typec->pd_ctrl_ver = 2; |
| 1292 | else if (resp.version_mask & EC_VER_MASK(1)) |
| 1293 | typec->pd_ctrl_ver = 1; |
| 1294 | else |
| 1295 | typec->pd_ctrl_ver = 0; |
| 1296 | |
| 1297 | dev_dbg(typec->dev, "PD Control has version mask 0x%02x\n" , |
| 1298 | typec->pd_ctrl_ver & 0xff); |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static void cros_typec_port_work(struct work_struct *work) |
| 1304 | { |
| 1305 | struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work); |
| 1306 | int ret, i; |
| 1307 | |
| 1308 | for (i = 0; i < typec->num_ports; i++) { |
| 1309 | ret = cros_typec_port_update(typec, port_num: i); |
| 1310 | if (ret < 0) |
| 1311 | dev_warn(typec->dev, "Update failed for port: %d\n" , i); |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | static int cros_ec_typec_event(struct notifier_block *nb, |
| 1316 | unsigned long host_event, void *_notify) |
| 1317 | { |
| 1318 | struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb); |
| 1319 | |
| 1320 | flush_work(work: &typec->port_work); |
| 1321 | schedule_work(work: &typec->port_work); |
| 1322 | |
| 1323 | return NOTIFY_OK; |
| 1324 | } |
| 1325 | |
| 1326 | #ifdef CONFIG_ACPI |
| 1327 | static const struct acpi_device_id cros_typec_acpi_id[] = { |
| 1328 | { "GOOG0014" , 0 }, |
| 1329 | {} |
| 1330 | }; |
| 1331 | MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id); |
| 1332 | #endif |
| 1333 | |
| 1334 | #ifdef CONFIG_OF |
| 1335 | static const struct of_device_id cros_typec_of_match[] = { |
| 1336 | { .compatible = "google,cros-ec-typec" , }, |
| 1337 | {} |
| 1338 | }; |
| 1339 | MODULE_DEVICE_TABLE(of, cros_typec_of_match); |
| 1340 | #endif |
| 1341 | |
| 1342 | static int cros_typec_probe(struct platform_device *pdev) |
| 1343 | { |
| 1344 | struct cros_ec_dev *ec_dev = NULL; |
| 1345 | struct device *dev = &pdev->dev; |
| 1346 | struct cros_typec_data *typec; |
| 1347 | struct ec_response_usb_pd_ports resp; |
| 1348 | int ret, i; |
| 1349 | |
| 1350 | typec = devm_kzalloc(dev, size: sizeof(*typec), GFP_KERNEL); |
| 1351 | if (!typec) |
| 1352 | return -ENOMEM; |
| 1353 | |
| 1354 | typec->dev = dev; |
| 1355 | |
| 1356 | typec->ec = dev_get_drvdata(dev: pdev->dev.parent); |
| 1357 | if (!typec->ec || !typec->ec->ec) { |
| 1358 | dev_warn(dev, "couldn't find parent EC device\n" ); |
| 1359 | return -EPROBE_DEFER; |
| 1360 | } |
| 1361 | |
| 1362 | platform_set_drvdata(pdev, data: typec); |
| 1363 | |
| 1364 | ret = cros_typec_get_cmd_version(typec); |
| 1365 | if (ret < 0) { |
| 1366 | dev_err(dev, "failed to get PD command version info\n" ); |
| 1367 | return ret; |
| 1368 | } |
| 1369 | |
| 1370 | ec_dev = dev_get_drvdata(dev: &typec->ec->ec->dev); |
| 1371 | if (!ec_dev) |
| 1372 | return -EPROBE_DEFER; |
| 1373 | |
| 1374 | typec->typec_cmd_supported = cros_ec_check_features(ec: ec_dev, feature: EC_FEATURE_TYPEC_CMD); |
| 1375 | typec->needs_mux_ack = cros_ec_check_features(ec: ec_dev, feature: EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK); |
| 1376 | typec->ap_driven_altmode = cros_ec_check_features( |
| 1377 | ec: ec_dev, feature: EC_FEATURE_TYPEC_REQUIRE_AP_MODE_ENTRY); |
| 1378 | |
| 1379 | ret = cros_ec_cmd(ec_dev: typec->ec, version: 0, EC_CMD_USB_PD_PORTS, NULL, outsize: 0, |
| 1380 | indata: &resp, insize: sizeof(resp)); |
| 1381 | if (ret < 0) |
| 1382 | return ret; |
| 1383 | |
| 1384 | typec->num_ports = resp.num_ports; |
| 1385 | if (typec->num_ports > EC_USB_PD_MAX_PORTS) { |
| 1386 | dev_warn(typec->dev, |
| 1387 | "Too many ports reported: %d, limiting to max: %d\n" , |
| 1388 | typec->num_ports, EC_USB_PD_MAX_PORTS); |
| 1389 | typec->num_ports = EC_USB_PD_MAX_PORTS; |
| 1390 | } |
| 1391 | |
| 1392 | ret = cros_typec_init_ports(typec); |
| 1393 | if (ret < 0) |
| 1394 | return ret; |
| 1395 | |
| 1396 | INIT_WORK(&typec->port_work, cros_typec_port_work); |
| 1397 | |
| 1398 | /* |
| 1399 | * Safe to call port update here, since we haven't registered the |
| 1400 | * PD notifier yet. |
| 1401 | */ |
| 1402 | for (i = 0; i < typec->num_ports; i++) { |
| 1403 | ret = cros_typec_port_update(typec, port_num: i); |
| 1404 | if (ret < 0) |
| 1405 | goto unregister_ports; |
| 1406 | } |
| 1407 | |
| 1408 | typec->nb.notifier_call = cros_ec_typec_event; |
| 1409 | ret = cros_usbpd_register_notify(nb: &typec->nb); |
| 1410 | if (ret < 0) |
| 1411 | goto unregister_ports; |
| 1412 | |
| 1413 | return 0; |
| 1414 | |
| 1415 | unregister_ports: |
| 1416 | cros_unregister_ports(typec); |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
| 1420 | static void cros_typec_remove(struct platform_device *pdev) |
| 1421 | { |
| 1422 | struct cros_typec_data *typec = platform_get_drvdata(pdev); |
| 1423 | |
| 1424 | cros_usbpd_unregister_notify(nb: &typec->nb); |
| 1425 | cancel_work_sync(work: &typec->port_work); |
| 1426 | cros_unregister_ports(typec); |
| 1427 | } |
| 1428 | |
| 1429 | static int __maybe_unused cros_typec_suspend(struct device *dev) |
| 1430 | { |
| 1431 | struct cros_typec_data *typec = dev_get_drvdata(dev); |
| 1432 | |
| 1433 | cancel_work_sync(work: &typec->port_work); |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | static int __maybe_unused cros_typec_resume(struct device *dev) |
| 1439 | { |
| 1440 | struct cros_typec_data *typec = dev_get_drvdata(dev); |
| 1441 | |
| 1442 | /* Refresh port state. */ |
| 1443 | schedule_work(work: &typec->port_work); |
| 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | static const struct dev_pm_ops cros_typec_pm_ops = { |
| 1449 | SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume) |
| 1450 | }; |
| 1451 | |
| 1452 | static struct platform_driver cros_typec_driver = { |
| 1453 | .driver = { |
| 1454 | .name = DRV_NAME, |
| 1455 | .acpi_match_table = ACPI_PTR(cros_typec_acpi_id), |
| 1456 | .of_match_table = of_match_ptr(cros_typec_of_match), |
| 1457 | .pm = &cros_typec_pm_ops, |
| 1458 | }, |
| 1459 | .probe = cros_typec_probe, |
| 1460 | .remove = cros_typec_remove, |
| 1461 | }; |
| 1462 | |
| 1463 | module_platform_driver(cros_typec_driver); |
| 1464 | |
| 1465 | MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>" ); |
| 1466 | MODULE_DESCRIPTION("Chrome OS EC Type C control" ); |
| 1467 | MODULE_LICENSE("GPL" ); |
| 1468 | |