1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector System Software Interface driver
5 * Copyright (C) 2017, Intel Corporation
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 1000
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 partner;
268 memset(&partner, 0, sizeof(partner));
270 switch (con->status.partner_type) {
271 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
272 partner.accessory = TYPEC_ACCESSORY_DEBUG;
274 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
275 partner.accessory = TYPEC_ACCESSORY_AUDIO;
281 partner.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
283 con->partner = typec_register_partner(con->port, &partner);
285 dev_err(con->ucsi->dev, "con%d: failed to register partner\n",
293 static void ucsi_unregister_partner(struct ucsi_connector *con)
295 typec_unregister_partner(con->partner);
299 static void ucsi_connector_change(struct work_struct *work)
301 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
303 struct ucsi *ucsi = con->ucsi;
304 struct ucsi_control ctrl;
307 mutex_lock(&ucsi->ppm_lock);
309 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
310 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
312 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
317 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
318 ucsi_pwr_opmode_change(con);
320 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
321 typec_set_pwr_role(con->port, con->status.pwr_dir);
323 /* Complete pending power role swap */
324 if (!completion_done(&con->complete))
325 complete(&con->complete);
328 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
329 switch (con->status.partner_type) {
330 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
331 typec_set_data_role(con->port, TYPEC_HOST);
333 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
334 typec_set_data_role(con->port, TYPEC_DEVICE);
340 /* Complete pending data role swap */
341 if (!completion_done(&con->complete))
342 complete(&con->complete);
345 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
346 if (con->status.connected)
347 ucsi_register_partner(con);
349 ucsi_unregister_partner(con);
352 ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
354 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
356 trace_ucsi_connector_change(con->num, &con->status);
359 clear_bit(EVENT_PENDING, &ucsi->flags);
360 mutex_unlock(&ucsi->ppm_lock);
364 * ucsi_notify - PPM notification handler
365 * @ucsi: Source UCSI Interface for the notifications
367 * Handle notifications from PPM of @ucsi.
369 void ucsi_notify(struct ucsi *ucsi)
371 struct ucsi_cci *cci;
373 /* There is no requirement to sync here, but no harm either. */
376 cci = &ucsi->ppm->data->cci;
379 ucsi->status = UCSI_ERROR;
381 ucsi->status = UCSI_BUSY;
383 ucsi->status = UCSI_IDLE;
385 if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
386 complete(&ucsi->complete);
387 } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
388 complete(&ucsi->complete);
389 } else if (cci->connector_change) {
390 struct ucsi_connector *con;
392 con = &ucsi->connector[cci->connector_change - 1];
394 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
395 schedule_work(&con->work);
398 trace_ucsi_notify(ucsi->ppm->data->raw_cci);
400 EXPORT_SYMBOL_GPL(ucsi_notify);
402 /* -------------------------------------------------------------------------- */
404 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
406 struct ucsi_control ctrl;
408 UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
410 return ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
413 static int ucsi_reset_ppm(struct ucsi *ucsi)
415 struct ucsi_control ctrl;
420 ctrl.cmd.cmd = UCSI_PPM_RESET;
421 trace_ucsi_command(&ctrl);
422 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
426 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
429 /* Here sync is critical. */
430 ret = ucsi_sync(ucsi);
434 if (ucsi->ppm->data->cci.reset_complete)
437 /* If the PPM is still doing something else, reset it again. */
438 if (ucsi->ppm->data->raw_cci) {
439 dev_warn_ratelimited(ucsi->dev,
440 "Failed to reset PPM! Trying again..\n");
442 trace_ucsi_command(&ctrl);
443 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
448 /* Letting the PPM settle down. */
452 } while (time_is_after_jiffies(tmo));
455 trace_ucsi_reset_ppm(&ctrl, ret);
460 static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
464 ret = ucsi_run_command(con->ucsi, ctrl, NULL, 0);
465 if (ret == -ETIMEDOUT) {
466 struct ucsi_control c;
468 /* PPM most likely stopped responding. Resetting everything. */
469 ucsi_reset_ppm(con->ucsi);
471 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
472 ucsi_run_command(con->ucsi, &c, NULL, 0);
474 ucsi_reset_connector(con, true);
481 ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
483 struct ucsi_connector *con = to_ucsi_connector(cap);
484 struct ucsi_control ctrl;
490 mutex_lock(&con->ucsi->ppm_lock);
492 if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
493 role == TYPEC_DEVICE) ||
494 (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
498 UCSI_CMD_SET_UOR(ctrl, con, role);
499 ret = ucsi_role_cmd(con, &ctrl);
503 mutex_unlock(&con->ucsi->ppm_lock);
505 if (!wait_for_completion_timeout(&con->complete,
506 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
512 mutex_unlock(&con->ucsi->ppm_lock);
518 ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
520 struct ucsi_connector *con = to_ucsi_connector(cap);
521 struct ucsi_control ctrl;
527 mutex_lock(&con->ucsi->ppm_lock);
529 if (con->status.pwr_dir == role)
532 UCSI_CMD_SET_PDR(ctrl, con, role);
533 ret = ucsi_role_cmd(con, &ctrl);
537 mutex_unlock(&con->ucsi->ppm_lock);
539 if (!wait_for_completion_timeout(&con->complete,
540 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
543 mutex_lock(&con->ucsi->ppm_lock);
545 /* Something has gone wrong while swapping the role */
546 if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
547 ucsi_reset_connector(con, true);
552 mutex_unlock(&con->ucsi->ppm_lock);
557 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
559 struct fwnode_handle *fwnode;
562 device_for_each_child_node(con->ucsi->dev, fwnode)
568 static int ucsi_register_port(struct ucsi *ucsi, int index)
570 struct ucsi_connector *con = &ucsi->connector[index];
571 struct typec_capability *cap = &con->typec_cap;
572 enum typec_accessory *accessory = cap->accessory;
573 struct ucsi_control ctrl;
576 INIT_WORK(&con->work, ucsi_connector_change);
577 init_completion(&con->complete);
578 con->num = index + 1;
581 /* Get connector capability */
582 UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
583 ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
587 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
588 cap->type = TYPEC_PORT_DRP;
589 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
590 cap->type = TYPEC_PORT_DFP;
591 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
592 cap->type = TYPEC_PORT_UFP;
594 cap->revision = ucsi->cap.typec_version;
595 cap->pd_revision = ucsi->cap.pd_version;
596 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
598 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
599 *accessory++ = TYPEC_ACCESSORY_AUDIO;
600 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
601 *accessory = TYPEC_ACCESSORY_DEBUG;
603 cap->fwnode = ucsi_find_fwnode(con);
604 cap->dr_set = ucsi_dr_swap;
605 cap->pr_set = ucsi_pr_swap;
607 /* Register the connector */
608 con->port = typec_register_port(ucsi->dev, cap);
613 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
614 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
616 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
620 ucsi_pwr_opmode_change(con);
621 typec_set_pwr_role(con->port, con->status.pwr_dir);
623 switch (con->status.partner_type) {
624 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
625 typec_set_data_role(con->port, TYPEC_HOST);
627 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
628 typec_set_data_role(con->port, TYPEC_DEVICE);
634 /* Check if there is already something connected */
635 if (con->status.connected)
636 ucsi_register_partner(con);
638 trace_ucsi_register_port(con->num, &con->status);
643 static void ucsi_init(struct work_struct *work)
645 struct ucsi *ucsi = container_of(work, struct ucsi, work);
646 struct ucsi_connector *con;
647 struct ucsi_control ctrl;
651 mutex_lock(&ucsi->ppm_lock);
654 ret = ucsi_reset_ppm(ucsi);
656 dev_err(ucsi->dev, "failed to reset PPM!\n");
660 /* Enable basic notifications */
661 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
662 UCSI_ENABLE_NTFY_ERROR);
663 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
667 /* Get PPM capabilities */
668 UCSI_CMD_GET_CAPABILITY(ctrl);
669 ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
673 if (!ucsi->cap.num_connectors) {
678 /* Allocate the connectors. Released in ucsi_unregister_ppm() */
679 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
680 sizeof(*ucsi->connector), GFP_KERNEL);
681 if (!ucsi->connector) {
686 /* Register all connectors */
687 for (i = 0; i < ucsi->cap.num_connectors; i++) {
688 ret = ucsi_register_port(ucsi, i);
693 /* Enable all notifications */
694 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
695 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
699 mutex_unlock(&ucsi->ppm_lock);
704 for (con = ucsi->connector; con->port; con++) {
705 ucsi_unregister_partner(con);
706 typec_unregister_port(con->port);
711 ucsi_reset_ppm(ucsi);
713 mutex_unlock(&ucsi->ppm_lock);
714 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
718 * ucsi_register_ppm - Register UCSI PPM Interface
719 * @dev: Device interface to the PPM
720 * @ppm: The PPM interface
722 * Allocates UCSI instance, associates it with @ppm and returns it to the
723 * caller, and schedules initialization of the interface.
725 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
729 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
731 return ERR_PTR(-ENOMEM);
733 INIT_WORK(&ucsi->work, ucsi_init);
734 init_completion(&ucsi->complete);
735 mutex_init(&ucsi->ppm_lock);
741 * Communication with the PPM takes a lot of time. It is not reasonable
742 * to initialize the driver here. Using a work for now.
744 queue_work(system_long_wq, &ucsi->work);
748 EXPORT_SYMBOL_GPL(ucsi_register_ppm);
751 * ucsi_unregister_ppm - Unregister UCSI PPM Interface
752 * @ucsi: struct ucsi associated with the PPM
754 * Unregister UCSI PPM that was created with ucsi_register().
756 void ucsi_unregister_ppm(struct ucsi *ucsi)
758 struct ucsi_control ctrl;
761 /* Make sure that we are not in the middle of driver initialization */
762 cancel_work_sync(&ucsi->work);
764 mutex_lock(&ucsi->ppm_lock);
766 /* Disable everything except command complete notification */
767 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
768 ucsi_run_command(ucsi, &ctrl, NULL, 0);
770 mutex_unlock(&ucsi->ppm_lock);
772 for (i = 0; i < ucsi->cap.num_connectors; i++) {
773 cancel_work_sync(&ucsi->connector[i].work);
774 ucsi_unregister_partner(&ucsi->connector[i]);
775 typec_unregister_port(ucsi->connector[i].port);
778 ucsi_reset_ppm(ucsi);
780 kfree(ucsi->connector);
783 EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
786 MODULE_LICENSE("GPL v2");
787 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");