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 cros_unregister_ports(typec);
394 return ret;
395 }
396
cros_typec_usb_safe_state(struct cros_typec_port * port)397 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
398 {
399 int ret;
400 port->state.mode = TYPEC_STATE_SAFE;
401
402 ret = cros_typec_retimer_set(port->retimer, port->state);
403 if (!ret)
404 ret = typec_mux_set(port->mux, &port->state);
405
406 return ret;
407 }
408
409 /**
410 * cros_typec_get_cable_vdo() - Get Cable VDO of the connected cable
411 * @port: Type-C port data
412 * @svid: Standard or Vendor ID to match
413 *
414 * Returns the Cable VDO if match is found and returns 0 if match is not found.
415 */
cros_typec_get_cable_vdo(struct cros_typec_port * port,u16 svid)416 static int cros_typec_get_cable_vdo(struct cros_typec_port *port, u16 svid)
417 {
418 struct list_head *head = &port->plug_mode_list;
419 struct cros_typec_altmode_node *node;
420 u32 ret = 0;
421
422 list_for_each_entry(node, head, list) {
423 if (node->amode->svid == svid)
424 return node->amode->vdo;
425 }
426
427 return ret;
428 }
429
430 /*
431 * Spoof the VDOs that were likely communicated by the partner for TBT alt
432 * mode.
433 */
cros_typec_enable_tbt(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)434 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
435 int port_num,
436 struct ec_response_usb_pd_control_v2 *pd_ctrl)
437 {
438 struct cros_typec_port *port = typec->ports[port_num];
439 struct typec_thunderbolt_data data;
440 int ret;
441
442 if (typec->pd_ctrl_ver < 2) {
443 dev_err(typec->dev,
444 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
445 return -ENOTSUPP;
446 }
447
448 /* Device Discover Mode VDO */
449 data.device_mode = TBT_MODE;
450
451 if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
452 data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
453
454 /* Cable Discover Mode VDO */
455 data.cable_mode = TBT_MODE;
456
457 data.cable_mode |= cros_typec_get_cable_vdo(port, USB_TYPEC_TBT_SID);
458
459 data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
460
461 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
462 data.cable_mode |= TBT_CABLE_OPTICAL;
463
464 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
465 data.cable_mode |= TBT_CABLE_LINK_TRAINING;
466
467 data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen);
468
469 /* Enter Mode VDO */
470 data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
471
472 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
473 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
474
475 if (!port->state.alt) {
476 port->state.alt = port->port_altmode[CROS_EC_ALTMODE_TBT];
477 ret = cros_typec_usb_safe_state(port);
478 if (ret)
479 return ret;
480 }
481
482 port->state.data = &data;
483 port->state.mode = TYPEC_TBT_MODE;
484
485 return typec_mux_set(port->mux, &port->state);
486 }
487
488 /* 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)489 static int cros_typec_enable_dp(struct cros_typec_data *typec,
490 int port_num,
491 struct ec_response_usb_pd_control_v2 *pd_ctrl)
492 {
493 struct cros_typec_port *port = typec->ports[port_num];
494 struct typec_displayport_data dp_data;
495 int ret;
496
497 if (typec->pd_ctrl_ver < 2) {
498 dev_err(typec->dev,
499 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
500 return -ENOTSUPP;
501 }
502
503 if (!pd_ctrl->dp_mode) {
504 dev_err(typec->dev, "No valid DP mode provided.\n");
505 return -EINVAL;
506 }
507
508 /* Status VDO. */
509 dp_data.status = DP_STATUS_ENABLED;
510 if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
511 dp_data.status |= DP_STATUS_IRQ_HPD;
512 if (port->mux_flags & USB_PD_MUX_HPD_LVL)
513 dp_data.status |= DP_STATUS_HPD_STATE;
514
515 /* Configuration VDO. */
516 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
517 if (!port->state.alt) {
518 port->state.alt = port->port_altmode[CROS_EC_ALTMODE_DP];
519 ret = cros_typec_usb_safe_state(port);
520 if (ret)
521 return ret;
522 }
523
524 port->state.data = &dp_data;
525 port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
526
527 ret = cros_typec_retimer_set(port->retimer, port->state);
528 if (!ret)
529 ret = typec_mux_set(port->mux, &port->state);
530
531 return ret;
532 }
533
cros_typec_enable_usb4(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)534 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
535 int port_num,
536 struct ec_response_usb_pd_control_v2 *pd_ctrl)
537 {
538 struct cros_typec_port *port = typec->ports[port_num];
539 struct enter_usb_data data;
540
541 data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
542
543 /* Cable Speed */
544 data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
545
546 /* Cable Type */
547 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
548 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
549 else if (cros_typec_get_cable_vdo(port, USB_TYPEC_TBT_SID) & TBT_CABLE_RETIMER)
550 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
551 else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
552 data.eudo |= EUDO_CABLE_TYPE_RE_DRIVER << EUDO_CABLE_TYPE_SHIFT;
553
554 data.active_link_training = !!(pd_ctrl->control_flags &
555 USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
556
557 port->state.alt = NULL;
558 port->state.data = &data;
559 port->state.mode = TYPEC_MODE_USB4;
560
561 return typec_mux_set(port->mux, &port->state);
562 }
563
cros_typec_configure_mux(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)564 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
565 struct ec_response_usb_pd_control_v2 *pd_ctrl)
566 {
567 struct cros_typec_port *port = typec->ports[port_num];
568 struct ec_response_usb_pd_mux_info resp;
569 struct ec_params_usb_pd_mux_info req = {
570 .port = port_num,
571 };
572 struct ec_params_usb_pd_mux_ack mux_ack;
573 enum typec_orientation orientation;
574 int ret;
575
576 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_MUX_INFO,
577 &req, sizeof(req), &resp, sizeof(resp));
578 if (ret < 0) {
579 dev_warn(typec->dev, "Failed to get mux info for port: %d, err = %d\n",
580 port_num, ret);
581 return ret;
582 }
583
584 /* No change needs to be made, let's exit early. */
585 if (port->mux_flags == resp.flags && port->role == pd_ctrl->role)
586 return 0;
587
588 port->mux_flags = resp.flags;
589 port->role = pd_ctrl->role;
590
591 if (port->mux_flags == USB_PD_MUX_NONE) {
592 ret = cros_typec_usb_disconnect_state(port);
593 goto mux_ack;
594 }
595
596 if (port->mux_flags & USB_PD_MUX_POLARITY_INVERTED)
597 orientation = TYPEC_ORIENTATION_REVERSE;
598 else
599 orientation = TYPEC_ORIENTATION_NORMAL;
600
601 ret = typec_switch_set(port->ori_sw, orientation);
602 if (ret)
603 return ret;
604
605 ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
606 pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
607 ? USB_ROLE_HOST : USB_ROLE_DEVICE);
608 if (ret)
609 return ret;
610
611 if (port->mux_flags & USB_PD_MUX_USB4_ENABLED) {
612 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
613 } else if (port->mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
614 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
615 } else if (port->mux_flags & USB_PD_MUX_DP_ENABLED) {
616 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
617 } else if (port->mux_flags & USB_PD_MUX_SAFE_MODE) {
618 ret = cros_typec_usb_safe_state(port);
619 } else if (port->mux_flags & USB_PD_MUX_USB_ENABLED) {
620 port->state.alt = NULL;
621 port->state.mode = TYPEC_STATE_USB;
622
623 ret = cros_typec_retimer_set(port->retimer, port->state);
624 if (!ret)
625 ret = typec_mux_set(port->mux, &port->state);
626 } else {
627 dev_dbg(typec->dev,
628 "Unrecognized mode requested, mux flags: %x\n",
629 port->mux_flags);
630 }
631
632 mux_ack:
633 if (!typec->needs_mux_ack)
634 return ret;
635
636 /* Sending Acknowledgment to EC */
637 mux_ack.port = port_num;
638
639 if (cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_MUX_ACK, &mux_ack,
640 sizeof(mux_ack), NULL, 0) < 0)
641 dev_warn(typec->dev,
642 "Failed to send Mux ACK to EC for port: %d\n",
643 port_num);
644
645 return ret;
646 }
647
cros_typec_set_port_params_v0(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control * resp)648 static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
649 int port_num, struct ec_response_usb_pd_control *resp)
650 {
651 struct typec_port *port = typec->ports[port_num]->port;
652 enum typec_orientation polarity;
653
654 if (!resp->enabled)
655 polarity = TYPEC_ORIENTATION_NONE;
656 else if (!resp->polarity)
657 polarity = TYPEC_ORIENTATION_NORMAL;
658 else
659 polarity = TYPEC_ORIENTATION_REVERSE;
660
661 typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
662 typec_set_orientation(port, polarity);
663 }
664
cros_typec_set_port_params_v1(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v1 * resp)665 static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
666 int port_num, struct ec_response_usb_pd_control_v1 *resp)
667 {
668 struct typec_port *port = typec->ports[port_num]->port;
669 enum typec_orientation polarity;
670 bool pd_en;
671 int ret;
672
673 if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
674 polarity = TYPEC_ORIENTATION_NONE;
675 else if (!resp->polarity)
676 polarity = TYPEC_ORIENTATION_NORMAL;
677 else
678 polarity = TYPEC_ORIENTATION_REVERSE;
679 typec_set_orientation(port, polarity);
680 typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
681 TYPEC_HOST : TYPEC_DEVICE);
682 typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
683 TYPEC_SOURCE : TYPEC_SINK);
684 typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
685 TYPEC_SOURCE : TYPEC_SINK);
686
687 /* Register/remove partners when a connect/disconnect occurs. */
688 if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
689 if (typec->ports[port_num]->partner)
690 return;
691
692 pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
693 ret = cros_typec_add_partner(typec, port_num, pd_en);
694 if (ret)
695 dev_warn(typec->dev,
696 "Failed to register partner on port: %d\n",
697 port_num);
698 } else {
699 cros_typec_remove_partner(typec, port_num);
700 cros_typec_remove_cable(typec, port_num);
701 }
702 }
703
704 /*
705 * Helper function to register partner/plug altmodes.
706 */
cros_typec_register_altmodes(struct cros_typec_data * typec,int port_num,bool is_partner)707 static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_num,
708 bool is_partner)
709 {
710 struct cros_typec_port *port = typec->ports[port_num];
711 struct ec_response_typec_discovery *sop_disc = port->disc_data;
712 struct cros_typec_altmode_node *node;
713 struct typec_altmode_desc desc;
714 struct typec_altmode *amode;
715 int num_altmodes = 0;
716 int ret = 0;
717 int i, j;
718
719 for (i = 0; i < sop_disc->svid_count; i++) {
720 for (j = 0; j < sop_disc->svids[i].mode_count; j++) {
721 memset(&desc, 0, sizeof(desc));
722 desc.svid = sop_disc->svids[i].svid;
723 desc.mode = j + 1;
724 desc.vdo = sop_disc->svids[i].mode_vdo[j];
725
726 if (is_partner)
727 amode = typec_partner_register_altmode(port->partner, &desc);
728 else
729 amode = typec_plug_register_altmode(port->plug, &desc);
730
731 if (IS_ERR(amode)) {
732 ret = PTR_ERR(amode);
733 goto err_cleanup;
734 }
735
736 /* If no memory is available we should unregister and exit. */
737 node = devm_kzalloc(typec->dev, sizeof(*node), GFP_KERNEL);
738 if (!node) {
739 ret = -ENOMEM;
740 typec_unregister_altmode(amode);
741 goto err_cleanup;
742 }
743
744 node->amode = amode;
745
746 if (is_partner)
747 list_add_tail(&node->list, &port->partner_mode_list);
748 else
749 list_add_tail(&node->list, &port->plug_mode_list);
750 num_altmodes++;
751 }
752 }
753
754 if (is_partner)
755 ret = typec_partner_set_num_altmodes(port->partner, num_altmodes);
756 else
757 ret = typec_plug_set_num_altmodes(port->plug, num_altmodes);
758
759 if (ret < 0) {
760 dev_err(typec->dev, "Unable to set %s num_altmodes for port: %d\n",
761 is_partner ? "partner" : "plug", port_num);
762 goto err_cleanup;
763 }
764
765 return 0;
766
767 err_cleanup:
768 cros_typec_unregister_altmodes(typec, port_num, is_partner);
769 return ret;
770 }
771
772 /*
773 * Parse the PD identity data from the EC PD discovery responses and copy that to the supplied
774 * PD identity struct.
775 */
cros_typec_parse_pd_identity(struct usb_pd_identity * id,struct ec_response_typec_discovery * disc)776 static void cros_typec_parse_pd_identity(struct usb_pd_identity *id,
777 struct ec_response_typec_discovery *disc)
778 {
779 int i;
780
781 /* First, update the PD identity VDOs for the partner. */
782 if (disc->identity_count > 0)
783 id->id_header = disc->discovery_vdo[0];
784 if (disc->identity_count > 1)
785 id->cert_stat = disc->discovery_vdo[1];
786 if (disc->identity_count > 2)
787 id->product = disc->discovery_vdo[2];
788
789 /* Copy the remaining identity VDOs till a maximum of 6. */
790 for (i = 3; i < disc->identity_count && i < VDO_MAX_OBJECTS; i++)
791 id->vdo[i - 3] = disc->discovery_vdo[i];
792 }
793
cros_typec_handle_sop_prime_disc(struct cros_typec_data * typec,int port_num,u16 pd_revision)794 static int cros_typec_handle_sop_prime_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
795 {
796 struct cros_typec_port *port = typec->ports[port_num];
797 struct ec_response_typec_discovery *disc = port->disc_data;
798 struct typec_cable_desc c_desc = {};
799 struct typec_plug_desc p_desc;
800 struct ec_params_typec_discovery req = {
801 .port = port_num,
802 .partner_type = TYPEC_PARTNER_SOP_PRIME,
803 };
804 u32 cable_plug_type;
805 int ret = 0;
806
807 memset(disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
808 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
809 disc, EC_PROTO2_MAX_RESPONSE_SIZE);
810 if (ret < 0) {
811 dev_err(typec->dev, "Failed to get SOP' discovery data for port: %d\n", port_num);
812 goto sop_prime_disc_exit;
813 }
814
815 /* Parse the PD identity data, even if only 0s were returned. */
816 cros_typec_parse_pd_identity(&port->c_identity, disc);
817
818 if (disc->identity_count != 0) {
819 cable_plug_type = VDO_TYPEC_CABLE_TYPE(port->c_identity.vdo[0]);
820 switch (cable_plug_type) {
821 case CABLE_ATYPE:
822 c_desc.type = USB_PLUG_TYPE_A;
823 break;
824 case CABLE_BTYPE:
825 c_desc.type = USB_PLUG_TYPE_B;
826 break;
827 case CABLE_CTYPE:
828 c_desc.type = USB_PLUG_TYPE_C;
829 break;
830 case CABLE_CAPTIVE:
831 c_desc.type = USB_PLUG_CAPTIVE;
832 break;
833 default:
834 c_desc.type = USB_PLUG_NONE;
835 }
836 c_desc.active = PD_IDH_PTYPE(port->c_identity.id_header) == IDH_PTYPE_ACABLE;
837 }
838
839 c_desc.identity = &port->c_identity;
840 c_desc.pd_revision = pd_revision;
841
842 port->cable = typec_register_cable(port->port, &c_desc);
843 if (IS_ERR(port->cable)) {
844 ret = PTR_ERR(port->cable);
845 port->cable = NULL;
846 goto sop_prime_disc_exit;
847 }
848
849 p_desc.index = TYPEC_PLUG_SOP_P;
850 port->plug = typec_register_plug(port->cable, &p_desc);
851 if (IS_ERR(port->plug)) {
852 ret = PTR_ERR(port->plug);
853 port->plug = NULL;
854 goto sop_prime_disc_exit;
855 }
856
857 ret = cros_typec_register_altmodes(typec, port_num, false);
858 if (ret < 0) {
859 dev_err(typec->dev, "Failed to register plug altmodes, port: %d\n", port_num);
860 goto sop_prime_disc_exit;
861 }
862
863 return 0;
864
865 sop_prime_disc_exit:
866 cros_typec_remove_cable(typec, port_num);
867 return ret;
868 }
869
cros_typec_handle_sop_disc(struct cros_typec_data * typec,int port_num,u16 pd_revision)870 static int cros_typec_handle_sop_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
871 {
872 struct cros_typec_port *port = typec->ports[port_num];
873 struct ec_response_typec_discovery *sop_disc = port->disc_data;
874 struct ec_params_typec_discovery req = {
875 .port = port_num,
876 .partner_type = TYPEC_PARTNER_SOP,
877 };
878 int ret = 0;
879
880 if (!port->partner) {
881 dev_err(typec->dev,
882 "SOP Discovery received without partner registered, port: %d\n",
883 port_num);
884 ret = -EINVAL;
885 goto disc_exit;
886 }
887
888 typec_partner_set_pd_revision(port->partner, pd_revision);
889
890 memset(sop_disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
891 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
892 sop_disc, EC_PROTO2_MAX_RESPONSE_SIZE);
893 if (ret < 0) {
894 dev_err(typec->dev, "Failed to get SOP discovery data for port: %d\n", port_num);
895 goto disc_exit;
896 }
897
898 cros_typec_parse_pd_identity(&port->p_identity, sop_disc);
899
900 ret = typec_partner_set_identity(port->partner);
901 if (ret < 0) {
902 dev_err(typec->dev, "Failed to update partner PD identity, port: %d\n", port_num);
903 goto disc_exit;
904 }
905
906 ret = cros_typec_register_altmodes(typec, port_num, true);
907 if (ret < 0) {
908 dev_err(typec->dev, "Failed to register partner altmodes, port: %d\n", port_num);
909 goto disc_exit;
910 }
911
912 disc_exit:
913 return ret;
914 }
915
cros_typec_send_clear_event(struct cros_typec_data * typec,int port_num,u32 events_mask)916 static int cros_typec_send_clear_event(struct cros_typec_data *typec, int port_num, u32 events_mask)
917 {
918 struct ec_params_typec_control req = {
919 .port = port_num,
920 .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS,
921 .clear_events_mask = events_mask,
922 };
923
924 return cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_CONTROL, &req,
925 sizeof(req), NULL, 0);
926 }
927
cros_typec_register_partner_pdos(struct cros_typec_data * typec,struct ec_response_typec_status * resp,int port_num)928 static void cros_typec_register_partner_pdos(struct cros_typec_data *typec,
929 struct ec_response_typec_status *resp, int port_num)
930 {
931 struct usb_power_delivery_capabilities_desc caps_desc = {};
932 struct usb_power_delivery_desc desc = {
933 .revision = (le16_to_cpu(resp->sop_revision) & 0xff00) >> 4,
934 };
935 struct cros_typec_port *port = typec->ports[port_num];
936
937 if (!port->partner || port->partner_pd)
938 return;
939
940 /* If no caps are available, don't bother creating a device. */
941 if (!resp->source_cap_count && !resp->sink_cap_count)
942 return;
943
944 port->partner_pd = typec_partner_usb_power_delivery_register(port->partner, &desc);
945 if (IS_ERR(port->partner_pd)) {
946 dev_warn(typec->dev, "Failed to register partner PD device, port: %d\n", port_num);
947 return;
948 }
949
950 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd);
951
952 memcpy(caps_desc.pdo, resp->source_cap_pdos, sizeof(u32) * resp->source_cap_count);
953 caps_desc.role = TYPEC_SOURCE;
954 port->partner_src_caps = usb_power_delivery_register_capabilities(port->partner_pd,
955 &caps_desc);
956 if (IS_ERR(port->partner_src_caps))
957 dev_warn(typec->dev, "Failed to register source caps, port: %d\n", port_num);
958
959 memset(&caps_desc, 0, sizeof(caps_desc));
960 memcpy(caps_desc.pdo, resp->sink_cap_pdos, sizeof(u32) * resp->sink_cap_count);
961 caps_desc.role = TYPEC_SINK;
962 port->partner_sink_caps = usb_power_delivery_register_capabilities(port->partner_pd,
963 &caps_desc);
964 if (IS_ERR(port->partner_sink_caps))
965 dev_warn(typec->dev, "Failed to register sink caps, port: %d\n", port_num);
966 }
967
cros_typec_handle_status(struct cros_typec_data * typec,int port_num)968 static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num)
969 {
970 struct ec_response_typec_status resp;
971 struct ec_params_typec_status req = {
972 .port = port_num,
973 };
974 int ret;
975
976 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_STATUS, &req, sizeof(req),
977 &resp, sizeof(resp));
978 if (ret < 0) {
979 dev_warn(typec->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n", port_num);
980 return;
981 }
982
983 /* If we got a hard reset, unregister everything and return. */
984 if (resp.events & PD_STATUS_EVENT_HARD_RESET) {
985 cros_typec_remove_partner(typec, port_num);
986 cros_typec_remove_cable(typec, port_num);
987
988 ret = cros_typec_send_clear_event(typec, port_num,
989 PD_STATUS_EVENT_HARD_RESET);
990 if (ret < 0)
991 dev_warn(typec->dev,
992 "Failed hard reset event clear, port: %d\n", port_num);
993 return;
994 }
995
996 /* Handle any events appropriately. */
997 if (resp.events & PD_STATUS_EVENT_SOP_DISC_DONE && !typec->ports[port_num]->sop_disc_done) {
998 u16 sop_revision;
999
1000 /* Convert BCD to the format preferred by the TypeC framework */
1001 sop_revision = (le16_to_cpu(resp.sop_revision) & 0xff00) >> 4;
1002 ret = cros_typec_handle_sop_disc(typec, port_num, sop_revision);
1003 if (ret < 0)
1004 dev_err(typec->dev, "Couldn't parse SOP Disc data, port: %d\n", port_num);
1005 else {
1006 typec->ports[port_num]->sop_disc_done = true;
1007 ret = cros_typec_send_clear_event(typec, port_num,
1008 PD_STATUS_EVENT_SOP_DISC_DONE);
1009 if (ret < 0)
1010 dev_warn(typec->dev,
1011 "Failed SOP Disc event clear, port: %d\n", port_num);
1012 }
1013 if (resp.sop_connected)
1014 typec_set_pwr_opmode(typec->ports[port_num]->port, TYPEC_PWR_MODE_PD);
1015
1016 cros_typec_register_partner_pdos(typec, &resp, port_num);
1017 }
1018
1019 if (resp.events & PD_STATUS_EVENT_SOP_PRIME_DISC_DONE &&
1020 !typec->ports[port_num]->sop_prime_disc_done) {
1021 u16 sop_prime_revision;
1022
1023 /* Convert BCD to the format preferred by the TypeC framework */
1024 sop_prime_revision = (le16_to_cpu(resp.sop_prime_revision) & 0xff00) >> 4;
1025 ret = cros_typec_handle_sop_prime_disc(typec, port_num, sop_prime_revision);
1026 if (ret < 0)
1027 dev_err(typec->dev, "Couldn't parse SOP' Disc data, port: %d\n", port_num);
1028 else {
1029 typec->ports[port_num]->sop_prime_disc_done = true;
1030 ret = cros_typec_send_clear_event(typec, port_num,
1031 PD_STATUS_EVENT_SOP_PRIME_DISC_DONE);
1032 if (ret < 0)
1033 dev_warn(typec->dev,
1034 "Failed SOP Disc event clear, port: %d\n", port_num);
1035 }
1036 }
1037
1038 if (resp.events & PD_STATUS_EVENT_VDM_REQ_REPLY) {
1039 cros_typec_handle_vdm_response(typec, port_num);
1040 ret = cros_typec_send_clear_event(typec, port_num, PD_STATUS_EVENT_VDM_REQ_REPLY);
1041 if (ret < 0)
1042 dev_warn(typec->dev, "Failed VDM Reply event clear, port: %d\n", port_num);
1043 }
1044
1045 if (resp.events & PD_STATUS_EVENT_VDM_ATTENTION) {
1046 cros_typec_handle_vdm_attention(typec, port_num);
1047 ret = cros_typec_send_clear_event(typec, port_num, PD_STATUS_EVENT_VDM_ATTENTION);
1048 if (ret < 0)
1049 dev_warn(typec->dev, "Failed VDM attention event clear, port: %d\n",
1050 port_num);
1051 }
1052 }
1053
cros_typec_port_update(struct cros_typec_data * typec,int port_num)1054 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
1055 {
1056 struct ec_params_usb_pd_control req;
1057 struct ec_response_usb_pd_control_v2 resp;
1058 int ret;
1059
1060 if (port_num < 0 || port_num >= typec->num_ports) {
1061 dev_err(typec->dev, "cannot get status for invalid port %d\n",
1062 port_num);
1063 return -EINVAL;
1064 }
1065
1066 req.port = port_num;
1067 req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
1068 req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
1069 req.swap = USB_PD_CTRL_SWAP_NONE;
1070
1071 ret = cros_ec_cmd(typec->ec, typec->pd_ctrl_ver,
1072 EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
1073 &resp, sizeof(resp));
1074 if (ret < 0)
1075 return ret;
1076
1077 /* Update the switches if they exist, according to requested state */
1078 ret = cros_typec_configure_mux(typec, port_num, &resp);
1079 if (ret)
1080 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
1081
1082 dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
1083 dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
1084 dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
1085 dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
1086
1087 if (typec->pd_ctrl_ver != 0)
1088 cros_typec_set_port_params_v1(typec, port_num,
1089 (struct ec_response_usb_pd_control_v1 *)&resp);
1090 else
1091 cros_typec_set_port_params_v0(typec, port_num,
1092 (struct ec_response_usb_pd_control *) &resp);
1093
1094 if (typec->typec_cmd_supported)
1095 cros_typec_handle_status(typec, port_num);
1096
1097 return 0;
1098 }
1099
cros_typec_get_cmd_version(struct cros_typec_data * typec)1100 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
1101 {
1102 struct ec_params_get_cmd_versions_v1 req_v1;
1103 struct ec_response_get_cmd_versions resp;
1104 int ret;
1105
1106 /* We're interested in the PD control command version. */
1107 req_v1.cmd = EC_CMD_USB_PD_CONTROL;
1108 ret = cros_ec_cmd(typec->ec, 1, EC_CMD_GET_CMD_VERSIONS,
1109 &req_v1, sizeof(req_v1), &resp, sizeof(resp));
1110 if (ret < 0)
1111 return ret;
1112
1113 if (resp.version_mask & EC_VER_MASK(2))
1114 typec->pd_ctrl_ver = 2;
1115 else if (resp.version_mask & EC_VER_MASK(1))
1116 typec->pd_ctrl_ver = 1;
1117 else
1118 typec->pd_ctrl_ver = 0;
1119
1120 dev_dbg(typec->dev, "PD Control has version mask 0x%02x\n",
1121 typec->pd_ctrl_ver & 0xff);
1122
1123 return 0;
1124 }
1125
cros_typec_port_work(struct work_struct * work)1126 static void cros_typec_port_work(struct work_struct *work)
1127 {
1128 struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
1129 int ret, i;
1130
1131 for (i = 0; i < typec->num_ports; i++) {
1132 ret = cros_typec_port_update(typec, i);
1133 if (ret < 0)
1134 dev_warn(typec->dev, "Update failed for port: %d\n", i);
1135 }
1136 }
1137
cros_ec_typec_event(struct notifier_block * nb,unsigned long host_event,void * _notify)1138 static int cros_ec_typec_event(struct notifier_block *nb,
1139 unsigned long host_event, void *_notify)
1140 {
1141 struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
1142
1143 flush_work(&typec->port_work);
1144 schedule_work(&typec->port_work);
1145
1146 return NOTIFY_OK;
1147 }
1148
1149 #ifdef CONFIG_ACPI
1150 static const struct acpi_device_id cros_typec_acpi_id[] = {
1151 { "GOOG0014", 0 },
1152 {}
1153 };
1154 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
1155 #endif
1156
1157 #ifdef CONFIG_OF
1158 static const struct of_device_id cros_typec_of_match[] = {
1159 { .compatible = "google,cros-ec-typec", },
1160 {}
1161 };
1162 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
1163 #endif
1164
cros_typec_probe(struct platform_device * pdev)1165 static int cros_typec_probe(struct platform_device *pdev)
1166 {
1167 struct cros_ec_dev *ec_dev = NULL;
1168 struct device *dev = &pdev->dev;
1169 struct cros_typec_data *typec;
1170 struct ec_response_usb_pd_ports resp;
1171 int ret, i;
1172
1173 typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
1174 if (!typec)
1175 return -ENOMEM;
1176
1177 typec->dev = dev;
1178
1179 typec->ec = dev_get_drvdata(pdev->dev.parent);
1180 if (!typec->ec) {
1181 dev_err(dev, "couldn't find parent EC device\n");
1182 return -ENODEV;
1183 }
1184
1185 platform_set_drvdata(pdev, typec);
1186
1187 ret = cros_typec_get_cmd_version(typec);
1188 if (ret < 0) {
1189 dev_err(dev, "failed to get PD command version info\n");
1190 return ret;
1191 }
1192
1193 ec_dev = dev_get_drvdata(&typec->ec->ec->dev);
1194 if (!ec_dev)
1195 return -EPROBE_DEFER;
1196
1197 typec->typec_cmd_supported = cros_ec_check_features(ec_dev, EC_FEATURE_TYPEC_CMD);
1198 typec->needs_mux_ack = cros_ec_check_features(ec_dev, EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK);
1199
1200 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
1201 &resp, sizeof(resp));
1202 if (ret < 0)
1203 return ret;
1204
1205 typec->num_ports = resp.num_ports;
1206 if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
1207 dev_warn(typec->dev,
1208 "Too many ports reported: %d, limiting to max: %d\n",
1209 typec->num_ports, EC_USB_PD_MAX_PORTS);
1210 typec->num_ports = EC_USB_PD_MAX_PORTS;
1211 }
1212
1213 ret = cros_typec_init_ports(typec);
1214 if (ret < 0)
1215 return ret;
1216
1217 INIT_WORK(&typec->port_work, cros_typec_port_work);
1218
1219 /*
1220 * Safe to call port update here, since we haven't registered the
1221 * PD notifier yet.
1222 */
1223 for (i = 0; i < typec->num_ports; i++) {
1224 ret = cros_typec_port_update(typec, i);
1225 if (ret < 0)
1226 goto unregister_ports;
1227 }
1228
1229 typec->nb.notifier_call = cros_ec_typec_event;
1230 ret = cros_usbpd_register_notify(&typec->nb);
1231 if (ret < 0)
1232 goto unregister_ports;
1233
1234 return 0;
1235
1236 unregister_ports:
1237 cros_unregister_ports(typec);
1238 return ret;
1239 }
1240
cros_typec_suspend(struct device * dev)1241 static int __maybe_unused cros_typec_suspend(struct device *dev)
1242 {
1243 struct cros_typec_data *typec = dev_get_drvdata(dev);
1244
1245 cancel_work_sync(&typec->port_work);
1246
1247 return 0;
1248 }
1249
cros_typec_resume(struct device * dev)1250 static int __maybe_unused cros_typec_resume(struct device *dev)
1251 {
1252 struct cros_typec_data *typec = dev_get_drvdata(dev);
1253
1254 /* Refresh port state. */
1255 schedule_work(&typec->port_work);
1256
1257 return 0;
1258 }
1259
1260 static const struct dev_pm_ops cros_typec_pm_ops = {
1261 SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
1262 };
1263
1264 static struct platform_driver cros_typec_driver = {
1265 .driver = {
1266 .name = DRV_NAME,
1267 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
1268 .of_match_table = of_match_ptr(cros_typec_of_match),
1269 .pm = &cros_typec_pm_ops,
1270 },
1271 .probe = cros_typec_probe,
1272 };
1273
1274 module_platform_driver(cros_typec_driver);
1275
1276 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
1277 MODULE_DESCRIPTION("Chrome OS EC Type C control");
1278 MODULE_LICENSE("GPL");
1279