1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector System Software Interface driver
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/completion.h>
10 #include <linux/property.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/usb/typec.h>
20 #define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
24 * UCSI_TIMEOUT_MS - PPM communication timeout
26 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
27 * specification) here as reference, but unfortunately we can't. It is very
28 * difficult to estimate the time it takes for the system to process the command
29 * before it is actually passed to the PPM.
31 #define UCSI_TIMEOUT_MS 5000
34 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
36 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
37 * if the PPM does not generate Connector Change events before that with
38 * partners that do not support USB Power Delivery, this should still work.
40 #define UCSI_SWAP_TIMEOUT_MS 5000
48 struct ucsi_connector {
52 struct work_struct work;
53 struct completion complete;
55 struct typec_port *port;
56 struct typec_partner *partner;
58 struct typec_capability typec_cap;
60 struct ucsi_connector_status status;
61 struct ucsi_connector_capability cap;
68 enum ucsi_status status;
69 struct completion complete;
70 struct ucsi_capability cap;
71 struct ucsi_connector *connector;
73 struct work_struct work;
75 /* PPM Communication lock */
76 struct mutex ppm_lock;
78 /* PPM communication flags */
80 #define EVENT_PENDING 0
81 #define COMMAND_PENDING 1
85 static inline int ucsi_sync(struct ucsi *ucsi)
87 if (ucsi->ppm && ucsi->ppm->sync)
88 return ucsi->ppm->sync(ucsi->ppm);
92 static int ucsi_command(struct ucsi *ucsi, struct ucsi_control *ctrl)
96 trace_ucsi_command(ctrl);
98 set_bit(COMMAND_PENDING, &ucsi->flags);
100 ret = ucsi->ppm->cmd(ucsi->ppm, ctrl);
104 if (!wait_for_completion_timeout(&ucsi->complete,
105 msecs_to_jiffies(UCSI_TIMEOUT_MS))) {
106 dev_warn(ucsi->dev, "PPM NOT RESPONDING\n");
111 clear_bit(COMMAND_PENDING, &ucsi->flags);
116 static int ucsi_ack(struct ucsi *ucsi, u8 ack)
118 struct ucsi_control ctrl;
123 set_bit(ACK_PENDING, &ucsi->flags);
125 UCSI_CMD_ACK(ctrl, ack);
126 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
130 /* Waiting for ACK with ACK CMD, but not with EVENT for now */
131 if (ack == UCSI_ACK_EVENT)
134 if (!wait_for_completion_timeout(&ucsi->complete,
135 msecs_to_jiffies(UCSI_TIMEOUT_MS)))
139 clear_bit(ACK_PENDING, &ucsi->flags);
142 dev_err(ucsi->dev, "%s: failed\n", __func__);
147 static int ucsi_run_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
148 void *data, size_t size)
150 struct ucsi_control _ctrl;
155 ret = ucsi_command(ucsi, ctrl);
159 switch (ucsi->status) {
161 ret = ucsi_sync(ucsi);
163 dev_warn(ucsi->dev, "%s: sync failed\n", __func__);
166 memcpy(data, ucsi->ppm->data->message_in, size);
168 data_length = ucsi->ppm->data->cci.data_length;
170 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
175 /* The caller decides whether to cancel or not */
179 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
184 _ctrl.cmd.cmd = UCSI_GET_ERROR_STATUS;
185 ret = ucsi_command(ucsi, &_ctrl);
187 dev_err(ucsi->dev, "reading error failed!\n");
191 memcpy(&error, ucsi->ppm->data->message_in, sizeof(error));
193 /* Something has really gone wrong */
194 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
199 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
204 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
207 case UCSI_ERROR_CC_COMMUNICATION_ERR:
210 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
213 case UCSI_ERROR_DEAD_BATTERY:
214 dev_warn(ucsi->dev, "Dead battery condition!\n");
217 /* The following mean a bug in this driver */
218 case UCSI_ERROR_INVALID_CON_NUM:
219 case UCSI_ERROR_UNREGONIZED_CMD:
220 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
222 "%s: possible UCSI driver bug - error 0x%x\n",
228 "%s: error without status\n", __func__);
236 trace_ucsi_run_command(ctrl, ret);
241 /* -------------------------------------------------------------------------- */
243 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
245 switch (con->status.pwr_op_mode) {
246 case UCSI_CONSTAT_PWR_OPMODE_PD:
247 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
249 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
250 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
252 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
253 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
256 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
261 static int ucsi_register_partner(struct ucsi_connector *con)
263 struct typec_partner_desc desc;
264 struct typec_partner *partner;
269 memset(&desc, 0, sizeof(desc));
271 switch (con->status.partner_type) {
272 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
273 desc.accessory = TYPEC_ACCESSORY_DEBUG;
275 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
276 desc.accessory = TYPEC_ACCESSORY_AUDIO;
282 desc.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
284 partner = typec_register_partner(con->port, &desc);
285 if (IS_ERR(partner)) {
286 dev_err(con->ucsi->dev,
287 "con%d: failed to register partner (%ld)\n", con->num,
289 return PTR_ERR(partner);
292 con->partner = partner;
297 static void ucsi_unregister_partner(struct ucsi_connector *con)
302 typec_unregister_partner(con->partner);
306 static void ucsi_connector_change(struct work_struct *work)
308 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
310 struct ucsi *ucsi = con->ucsi;
311 struct ucsi_control ctrl;
314 mutex_lock(&ucsi->ppm_lock);
316 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
317 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
319 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
324 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
325 ucsi_pwr_opmode_change(con);
327 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
328 typec_set_pwr_role(con->port, con->status.pwr_dir);
330 /* Complete pending power role swap */
331 if (!completion_done(&con->complete))
332 complete(&con->complete);
335 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
336 switch (con->status.partner_type) {
337 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
338 typec_set_data_role(con->port, TYPEC_HOST);
340 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
341 typec_set_data_role(con->port, TYPEC_DEVICE);
347 /* Complete pending data role swap */
348 if (!completion_done(&con->complete))
349 complete(&con->complete);
352 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
353 typec_set_pwr_role(con->port, con->status.pwr_dir);
355 switch (con->status.partner_type) {
356 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
357 typec_set_data_role(con->port, TYPEC_HOST);
359 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
360 typec_set_data_role(con->port, TYPEC_DEVICE);
366 if (con->status.connected)
367 ucsi_register_partner(con);
369 ucsi_unregister_partner(con);
372 ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
374 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
376 trace_ucsi_connector_change(con->num, &con->status);
379 clear_bit(EVENT_PENDING, &ucsi->flags);
380 mutex_unlock(&ucsi->ppm_lock);
384 * ucsi_notify - PPM notification handler
385 * @ucsi: Source UCSI Interface for the notifications
387 * Handle notifications from PPM of @ucsi.
389 void ucsi_notify(struct ucsi *ucsi)
391 struct ucsi_cci *cci;
393 /* There is no requirement to sync here, but no harm either. */
396 cci = &ucsi->ppm->data->cci;
399 ucsi->status = UCSI_ERROR;
401 ucsi->status = UCSI_BUSY;
403 ucsi->status = UCSI_IDLE;
405 if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
406 complete(&ucsi->complete);
407 } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
408 complete(&ucsi->complete);
409 } else if (cci->connector_change) {
410 struct ucsi_connector *con;
412 con = &ucsi->connector[cci->connector_change - 1];
414 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
415 schedule_work(&con->work);
418 trace_ucsi_notify(ucsi->ppm->data->raw_cci);
420 EXPORT_SYMBOL_GPL(ucsi_notify);
422 /* -------------------------------------------------------------------------- */
424 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
426 struct ucsi_control ctrl;
428 UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
430 return ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
433 static int ucsi_reset_ppm(struct ucsi *ucsi)
435 struct ucsi_control ctrl;
440 ctrl.cmd.cmd = UCSI_PPM_RESET;
441 trace_ucsi_command(&ctrl);
442 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
446 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
449 /* Here sync is critical. */
450 ret = ucsi_sync(ucsi);
454 if (ucsi->ppm->data->cci.reset_complete)
457 /* If the PPM is still doing something else, reset it again. */
458 if (ucsi->ppm->data->raw_cci) {
459 dev_warn_ratelimited(ucsi->dev,
460 "Failed to reset PPM! Trying again..\n");
462 trace_ucsi_command(&ctrl);
463 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
468 /* Letting the PPM settle down. */
472 } while (time_is_after_jiffies(tmo));
475 trace_ucsi_reset_ppm(&ctrl, ret);
480 static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
484 ret = ucsi_run_command(con->ucsi, ctrl, NULL, 0);
485 if (ret == -ETIMEDOUT) {
486 struct ucsi_control c;
488 /* PPM most likely stopped responding. Resetting everything. */
489 ucsi_reset_ppm(con->ucsi);
491 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
492 ucsi_run_command(con->ucsi, &c, NULL, 0);
494 ucsi_reset_connector(con, true);
501 ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
503 struct ucsi_connector *con = to_ucsi_connector(cap);
504 struct ucsi_control ctrl;
510 mutex_lock(&con->ucsi->ppm_lock);
512 if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
513 role == TYPEC_DEVICE) ||
514 (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
518 UCSI_CMD_SET_UOR(ctrl, con, role);
519 ret = ucsi_role_cmd(con, &ctrl);
523 mutex_unlock(&con->ucsi->ppm_lock);
525 if (!wait_for_completion_timeout(&con->complete,
526 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
532 mutex_unlock(&con->ucsi->ppm_lock);
538 ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
540 struct ucsi_connector *con = to_ucsi_connector(cap);
541 struct ucsi_control ctrl;
547 mutex_lock(&con->ucsi->ppm_lock);
549 if (con->status.pwr_dir == role)
552 UCSI_CMD_SET_PDR(ctrl, con, role);
553 ret = ucsi_role_cmd(con, &ctrl);
557 mutex_unlock(&con->ucsi->ppm_lock);
559 if (!wait_for_completion_timeout(&con->complete,
560 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
563 mutex_lock(&con->ucsi->ppm_lock);
565 /* Something has gone wrong while swapping the role */
566 if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
567 ucsi_reset_connector(con, true);
572 mutex_unlock(&con->ucsi->ppm_lock);
577 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
579 struct fwnode_handle *fwnode;
582 device_for_each_child_node(con->ucsi->dev, fwnode)
588 static int ucsi_register_port(struct ucsi *ucsi, int index)
590 struct ucsi_connector *con = &ucsi->connector[index];
591 struct typec_capability *cap = &con->typec_cap;
592 enum typec_accessory *accessory = cap->accessory;
593 struct ucsi_control ctrl;
596 INIT_WORK(&con->work, ucsi_connector_change);
597 init_completion(&con->complete);
598 con->num = index + 1;
601 /* Get connector capability */
602 UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
603 ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
607 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
608 cap->data = TYPEC_PORT_DRD;
609 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
610 cap->data = TYPEC_PORT_DFP;
611 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
612 cap->data = TYPEC_PORT_UFP;
614 if (con->cap.provider && con->cap.consumer)
615 cap->type = TYPEC_PORT_DRP;
616 else if (con->cap.provider)
617 cap->type = TYPEC_PORT_SRC;
618 else if (con->cap.consumer)
619 cap->type = TYPEC_PORT_SNK;
621 cap->revision = ucsi->cap.typec_version;
622 cap->pd_revision = ucsi->cap.pd_version;
623 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
625 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
626 *accessory++ = TYPEC_ACCESSORY_AUDIO;
627 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
628 *accessory = TYPEC_ACCESSORY_DEBUG;
630 cap->fwnode = ucsi_find_fwnode(con);
631 cap->dr_set = ucsi_dr_swap;
632 cap->pr_set = ucsi_pr_swap;
634 /* Register the connector */
635 con->port = typec_register_port(ucsi->dev, cap);
636 if (IS_ERR(con->port))
637 return PTR_ERR(con->port);
640 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
641 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
643 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
647 ucsi_pwr_opmode_change(con);
648 typec_set_pwr_role(con->port, con->status.pwr_dir);
650 switch (con->status.partner_type) {
651 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
652 typec_set_data_role(con->port, TYPEC_HOST);
654 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
655 typec_set_data_role(con->port, TYPEC_DEVICE);
661 /* Check if there is already something connected */
662 if (con->status.connected)
663 ucsi_register_partner(con);
665 trace_ucsi_register_port(con->num, &con->status);
670 static void ucsi_init(struct work_struct *work)
672 struct ucsi *ucsi = container_of(work, struct ucsi, work);
673 struct ucsi_connector *con;
674 struct ucsi_control ctrl;
678 mutex_lock(&ucsi->ppm_lock);
681 ret = ucsi_reset_ppm(ucsi);
683 dev_err(ucsi->dev, "failed to reset PPM!\n");
687 /* Enable basic notifications */
688 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
689 UCSI_ENABLE_NTFY_ERROR);
690 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
694 /* Get PPM capabilities */
695 UCSI_CMD_GET_CAPABILITY(ctrl);
696 ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
700 if (!ucsi->cap.num_connectors) {
705 /* Allocate the connectors. Released in ucsi_unregister_ppm() */
706 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
707 sizeof(*ucsi->connector), GFP_KERNEL);
708 if (!ucsi->connector) {
713 /* Register all connectors */
714 for (i = 0; i < ucsi->cap.num_connectors; i++) {
715 ret = ucsi_register_port(ucsi, i);
720 /* Enable all notifications */
721 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
722 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
726 mutex_unlock(&ucsi->ppm_lock);
731 for (con = ucsi->connector; con->port; con++) {
732 ucsi_unregister_partner(con);
733 typec_unregister_port(con->port);
738 ucsi_reset_ppm(ucsi);
740 mutex_unlock(&ucsi->ppm_lock);
741 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
745 * ucsi_register_ppm - Register UCSI PPM Interface
746 * @dev: Device interface to the PPM
747 * @ppm: The PPM interface
749 * Allocates UCSI instance, associates it with @ppm and returns it to the
750 * caller, and schedules initialization of the interface.
752 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
756 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
758 return ERR_PTR(-ENOMEM);
760 INIT_WORK(&ucsi->work, ucsi_init);
761 init_completion(&ucsi->complete);
762 mutex_init(&ucsi->ppm_lock);
768 * Communication with the PPM takes a lot of time. It is not reasonable
769 * to initialize the driver here. Using a work for now.
771 queue_work(system_long_wq, &ucsi->work);
775 EXPORT_SYMBOL_GPL(ucsi_register_ppm);
778 * ucsi_unregister_ppm - Unregister UCSI PPM Interface
779 * @ucsi: struct ucsi associated with the PPM
781 * Unregister UCSI PPM that was created with ucsi_register().
783 void ucsi_unregister_ppm(struct ucsi *ucsi)
785 struct ucsi_control ctrl;
788 /* Make sure that we are not in the middle of driver initialization */
789 cancel_work_sync(&ucsi->work);
791 mutex_lock(&ucsi->ppm_lock);
793 /* Disable everything except command complete notification */
794 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
795 ucsi_run_command(ucsi, &ctrl, NULL, 0);
797 mutex_unlock(&ucsi->ppm_lock);
799 for (i = 0; i < ucsi->cap.num_connectors; i++) {
800 cancel_work_sync(&ucsi->connector[i].work);
801 ucsi_unregister_partner(&ucsi->connector[i]);
802 typec_unregister_port(ucsi->connector[i].port);
805 ucsi_reset_ppm(ucsi);
807 kfree(ucsi->connector);
810 EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
812 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
813 MODULE_LICENSE("GPL v2");
814 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");