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 if (con->status.connected)
354 ucsi_register_partner(con);
356 ucsi_unregister_partner(con);
359 ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
361 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
363 trace_ucsi_connector_change(con->num, &con->status);
366 clear_bit(EVENT_PENDING, &ucsi->flags);
367 mutex_unlock(&ucsi->ppm_lock);
371 * ucsi_notify - PPM notification handler
372 * @ucsi: Source UCSI Interface for the notifications
374 * Handle notifications from PPM of @ucsi.
376 void ucsi_notify(struct ucsi *ucsi)
378 struct ucsi_cci *cci;
380 /* There is no requirement to sync here, but no harm either. */
383 cci = &ucsi->ppm->data->cci;
386 ucsi->status = UCSI_ERROR;
388 ucsi->status = UCSI_BUSY;
390 ucsi->status = UCSI_IDLE;
392 if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
393 complete(&ucsi->complete);
394 } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
395 complete(&ucsi->complete);
396 } else if (cci->connector_change) {
397 struct ucsi_connector *con;
399 con = &ucsi->connector[cci->connector_change - 1];
401 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
402 schedule_work(&con->work);
405 trace_ucsi_notify(ucsi->ppm->data->raw_cci);
407 EXPORT_SYMBOL_GPL(ucsi_notify);
409 /* -------------------------------------------------------------------------- */
411 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
413 struct ucsi_control ctrl;
415 UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
417 return ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
420 static int ucsi_reset_ppm(struct ucsi *ucsi)
422 struct ucsi_control ctrl;
427 ctrl.cmd.cmd = UCSI_PPM_RESET;
428 trace_ucsi_command(&ctrl);
429 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
433 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
436 /* Here sync is critical. */
437 ret = ucsi_sync(ucsi);
441 if (ucsi->ppm->data->cci.reset_complete)
444 /* If the PPM is still doing something else, reset it again. */
445 if (ucsi->ppm->data->raw_cci) {
446 dev_warn_ratelimited(ucsi->dev,
447 "Failed to reset PPM! Trying again..\n");
449 trace_ucsi_command(&ctrl);
450 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
455 /* Letting the PPM settle down. */
459 } while (time_is_after_jiffies(tmo));
462 trace_ucsi_reset_ppm(&ctrl, ret);
467 static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
471 ret = ucsi_run_command(con->ucsi, ctrl, NULL, 0);
472 if (ret == -ETIMEDOUT) {
473 struct ucsi_control c;
475 /* PPM most likely stopped responding. Resetting everything. */
476 ucsi_reset_ppm(con->ucsi);
478 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
479 ucsi_run_command(con->ucsi, &c, NULL, 0);
481 ucsi_reset_connector(con, true);
488 ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
490 struct ucsi_connector *con = to_ucsi_connector(cap);
491 struct ucsi_control ctrl;
497 mutex_lock(&con->ucsi->ppm_lock);
499 if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
500 role == TYPEC_DEVICE) ||
501 (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
505 UCSI_CMD_SET_UOR(ctrl, con, role);
506 ret = ucsi_role_cmd(con, &ctrl);
510 mutex_unlock(&con->ucsi->ppm_lock);
512 if (!wait_for_completion_timeout(&con->complete,
513 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
519 mutex_unlock(&con->ucsi->ppm_lock);
525 ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
527 struct ucsi_connector *con = to_ucsi_connector(cap);
528 struct ucsi_control ctrl;
534 mutex_lock(&con->ucsi->ppm_lock);
536 if (con->status.pwr_dir == role)
539 UCSI_CMD_SET_PDR(ctrl, con, role);
540 ret = ucsi_role_cmd(con, &ctrl);
544 mutex_unlock(&con->ucsi->ppm_lock);
546 if (!wait_for_completion_timeout(&con->complete,
547 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
550 mutex_lock(&con->ucsi->ppm_lock);
552 /* Something has gone wrong while swapping the role */
553 if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
554 ucsi_reset_connector(con, true);
559 mutex_unlock(&con->ucsi->ppm_lock);
564 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
566 struct fwnode_handle *fwnode;
569 device_for_each_child_node(con->ucsi->dev, fwnode)
575 static int ucsi_register_port(struct ucsi *ucsi, int index)
577 struct ucsi_connector *con = &ucsi->connector[index];
578 struct typec_capability *cap = &con->typec_cap;
579 enum typec_accessory *accessory = cap->accessory;
580 struct ucsi_control ctrl;
583 INIT_WORK(&con->work, ucsi_connector_change);
584 init_completion(&con->complete);
585 con->num = index + 1;
588 /* Get connector capability */
589 UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
590 ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
594 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
595 cap->data = TYPEC_PORT_DRD;
596 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
597 cap->data = TYPEC_PORT_DFP;
598 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
599 cap->data = TYPEC_PORT_UFP;
601 if (con->cap.provider && con->cap.consumer)
602 cap->type = TYPEC_PORT_DRP;
603 else if (con->cap.provider)
604 cap->type = TYPEC_PORT_SRC;
605 else if (con->cap.consumer)
606 cap->type = TYPEC_PORT_SNK;
608 cap->revision = ucsi->cap.typec_version;
609 cap->pd_revision = ucsi->cap.pd_version;
610 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
612 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
613 *accessory++ = TYPEC_ACCESSORY_AUDIO;
614 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
615 *accessory = TYPEC_ACCESSORY_DEBUG;
617 cap->fwnode = ucsi_find_fwnode(con);
618 cap->dr_set = ucsi_dr_swap;
619 cap->pr_set = ucsi_pr_swap;
621 /* Register the connector */
622 con->port = typec_register_port(ucsi->dev, cap);
623 if (IS_ERR(con->port))
624 return PTR_ERR(con->port);
627 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
628 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
630 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
634 ucsi_pwr_opmode_change(con);
635 typec_set_pwr_role(con->port, con->status.pwr_dir);
637 switch (con->status.partner_type) {
638 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
639 typec_set_data_role(con->port, TYPEC_HOST);
641 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
642 typec_set_data_role(con->port, TYPEC_DEVICE);
648 /* Check if there is already something connected */
649 if (con->status.connected)
650 ucsi_register_partner(con);
652 trace_ucsi_register_port(con->num, &con->status);
657 static void ucsi_init(struct work_struct *work)
659 struct ucsi *ucsi = container_of(work, struct ucsi, work);
660 struct ucsi_connector *con;
661 struct ucsi_control ctrl;
665 mutex_lock(&ucsi->ppm_lock);
668 ret = ucsi_reset_ppm(ucsi);
670 dev_err(ucsi->dev, "failed to reset PPM!\n");
674 /* Enable basic notifications */
675 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
676 UCSI_ENABLE_NTFY_ERROR);
677 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
681 /* Get PPM capabilities */
682 UCSI_CMD_GET_CAPABILITY(ctrl);
683 ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
687 if (!ucsi->cap.num_connectors) {
692 /* Allocate the connectors. Released in ucsi_unregister_ppm() */
693 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
694 sizeof(*ucsi->connector), GFP_KERNEL);
695 if (!ucsi->connector) {
700 /* Register all connectors */
701 for (i = 0; i < ucsi->cap.num_connectors; i++) {
702 ret = ucsi_register_port(ucsi, i);
707 /* Enable all notifications */
708 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
709 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
713 mutex_unlock(&ucsi->ppm_lock);
718 for (con = ucsi->connector; con->port; con++) {
719 ucsi_unregister_partner(con);
720 typec_unregister_port(con->port);
725 ucsi_reset_ppm(ucsi);
727 mutex_unlock(&ucsi->ppm_lock);
728 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
732 * ucsi_register_ppm - Register UCSI PPM Interface
733 * @dev: Device interface to the PPM
734 * @ppm: The PPM interface
736 * Allocates UCSI instance, associates it with @ppm and returns it to the
737 * caller, and schedules initialization of the interface.
739 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
743 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
745 return ERR_PTR(-ENOMEM);
747 INIT_WORK(&ucsi->work, ucsi_init);
748 init_completion(&ucsi->complete);
749 mutex_init(&ucsi->ppm_lock);
755 * Communication with the PPM takes a lot of time. It is not reasonable
756 * to initialize the driver here. Using a work for now.
758 queue_work(system_long_wq, &ucsi->work);
762 EXPORT_SYMBOL_GPL(ucsi_register_ppm);
765 * ucsi_unregister_ppm - Unregister UCSI PPM Interface
766 * @ucsi: struct ucsi associated with the PPM
768 * Unregister UCSI PPM that was created with ucsi_register().
770 void ucsi_unregister_ppm(struct ucsi *ucsi)
772 struct ucsi_control ctrl;
775 /* Make sure that we are not in the middle of driver initialization */
776 cancel_work_sync(&ucsi->work);
778 mutex_lock(&ucsi->ppm_lock);
780 /* Disable everything except command complete notification */
781 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
782 ucsi_run_command(ucsi, &ctrl, NULL, 0);
784 mutex_unlock(&ucsi->ppm_lock);
786 for (i = 0; i < ucsi->cap.num_connectors; i++) {
787 cancel_work_sync(&ucsi->connector[i].work);
788 ucsi_unregister_partner(&ucsi->connector[i]);
789 typec_unregister_port(ucsi->connector[i].port);
792 ucsi_reset_ppm(ucsi);
794 kfree(ucsi->connector);
797 EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
799 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
800 MODULE_LICENSE("GPL v2");
801 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");