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
cros_typec_parse_port_props(struct typec_capability * cap,struct fwnode_handle * fwnode,struct device * dev)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
cros_typec_get_switch_handles(struct cros_typec_port * port,struct fwnode_handle * fwnode,struct device * dev)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
cros_typec_add_partner(struct cros_typec_data * typec,int port_num,bool pd_en)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
cros_typec_remove_partner(struct cros_typec_data * typec,int port_num)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
cros_unregister_ports(struct cros_typec_data * typec)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 */
cros_typec_register_port_altmodes(struct cros_typec_data * typec,int port_num)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
cros_typec_init_ports(struct cros_typec_data * typec)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
cros_typec_ec_command(struct cros_typec_data * typec,unsigned int version,unsigned int command,void * outdata,unsigned int outsize,void * indata,unsigned int insize)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
cros_typec_set_port_params_v0(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control * resp)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
cros_typec_set_port_params_v1(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v1 * resp)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
cros_typec_get_mux_info(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_mux_info * resp)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
cros_typec_usb_safe_state(struct cros_typec_port * port)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 */
cros_typec_enable_tbt(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)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 if (pd_ctrl->cable_gen)
442 data.cable_mode |= TBT_CABLE_ROUNDED;
443
444 /* Enter Mode VDO */
445 data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
446
447 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
448 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
449
450 if (!port->state.alt) {
451 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT];
452 ret = cros_typec_usb_safe_state(port);
453 if (ret)
454 return ret;
455 }
456
457 port->state.data = &data;
458 port->state.mode = TYPEC_TBT_MODE;
459
460 return typec_mux_set(port->mux, &port->state);
461 }
462
463 /* 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)464 static int cros_typec_enable_dp(struct cros_typec_data *typec,
465 int port_num,
466 struct ec_response_usb_pd_control_v2 *pd_ctrl)
467 {
468 struct cros_typec_port *port = typec->ports[port_num];
469 struct typec_displayport_data dp_data;
470 int ret;
471
472 if (typec->pd_ctrl_ver < 2) {
473 dev_err(typec->dev,
474 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
475 return -ENOTSUPP;
476 }
477
478 /* Status VDO. */
479 dp_data.status = DP_STATUS_ENABLED;
480 if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
481 dp_data.status |= DP_STATUS_IRQ_HPD;
482 if (port->mux_flags & USB_PD_MUX_HPD_LVL)
483 dp_data.status |= DP_STATUS_HPD_STATE;
484
485 /* Configuration VDO. */
486 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
487 if (!port->state.alt) {
488 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP];
489 ret = cros_typec_usb_safe_state(port);
490 if (ret)
491 return ret;
492 }
493
494 port->state.data = &dp_data;
495 port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
496
497 return typec_mux_set(port->mux, &port->state);
498 }
499
cros_typec_enable_usb4(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)500 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
501 int port_num,
502 struct ec_response_usb_pd_control_v2 *pd_ctrl)
503 {
504 struct cros_typec_port *port = typec->ports[port_num];
505 struct enter_usb_data data;
506
507 data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
508
509 /* Cable Speed */
510 data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
511
512 /* Cable Type */
513 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
514 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
515 else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
516 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
517
518 data.active_link_training = !!(pd_ctrl->control_flags &
519 USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
520
521 port->state.alt = NULL;
522 port->state.data = &data;
523 port->state.mode = TYPEC_MODE_USB4;
524
525 return typec_mux_set(port->mux, &port->state);
526 }
527
cros_typec_configure_mux(struct cros_typec_data * typec,int port_num,uint8_t mux_flags,struct ec_response_usb_pd_control_v2 * pd_ctrl)528 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
529 uint8_t mux_flags,
530 struct ec_response_usb_pd_control_v2 *pd_ctrl)
531 {
532 struct cros_typec_port *port = typec->ports[port_num];
533 enum typec_orientation orientation;
534 int ret;
535
536 if (!port->partner)
537 return 0;
538
539 if (mux_flags & USB_PD_MUX_POLARITY_INVERTED)
540 orientation = TYPEC_ORIENTATION_REVERSE;
541 else
542 orientation = TYPEC_ORIENTATION_NORMAL;
543
544 ret = typec_switch_set(port->ori_sw, orientation);
545 if (ret)
546 return ret;
547
548 ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
549 pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
550 ? USB_ROLE_HOST : USB_ROLE_DEVICE);
551 if (ret)
552 return ret;
553
554 if (mux_flags & USB_PD_MUX_USB4_ENABLED) {
555 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
556 } else if (mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
557 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
558 } else if (mux_flags & USB_PD_MUX_DP_ENABLED) {
559 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
560 } else if (mux_flags & USB_PD_MUX_SAFE_MODE) {
561 ret = cros_typec_usb_safe_state(port);
562 } else if (mux_flags & USB_PD_MUX_USB_ENABLED) {
563 port->state.alt = NULL;
564 port->state.mode = TYPEC_STATE_USB;
565 ret = typec_mux_set(port->mux, &port->state);
566 } else {
567 dev_info(typec->dev,
568 "Unsupported mode requested, mux flags: %x\n",
569 mux_flags);
570 ret = -ENOTSUPP;
571 }
572
573 return ret;
574 }
575
cros_typec_port_update(struct cros_typec_data * typec,int port_num)576 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
577 {
578 struct ec_params_usb_pd_control req;
579 struct ec_response_usb_pd_control_v2 resp;
580 struct ec_response_usb_pd_mux_info mux_resp;
581 int ret;
582
583 if (port_num < 0 || port_num >= typec->num_ports) {
584 dev_err(typec->dev, "cannot get status for invalid port %d\n",
585 port_num);
586 return -EINVAL;
587 }
588
589 req.port = port_num;
590 req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
591 req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
592 req.swap = USB_PD_CTRL_SWAP_NONE;
593
594 ret = cros_typec_ec_command(typec, typec->pd_ctrl_ver,
595 EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
596 &resp, sizeof(resp));
597 if (ret < 0)
598 return ret;
599
600 dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
601 dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
602 dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
603 dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
604
605 if (typec->pd_ctrl_ver != 0)
606 cros_typec_set_port_params_v1(typec, port_num,
607 (struct ec_response_usb_pd_control_v1 *)&resp);
608 else
609 cros_typec_set_port_params_v0(typec, port_num,
610 (struct ec_response_usb_pd_control *) &resp);
611
612 /* Update the switches if they exist, according to requested state */
613 ret = cros_typec_get_mux_info(typec, port_num, &mux_resp);
614 if (ret < 0) {
615 dev_warn(typec->dev,
616 "Failed to get mux info for port: %d, err = %d\n",
617 port_num, ret);
618 return 0;
619 }
620
621 /* No change needs to be made, let's exit early. */
622 if (typec->ports[port_num]->mux_flags == mux_resp.flags)
623 return 0;
624
625 typec->ports[port_num]->mux_flags = mux_resp.flags;
626 ret = cros_typec_configure_mux(typec, port_num, mux_resp.flags, &resp);
627 if (ret)
628 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
629
630 return ret;
631 }
632
cros_typec_get_cmd_version(struct cros_typec_data * typec)633 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
634 {
635 struct ec_params_get_cmd_versions_v1 req_v1;
636 struct ec_response_get_cmd_versions resp;
637 int ret;
638
639 /* We're interested in the PD control command version. */
640 req_v1.cmd = EC_CMD_USB_PD_CONTROL;
641 ret = cros_typec_ec_command(typec, 1, EC_CMD_GET_CMD_VERSIONS,
642 &req_v1, sizeof(req_v1), &resp,
643 sizeof(resp));
644 if (ret < 0)
645 return ret;
646
647 if (resp.version_mask & EC_VER_MASK(2))
648 typec->pd_ctrl_ver = 2;
649 else if (resp.version_mask & EC_VER_MASK(1))
650 typec->pd_ctrl_ver = 1;
651 else
652 typec->pd_ctrl_ver = 0;
653
654 dev_dbg(typec->dev, "PD Control has version mask 0x%hhx\n",
655 typec->pd_ctrl_ver);
656
657 return 0;
658 }
659
cros_typec_port_work(struct work_struct * work)660 static void cros_typec_port_work(struct work_struct *work)
661 {
662 struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
663 int ret, i;
664
665 for (i = 0; i < typec->num_ports; i++) {
666 ret = cros_typec_port_update(typec, i);
667 if (ret < 0)
668 dev_warn(typec->dev, "Update failed for port: %d\n", i);
669 }
670 }
671
cros_ec_typec_event(struct notifier_block * nb,unsigned long host_event,void * _notify)672 static int cros_ec_typec_event(struct notifier_block *nb,
673 unsigned long host_event, void *_notify)
674 {
675 struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
676
677 schedule_work(&typec->port_work);
678
679 return NOTIFY_OK;
680 }
681
682 #ifdef CONFIG_ACPI
683 static const struct acpi_device_id cros_typec_acpi_id[] = {
684 { "GOOG0014", 0 },
685 {}
686 };
687 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
688 #endif
689
690 #ifdef CONFIG_OF
691 static const struct of_device_id cros_typec_of_match[] = {
692 { .compatible = "google,cros-ec-typec", },
693 {}
694 };
695 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
696 #endif
697
cros_typec_probe(struct platform_device * pdev)698 static int cros_typec_probe(struct platform_device *pdev)
699 {
700 struct device *dev = &pdev->dev;
701 struct cros_typec_data *typec;
702 struct ec_response_usb_pd_ports resp;
703 int ret, i;
704
705 typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
706 if (!typec)
707 return -ENOMEM;
708
709 typec->dev = dev;
710 typec->ec = dev_get_drvdata(pdev->dev.parent);
711 platform_set_drvdata(pdev, typec);
712
713 ret = cros_typec_get_cmd_version(typec);
714 if (ret < 0) {
715 dev_err(dev, "failed to get PD command version info\n");
716 return ret;
717 }
718
719 ret = cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
720 &resp, sizeof(resp));
721 if (ret < 0)
722 return ret;
723
724 typec->num_ports = resp.num_ports;
725 if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
726 dev_warn(typec->dev,
727 "Too many ports reported: %d, limiting to max: %d\n",
728 typec->num_ports, EC_USB_PD_MAX_PORTS);
729 typec->num_ports = EC_USB_PD_MAX_PORTS;
730 }
731
732 ret = cros_typec_init_ports(typec);
733 if (ret < 0)
734 return ret;
735
736 INIT_WORK(&typec->port_work, cros_typec_port_work);
737
738 /*
739 * Safe to call port update here, since we haven't registered the
740 * PD notifier yet.
741 */
742 for (i = 0; i < typec->num_ports; i++) {
743 ret = cros_typec_port_update(typec, i);
744 if (ret < 0)
745 goto unregister_ports;
746 }
747
748 typec->nb.notifier_call = cros_ec_typec_event;
749 ret = cros_usbpd_register_notify(&typec->nb);
750 if (ret < 0)
751 goto unregister_ports;
752
753 return 0;
754
755 unregister_ports:
756 cros_unregister_ports(typec);
757 return ret;
758 }
759
cros_typec_suspend(struct device * dev)760 static int __maybe_unused cros_typec_suspend(struct device *dev)
761 {
762 struct cros_typec_data *typec = dev_get_drvdata(dev);
763
764 cancel_work_sync(&typec->port_work);
765
766 return 0;
767 }
768
cros_typec_resume(struct device * dev)769 static int __maybe_unused cros_typec_resume(struct device *dev)
770 {
771 struct cros_typec_data *typec = dev_get_drvdata(dev);
772
773 /* Refresh port state. */
774 schedule_work(&typec->port_work);
775
776 return 0;
777 }
778
779 static const struct dev_pm_ops cros_typec_pm_ops = {
780 SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
781 };
782
783 static struct platform_driver cros_typec_driver = {
784 .driver = {
785 .name = DRV_NAME,
786 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
787 .of_match_table = of_match_ptr(cros_typec_of_match),
788 .pm = &cros_typec_pm_ops,
789 },
790 .probe = cros_typec_probe,
791 };
792
793 module_platform_driver(cros_typec_driver);
794
795 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
796 MODULE_DESCRIPTION("Chrome OS EC Type C control");
797 MODULE_LICENSE("GPL");
798