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_dp.h>
21 * UCSI_TIMEOUT_MS - PPM communication timeout
23 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
24 * specification) here as reference, but unfortunately we can't. It is very
25 * difficult to estimate the time it takes for the system to process the command
26 * before it is actually passed to the PPM.
28 #define UCSI_TIMEOUT_MS 5000
31 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
33 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
34 * if the PPM does not generate Connector Change events before that with
35 * partners that do not support USB Power Delivery, this should still work.
37 #define UCSI_SWAP_TIMEOUT_MS 5000
39 static int ucsi_acknowledge_command(struct ucsi *ucsi)
43 ctrl = UCSI_ACK_CC_CI;
44 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
46 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
49 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
53 ctrl = UCSI_ACK_CC_CI;
54 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
56 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
59 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
61 static int ucsi_read_error(struct ucsi *ucsi)
66 /* Acknowledge the command that failed */
67 ret = ucsi_acknowledge_command(ucsi);
71 ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
75 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
79 ret = ucsi_acknowledge_command(ucsi);
84 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
86 case UCSI_ERROR_CC_COMMUNICATION_ERR:
88 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
90 case UCSI_ERROR_DEAD_BATTERY:
91 dev_warn(ucsi->dev, "Dead battery condition!\n");
93 case UCSI_ERROR_INVALID_CON_NUM:
94 case UCSI_ERROR_UNREGONIZED_CMD:
95 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
96 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
98 case UCSI_ERROR_OVERCURRENT:
99 dev_warn(ucsi->dev, "Overcurrent condition\n");
101 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
102 dev_warn(ucsi->dev, "Partner rejected swap\n");
104 case UCSI_ERROR_HARD_RESET:
105 dev_warn(ucsi->dev, "Hard reset occurred\n");
107 case UCSI_ERROR_PPM_POLICY_CONFLICT:
108 dev_warn(ucsi->dev, "PPM Policy conflict\n");
110 case UCSI_ERROR_SWAP_REJECTED:
111 dev_warn(ucsi->dev, "Swap rejected\n");
113 case UCSI_ERROR_UNDEFINED:
115 dev_err(ucsi->dev, "unknown error %u\n", error);
122 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
127 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
131 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
135 if (cci & UCSI_CCI_BUSY) {
136 ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
140 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
143 if (cci & UCSI_CCI_NOT_SUPPORTED)
146 if (cci & UCSI_CCI_ERROR) {
147 if (cmd == UCSI_GET_ERROR_STATUS)
149 return ucsi_read_error(ucsi);
152 return UCSI_CCI_LENGTH(cci);
155 int ucsi_send_command(struct ucsi *ucsi, u64 command,
156 void *data, size_t size)
161 mutex_lock(&ucsi->ppm_lock);
163 ret = ucsi_exec_command(ucsi, command);
170 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
175 ret = ucsi_acknowledge_command(ucsi);
181 mutex_unlock(&ucsi->ppm_lock);
184 EXPORT_SYMBOL_GPL(ucsi_send_command);
186 /* -------------------------------------------------------------------------- */
189 struct delayed_work work;
190 struct list_head node;
193 struct ucsi_connector *con;
194 int (*cb)(struct ucsi_connector *);
197 static void ucsi_poll_worker(struct work_struct *work)
199 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
200 struct ucsi_connector *con = uwork->con;
203 mutex_lock(&con->lock);
206 list_del(&uwork->node);
207 mutex_unlock(&con->lock);
212 ret = uwork->cb(con);
214 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
215 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
217 list_del(&uwork->node);
221 mutex_unlock(&con->lock);
224 static int ucsi_partner_task(struct ucsi_connector *con,
225 int (*cb)(struct ucsi_connector *),
226 int retries, unsigned long delay)
228 struct ucsi_work *uwork;
233 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
237 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
238 uwork->count = retries;
239 uwork->delay = delay;
243 list_add_tail(&uwork->node, &con->partner_tasks);
244 queue_delayed_work(con->wq, &uwork->work, delay);
249 /* -------------------------------------------------------------------------- */
251 void ucsi_altmode_update_active(struct ucsi_connector *con)
253 const struct typec_altmode *altmode = NULL;
259 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
260 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
262 if (con->ucsi->version > 0x0100) {
263 dev_err(con->ucsi->dev,
264 "GET_CURRENT_CAM command failed\n");
270 if (cur < UCSI_MAX_ALTMODES)
271 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
273 for (i = 0; con->partner_altmode[i]; i++)
274 typec_altmode_update_active(con->partner_altmode[i],
275 con->partner_altmode[i] == altmode);
278 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
283 for (i = 0; alt[i]; i++) {
284 if (i > MODE_DISCOVERY_MAX)
287 if (alt[i]->svid == svid)
294 static int ucsi_next_altmode(struct typec_altmode **alt)
298 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
305 static int ucsi_get_num_altmode(struct typec_altmode **alt)
309 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
316 static int ucsi_register_altmode(struct ucsi_connector *con,
317 struct typec_altmode_desc *desc,
320 struct typec_altmode *alt;
325 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
328 case UCSI_RECIPIENT_CON:
329 i = ucsi_next_altmode(con->port_altmode);
335 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
341 switch (desc->svid) {
342 case USB_TYPEC_DP_SID:
343 alt = ucsi_register_displayport(con, override, i, desc);
345 case USB_TYPEC_NVIDIA_VLINK_SID:
346 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
347 alt = typec_port_register_altmode(con->port,
350 alt = ucsi_register_displayport(con, override,
354 alt = typec_port_register_altmode(con->port, desc);
363 con->port_altmode[i] = alt;
365 case UCSI_RECIPIENT_SOP:
366 i = ucsi_next_altmode(con->partner_altmode);
372 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
378 alt = typec_partner_register_altmode(con->partner, desc);
384 con->partner_altmode[i] = alt;
390 trace_ucsi_register_altmode(recipient, alt);
395 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
396 desc->svid, desc->mode);
402 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
404 int max_altmodes = UCSI_MAX_ALTMODES;
405 struct typec_altmode_desc desc;
406 struct ucsi_altmode alt;
407 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
408 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
409 struct ucsi *ucsi = con->ucsi;
410 bool multi_dp = false;
417 if (recipient == UCSI_RECIPIENT_CON)
418 max_altmodes = con->ucsi->cap.num_alt_modes;
420 memset(orig, 0, sizeof(orig));
421 memset(updated, 0, sizeof(updated));
423 /* First get all the alternate modes */
424 for (i = 0; i < max_altmodes; i++) {
425 memset(&alt, 0, sizeof(alt));
426 command = UCSI_GET_ALTERNATE_MODES;
427 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
428 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
429 command |= UCSI_GET_ALTMODE_OFFSET(i);
430 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
432 * We are collecting all altmodes first and then registering.
433 * Some type-C device will return zero length data beyond last
434 * alternate modes. We should not return if length is zero.
439 /* We got all altmodes, now break out and register them */
440 if (!len || !alt.svid)
443 orig[k].mid = alt.mid;
444 orig[k].svid = alt.svid;
448 * Update the original altmode table as some ppms may report
449 * multiple DP altmodes.
451 if (recipient == UCSI_RECIPIENT_CON)
452 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
454 /* now register altmodes */
455 for (i = 0; i < max_altmodes; i++) {
456 memset(&desc, 0, sizeof(desc));
457 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
458 desc.svid = updated[i].svid;
459 desc.vdo = updated[i].mid;
461 desc.svid = orig[i].svid;
462 desc.vdo = orig[i].mid;
464 desc.roles = TYPEC_PORT_DRD;
469 ret = ucsi_register_altmode(con, &desc, recipient);
477 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
479 int max_altmodes = UCSI_MAX_ALTMODES;
480 struct typec_altmode_desc desc;
481 struct ucsi_altmode alt[2];
489 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
492 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
495 if (con->ucsi->ops->update_altmodes)
496 return ucsi_register_altmodes_nvidia(con, recipient);
498 if (recipient == UCSI_RECIPIENT_CON)
499 max_altmodes = con->ucsi->cap.num_alt_modes;
501 for (i = 0; i < max_altmodes;) {
502 memset(alt, 0, sizeof(alt));
503 command = UCSI_GET_ALTERNATE_MODES;
504 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
505 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
506 command |= UCSI_GET_ALTMODE_OFFSET(i);
507 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
514 * This code is requesting one alt mode at a time, but some PPMs
515 * may still return two. If that happens both alt modes need be
516 * registered and the offset for the next alt mode has to be
519 num = len / sizeof(alt[0]);
522 for (j = 0; j < num; j++) {
526 memset(&desc, 0, sizeof(desc));
527 desc.vdo = alt[j].mid;
528 desc.svid = alt[j].svid;
529 desc.roles = TYPEC_PORT_DRD;
531 ret = ucsi_register_altmode(con, &desc, recipient);
540 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
542 const struct typec_altmode *pdev;
543 struct typec_altmode **adev;
547 case UCSI_RECIPIENT_CON:
548 adev = con->port_altmode;
550 case UCSI_RECIPIENT_SOP:
551 adev = con->partner_altmode;
558 if (recipient == UCSI_RECIPIENT_SOP &&
559 (adev[i]->svid == USB_TYPEC_DP_SID ||
560 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
561 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
562 pdev = typec_altmode_get_partner(adev[i]);
563 ucsi_displayport_remove_partner((void *)pdev);
565 typec_unregister_altmode(adev[i]);
570 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
571 u32 *pdos, int offset, int num_pdos)
573 struct ucsi *ucsi = con->ucsi;
577 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
578 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
579 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
580 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
581 command |= UCSI_GET_PDOS_SRC_PDOS;
582 ret = ucsi_send_command(ucsi, command, pdos + offset,
583 num_pdos * sizeof(u32));
584 if (ret < 0 && ret != -ETIMEDOUT)
585 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
590 static int ucsi_get_src_pdos(struct ucsi_connector *con)
594 /* UCSI max payload means only getting at most 4 PDOs at a time */
595 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
599 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
600 if (con->num_pdos < UCSI_MAX_PDOS)
603 /* get the remaining PDOs, if any */
604 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
605 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
609 con->num_pdos += ret / sizeof(u32);
611 ucsi_port_psy_changed(con);
616 static int ucsi_check_altmodes(struct ucsi_connector *con)
618 int ret, num_partner_am;
620 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
621 if (ret && ret != -ETIMEDOUT)
622 dev_err(con->ucsi->dev,
623 "con%d: failed to register partner alt modes (%d)\n",
626 /* Ignoring the errors in this case. */
627 if (con->partner_altmode[0]) {
628 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
629 if (num_partner_am > 0)
630 typec_partner_set_num_altmodes(con->partner, num_partner_am);
631 ucsi_altmode_update_active(con);
638 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
640 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
641 case UCSI_CONSTAT_PWR_OPMODE_PD:
642 con->rdo = con->status.request_data_obj;
643 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
644 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
645 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
647 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
649 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
651 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
653 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
657 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
662 static int ucsi_register_partner(struct ucsi_connector *con)
664 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
665 struct typec_partner_desc desc;
666 struct typec_partner *partner;
671 memset(&desc, 0, sizeof(desc));
673 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
674 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
675 desc.accessory = TYPEC_ACCESSORY_DEBUG;
677 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
678 desc.accessory = TYPEC_ACCESSORY_AUDIO;
684 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
686 partner = typec_register_partner(con->port, &desc);
687 if (IS_ERR(partner)) {
688 dev_err(con->ucsi->dev,
689 "con%d: failed to register partner (%ld)\n", con->num,
691 return PTR_ERR(partner);
694 con->partner = partner;
699 static void ucsi_unregister_partner(struct ucsi_connector *con)
704 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
705 typec_unregister_partner(con->partner);
709 static void ucsi_partner_change(struct ucsi_connector *con)
711 enum usb_role u_role = USB_ROLE_NONE;
714 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
715 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
716 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
717 u_role = USB_ROLE_HOST;
719 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
720 typec_set_data_role(con->port, TYPEC_HOST);
722 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
723 u_role = USB_ROLE_DEVICE;
724 typec_set_data_role(con->port, TYPEC_DEVICE);
730 /* Only notify USB controller if partner supports USB data */
731 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
732 u_role = USB_ROLE_NONE;
734 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
736 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
740 static int ucsi_check_connection(struct ucsi_connector *con)
742 u8 prev_flags = con->status.flags;
746 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
747 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
749 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
753 if (con->status.flags == prev_flags)
756 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
757 ucsi_register_partner(con);
758 ucsi_pwr_opmode_change(con);
759 ucsi_partner_change(con);
761 ucsi_partner_change(con);
762 ucsi_port_psy_changed(con);
763 ucsi_unregister_partner(con);
769 static void ucsi_handle_connector_change(struct work_struct *work)
771 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
773 struct ucsi *ucsi = con->ucsi;
774 enum typec_role role;
778 mutex_lock(&con->lock);
780 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
781 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
783 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
788 trace_ucsi_connector_change(con->num, &con->status);
790 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
792 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
793 typec_set_pwr_role(con->port, role);
795 /* Complete pending power role swap */
796 if (!completion_done(&con->complete))
797 complete(&con->complete);
800 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
801 typec_set_pwr_role(con->port, role);
802 ucsi_port_psy_changed(con);
803 ucsi_partner_change(con);
805 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
806 ucsi_register_partner(con);
807 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
809 ucsi_unregister_partner(con);
813 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
814 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
815 ucsi_pwr_opmode_change(con);
817 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
818 ucsi_partner_change(con);
820 /* Complete pending data role swap */
821 if (!completion_done(&con->complete))
822 complete(&con->complete);
825 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
826 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
828 clear_bit(EVENT_PENDING, &con->ucsi->flags);
830 ret = ucsi_acknowledge_connector_change(ucsi);
832 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
835 mutex_unlock(&con->lock);
839 * ucsi_connector_change - Process Connector Change Event
840 * @ucsi: UCSI Interface
841 * @num: Connector number
843 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
845 struct ucsi_connector *con = &ucsi->connector[num - 1];
847 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
848 dev_dbg(ucsi->dev, "Bogus connector change event\n");
852 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
853 schedule_work(&con->work);
855 EXPORT_SYMBOL_GPL(ucsi_connector_change);
857 /* -------------------------------------------------------------------------- */
859 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
863 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
864 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
866 return ucsi_send_command(con->ucsi, command, NULL, 0);
869 static int ucsi_reset_ppm(struct ucsi *ucsi)
871 u64 command = UCSI_PPM_RESET;
876 mutex_lock(&ucsi->ppm_lock);
878 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
883 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
886 if (time_is_before_jiffies(tmo)) {
891 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
895 /* If the PPM is still doing something else, reset it again. */
896 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
897 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
905 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
908 mutex_unlock(&ucsi->ppm_lock);
912 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
916 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
917 if (ret == -ETIMEDOUT) {
920 /* PPM most likely stopped responding. Resetting everything. */
921 ucsi_reset_ppm(con->ucsi);
923 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
924 ucsi_send_command(con->ucsi, c, NULL, 0);
926 ucsi_reset_connector(con, true);
932 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
934 struct ucsi_connector *con = typec_get_drvdata(port);
939 mutex_lock(&con->lock);
946 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
947 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
948 role == TYPEC_DEVICE) ||
949 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
953 reinit_completion(&con->complete);
955 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
956 command |= UCSI_SET_UOR_ROLE(role);
957 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
958 ret = ucsi_role_cmd(con, command);
962 mutex_unlock(&con->lock);
964 if (!wait_for_completion_timeout(&con->complete,
965 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
971 mutex_unlock(&con->lock);
976 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
978 struct ucsi_connector *con = typec_get_drvdata(port);
979 enum typec_role cur_role;
983 mutex_lock(&con->lock);
990 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
992 if (cur_role == role)
995 reinit_completion(&con->complete);
997 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
998 command |= UCSI_SET_PDR_ROLE(role);
999 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1000 ret = ucsi_role_cmd(con, command);
1004 mutex_unlock(&con->lock);
1006 if (!wait_for_completion_timeout(&con->complete,
1007 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1010 mutex_lock(&con->lock);
1012 /* Something has gone wrong while swapping the role */
1013 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1014 UCSI_CONSTAT_PWR_OPMODE_PD) {
1015 ucsi_reset_connector(con, true);
1020 mutex_unlock(&con->lock);
1025 static const struct typec_operations ucsi_ops = {
1026 .dr_set = ucsi_dr_swap,
1027 .pr_set = ucsi_pr_swap
1030 /* Caller must call fwnode_handle_put() after use */
1031 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1033 struct fwnode_handle *fwnode;
1036 device_for_each_child_node(con->ucsi->dev, fwnode)
1037 if (i++ == con->num)
1042 static int ucsi_register_port(struct ucsi *ucsi, int index)
1044 struct ucsi_connector *con = &ucsi->connector[index];
1045 struct typec_capability *cap = &con->typec_cap;
1046 enum typec_accessory *accessory = cap->accessory;
1047 enum usb_role u_role = USB_ROLE_NONE;
1052 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1056 con->wq = create_singlethread_workqueue(name);
1061 INIT_WORK(&con->work, ucsi_handle_connector_change);
1062 init_completion(&con->complete);
1063 mutex_init(&con->lock);
1064 INIT_LIST_HEAD(&con->partner_tasks);
1065 con->num = index + 1;
1068 cap->fwnode = ucsi_find_fwnode(con);
1069 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1070 if (IS_ERR(con->usb_role_sw))
1071 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1072 "con%d: failed to get usb role switch\n", con->num);
1074 /* Delay other interactions with the con until registration is complete */
1075 mutex_lock(&con->lock);
1077 /* Get connector capability */
1078 command = UCSI_GET_CONNECTOR_CAPABILITY;
1079 command |= UCSI_CONNECTOR_NUMBER(con->num);
1080 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1084 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1085 cap->data = TYPEC_PORT_DRD;
1086 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1087 cap->data = TYPEC_PORT_DFP;
1088 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1089 cap->data = TYPEC_PORT_UFP;
1091 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1092 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1093 cap->type = TYPEC_PORT_DRP;
1094 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1095 cap->type = TYPEC_PORT_SRC;
1096 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1097 cap->type = TYPEC_PORT_SNK;
1099 cap->revision = ucsi->cap.typec_version;
1100 cap->pd_revision = ucsi->cap.pd_version;
1101 cap->svdm_version = SVDM_VER_2_0;
1102 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1104 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1105 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1106 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1107 *accessory = TYPEC_ACCESSORY_DEBUG;
1109 cap->driver_data = con;
1110 cap->ops = &ucsi_ops;
1112 ret = ucsi_register_port_psy(con);
1116 /* Register the connector */
1117 con->port = typec_register_port(ucsi->dev, cap);
1118 if (IS_ERR(con->port)) {
1119 ret = PTR_ERR(con->port);
1123 /* Alternate modes */
1124 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1126 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1131 /* Get the status */
1132 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1133 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1135 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1139 ret = 0; /* ucsi_send_command() returns length on success */
1141 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1142 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1143 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1144 u_role = USB_ROLE_HOST;
1146 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1147 typec_set_data_role(con->port, TYPEC_HOST);
1149 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1150 u_role = USB_ROLE_DEVICE;
1151 typec_set_data_role(con->port, TYPEC_DEVICE);
1157 /* Check if there is already something connected */
1158 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1159 typec_set_pwr_role(con->port,
1160 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1161 ucsi_pwr_opmode_change(con);
1162 ucsi_register_partner(con);
1163 ucsi_port_psy_changed(con);
1166 /* Only notify USB controller if partner supports USB data */
1167 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1168 u_role = USB_ROLE_NONE;
1170 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1172 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1178 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1179 UCSI_CONSTAT_PWR_OPMODE_PD) {
1180 ucsi_get_src_pdos(con);
1181 ucsi_check_altmodes(con);
1184 trace_ucsi_register_port(con->num, &con->status);
1187 fwnode_handle_put(cap->fwnode);
1189 mutex_unlock(&con->lock);
1191 if (ret && con->wq) {
1192 destroy_workqueue(con->wq);
1200 * ucsi_init - Initialize UCSI interface
1201 * @ucsi: UCSI to be initialized
1203 * Registers all ports @ucsi has and enables all notification events.
1205 static int ucsi_init(struct ucsi *ucsi)
1207 struct ucsi_connector *con;
1213 ret = ucsi_reset_ppm(ucsi);
1215 dev_err(ucsi->dev, "failed to reset PPM!\n");
1219 /* Enable basic notifications */
1220 ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1221 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1222 ret = ucsi_send_command(ucsi, command, NULL, 0);
1226 /* Get PPM capabilities */
1227 command = UCSI_GET_CAPABILITY;
1228 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1232 if (!ucsi->cap.num_connectors) {
1237 /* Allocate the connectors. Released in ucsi_unregister() */
1238 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1239 sizeof(*ucsi->connector), GFP_KERNEL);
1240 if (!ucsi->connector) {
1245 /* Register all connectors */
1246 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1247 ret = ucsi_register_port(ucsi, i);
1249 goto err_unregister;
1252 /* Enable all notifications */
1253 ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1254 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1255 ret = ucsi_send_command(ucsi, command, NULL, 0);
1257 goto err_unregister;
1262 for (con = ucsi->connector; con->port; con++) {
1263 ucsi_unregister_partner(con);
1264 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1265 ucsi_unregister_port_psy(con);
1267 destroy_workqueue(con->wq);
1268 typec_unregister_port(con->port);
1272 kfree(ucsi->connector);
1273 ucsi->connector = NULL;
1276 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1277 ucsi_reset_ppm(ucsi);
1282 static void ucsi_resume_work(struct work_struct *work)
1284 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1285 struct ucsi_connector *con;
1289 /* Restore UCSI notification enable mask after system resume */
1290 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1291 ret = ucsi_send_command(ucsi, command, NULL, 0);
1293 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1297 for (con = ucsi->connector; con->port; con++) {
1298 mutex_lock(&con->lock);
1299 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1300 mutex_unlock(&con->lock);
1304 int ucsi_resume(struct ucsi *ucsi)
1306 if (ucsi->connector)
1307 queue_work(system_long_wq, &ucsi->resume_work);
1310 EXPORT_SYMBOL_GPL(ucsi_resume);
1312 static void ucsi_init_work(struct work_struct *work)
1314 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1317 ret = ucsi_init(ucsi);
1319 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1321 if (ret == -EPROBE_DEFER) {
1322 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1325 queue_delayed_work(system_long_wq, &ucsi->work,
1326 UCSI_ROLE_SWITCH_INTERVAL);
1331 * ucsi_get_drvdata - Return private driver data pointer
1332 * @ucsi: UCSI interface
1334 void *ucsi_get_drvdata(struct ucsi *ucsi)
1336 return ucsi->driver_data;
1338 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1341 * ucsi_set_drvdata - Assign private driver data pointer
1342 * @ucsi: UCSI interface
1343 * @data: Private data pointer
1345 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1347 ucsi->driver_data = data;
1349 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1352 * ucsi_create - Allocate UCSI instance
1353 * @dev: Device interface to the PPM (Platform Policy Manager)
1354 * @ops: I/O routines
1356 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1360 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1361 return ERR_PTR(-EINVAL);
1363 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1365 return ERR_PTR(-ENOMEM);
1367 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1368 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1369 mutex_init(&ucsi->ppm_lock);
1375 EXPORT_SYMBOL_GPL(ucsi_create);
1378 * ucsi_destroy - Free UCSI instance
1379 * @ucsi: UCSI instance to be freed
1381 void ucsi_destroy(struct ucsi *ucsi)
1385 EXPORT_SYMBOL_GPL(ucsi_destroy);
1388 * ucsi_register - Register UCSI interface
1389 * @ucsi: UCSI instance
1391 int ucsi_register(struct ucsi *ucsi)
1395 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1396 sizeof(ucsi->version));
1403 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1407 EXPORT_SYMBOL_GPL(ucsi_register);
1410 * ucsi_unregister - Unregister UCSI interface
1411 * @ucsi: UCSI interface to be unregistered
1413 * Unregister UCSI interface that was created with ucsi_register().
1415 void ucsi_unregister(struct ucsi *ucsi)
1417 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1420 /* Make sure that we are not in the middle of driver initialization */
1421 cancel_delayed_work_sync(&ucsi->work);
1422 cancel_work_sync(&ucsi->resume_work);
1424 /* Disable notifications */
1425 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1427 if (!ucsi->connector)
1430 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1431 cancel_work_sync(&ucsi->connector[i].work);
1432 ucsi_unregister_partner(&ucsi->connector[i]);
1433 ucsi_unregister_altmodes(&ucsi->connector[i],
1434 UCSI_RECIPIENT_CON);
1435 ucsi_unregister_port_psy(&ucsi->connector[i]);
1437 if (ucsi->connector[i].wq) {
1438 struct ucsi_work *uwork;
1440 mutex_lock(&ucsi->connector[i].lock);
1442 * queue delayed items immediately so they can execute
1443 * and free themselves before the wq is destroyed
1445 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1446 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1447 mutex_unlock(&ucsi->connector[i].lock);
1448 destroy_workqueue(ucsi->connector[i].wq);
1450 typec_unregister_port(ucsi->connector[i].port);
1453 kfree(ucsi->connector);
1455 EXPORT_SYMBOL_GPL(ucsi_unregister);
1457 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1458 MODULE_LICENSE("GPL v2");
1459 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");