2 * USB Type-C Connector System Software Interface driver
4 * Copyright (C) 2017, Intel Corporation
5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/completion.h>
13 #include <linux/property.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/usb/typec.h>
23 #define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
27 * UCSI_TIMEOUT_MS - PPM communication timeout
29 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
30 * specification) here as reference, but unfortunately we can't. It is very
31 * difficult to estimate the time it takes for the system to process the command
32 * before it is actually passed to the PPM.
34 #define UCSI_TIMEOUT_MS 5000
37 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
39 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
40 * if the PPM does not generate Connector Change events before that with
41 * partners that do not support USB Power Delivery, this should still work.
43 #define UCSI_SWAP_TIMEOUT_MS 5000
51 struct ucsi_connector {
55 struct work_struct work;
56 struct completion complete;
58 struct typec_port *port;
59 struct typec_partner *partner;
61 struct typec_capability typec_cap;
63 struct ucsi_connector_status status;
64 struct ucsi_connector_capability cap;
71 enum ucsi_status status;
72 struct completion complete;
73 struct ucsi_capability cap;
74 struct ucsi_connector *connector;
76 struct work_struct work;
78 /* PPM Communication lock */
79 struct mutex ppm_lock;
81 /* PPM communication flags */
83 #define EVENT_PENDING 0
84 #define COMMAND_PENDING 1
88 static inline int ucsi_sync(struct ucsi *ucsi)
90 if (ucsi->ppm && ucsi->ppm->sync)
91 return ucsi->ppm->sync(ucsi->ppm);
95 static int ucsi_command(struct ucsi *ucsi, struct ucsi_control *ctrl)
99 trace_ucsi_command(ctrl);
101 set_bit(COMMAND_PENDING, &ucsi->flags);
103 ret = ucsi->ppm->cmd(ucsi->ppm, ctrl);
107 if (!wait_for_completion_timeout(&ucsi->complete,
108 msecs_to_jiffies(UCSI_TIMEOUT_MS))) {
109 dev_warn(ucsi->dev, "PPM NOT RESPONDING\n");
114 clear_bit(COMMAND_PENDING, &ucsi->flags);
119 static int ucsi_ack(struct ucsi *ucsi, u8 ack)
121 struct ucsi_control ctrl;
126 set_bit(ACK_PENDING, &ucsi->flags);
128 UCSI_CMD_ACK(ctrl, ack);
129 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
133 /* Waiting for ACK with ACK CMD, but not with EVENT for now */
134 if (ack == UCSI_ACK_EVENT)
137 if (!wait_for_completion_timeout(&ucsi->complete,
138 msecs_to_jiffies(UCSI_TIMEOUT_MS)))
142 clear_bit(ACK_PENDING, &ucsi->flags);
145 dev_err(ucsi->dev, "%s: failed\n", __func__);
150 static int ucsi_run_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
151 void *data, size_t size)
153 struct ucsi_control _ctrl;
158 ret = ucsi_command(ucsi, ctrl);
162 switch (ucsi->status) {
164 ret = ucsi_sync(ucsi);
166 dev_warn(ucsi->dev, "%s: sync failed\n", __func__);
169 memcpy(data, ucsi->ppm->data->message_in, size);
171 data_length = ucsi->ppm->data->cci.data_length;
173 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
178 /* The caller decides whether to cancel or not */
182 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
187 _ctrl.cmd.cmd = UCSI_GET_ERROR_STATUS;
188 ret = ucsi_command(ucsi, &_ctrl);
190 dev_err(ucsi->dev, "reading error failed!\n");
194 memcpy(&error, ucsi->ppm->data->message_in, sizeof(error));
196 /* Something has really gone wrong */
197 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
202 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
207 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
210 case UCSI_ERROR_CC_COMMUNICATION_ERR:
213 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
216 case UCSI_ERROR_DEAD_BATTERY:
217 dev_warn(ucsi->dev, "Dead battery condition!\n");
220 /* The following mean a bug in this driver */
221 case UCSI_ERROR_INVALID_CON_NUM:
222 case UCSI_ERROR_UNREGONIZED_CMD:
223 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
225 "%s: possible UCSI driver bug - error 0x%x\n",
231 "%s: error without status\n", __func__);
239 trace_ucsi_run_command(ctrl, ret);
244 /* -------------------------------------------------------------------------- */
246 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
248 switch (con->status.pwr_op_mode) {
249 case UCSI_CONSTAT_PWR_OPMODE_PD:
250 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
252 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
253 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
255 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
256 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
259 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
264 static int ucsi_register_partner(struct ucsi_connector *con)
266 struct typec_partner_desc partner;
271 memset(&partner, 0, sizeof(partner));
273 switch (con->status.partner_type) {
274 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
275 partner.accessory = TYPEC_ACCESSORY_DEBUG;
277 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
278 partner.accessory = TYPEC_ACCESSORY_AUDIO;
284 partner.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
286 con->partner = typec_register_partner(con->port, &partner);
288 dev_err(con->ucsi->dev, "con%d: failed to register partner\n",
296 static void ucsi_unregister_partner(struct ucsi_connector *con)
298 typec_unregister_partner(con->partner);
302 static void ucsi_connector_change(struct work_struct *work)
304 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
306 struct ucsi *ucsi = con->ucsi;
307 struct ucsi_control ctrl;
310 mutex_lock(&ucsi->ppm_lock);
312 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
313 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
315 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
320 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
321 ucsi_pwr_opmode_change(con);
323 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
324 typec_set_pwr_role(con->port, con->status.pwr_dir);
326 /* Complete pending power role swap */
327 if (!completion_done(&con->complete))
328 complete(&con->complete);
331 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
332 switch (con->status.partner_type) {
333 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
334 typec_set_data_role(con->port, TYPEC_HOST);
336 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
337 typec_set_data_role(con->port, TYPEC_DEVICE);
343 /* Complete pending data role swap */
344 if (!completion_done(&con->complete))
345 complete(&con->complete);
348 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
349 typec_set_pwr_role(con->port, con->status.pwr_dir);
351 switch (con->status.partner_type) {
352 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
353 typec_set_data_role(con->port, TYPEC_HOST);
355 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
356 typec_set_data_role(con->port, TYPEC_DEVICE);
362 if (con->status.connected)
363 ucsi_register_partner(con);
365 ucsi_unregister_partner(con);
368 ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
370 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
372 trace_ucsi_connector_change(con->num, &con->status);
375 clear_bit(EVENT_PENDING, &ucsi->flags);
376 mutex_unlock(&ucsi->ppm_lock);
380 * ucsi_notify - PPM notification handler
381 * @ucsi: Source UCSI Interface for the notifications
383 * Handle notifications from PPM of @ucsi.
385 void ucsi_notify(struct ucsi *ucsi)
387 struct ucsi_cci *cci;
389 /* There is no requirement to sync here, but no harm either. */
392 cci = &ucsi->ppm->data->cci;
395 ucsi->status = UCSI_ERROR;
397 ucsi->status = UCSI_BUSY;
399 ucsi->status = UCSI_IDLE;
401 if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
402 complete(&ucsi->complete);
403 } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
404 complete(&ucsi->complete);
405 } else if (cci->connector_change) {
406 struct ucsi_connector *con;
408 con = &ucsi->connector[cci->connector_change - 1];
410 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
411 schedule_work(&con->work);
414 trace_ucsi_notify(ucsi->ppm->data->raw_cci);
416 EXPORT_SYMBOL_GPL(ucsi_notify);
418 /* -------------------------------------------------------------------------- */
420 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
422 struct ucsi_control ctrl;
424 UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
426 return ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
429 static int ucsi_reset_ppm(struct ucsi *ucsi)
431 struct ucsi_control ctrl;
436 ctrl.cmd.cmd = UCSI_PPM_RESET;
437 trace_ucsi_command(&ctrl);
438 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
442 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
445 /* Here sync is critical. */
446 ret = ucsi_sync(ucsi);
450 if (ucsi->ppm->data->cci.reset_complete)
453 /* If the PPM is still doing something else, reset it again. */
454 if (ucsi->ppm->data->raw_cci) {
455 dev_warn_ratelimited(ucsi->dev,
456 "Failed to reset PPM! Trying again..\n");
458 trace_ucsi_command(&ctrl);
459 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
464 /* Letting the PPM settle down. */
468 } while (time_is_after_jiffies(tmo));
471 trace_ucsi_reset_ppm(&ctrl, ret);
476 static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
480 ret = ucsi_run_command(con->ucsi, ctrl, NULL, 0);
481 if (ret == -ETIMEDOUT) {
482 struct ucsi_control c;
484 /* PPM most likely stopped responding. Resetting everything. */
485 ucsi_reset_ppm(con->ucsi);
487 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
488 ucsi_run_command(con->ucsi, &c, NULL, 0);
490 ucsi_reset_connector(con, true);
497 ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
499 struct ucsi_connector *con = to_ucsi_connector(cap);
500 struct ucsi_control ctrl;
506 mutex_lock(&con->ucsi->ppm_lock);
508 if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
509 role == TYPEC_DEVICE) ||
510 (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
514 UCSI_CMD_SET_UOR(ctrl, con, role);
515 ret = ucsi_role_cmd(con, &ctrl);
519 mutex_unlock(&con->ucsi->ppm_lock);
521 if (!wait_for_completion_timeout(&con->complete,
522 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
528 mutex_unlock(&con->ucsi->ppm_lock);
534 ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
536 struct ucsi_connector *con = to_ucsi_connector(cap);
537 struct ucsi_control ctrl;
543 mutex_lock(&con->ucsi->ppm_lock);
545 if (con->status.pwr_dir == role)
548 UCSI_CMD_SET_PDR(ctrl, con, role);
549 ret = ucsi_role_cmd(con, &ctrl);
553 mutex_unlock(&con->ucsi->ppm_lock);
555 if (!wait_for_completion_timeout(&con->complete,
556 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
559 mutex_lock(&con->ucsi->ppm_lock);
561 /* Something has gone wrong while swapping the role */
562 if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
563 ucsi_reset_connector(con, true);
568 mutex_unlock(&con->ucsi->ppm_lock);
573 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
575 struct fwnode_handle *fwnode;
578 device_for_each_child_node(con->ucsi->dev, fwnode)
584 static int ucsi_register_port(struct ucsi *ucsi, int index)
586 struct ucsi_connector *con = &ucsi->connector[index];
587 struct typec_capability *cap = &con->typec_cap;
588 enum typec_accessory *accessory = cap->accessory;
589 struct ucsi_control ctrl;
592 INIT_WORK(&con->work, ucsi_connector_change);
593 init_completion(&con->complete);
594 con->num = index + 1;
597 /* Get connector capability */
598 UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
599 ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
603 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
604 cap->type = TYPEC_PORT_DRP;
605 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
606 cap->type = TYPEC_PORT_DFP;
607 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
608 cap->type = TYPEC_PORT_UFP;
610 cap->revision = ucsi->cap.typec_version;
611 cap->pd_revision = ucsi->cap.pd_version;
612 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
614 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
615 *accessory++ = TYPEC_ACCESSORY_AUDIO;
616 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
617 *accessory = TYPEC_ACCESSORY_DEBUG;
619 cap->fwnode = ucsi_find_fwnode(con);
620 cap->dr_set = ucsi_dr_swap;
621 cap->pr_set = ucsi_pr_swap;
623 /* Register the connector */
624 con->port = typec_register_port(ucsi->dev, cap);
629 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
630 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
632 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
636 ucsi_pwr_opmode_change(con);
637 typec_set_pwr_role(con->port, con->status.pwr_dir);
639 switch (con->status.partner_type) {
640 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
641 typec_set_data_role(con->port, TYPEC_HOST);
643 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
644 typec_set_data_role(con->port, TYPEC_DEVICE);
650 /* Check if there is already something connected */
651 if (con->status.connected)
652 ucsi_register_partner(con);
654 trace_ucsi_register_port(con->num, &con->status);
659 static void ucsi_init(struct work_struct *work)
661 struct ucsi *ucsi = container_of(work, struct ucsi, work);
662 struct ucsi_connector *con;
663 struct ucsi_control ctrl;
667 mutex_lock(&ucsi->ppm_lock);
670 ret = ucsi_reset_ppm(ucsi);
672 dev_err(ucsi->dev, "failed to reset PPM!\n");
676 /* Enable basic notifications */
677 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
678 UCSI_ENABLE_NTFY_ERROR);
679 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
683 /* Get PPM capabilities */
684 UCSI_CMD_GET_CAPABILITY(ctrl);
685 ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
689 if (!ucsi->cap.num_connectors) {
694 /* Allocate the connectors. Released in ucsi_unregister_ppm() */
695 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
696 sizeof(*ucsi->connector), GFP_KERNEL);
697 if (!ucsi->connector) {
702 /* Register all connectors */
703 for (i = 0; i < ucsi->cap.num_connectors; i++) {
704 ret = ucsi_register_port(ucsi, i);
709 /* Enable all notifications */
710 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
711 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
715 mutex_unlock(&ucsi->ppm_lock);
720 for (con = ucsi->connector; con->port; con++) {
721 ucsi_unregister_partner(con);
722 typec_unregister_port(con->port);
727 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
728 ucsi_reset_ppm(ucsi);
730 mutex_unlock(&ucsi->ppm_lock);
731 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
735 * ucsi_register_ppm - Register UCSI PPM Interface
736 * @dev: Device interface to the PPM
737 * @ppm: The PPM interface
739 * Allocates UCSI instance, associates it with @ppm and returns it to the
740 * caller, and schedules initialization of the interface.
742 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
746 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
748 return ERR_PTR(-ENOMEM);
750 INIT_WORK(&ucsi->work, ucsi_init);
751 init_completion(&ucsi->complete);
752 mutex_init(&ucsi->ppm_lock);
758 * Communication with the PPM takes a lot of time. It is not reasonable
759 * to initialize the driver here. Using a work for now.
761 queue_work(system_long_wq, &ucsi->work);
765 EXPORT_SYMBOL_GPL(ucsi_register_ppm);
768 * ucsi_unregister_ppm - Unregister UCSI PPM Interface
769 * @ucsi: struct ucsi associated with the PPM
771 * Unregister UCSI PPM that was created with ucsi_register().
773 void ucsi_unregister_ppm(struct ucsi *ucsi)
775 struct ucsi_control ctrl;
778 /* Make sure that we are not in the middle of driver initialization */
779 cancel_work_sync(&ucsi->work);
781 mutex_lock(&ucsi->ppm_lock);
783 /* Disable everything except command complete notification */
784 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
785 ucsi_run_command(ucsi, &ctrl, NULL, 0);
787 mutex_unlock(&ucsi->ppm_lock);
789 for (i = 0; i < ucsi->cap.num_connectors; i++) {
790 cancel_work_sync(&ucsi->connector[i].work);
791 ucsi_unregister_partner(&ucsi->connector[i]);
792 typec_unregister_port(ucsi->connector[i].port);
795 ucsi_reset_ppm(ucsi);
797 kfree(ucsi->connector);
800 EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
802 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
803 MODULE_LICENSE("GPL v2");
804 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");