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 int ucsi_resume(struct ucsi *ucsi)
190 /* Restore UCSI notification enable mask after system resume */
191 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
193 return ucsi_send_command(ucsi, command, NULL, 0);
195 EXPORT_SYMBOL_GPL(ucsi_resume);
196 /* -------------------------------------------------------------------------- */
199 struct delayed_work work;
202 struct ucsi_connector *con;
203 int (*cb)(struct ucsi_connector *);
206 static void ucsi_poll_worker(struct work_struct *work)
208 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
209 struct ucsi_connector *con = uwork->con;
212 mutex_lock(&con->lock);
215 mutex_unlock(&con->lock);
220 ret = uwork->cb(con);
222 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
223 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
227 mutex_unlock(&con->lock);
230 static int ucsi_partner_task(struct ucsi_connector *con,
231 int (*cb)(struct ucsi_connector *),
232 int retries, unsigned long delay)
234 struct ucsi_work *uwork;
239 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
243 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
244 uwork->count = retries;
245 uwork->delay = delay;
249 queue_delayed_work(con->wq, &uwork->work, delay);
254 /* -------------------------------------------------------------------------- */
256 void ucsi_altmode_update_active(struct ucsi_connector *con)
258 const struct typec_altmode *altmode = NULL;
264 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
265 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
267 if (con->ucsi->version > 0x0100) {
268 dev_err(con->ucsi->dev,
269 "GET_CURRENT_CAM command failed\n");
275 if (cur < UCSI_MAX_ALTMODES)
276 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
278 for (i = 0; con->partner_altmode[i]; i++)
279 typec_altmode_update_active(con->partner_altmode[i],
280 con->partner_altmode[i] == altmode);
283 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
288 for (i = 0; alt[i]; i++) {
289 if (i > MODE_DISCOVERY_MAX)
292 if (alt[i]->svid == svid)
299 static int ucsi_next_altmode(struct typec_altmode **alt)
303 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
310 static int ucsi_get_num_altmode(struct typec_altmode **alt)
314 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
321 static int ucsi_register_altmode(struct ucsi_connector *con,
322 struct typec_altmode_desc *desc,
325 struct typec_altmode *alt;
330 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
333 case UCSI_RECIPIENT_CON:
334 i = ucsi_next_altmode(con->port_altmode);
340 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
346 switch (desc->svid) {
347 case USB_TYPEC_DP_SID:
348 alt = ucsi_register_displayport(con, override, i, desc);
350 case USB_TYPEC_NVIDIA_VLINK_SID:
351 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
352 alt = typec_port_register_altmode(con->port,
355 alt = ucsi_register_displayport(con, override,
359 alt = typec_port_register_altmode(con->port, desc);
368 con->port_altmode[i] = alt;
370 case UCSI_RECIPIENT_SOP:
371 i = ucsi_next_altmode(con->partner_altmode);
377 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
383 alt = typec_partner_register_altmode(con->partner, desc);
389 con->partner_altmode[i] = alt;
395 trace_ucsi_register_altmode(recipient, alt);
400 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
401 desc->svid, desc->mode);
407 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
409 int max_altmodes = UCSI_MAX_ALTMODES;
410 struct typec_altmode_desc desc;
411 struct ucsi_altmode alt;
412 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
413 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
414 struct ucsi *ucsi = con->ucsi;
415 bool multi_dp = false;
422 if (recipient == UCSI_RECIPIENT_CON)
423 max_altmodes = con->ucsi->cap.num_alt_modes;
425 memset(orig, 0, sizeof(orig));
426 memset(updated, 0, sizeof(updated));
428 /* First get all the alternate modes */
429 for (i = 0; i < max_altmodes; i++) {
430 memset(&alt, 0, sizeof(alt));
431 command = UCSI_GET_ALTERNATE_MODES;
432 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
433 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
434 command |= UCSI_GET_ALTMODE_OFFSET(i);
435 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
437 * We are collecting all altmodes first and then registering.
438 * Some type-C device will return zero length data beyond last
439 * alternate modes. We should not return if length is zero.
444 /* We got all altmodes, now break out and register them */
445 if (!len || !alt.svid)
448 orig[k].mid = alt.mid;
449 orig[k].svid = alt.svid;
453 * Update the original altmode table as some ppms may report
454 * multiple DP altmodes.
456 if (recipient == UCSI_RECIPIENT_CON)
457 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
459 /* now register altmodes */
460 for (i = 0; i < max_altmodes; i++) {
461 memset(&desc, 0, sizeof(desc));
462 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
463 desc.svid = updated[i].svid;
464 desc.vdo = updated[i].mid;
466 desc.svid = orig[i].svid;
467 desc.vdo = orig[i].mid;
469 desc.roles = TYPEC_PORT_DRD;
474 ret = ucsi_register_altmode(con, &desc, recipient);
482 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
484 int max_altmodes = UCSI_MAX_ALTMODES;
485 struct typec_altmode_desc desc;
486 struct ucsi_altmode alt[2];
494 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
497 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
500 if (con->ucsi->ops->update_altmodes)
501 return ucsi_register_altmodes_nvidia(con, recipient);
503 if (recipient == UCSI_RECIPIENT_CON)
504 max_altmodes = con->ucsi->cap.num_alt_modes;
506 for (i = 0; i < max_altmodes;) {
507 memset(alt, 0, sizeof(alt));
508 command = UCSI_GET_ALTERNATE_MODES;
509 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
510 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
511 command |= UCSI_GET_ALTMODE_OFFSET(i);
512 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
519 * This code is requesting one alt mode at a time, but some PPMs
520 * may still return two. If that happens both alt modes need be
521 * registered and the offset for the next alt mode has to be
524 num = len / sizeof(alt[0]);
527 for (j = 0; j < num; j++) {
531 memset(&desc, 0, sizeof(desc));
532 desc.vdo = alt[j].mid;
533 desc.svid = alt[j].svid;
534 desc.roles = TYPEC_PORT_DRD;
536 ret = ucsi_register_altmode(con, &desc, recipient);
545 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
547 const struct typec_altmode *pdev;
548 struct typec_altmode **adev;
552 case UCSI_RECIPIENT_CON:
553 adev = con->port_altmode;
555 case UCSI_RECIPIENT_SOP:
556 adev = con->partner_altmode;
563 if (recipient == UCSI_RECIPIENT_SOP &&
564 (adev[i]->svid == USB_TYPEC_DP_SID ||
565 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
566 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
567 pdev = typec_altmode_get_partner(adev[i]);
568 ucsi_displayport_remove_partner((void *)pdev);
570 typec_unregister_altmode(adev[i]);
575 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
576 u32 *pdos, int offset, int num_pdos)
578 struct ucsi *ucsi = con->ucsi;
582 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
583 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
584 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
585 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
586 command |= UCSI_GET_PDOS_SRC_PDOS;
587 ret = ucsi_send_command(ucsi, command, pdos + offset,
588 num_pdos * sizeof(u32));
589 if (ret < 0 && ret != -ETIMEDOUT)
590 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
595 static int ucsi_get_src_pdos(struct ucsi_connector *con)
599 /* UCSI max payload means only getting at most 4 PDOs at a time */
600 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
604 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
605 if (con->num_pdos < UCSI_MAX_PDOS)
608 /* get the remaining PDOs, if any */
609 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
610 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
614 con->num_pdos += ret / sizeof(u32);
616 ucsi_port_psy_changed(con);
621 static int ucsi_check_altmodes(struct ucsi_connector *con)
623 int ret, num_partner_am;
625 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
626 if (ret && ret != -ETIMEDOUT)
627 dev_err(con->ucsi->dev,
628 "con%d: failed to register partner alt modes (%d)\n",
631 /* Ignoring the errors in this case. */
632 if (con->partner_altmode[0]) {
633 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
634 if (num_partner_am > 0)
635 typec_partner_set_num_altmodes(con->partner, num_partner_am);
636 ucsi_altmode_update_active(con);
643 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
645 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
646 case UCSI_CONSTAT_PWR_OPMODE_PD:
647 con->rdo = con->status.request_data_obj;
648 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
649 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
650 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
652 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
654 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
656 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
658 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
662 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
667 static int ucsi_register_partner(struct ucsi_connector *con)
669 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
670 struct typec_partner_desc desc;
671 struct typec_partner *partner;
676 memset(&desc, 0, sizeof(desc));
678 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
679 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
680 desc.accessory = TYPEC_ACCESSORY_DEBUG;
682 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
683 desc.accessory = TYPEC_ACCESSORY_AUDIO;
689 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
691 partner = typec_register_partner(con->port, &desc);
692 if (IS_ERR(partner)) {
693 dev_err(con->ucsi->dev,
694 "con%d: failed to register partner (%ld)\n", con->num,
696 return PTR_ERR(partner);
699 con->partner = partner;
704 static void ucsi_unregister_partner(struct ucsi_connector *con)
709 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
710 typec_unregister_partner(con->partner);
714 static void ucsi_partner_change(struct ucsi_connector *con)
716 enum usb_role u_role = USB_ROLE_NONE;
719 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
720 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
721 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
722 u_role = USB_ROLE_HOST;
724 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
725 typec_set_data_role(con->port, TYPEC_HOST);
727 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
728 u_role = USB_ROLE_DEVICE;
729 typec_set_data_role(con->port, TYPEC_DEVICE);
735 /* Only notify USB controller if partner supports USB data */
736 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
737 u_role = USB_ROLE_NONE;
739 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
741 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
745 static int ucsi_check_connection(struct ucsi_connector *con)
750 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
751 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
753 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
757 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
758 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
759 UCSI_CONSTAT_PWR_OPMODE_PD)
760 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
762 ucsi_partner_change(con);
763 ucsi_port_psy_changed(con);
764 ucsi_unregister_partner(con);
770 static void ucsi_handle_connector_change(struct work_struct *work)
772 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
774 struct ucsi *ucsi = con->ucsi;
775 enum typec_role role;
779 mutex_lock(&con->lock);
781 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
782 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
784 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
789 trace_ucsi_connector_change(con->num, &con->status);
791 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
793 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
794 typec_set_pwr_role(con->port, role);
796 /* Complete pending power role swap */
797 if (!completion_done(&con->complete))
798 complete(&con->complete);
801 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
802 typec_set_pwr_role(con->port, role);
803 ucsi_port_psy_changed(con);
804 ucsi_partner_change(con);
806 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
807 ucsi_register_partner(con);
808 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
810 ucsi_unregister_partner(con);
814 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
815 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
816 ucsi_pwr_opmode_change(con);
818 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
819 ucsi_partner_change(con);
821 /* Complete pending data role swap */
822 if (!completion_done(&con->complete))
823 complete(&con->complete);
826 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
827 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
829 clear_bit(EVENT_PENDING, &con->ucsi->flags);
831 ret = ucsi_acknowledge_connector_change(ucsi);
833 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
836 mutex_unlock(&con->lock);
840 * ucsi_connector_change - Process Connector Change Event
841 * @ucsi: UCSI Interface
842 * @num: Connector number
844 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
846 struct ucsi_connector *con = &ucsi->connector[num - 1];
848 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
849 dev_dbg(ucsi->dev, "Bogus connector change event\n");
853 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
854 schedule_work(&con->work);
856 EXPORT_SYMBOL_GPL(ucsi_connector_change);
858 /* -------------------------------------------------------------------------- */
860 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
864 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
865 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
867 return ucsi_send_command(con->ucsi, command, NULL, 0);
870 static int ucsi_reset_ppm(struct ucsi *ucsi)
872 u64 command = UCSI_PPM_RESET;
877 mutex_lock(&ucsi->ppm_lock);
879 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
884 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
887 if (time_is_before_jiffies(tmo)) {
892 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
896 /* If the PPM is still doing something else, reset it again. */
897 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
898 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
906 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
909 mutex_unlock(&ucsi->ppm_lock);
913 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
917 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
918 if (ret == -ETIMEDOUT) {
921 /* PPM most likely stopped responding. Resetting everything. */
922 ucsi_reset_ppm(con->ucsi);
924 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
925 ucsi_send_command(con->ucsi, c, NULL, 0);
927 ucsi_reset_connector(con, true);
933 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
935 struct ucsi_connector *con = typec_get_drvdata(port);
940 mutex_lock(&con->lock);
947 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
948 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
949 role == TYPEC_DEVICE) ||
950 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
954 reinit_completion(&con->complete);
956 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
957 command |= UCSI_SET_UOR_ROLE(role);
958 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
959 ret = ucsi_role_cmd(con, command);
963 mutex_unlock(&con->lock);
965 if (!wait_for_completion_timeout(&con->complete,
966 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
972 mutex_unlock(&con->lock);
977 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
979 struct ucsi_connector *con = typec_get_drvdata(port);
980 enum typec_role cur_role;
984 mutex_lock(&con->lock);
991 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
993 if (cur_role == role)
996 reinit_completion(&con->complete);
998 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
999 command |= UCSI_SET_PDR_ROLE(role);
1000 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1001 ret = ucsi_role_cmd(con, command);
1005 mutex_unlock(&con->lock);
1007 if (!wait_for_completion_timeout(&con->complete,
1008 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1011 mutex_lock(&con->lock);
1013 /* Something has gone wrong while swapping the role */
1014 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1015 UCSI_CONSTAT_PWR_OPMODE_PD) {
1016 ucsi_reset_connector(con, true);
1021 mutex_unlock(&con->lock);
1026 static const struct typec_operations ucsi_ops = {
1027 .dr_set = ucsi_dr_swap,
1028 .pr_set = ucsi_pr_swap
1031 /* Caller must call fwnode_handle_put() after use */
1032 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1034 struct fwnode_handle *fwnode;
1037 device_for_each_child_node(con->ucsi->dev, fwnode)
1038 if (i++ == con->num)
1043 static int ucsi_register_port(struct ucsi *ucsi, int index)
1045 struct ucsi_connector *con = &ucsi->connector[index];
1046 struct typec_capability *cap = &con->typec_cap;
1047 enum typec_accessory *accessory = cap->accessory;
1048 enum usb_role u_role = USB_ROLE_NONE;
1053 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1057 con->wq = create_singlethread_workqueue(name);
1062 INIT_WORK(&con->work, ucsi_handle_connector_change);
1063 init_completion(&con->complete);
1064 mutex_init(&con->lock);
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);
1273 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1274 ucsi_reset_ppm(ucsi);
1279 static void ucsi_init_work(struct work_struct *work)
1281 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1284 ret = ucsi_init(ucsi);
1286 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1288 if (ret == -EPROBE_DEFER) {
1289 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1292 queue_delayed_work(system_long_wq, &ucsi->work,
1293 UCSI_ROLE_SWITCH_INTERVAL);
1298 * ucsi_get_drvdata - Return private driver data pointer
1299 * @ucsi: UCSI interface
1301 void *ucsi_get_drvdata(struct ucsi *ucsi)
1303 return ucsi->driver_data;
1305 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1308 * ucsi_set_drvdata - Assign private driver data pointer
1309 * @ucsi: UCSI interface
1310 * @data: Private data pointer
1312 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1314 ucsi->driver_data = data;
1316 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1319 * ucsi_create - Allocate UCSI instance
1320 * @dev: Device interface to the PPM (Platform Policy Manager)
1321 * @ops: I/O routines
1323 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1327 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1328 return ERR_PTR(-EINVAL);
1330 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1332 return ERR_PTR(-ENOMEM);
1334 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1335 mutex_init(&ucsi->ppm_lock);
1341 EXPORT_SYMBOL_GPL(ucsi_create);
1344 * ucsi_destroy - Free UCSI instance
1345 * @ucsi: UCSI instance to be freed
1347 void ucsi_destroy(struct ucsi *ucsi)
1351 EXPORT_SYMBOL_GPL(ucsi_destroy);
1354 * ucsi_register - Register UCSI interface
1355 * @ucsi: UCSI instance
1357 int ucsi_register(struct ucsi *ucsi)
1361 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1362 sizeof(ucsi->version));
1369 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1373 EXPORT_SYMBOL_GPL(ucsi_register);
1376 * ucsi_unregister - Unregister UCSI interface
1377 * @ucsi: UCSI interface to be unregistered
1379 * Unregister UCSI interface that was created with ucsi_register().
1381 void ucsi_unregister(struct ucsi *ucsi)
1383 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1386 /* Make sure that we are not in the middle of driver initialization */
1387 cancel_delayed_work_sync(&ucsi->work);
1389 /* Disable notifications */
1390 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1392 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1393 cancel_work_sync(&ucsi->connector[i].work);
1394 ucsi_unregister_partner(&ucsi->connector[i]);
1395 ucsi_unregister_altmodes(&ucsi->connector[i],
1396 UCSI_RECIPIENT_CON);
1397 ucsi_unregister_port_psy(&ucsi->connector[i]);
1398 if (ucsi->connector[i].wq)
1399 destroy_workqueue(ucsi->connector[i].wq);
1400 typec_unregister_port(ucsi->connector[i].port);
1403 kfree(ucsi->connector);
1405 EXPORT_SYMBOL_GPL(ucsi_unregister);
1407 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1408 MODULE_LICENSE("GPL v2");
1409 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");