Merge branch 'parisc-5.11-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-2.6-microblaze.git] / drivers / platform / chrome / cros_ec_typec.c
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_ec_proto.h>
14 #include <linux/platform_data/cros_usbpd_notify.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/pd.h>
17 #include <linux/usb/typec.h>
18 #include <linux/usb/typec_altmode.h>
19 #include <linux/usb/typec_dp.h>
20 #include <linux/usb/typec_mux.h>
21 #include <linux/usb/typec_tbt.h>
22 #include <linux/usb/role.h>
23
24 #define DRV_NAME "cros-ec-typec"
25
26 /* Supported alt modes. */
27 enum {
28         CROS_EC_ALTMODE_DP = 0,
29         CROS_EC_ALTMODE_TBT,
30         CROS_EC_ALTMODE_MAX,
31 };
32
33 /* Per port data. */
34 struct cros_typec_port {
35         struct typec_port *port;
36         /* Initial capabilities for the port. */
37         struct typec_capability caps;
38         struct typec_partner *partner;
39         /* Port partner PD identity info. */
40         struct usb_pd_identity p_identity;
41         struct typec_switch *ori_sw;
42         struct typec_mux *mux;
43         struct usb_role_switch *role_sw;
44
45         /* Variables keeping track of switch state. */
46         struct typec_mux_state state;
47         uint8_t mux_flags;
48
49         /* Port alt modes. */
50         struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX];
51 };
52
53 /* Platform-specific data for the Chrome OS EC Type C controller. */
54 struct cros_typec_data {
55         struct device *dev;
56         struct cros_ec_device *ec;
57         int num_ports;
58         unsigned int pd_ctrl_ver;
59         /* Array of ports, indexed by port number. */
60         struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
61         struct notifier_block nb;
62         struct work_struct port_work;
63 };
64
65 static int cros_typec_parse_port_props(struct typec_capability *cap,
66                                        struct fwnode_handle *fwnode,
67                                        struct device *dev)
68 {
69         const char *buf;
70         int ret;
71
72         memset(cap, 0, sizeof(*cap));
73         ret = fwnode_property_read_string(fwnode, "power-role", &buf);
74         if (ret) {
75                 dev_err(dev, "power-role not found: %d\n", ret);
76                 return ret;
77         }
78
79         ret = typec_find_port_power_role(buf);
80         if (ret < 0)
81                 return ret;
82         cap->type = ret;
83
84         ret = fwnode_property_read_string(fwnode, "data-role", &buf);
85         if (ret) {
86                 dev_err(dev, "data-role not found: %d\n", ret);
87                 return ret;
88         }
89
90         ret = typec_find_port_data_role(buf);
91         if (ret < 0)
92                 return ret;
93         cap->data = ret;
94
95         ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
96         if (ret) {
97                 dev_err(dev, "try-power-role not found: %d\n", ret);
98                 return ret;
99         }
100
101         ret = typec_find_power_role(buf);
102         if (ret < 0)
103                 return ret;
104         cap->prefer_role = ret;
105
106         cap->fwnode = fwnode;
107
108         return 0;
109 }
110
111 static int cros_typec_get_switch_handles(struct cros_typec_port *port,
112                                          struct fwnode_handle *fwnode,
113                                          struct device *dev)
114 {
115         port->mux = fwnode_typec_mux_get(fwnode, NULL);
116         if (IS_ERR(port->mux)) {
117                 dev_dbg(dev, "Mux handle not found.\n");
118                 goto mux_err;
119         }
120
121         port->ori_sw = fwnode_typec_switch_get(fwnode);
122         if (IS_ERR(port->ori_sw)) {
123                 dev_dbg(dev, "Orientation switch handle not found.\n");
124                 goto ori_sw_err;
125         }
126
127         port->role_sw = fwnode_usb_role_switch_get(fwnode);
128         if (IS_ERR(port->role_sw)) {
129                 dev_dbg(dev, "USB role switch handle not found.\n");
130                 goto role_sw_err;
131         }
132
133         return 0;
134
135 role_sw_err:
136         usb_role_switch_put(port->role_sw);
137 ori_sw_err:
138         typec_switch_put(port->ori_sw);
139 mux_err:
140         typec_mux_put(port->mux);
141
142         return -ENODEV;
143 }
144
145 static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
146                                   bool pd_en)
147 {
148         struct cros_typec_port *port = typec->ports[port_num];
149         struct typec_partner_desc p_desc = {
150                 .usb_pd = pd_en,
151         };
152         int ret = 0;
153
154         /*
155          * Fill an initial PD identity, which will then be updated with info
156          * from the EC.
157          */
158         p_desc.identity = &port->p_identity;
159
160         port->partner = typec_register_partner(port->port, &p_desc);
161         if (IS_ERR(port->partner)) {
162                 ret = PTR_ERR(port->partner);
163                 port->partner = NULL;
164         }
165
166         return ret;
167 }
168
169 static void cros_typec_remove_partner(struct cros_typec_data *typec,
170                                      int port_num)
171 {
172         struct cros_typec_port *port = typec->ports[port_num];
173
174         port->state.alt = NULL;
175         port->state.mode = TYPEC_STATE_USB;
176         port->state.data = NULL;
177
178         usb_role_switch_set_role(port->role_sw, USB_ROLE_NONE);
179         typec_switch_set(port->ori_sw, TYPEC_ORIENTATION_NONE);
180         typec_mux_set(port->mux, &port->state);
181
182         typec_unregister_partner(port->partner);
183         port->partner = NULL;
184 }
185
186 static void cros_unregister_ports(struct cros_typec_data *typec)
187 {
188         int i;
189
190         for (i = 0; i < typec->num_ports; i++) {
191                 if (!typec->ports[i])
192                         continue;
193                 cros_typec_remove_partner(typec, i);
194                 usb_role_switch_put(typec->ports[i]->role_sw);
195                 typec_switch_put(typec->ports[i]->ori_sw);
196                 typec_mux_put(typec->ports[i]->mux);
197                 typec_unregister_port(typec->ports[i]->port);
198         }
199 }
200
201 /*
202  * Fake the alt mode structs until we actually start registering Type C port
203  * and partner alt modes.
204  */
205 static void cros_typec_register_port_altmodes(struct cros_typec_data *typec,
206                                               int port_num)
207 {
208         struct cros_typec_port *port = typec->ports[port_num];
209
210         /* All PD capable CrOS devices are assumed to support DP altmode. */
211         port->p_altmode[CROS_EC_ALTMODE_DP].svid = USB_TYPEC_DP_SID;
212         port->p_altmode[CROS_EC_ALTMODE_DP].mode = USB_TYPEC_DP_MODE;
213
214         /*
215          * Register TBT compatibility alt mode. The EC will not enter the mode
216          * if it doesn't support it, so it's safe to register it unconditionally
217          * here for now.
218          */
219         port->p_altmode[CROS_EC_ALTMODE_TBT].svid = USB_TYPEC_TBT_SID;
220         port->p_altmode[CROS_EC_ALTMODE_TBT].mode = TYPEC_ANY_MODE;
221
222         port->state.alt = NULL;
223         port->state.mode = TYPEC_STATE_USB;
224         port->state.data = NULL;
225 }
226
227 static int cros_typec_init_ports(struct cros_typec_data *typec)
228 {
229         struct device *dev = typec->dev;
230         struct typec_capability *cap;
231         struct fwnode_handle *fwnode;
232         struct cros_typec_port *cros_port;
233         const char *port_prop;
234         int ret;
235         int nports;
236         u32 port_num = 0;
237
238         nports = device_get_child_node_count(dev);
239         if (nports == 0) {
240                 dev_err(dev, "No port entries found.\n");
241                 return -ENODEV;
242         }
243
244         if (nports > typec->num_ports) {
245                 dev_err(dev, "More ports listed than can be supported.\n");
246                 return -EINVAL;
247         }
248
249         /* DT uses "reg" to specify port number. */
250         port_prop = dev->of_node ? "reg" : "port-number";
251         device_for_each_child_node(dev, fwnode) {
252                 if (fwnode_property_read_u32(fwnode, port_prop, &port_num)) {
253                         ret = -EINVAL;
254                         dev_err(dev, "No port-number for port, aborting.\n");
255                         goto unregister_ports;
256                 }
257
258                 if (port_num >= typec->num_ports) {
259                         dev_err(dev, "Invalid port number.\n");
260                         ret = -EINVAL;
261                         goto unregister_ports;
262                 }
263
264                 dev_dbg(dev, "Registering port %d\n", port_num);
265
266                 cros_port = devm_kzalloc(dev, sizeof(*cros_port), GFP_KERNEL);
267                 if (!cros_port) {
268                         ret = -ENOMEM;
269                         goto unregister_ports;
270                 }
271
272                 typec->ports[port_num] = cros_port;
273                 cap = &cros_port->caps;
274
275                 ret = cros_typec_parse_port_props(cap, fwnode, dev);
276                 if (ret < 0)
277                         goto unregister_ports;
278
279                 cros_port->port = typec_register_port(dev, cap);
280                 if (IS_ERR(cros_port->port)) {
281                         dev_err(dev, "Failed to register port %d\n", port_num);
282                         ret = PTR_ERR(cros_port->port);
283                         goto unregister_ports;
284                 }
285
286                 ret = cros_typec_get_switch_handles(cros_port, fwnode, dev);
287                 if (ret)
288                         dev_dbg(dev, "No switch control for port %d\n",
289                                 port_num);
290
291                 cros_typec_register_port_altmodes(typec, port_num);
292         }
293
294         return 0;
295
296 unregister_ports:
297         cros_unregister_ports(typec);
298         return ret;
299 }
300
301 static int cros_typec_ec_command(struct cros_typec_data *typec,
302                                  unsigned int version,
303                                  unsigned int command,
304                                  void *outdata,
305                                  unsigned int outsize,
306                                  void *indata,
307                                  unsigned int insize)
308 {
309         struct cros_ec_command *msg;
310         int ret;
311
312         msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
313         if (!msg)
314                 return -ENOMEM;
315
316         msg->version = version;
317         msg->command = command;
318         msg->outsize = outsize;
319         msg->insize = insize;
320
321         if (outsize)
322                 memcpy(msg->data, outdata, outsize);
323
324         ret = cros_ec_cmd_xfer_status(typec->ec, msg);
325         if (ret >= 0 && insize)
326                 memcpy(indata, msg->data, insize);
327
328         kfree(msg);
329         return ret;
330 }
331
332 static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
333                 int port_num, struct ec_response_usb_pd_control *resp)
334 {
335         struct typec_port *port = typec->ports[port_num]->port;
336         enum typec_orientation polarity;
337
338         if (!resp->enabled)
339                 polarity = TYPEC_ORIENTATION_NONE;
340         else if (!resp->polarity)
341                 polarity = TYPEC_ORIENTATION_NORMAL;
342         else
343                 polarity = TYPEC_ORIENTATION_REVERSE;
344
345         typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
346         typec_set_orientation(port, polarity);
347 }
348
349 static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
350                 int port_num, struct ec_response_usb_pd_control_v1 *resp)
351 {
352         struct typec_port *port = typec->ports[port_num]->port;
353         enum typec_orientation polarity;
354         bool pd_en;
355         int ret;
356
357         if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
358                 polarity = TYPEC_ORIENTATION_NONE;
359         else if (!resp->polarity)
360                 polarity = TYPEC_ORIENTATION_NORMAL;
361         else
362                 polarity = TYPEC_ORIENTATION_REVERSE;
363         typec_set_orientation(port, polarity);
364         typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
365                         TYPEC_HOST : TYPEC_DEVICE);
366         typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
367                         TYPEC_SOURCE : TYPEC_SINK);
368         typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
369                         TYPEC_SOURCE : TYPEC_SINK);
370
371         /* Register/remove partners when a connect/disconnect occurs. */
372         if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
373                 if (typec->ports[port_num]->partner)
374                         return;
375
376                 pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
377                 ret = cros_typec_add_partner(typec, port_num, pd_en);
378                 if (ret)
379                         dev_warn(typec->dev,
380                                  "Failed to register partner on port: %d\n",
381                                  port_num);
382         } else {
383                 if (!typec->ports[port_num]->partner)
384                         return;
385                 cros_typec_remove_partner(typec, port_num);
386         }
387 }
388
389 static int cros_typec_get_mux_info(struct cros_typec_data *typec, int port_num,
390                                    struct ec_response_usb_pd_mux_info *resp)
391 {
392         struct ec_params_usb_pd_mux_info req = {
393                 .port = port_num,
394         };
395
396         return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req,
397                                      sizeof(req), resp, sizeof(*resp));
398 }
399
400 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
401 {
402         port->state.mode = TYPEC_STATE_SAFE;
403
404         return typec_mux_set(port->mux, &port->state);
405 }
406
407 /*
408  * Spoof the VDOs that were likely communicated by the partner for TBT alt
409  * mode.
410  */
411 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
412                                  int port_num,
413                                  struct ec_response_usb_pd_control_v2 *pd_ctrl)
414 {
415         struct cros_typec_port *port = typec->ports[port_num];
416         struct typec_thunderbolt_data data;
417         int ret;
418
419         if (typec->pd_ctrl_ver < 2) {
420                 dev_err(typec->dev,
421                         "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
422                 return -ENOTSUPP;
423         }
424
425         /* Device Discover Mode VDO */
426         data.device_mode = TBT_MODE;
427
428         if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
429                 data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
430
431         /* Cable Discover Mode VDO */
432         data.cable_mode = TBT_MODE;
433         data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
434
435         if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
436                 data.cable_mode |= TBT_CABLE_OPTICAL;
437
438         if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
439                 data.cable_mode |= TBT_CABLE_LINK_TRAINING;
440
441         data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen);
442
443         /* Enter Mode VDO */
444         data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
445
446         if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
447                 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
448
449         if (!port->state.alt) {
450                 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT];
451                 ret = cros_typec_usb_safe_state(port);
452                 if (ret)
453                         return ret;
454         }
455
456         port->state.data = &data;
457         port->state.mode = TYPEC_TBT_MODE;
458
459         return typec_mux_set(port->mux, &port->state);
460 }
461
462 /* Spoof the VDOs that were likely communicated by the partner. */
463 static int cros_typec_enable_dp(struct cros_typec_data *typec,
464                                 int port_num,
465                                 struct ec_response_usb_pd_control_v2 *pd_ctrl)
466 {
467         struct cros_typec_port *port = typec->ports[port_num];
468         struct typec_displayport_data dp_data;
469         int ret;
470
471         if (typec->pd_ctrl_ver < 2) {
472                 dev_err(typec->dev,
473                         "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
474                 return -ENOTSUPP;
475         }
476
477         /* Status VDO. */
478         dp_data.status = DP_STATUS_ENABLED;
479         if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
480                 dp_data.status |= DP_STATUS_IRQ_HPD;
481         if (port->mux_flags & USB_PD_MUX_HPD_LVL)
482                 dp_data.status |= DP_STATUS_HPD_STATE;
483
484         /* Configuration VDO. */
485         dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
486         if (!port->state.alt) {
487                 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP];
488                 ret = cros_typec_usb_safe_state(port);
489                 if (ret)
490                         return ret;
491         }
492
493         port->state.data = &dp_data;
494         port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
495
496         return typec_mux_set(port->mux, &port->state);
497 }
498
499 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
500                                   int port_num,
501                                   struct ec_response_usb_pd_control_v2 *pd_ctrl)
502 {
503         struct cros_typec_port *port = typec->ports[port_num];
504         struct enter_usb_data data;
505
506         data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
507
508         /* Cable Speed */
509         data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
510
511         /* Cable Type */
512         if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
513                 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
514         else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
515                 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
516
517         data.active_link_training = !!(pd_ctrl->control_flags &
518                                        USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
519
520         port->state.alt = NULL;
521         port->state.data = &data;
522         port->state.mode = TYPEC_MODE_USB4;
523
524         return typec_mux_set(port->mux, &port->state);
525 }
526
527 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
528                                 uint8_t mux_flags,
529                                 struct ec_response_usb_pd_control_v2 *pd_ctrl)
530 {
531         struct cros_typec_port *port = typec->ports[port_num];
532         enum typec_orientation orientation;
533         int ret;
534
535         if (!port->partner)
536                 return 0;
537
538         if (mux_flags & USB_PD_MUX_POLARITY_INVERTED)
539                 orientation = TYPEC_ORIENTATION_REVERSE;
540         else
541                 orientation = TYPEC_ORIENTATION_NORMAL;
542
543         ret = typec_switch_set(port->ori_sw, orientation);
544         if (ret)
545                 return ret;
546
547         ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
548                                         pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
549                                         ? USB_ROLE_HOST : USB_ROLE_DEVICE);
550         if (ret)
551                 return ret;
552
553         if (mux_flags & USB_PD_MUX_USB4_ENABLED) {
554                 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
555         } else if (mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
556                 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
557         } else if (mux_flags & USB_PD_MUX_DP_ENABLED) {
558                 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
559         } else if (mux_flags & USB_PD_MUX_SAFE_MODE) {
560                 ret = cros_typec_usb_safe_state(port);
561         } else if (mux_flags & USB_PD_MUX_USB_ENABLED) {
562                 port->state.alt = NULL;
563                 port->state.mode = TYPEC_STATE_USB;
564                 ret = typec_mux_set(port->mux, &port->state);
565         } else {
566                 dev_info(typec->dev,
567                          "Unsupported mode requested, mux flags: %x\n",
568                          mux_flags);
569                 ret = -ENOTSUPP;
570         }
571
572         return ret;
573 }
574
575 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
576 {
577         struct ec_params_usb_pd_control req;
578         struct ec_response_usb_pd_control_v2 resp;
579         struct ec_response_usb_pd_mux_info mux_resp;
580         int ret;
581
582         if (port_num < 0 || port_num >= typec->num_ports) {
583                 dev_err(typec->dev, "cannot get status for invalid port %d\n",
584                         port_num);
585                 return -EINVAL;
586         }
587
588         req.port = port_num;
589         req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
590         req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
591         req.swap = USB_PD_CTRL_SWAP_NONE;
592
593         ret = cros_typec_ec_command(typec, typec->pd_ctrl_ver,
594                                     EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
595                                     &resp, sizeof(resp));
596         if (ret < 0)
597                 return ret;
598
599         dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
600         dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
601         dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
602         dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
603
604         if (typec->pd_ctrl_ver != 0)
605                 cros_typec_set_port_params_v1(typec, port_num,
606                         (struct ec_response_usb_pd_control_v1 *)&resp);
607         else
608                 cros_typec_set_port_params_v0(typec, port_num,
609                         (struct ec_response_usb_pd_control *) &resp);
610
611         /* Update the switches if they exist, according to requested state */
612         ret = cros_typec_get_mux_info(typec, port_num, &mux_resp);
613         if (ret < 0) {
614                 dev_warn(typec->dev,
615                          "Failed to get mux info for port: %d, err = %d\n",
616                          port_num, ret);
617                 return 0;
618         }
619
620         /* No change needs to be made, let's exit early. */
621         if (typec->ports[port_num]->mux_flags == mux_resp.flags)
622                 return 0;
623
624         typec->ports[port_num]->mux_flags = mux_resp.flags;
625         ret = cros_typec_configure_mux(typec, port_num, mux_resp.flags, &resp);
626         if (ret)
627                 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
628
629         return ret;
630 }
631
632 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
633 {
634         struct ec_params_get_cmd_versions_v1 req_v1;
635         struct ec_response_get_cmd_versions resp;
636         int ret;
637
638         /* We're interested in the PD control command version. */
639         req_v1.cmd = EC_CMD_USB_PD_CONTROL;
640         ret = cros_typec_ec_command(typec, 1, EC_CMD_GET_CMD_VERSIONS,
641                                     &req_v1, sizeof(req_v1), &resp,
642                                     sizeof(resp));
643         if (ret < 0)
644                 return ret;
645
646         if (resp.version_mask & EC_VER_MASK(2))
647                 typec->pd_ctrl_ver = 2;
648         else if (resp.version_mask & EC_VER_MASK(1))
649                 typec->pd_ctrl_ver = 1;
650         else
651                 typec->pd_ctrl_ver = 0;
652
653         dev_dbg(typec->dev, "PD Control has version mask 0x%hhx\n",
654                 typec->pd_ctrl_ver);
655
656         return 0;
657 }
658
659 static void cros_typec_port_work(struct work_struct *work)
660 {
661         struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
662         int ret, i;
663
664         for (i = 0; i < typec->num_ports; i++) {
665                 ret = cros_typec_port_update(typec, i);
666                 if (ret < 0)
667                         dev_warn(typec->dev, "Update failed for port: %d\n", i);
668         }
669 }
670
671 static int cros_ec_typec_event(struct notifier_block *nb,
672                                unsigned long host_event, void *_notify)
673 {
674         struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
675
676         schedule_work(&typec->port_work);
677
678         return NOTIFY_OK;
679 }
680
681 #ifdef CONFIG_ACPI
682 static const struct acpi_device_id cros_typec_acpi_id[] = {
683         { "GOOG0014", 0 },
684         {}
685 };
686 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
687 #endif
688
689 #ifdef CONFIG_OF
690 static const struct of_device_id cros_typec_of_match[] = {
691         { .compatible = "google,cros-ec-typec", },
692         {}
693 };
694 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
695 #endif
696
697 static int cros_typec_probe(struct platform_device *pdev)
698 {
699         struct device *dev = &pdev->dev;
700         struct cros_typec_data *typec;
701         struct ec_response_usb_pd_ports resp;
702         int ret, i;
703
704         typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
705         if (!typec)
706                 return -ENOMEM;
707
708         typec->dev = dev;
709         typec->ec = dev_get_drvdata(pdev->dev.parent);
710         platform_set_drvdata(pdev, typec);
711
712         ret = cros_typec_get_cmd_version(typec);
713         if (ret < 0) {
714                 dev_err(dev, "failed to get PD command version info\n");
715                 return ret;
716         }
717
718         ret = cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
719                                     &resp, sizeof(resp));
720         if (ret < 0)
721                 return ret;
722
723         typec->num_ports = resp.num_ports;
724         if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
725                 dev_warn(typec->dev,
726                          "Too many ports reported: %d, limiting to max: %d\n",
727                          typec->num_ports, EC_USB_PD_MAX_PORTS);
728                 typec->num_ports = EC_USB_PD_MAX_PORTS;
729         }
730
731         ret = cros_typec_init_ports(typec);
732         if (ret < 0)
733                 return ret;
734
735         INIT_WORK(&typec->port_work, cros_typec_port_work);
736
737         /*
738          * Safe to call port update here, since we haven't registered the
739          * PD notifier yet.
740          */
741         for (i = 0; i < typec->num_ports; i++) {
742                 ret = cros_typec_port_update(typec, i);
743                 if (ret < 0)
744                         goto unregister_ports;
745         }
746
747         typec->nb.notifier_call = cros_ec_typec_event;
748         ret = cros_usbpd_register_notify(&typec->nb);
749         if (ret < 0)
750                 goto unregister_ports;
751
752         return 0;
753
754 unregister_ports:
755         cros_unregister_ports(typec);
756         return ret;
757 }
758
759 static int __maybe_unused cros_typec_suspend(struct device *dev)
760 {
761         struct cros_typec_data *typec = dev_get_drvdata(dev);
762
763         cancel_work_sync(&typec->port_work);
764
765         return 0;
766 }
767
768 static int __maybe_unused cros_typec_resume(struct device *dev)
769 {
770         struct cros_typec_data *typec = dev_get_drvdata(dev);
771
772         /* Refresh port state. */
773         schedule_work(&typec->port_work);
774
775         return 0;
776 }
777
778 static const struct dev_pm_ops cros_typec_pm_ops = {
779         SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
780 };
781
782 static struct platform_driver cros_typec_driver = {
783         .driver = {
784                 .name = DRV_NAME,
785                 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
786                 .of_match_table = of_match_ptr(cros_typec_of_match),
787                 .pm = &cros_typec_pm_ops,
788         },
789         .probe = cros_typec_probe,
790 };
791
792 module_platform_driver(cros_typec_driver);
793
794 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
795 MODULE_DESCRIPTION("Chrome OS EC Type C control");
796 MODULE_LICENSE("GPL");