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