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));
80 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
82 case UCSI_ERROR_CC_COMMUNICATION_ERR:
84 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
86 case UCSI_ERROR_DEAD_BATTERY:
87 dev_warn(ucsi->dev, "Dead battery condition!\n");
89 case UCSI_ERROR_INVALID_CON_NUM:
90 case UCSI_ERROR_UNREGONIZED_CMD:
91 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
92 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
94 case UCSI_ERROR_OVERCURRENT:
95 dev_warn(ucsi->dev, "Overcurrent condition\n");
97 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
98 dev_warn(ucsi->dev, "Partner rejected swap\n");
100 case UCSI_ERROR_HARD_RESET:
101 dev_warn(ucsi->dev, "Hard reset occurred\n");
103 case UCSI_ERROR_PPM_POLICY_CONFLICT:
104 dev_warn(ucsi->dev, "PPM Policy conflict\n");
106 case UCSI_ERROR_SWAP_REJECTED:
107 dev_warn(ucsi->dev, "Swap rejected\n");
109 case UCSI_ERROR_UNDEFINED:
111 dev_err(ucsi->dev, "unknown error %u\n", error);
118 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
123 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
127 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
131 if (cci & UCSI_CCI_BUSY) {
132 ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
136 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
139 if (cci & UCSI_CCI_NOT_SUPPORTED)
142 if (cci & UCSI_CCI_ERROR) {
143 if (cmd == UCSI_GET_ERROR_STATUS)
145 return ucsi_read_error(ucsi);
148 return UCSI_CCI_LENGTH(cci);
151 int ucsi_send_command(struct ucsi *ucsi, u64 command,
152 void *data, size_t size)
157 mutex_lock(&ucsi->ppm_lock);
159 ret = ucsi_exec_command(ucsi, command);
166 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
171 ret = ucsi_acknowledge_command(ucsi);
177 mutex_unlock(&ucsi->ppm_lock);
180 EXPORT_SYMBOL_GPL(ucsi_send_command);
182 int ucsi_resume(struct ucsi *ucsi)
186 /* Restore UCSI notification enable mask after system resume */
187 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
189 return ucsi_send_command(ucsi, command, NULL, 0);
191 EXPORT_SYMBOL_GPL(ucsi_resume);
192 /* -------------------------------------------------------------------------- */
195 struct delayed_work work;
198 struct ucsi_connector *con;
199 int (*cb)(struct ucsi_connector *);
202 static void ucsi_poll_worker(struct work_struct *work)
204 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
205 struct ucsi_connector *con = uwork->con;
208 mutex_lock(&con->lock);
211 mutex_unlock(&con->lock);
216 ret = uwork->cb(con);
218 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
219 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
223 mutex_unlock(&con->lock);
226 static int ucsi_partner_task(struct ucsi_connector *con,
227 int (*cb)(struct ucsi_connector *),
228 int retries, unsigned long delay)
230 struct ucsi_work *uwork;
235 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
239 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
240 uwork->count = retries;
241 uwork->delay = delay;
245 queue_delayed_work(con->wq, &uwork->work, delay);
250 /* -------------------------------------------------------------------------- */
252 void ucsi_altmode_update_active(struct ucsi_connector *con)
254 const struct typec_altmode *altmode = NULL;
260 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
261 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
263 if (con->ucsi->version > 0x0100) {
264 dev_err(con->ucsi->dev,
265 "GET_CURRENT_CAM command failed\n");
271 if (cur < UCSI_MAX_ALTMODES)
272 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
274 for (i = 0; con->partner_altmode[i]; i++)
275 typec_altmode_update_active(con->partner_altmode[i],
276 con->partner_altmode[i] == altmode);
279 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
284 for (i = 0; alt[i]; i++) {
285 if (i > MODE_DISCOVERY_MAX)
288 if (alt[i]->svid == svid)
295 static int ucsi_next_altmode(struct typec_altmode **alt)
299 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
306 static int ucsi_get_num_altmode(struct typec_altmode **alt)
310 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
317 static int ucsi_register_altmode(struct ucsi_connector *con,
318 struct typec_altmode_desc *desc,
321 struct typec_altmode *alt;
326 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
329 case UCSI_RECIPIENT_CON:
330 i = ucsi_next_altmode(con->port_altmode);
336 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
342 switch (desc->svid) {
343 case USB_TYPEC_DP_SID:
344 alt = ucsi_register_displayport(con, override, i, desc);
346 case USB_TYPEC_NVIDIA_VLINK_SID:
347 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
348 alt = typec_port_register_altmode(con->port,
351 alt = ucsi_register_displayport(con, override,
355 alt = typec_port_register_altmode(con->port, desc);
364 con->port_altmode[i] = alt;
366 case UCSI_RECIPIENT_SOP:
367 i = ucsi_next_altmode(con->partner_altmode);
373 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
379 alt = typec_partner_register_altmode(con->partner, desc);
385 con->partner_altmode[i] = alt;
391 trace_ucsi_register_altmode(recipient, alt);
396 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
397 desc->svid, desc->mode);
403 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
405 int max_altmodes = UCSI_MAX_ALTMODES;
406 struct typec_altmode_desc desc;
407 struct ucsi_altmode alt;
408 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
409 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
410 struct ucsi *ucsi = con->ucsi;
411 bool multi_dp = false;
418 if (recipient == UCSI_RECIPIENT_CON)
419 max_altmodes = con->ucsi->cap.num_alt_modes;
421 memset(orig, 0, sizeof(orig));
422 memset(updated, 0, sizeof(updated));
424 /* First get all the alternate modes */
425 for (i = 0; i < max_altmodes; i++) {
426 memset(&alt, 0, sizeof(alt));
427 command = UCSI_GET_ALTERNATE_MODES;
428 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
429 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
430 command |= UCSI_GET_ALTMODE_OFFSET(i);
431 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
433 * We are collecting all altmodes first and then registering.
434 * Some type-C device will return zero length data beyond last
435 * alternate modes. We should not return if length is zero.
440 /* We got all altmodes, now break out and register them */
441 if (!len || !alt.svid)
444 orig[k].mid = alt.mid;
445 orig[k].svid = alt.svid;
449 * Update the original altmode table as some ppms may report
450 * multiple DP altmodes.
452 if (recipient == UCSI_RECIPIENT_CON)
453 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
455 /* now register altmodes */
456 for (i = 0; i < max_altmodes; i++) {
457 memset(&desc, 0, sizeof(desc));
458 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
459 desc.svid = updated[i].svid;
460 desc.vdo = updated[i].mid;
462 desc.svid = orig[i].svid;
463 desc.vdo = orig[i].mid;
465 desc.roles = TYPEC_PORT_DRD;
470 ret = ucsi_register_altmode(con, &desc, recipient);
478 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
480 int max_altmodes = UCSI_MAX_ALTMODES;
481 struct typec_altmode_desc desc;
482 struct ucsi_altmode alt[2];
490 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
493 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
496 if (con->ucsi->ops->update_altmodes)
497 return ucsi_register_altmodes_nvidia(con, recipient);
499 if (recipient == UCSI_RECIPIENT_CON)
500 max_altmodes = con->ucsi->cap.num_alt_modes;
502 for (i = 0; i < max_altmodes;) {
503 memset(alt, 0, sizeof(alt));
504 command = UCSI_GET_ALTERNATE_MODES;
505 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
506 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
507 command |= UCSI_GET_ALTMODE_OFFSET(i);
508 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
515 * This code is requesting one alt mode at a time, but some PPMs
516 * may still return two. If that happens both alt modes need be
517 * registered and the offset for the next alt mode has to be
520 num = len / sizeof(alt[0]);
523 for (j = 0; j < num; j++) {
527 memset(&desc, 0, sizeof(desc));
528 desc.vdo = alt[j].mid;
529 desc.svid = alt[j].svid;
530 desc.roles = TYPEC_PORT_DRD;
532 ret = ucsi_register_altmode(con, &desc, recipient);
541 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
543 const struct typec_altmode *pdev;
544 struct typec_altmode **adev;
548 case UCSI_RECIPIENT_CON:
549 adev = con->port_altmode;
551 case UCSI_RECIPIENT_SOP:
552 adev = con->partner_altmode;
559 if (recipient == UCSI_RECIPIENT_SOP &&
560 (adev[i]->svid == USB_TYPEC_DP_SID ||
561 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
562 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
563 pdev = typec_altmode_get_partner(adev[i]);
564 ucsi_displayport_remove_partner((void *)pdev);
566 typec_unregister_altmode(adev[i]);
571 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
572 u32 *pdos, int offset, int num_pdos)
574 struct ucsi *ucsi = con->ucsi;
578 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
579 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
580 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
581 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
582 command |= UCSI_GET_PDOS_SRC_PDOS;
583 ret = ucsi_send_command(ucsi, command, pdos + offset,
584 num_pdos * sizeof(u32));
585 if (ret < 0 && ret != -ETIMEDOUT)
586 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
587 if (ret == 0 && offset == 0)
588 dev_warn(ucsi->dev, "UCSI_GET_PDOS returned 0 bytes\n");
593 static int ucsi_get_src_pdos(struct ucsi_connector *con)
597 /* UCSI max payload means only getting at most 4 PDOs at a time */
598 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
602 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
603 if (con->num_pdos < UCSI_MAX_PDOS)
606 /* get the remaining PDOs, if any */
607 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
608 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
612 con->num_pdos += ret / sizeof(u32);
614 ucsi_port_psy_changed(con);
619 static int ucsi_check_altmodes(struct ucsi_connector *con)
621 int ret, num_partner_am;
623 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
624 if (ret && ret != -ETIMEDOUT)
625 dev_err(con->ucsi->dev,
626 "con%d: failed to register partner alt modes (%d)\n",
629 /* Ignoring the errors in this case. */
630 if (con->partner_altmode[0]) {
631 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
632 if (num_partner_am > 0)
633 typec_partner_set_num_altmodes(con->partner, num_partner_am);
634 ucsi_altmode_update_active(con);
641 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
643 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
644 case UCSI_CONSTAT_PWR_OPMODE_PD:
645 con->rdo = con->status.request_data_obj;
646 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
647 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
648 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
650 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
652 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
654 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
656 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
660 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
665 static int ucsi_register_partner(struct ucsi_connector *con)
667 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
668 struct typec_partner_desc desc;
669 struct typec_partner *partner;
674 memset(&desc, 0, sizeof(desc));
676 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
677 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
678 desc.accessory = TYPEC_ACCESSORY_DEBUG;
680 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
681 desc.accessory = TYPEC_ACCESSORY_AUDIO;
687 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
689 partner = typec_register_partner(con->port, &desc);
690 if (IS_ERR(partner)) {
691 dev_err(con->ucsi->dev,
692 "con%d: failed to register partner (%ld)\n", con->num,
694 return PTR_ERR(partner);
697 con->partner = partner;
702 static void ucsi_unregister_partner(struct ucsi_connector *con)
707 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
708 typec_unregister_partner(con->partner);
712 static void ucsi_partner_change(struct ucsi_connector *con)
714 enum usb_role u_role = USB_ROLE_NONE;
717 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
718 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
719 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
720 u_role = USB_ROLE_HOST;
722 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
723 typec_set_data_role(con->port, TYPEC_HOST);
725 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
726 u_role = USB_ROLE_DEVICE;
727 typec_set_data_role(con->port, TYPEC_DEVICE);
733 /* Only notify USB controller if partner supports USB data */
734 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
735 u_role = USB_ROLE_NONE;
737 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
739 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
743 static int ucsi_check_connection(struct ucsi_connector *con)
748 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
749 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
751 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
755 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
756 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
757 UCSI_CONSTAT_PWR_OPMODE_PD)
758 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
760 ucsi_partner_change(con);
761 ucsi_port_psy_changed(con);
762 ucsi_unregister_partner(con);
768 static void ucsi_handle_connector_change(struct work_struct *work)
770 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
772 struct ucsi *ucsi = con->ucsi;
773 enum typec_role role;
777 mutex_lock(&con->lock);
779 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
780 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
782 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
787 trace_ucsi_connector_change(con->num, &con->status);
789 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
791 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
792 typec_set_pwr_role(con->port, role);
794 /* Complete pending power role swap */
795 if (!completion_done(&con->complete))
796 complete(&con->complete);
799 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
800 typec_set_pwr_role(con->port, role);
801 ucsi_port_psy_changed(con);
802 ucsi_partner_change(con);
804 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
805 ucsi_register_partner(con);
806 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
808 ucsi_unregister_partner(con);
812 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
813 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
814 ucsi_pwr_opmode_change(con);
816 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
817 ucsi_partner_change(con);
819 /* Complete pending data role swap */
820 if (!completion_done(&con->complete))
821 complete(&con->complete);
824 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
825 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
827 clear_bit(EVENT_PENDING, &con->ucsi->flags);
829 ret = ucsi_acknowledge_connector_change(ucsi);
831 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
834 mutex_unlock(&con->lock);
838 * ucsi_connector_change - Process Connector Change Event
839 * @ucsi: UCSI Interface
840 * @num: Connector number
842 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
844 struct ucsi_connector *con = &ucsi->connector[num - 1];
846 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
847 dev_dbg(ucsi->dev, "Bogus connector change event\n");
851 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
852 schedule_work(&con->work);
854 EXPORT_SYMBOL_GPL(ucsi_connector_change);
856 /* -------------------------------------------------------------------------- */
858 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
862 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
863 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
865 return ucsi_send_command(con->ucsi, command, NULL, 0);
868 static int ucsi_reset_ppm(struct ucsi *ucsi)
870 u64 command = UCSI_PPM_RESET;
875 mutex_lock(&ucsi->ppm_lock);
877 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
882 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
885 if (time_is_before_jiffies(tmo)) {
890 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
894 /* If the PPM is still doing something else, reset it again. */
895 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
896 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
904 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
907 mutex_unlock(&ucsi->ppm_lock);
911 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
915 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
916 if (ret == -ETIMEDOUT) {
919 /* PPM most likely stopped responding. Resetting everything. */
920 ucsi_reset_ppm(con->ucsi);
922 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
923 ucsi_send_command(con->ucsi, c, NULL, 0);
925 ucsi_reset_connector(con, true);
931 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
933 struct ucsi_connector *con = typec_get_drvdata(port);
938 mutex_lock(&con->lock);
945 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
946 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
947 role == TYPEC_DEVICE) ||
948 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
952 reinit_completion(&con->complete);
954 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
955 command |= UCSI_SET_UOR_ROLE(role);
956 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
957 ret = ucsi_role_cmd(con, command);
961 mutex_unlock(&con->lock);
963 if (!wait_for_completion_timeout(&con->complete,
964 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
970 mutex_unlock(&con->lock);
975 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
977 struct ucsi_connector *con = typec_get_drvdata(port);
978 enum typec_role cur_role;
982 mutex_lock(&con->lock);
989 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
991 if (cur_role == role)
994 reinit_completion(&con->complete);
996 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
997 command |= UCSI_SET_PDR_ROLE(role);
998 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
999 ret = ucsi_role_cmd(con, command);
1003 mutex_unlock(&con->lock);
1005 if (!wait_for_completion_timeout(&con->complete,
1006 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1009 mutex_lock(&con->lock);
1011 /* Something has gone wrong while swapping the role */
1012 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1013 UCSI_CONSTAT_PWR_OPMODE_PD) {
1014 ucsi_reset_connector(con, true);
1019 mutex_unlock(&con->lock);
1024 static const struct typec_operations ucsi_ops = {
1025 .dr_set = ucsi_dr_swap,
1026 .pr_set = ucsi_pr_swap
1029 /* Caller must call fwnode_handle_put() after use */
1030 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1032 struct fwnode_handle *fwnode;
1035 device_for_each_child_node(con->ucsi->dev, fwnode)
1036 if (i++ == con->num)
1041 static int ucsi_register_port(struct ucsi *ucsi, int index)
1043 struct ucsi_connector *con = &ucsi->connector[index];
1044 struct typec_capability *cap = &con->typec_cap;
1045 enum typec_accessory *accessory = cap->accessory;
1046 enum usb_role u_role = USB_ROLE_NONE;
1051 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1055 con->wq = create_singlethread_workqueue(name);
1060 INIT_WORK(&con->work, ucsi_handle_connector_change);
1061 init_completion(&con->complete);
1062 mutex_init(&con->lock);
1063 con->num = index + 1;
1066 cap->fwnode = ucsi_find_fwnode(con);
1067 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1068 if (IS_ERR(con->usb_role_sw)) {
1069 dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
1071 return PTR_ERR(con->usb_role_sw);
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);
1199 static void ucsi_unregister_connectors(struct ucsi *ucsi)
1201 struct ucsi_connector *con;
1204 if (!ucsi->connector)
1207 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1208 con = &ucsi->connector[i];
1213 cancel_work_sync(&con->work);
1214 ucsi_unregister_partner(con);
1215 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1216 ucsi_unregister_port_psy(con);
1217 destroy_workqueue(con->wq);
1218 typec_unregister_port(con->port);
1221 kfree(ucsi->connector);
1222 ucsi->connector = NULL;
1226 * ucsi_init - Initialize UCSI interface
1227 * @ucsi: UCSI to be initialized
1229 * Registers all ports @ucsi has and enables all notification events.
1231 static int ucsi_init(struct ucsi *ucsi)
1238 ret = ucsi_reset_ppm(ucsi);
1240 dev_err(ucsi->dev, "failed to reset PPM!\n");
1244 /* Enable basic notifications */
1245 ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1246 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1247 ret = ucsi_send_command(ucsi, command, NULL, 0);
1251 /* Get PPM capabilities */
1252 command = UCSI_GET_CAPABILITY;
1253 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1257 if (!ucsi->cap.num_connectors) {
1262 /* Allocate the connectors. Released in ucsi_unregister() */
1263 ucsi->connector = kcalloc(ucsi->cap.num_connectors,
1264 sizeof(*ucsi->connector), GFP_KERNEL);
1265 if (!ucsi->connector) {
1270 /* Register all connectors */
1271 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1272 ret = ucsi_register_port(ucsi, i);
1274 goto err_unregister;
1277 /* Enable all notifications */
1278 ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1279 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1280 ret = ucsi_send_command(ucsi, command, NULL, 0);
1282 goto err_unregister;
1287 ucsi_unregister_connectors(ucsi);
1290 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1291 ucsi_reset_ppm(ucsi);
1296 static void ucsi_init_work(struct work_struct *work)
1298 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1301 ret = ucsi_init(ucsi);
1303 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1305 if (ret == -EPROBE_DEFER) {
1306 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1309 queue_delayed_work(system_long_wq, &ucsi->work,
1310 UCSI_ROLE_SWITCH_INTERVAL);
1315 * ucsi_get_drvdata - Return private driver data pointer
1316 * @ucsi: UCSI interface
1318 void *ucsi_get_drvdata(struct ucsi *ucsi)
1320 return ucsi->driver_data;
1322 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1325 * ucsi_set_drvdata - Assign private driver data pointer
1326 * @ucsi: UCSI interface
1327 * @data: Private data pointer
1329 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1331 ucsi->driver_data = data;
1333 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1336 * ucsi_create - Allocate UCSI instance
1337 * @dev: Device interface to the PPM (Platform Policy Manager)
1338 * @ops: I/O routines
1340 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1344 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1345 return ERR_PTR(-EINVAL);
1347 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1349 return ERR_PTR(-ENOMEM);
1351 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1352 mutex_init(&ucsi->ppm_lock);
1358 EXPORT_SYMBOL_GPL(ucsi_create);
1361 * ucsi_destroy - Free UCSI instance
1362 * @ucsi: UCSI instance to be freed
1364 void ucsi_destroy(struct ucsi *ucsi)
1368 EXPORT_SYMBOL_GPL(ucsi_destroy);
1371 * ucsi_register - Register UCSI interface
1372 * @ucsi: UCSI instance
1374 int ucsi_register(struct ucsi *ucsi)
1378 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1379 sizeof(ucsi->version));
1386 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1390 EXPORT_SYMBOL_GPL(ucsi_register);
1393 * ucsi_unregister - Unregister UCSI interface
1394 * @ucsi: UCSI interface to be unregistered
1396 * Unregister UCSI interface that was created with ucsi_register().
1398 void ucsi_unregister(struct ucsi *ucsi)
1400 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1402 /* Make sure that we are not in the middle of driver initialization */
1403 cancel_delayed_work_sync(&ucsi->work);
1405 /* Disable notifications */
1406 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1408 ucsi_unregister_connectors(ucsi);
1410 EXPORT_SYMBOL_GPL(ucsi_unregister);
1412 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1413 MODULE_LICENSE("GPL v2");
1414 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");