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