File Index
Files and directories
Sign in
FILE

ucsi.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/typec/ucsi/ucsi.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * USB Type-C Connector System Software Interface driver
   4 *
   5 * Copyright (C) 2017, Intel Corporation
   6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
   7 */
   8
   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>
  16
  17#include "ucsi.h"
  18#include "trace.h"
  19
  20/*
  21 * UCSI_TIMEOUT_MS - PPM communication timeout
  22 *
  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.
  27 */
  28#define UCSI_TIMEOUT_MS		10000
  29
  30/*
  31 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
  32 *
  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.
  36 */
  37#define UCSI_SWAP_TIMEOUT_MS	5000
  38
  39void ucsi_notify_common(struct ucsi *ucsi, u32 cci)
  40{
  41	/* Ignore bogus data in CCI if busy indicator is set. */
  42	if (cci & UCSI_CCI_BUSY)
  43		return;
  44
  45	if (UCSI_CCI_CONNECTOR(cci)) {
  46		if (!ucsi->cap.num_connectors ||
  47		    UCSI_CCI_CONNECTOR(cci) <= ucsi->cap.num_connectors)
  48			ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
  49		else
  50			dev_err(ucsi->dev, "bogus connector number in CCI: %lu\n",
  51				UCSI_CCI_CONNECTOR(cci));
  52	}
  53
  54	if (cci & UCSI_CCI_ACK_COMPLETE &&
  55	    test_and_clear_bit(ACK_PENDING, &ucsi->flags))
  56		complete(&ucsi->complete);
  57
  58	if (cci & UCSI_CCI_COMMAND_COMPLETE &&
  59	    test_and_clear_bit(COMMAND_PENDING, &ucsi->flags))
  60		complete(&ucsi->complete);
  61}
  62EXPORT_SYMBOL_GPL(ucsi_notify_common);
  63
  64int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci,
  65			     void *data, size_t size)
  66{
  67	bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
  68	int ret;
  69
  70	if (ack)
  71		set_bit(ACK_PENDING, &ucsi->flags);
  72	else
  73		set_bit(COMMAND_PENDING, &ucsi->flags);
  74
  75	reinit_completion(&ucsi->complete);
  76
  77	ret = ucsi->ops->async_control(ucsi, command);
  78	if (ret)
  79		goto out_clear_bit;
  80
  81	if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
  82		ret = -ETIMEDOUT;
  83
  84out_clear_bit:
  85	if (ack)
  86		clear_bit(ACK_PENDING, &ucsi->flags);
  87	else
  88		clear_bit(COMMAND_PENDING, &ucsi->flags);
  89
  90	if (!ret && cci)
  91		ret = ucsi->ops->read_cci(ucsi, cci);
  92
  93	if (!ret && data &&
  94	    (*cci & UCSI_CCI_COMMAND_COMPLETE))
  95		ret = ucsi->ops->read_message_in(ucsi, data, size);
  96
  97	return ret;
  98}
  99EXPORT_SYMBOL_GPL(ucsi_sync_control_common);
 100
 101static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
 102{
 103	u64 ctrl;
 104
 105	ctrl = UCSI_ACK_CC_CI;
 106	ctrl |= UCSI_ACK_COMMAND_COMPLETE;
 107	if (conn_ack) {
 108		clear_bit(EVENT_PENDING, &ucsi->flags);
 109		ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
 110	}
 111
 112	return ucsi->ops->sync_control(ucsi, ctrl, NULL, NULL, 0);
 113}
 114
 115static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
 116			    void *data, size_t size, bool conn_ack)
 117{
 118	int ret, err;
 119
 120	*cci = 0;
 121
 122	if (size > UCSI_MAX_DATA_LENGTH(ucsi))
 123		return -EINVAL;
 124
 125	ret = ucsi->ops->sync_control(ucsi, command, cci, data, size);
 126
 127	if (*cci & UCSI_CCI_BUSY)
 128		return ucsi_run_command(ucsi, UCSI_CANCEL, cci, NULL, 0, false) ?: -EBUSY;
 129	if (ret)
 130		return ret;
 131
 132	if (!(*cci & UCSI_CCI_COMMAND_COMPLETE))
 133		return -EIO;
 134
 135	if (*cci & UCSI_CCI_NOT_SUPPORTED)
 136		err = -EOPNOTSUPP;
 137	else if (*cci & UCSI_CCI_ERROR)
 138		err = -EIO;
 139	else
 140		err = 0;
 141
 142	/*
 143	 * Don't ACK connection change if there was an error.
 144	 */
 145	ret = ucsi_acknowledge(ucsi, err ? false : conn_ack);
 146	if (ret)
 147		return ret;
 148
 149	return err ?: UCSI_CCI_LENGTH(*cci);
 150}
 151
 152static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num)
 153{
 154	u64 command;
 155	u16 error;
 156	u32 cci;
 157	int ret;
 158
 159	command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num);
 160	ret = ucsi_run_command(ucsi, command, &cci, &error, sizeof(error), false);
 161	if (ret < 0)
 162		return ret;
 163
 164	switch (error) {
 165	case UCSI_ERROR_INCOMPATIBLE_PARTNER:
 166		return -EOPNOTSUPP;
 167	case UCSI_ERROR_CC_COMMUNICATION_ERR:
 168		return -ECOMM;
 169	case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
 170		return -EPROTO;
 171	case UCSI_ERROR_DEAD_BATTERY:
 172		dev_warn(ucsi->dev, "Dead battery condition!\n");
 173		return -EPERM;
 174	case UCSI_ERROR_INVALID_CON_NUM:
 175	case UCSI_ERROR_UNREGONIZED_CMD:
 176	case UCSI_ERROR_INVALID_CMD_ARGUMENT:
 177		dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
 178		return -EINVAL;
 179	case UCSI_ERROR_OVERCURRENT:
 180		dev_warn(ucsi->dev, "Overcurrent condition\n");
 181		break;
 182	case UCSI_ERROR_PARTNER_REJECTED_SWAP:
 183		dev_warn(ucsi->dev, "Partner rejected swap\n");
 184		break;
 185	case UCSI_ERROR_HARD_RESET:
 186		dev_warn(ucsi->dev, "Hard reset occurred\n");
 187		break;
 188	case UCSI_ERROR_PPM_POLICY_CONFLICT:
 189		dev_warn(ucsi->dev, "PPM Policy conflict\n");
 190		break;
 191	case UCSI_ERROR_SWAP_REJECTED:
 192		dev_warn(ucsi->dev, "Swap rejected\n");
 193		break;
 194	case UCSI_ERROR_REVERSE_CURRENT_PROTECTION:
 195		dev_warn(ucsi->dev, "Reverse Current Protection detected\n");
 196		break;
 197	case UCSI_ERROR_SET_SINK_PATH_REJECTED:
 198		dev_warn(ucsi->dev, "Set Sink Path rejected\n");
 199		break;
 200	case UCSI_ERROR_UNDEFINED:
 201	default:
 202		dev_err(ucsi->dev, "unknown error %u\n", error);
 203		break;
 204	}
 205
 206	return -EIO;
 207}
 208
 209static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd,
 210				    void *data, size_t size, bool conn_ack)
 211{
 212	u8 connector_num;
 213	u32 cci;
 214	int ret;
 215
 216	if (ucsi->version > UCSI_VERSION_1_2) {
 217		switch (UCSI_COMMAND(cmd)) {
 218		case UCSI_GET_ALTERNATE_MODES:
 219			connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd);
 220			break;
 221		case UCSI_PPM_RESET:
 222		case UCSI_CANCEL:
 223		case UCSI_ACK_CC_CI:
 224		case UCSI_SET_NOTIFICATION_ENABLE:
 225		case UCSI_GET_CAPABILITY:
 226			connector_num = 0;
 227			break;
 228		default:
 229			connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd);
 230			break;
 231		}
 232	} else {
 233		connector_num = 0;
 234	}
 235
 236	mutex_lock(&ucsi->ppm_lock);
 237
 238	ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack);
 239
 240	if (cci & UCSI_CCI_ERROR)
 241		ret = ucsi_read_error(ucsi, connector_num);
 242
 243	mutex_unlock(&ucsi->ppm_lock);
 244	return ret;
 245}
 246
 247int ucsi_send_command(struct ucsi *ucsi, u64 command,
 248		      void *data, size_t size)
 249{
 250	return ucsi_send_command_common(ucsi, command, data, size, false);
 251}
 252EXPORT_SYMBOL_GPL(ucsi_send_command);
 253
 254/* -------------------------------------------------------------------------- */
 255
 256struct ucsi_work {
 257	struct delayed_work work;
 258	struct list_head node;
 259	unsigned long delay;
 260	unsigned int count;
 261	struct ucsi_connector *con;
 262	int (*cb)(struct ucsi_connector *);
 263};
 264
 265static void ucsi_poll_worker(struct work_struct *work)
 266{
 267	struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
 268	struct ucsi_connector *con = uwork->con;
 269	int ret;
 270
 271	mutex_lock(&con->lock);
 272
 273	if (!con->partner) {
 274		list_del(&uwork->node);
 275		mutex_unlock(&con->lock);
 276		kfree(uwork);
 277		return;
 278	}
 279
 280	ret = uwork->cb(con);
 281
 282	if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
 283		queue_delayed_work(con->wq, &uwork->work, uwork->delay);
 284	} else {
 285		list_del(&uwork->node);
 286		kfree(uwork);
 287	}
 288
 289	mutex_unlock(&con->lock);
 290}
 291
 292static int ucsi_partner_task(struct ucsi_connector *con,
 293			     int (*cb)(struct ucsi_connector *),
 294			     int retries, unsigned long delay)
 295{
 296	struct ucsi_work *uwork;
 297
 298	if (!con->partner)
 299		return 0;
 300
 301	uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
 302	if (!uwork)
 303		return -ENOMEM;
 304
 305	INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
 306	uwork->count = retries;
 307	uwork->delay = delay;
 308	uwork->con = con;
 309	uwork->cb = cb;
 310
 311	list_add_tail(&uwork->node, &con->partner_tasks);
 312	queue_delayed_work(con->wq, &uwork->work, delay);
 313
 314	return 0;
 315}
 316
 317/* -------------------------------------------------------------------------- */
 318
 319void ucsi_altmode_update_active(struct ucsi_connector *con)
 320{
 321	const struct typec_altmode *altmode = NULL;
 322	u64 command;
 323	int ret;
 324	u8 cur;
 325	int i;
 326
 327	command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
 328	ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
 329	if (ret < 0) {
 330		if (con->ucsi->version > 0x0100) {
 331			dev_err(con->ucsi->dev,
 332				"GET_CURRENT_CAM command failed\n");
 333			return;
 334		}
 335		cur = 0xff;
 336	}
 337
 338	if (cur < UCSI_MAX_ALTMODES)
 339		altmode = typec_altmode_get_partner(con->port_altmode[cur]);
 340
 341	for (i = 0; con->partner_altmode[i]; i++)
 342		typec_altmode_update_active(con->partner_altmode[i],
 343					    con->partner_altmode[i] == altmode);
 344}
 345
 346static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
 347{
 348	u8 mode = 1;
 349	int i;
 350
 351	for (i = 0; alt[i]; i++) {
 352		if (i > MODE_DISCOVERY_MAX)
 353			return -ERANGE;
 354
 355		if (alt[i]->svid == svid)
 356			mode++;
 357	}
 358
 359	return mode;
 360}
 361
 362static int ucsi_next_altmode(struct typec_altmode **alt)
 363{
 364	int i = 0;
 365
 366	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
 367		if (!alt[i])
 368			return i;
 369
 370	return -ENOENT;
 371}
 372
 373static int ucsi_get_num_altmode(struct typec_altmode **alt)
 374{
 375	int i;
 376
 377	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
 378		if (!alt[i])
 379			break;
 380
 381	return i;
 382}
 383
 384static int ucsi_register_altmode(struct ucsi_connector *con,
 385				 struct typec_altmode_desc *desc,
 386				 u8 recipient)
 387{
 388	struct typec_altmode *alt;
 389	bool override;
 390	int ret;
 391	int i;
 392
 393	override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
 394
 395	switch (recipient) {
 396	case UCSI_RECIPIENT_CON:
 397		i = ucsi_next_altmode(con->port_altmode);
 398		if (i < 0) {
 399			ret = i;
 400			goto err;
 401		}
 402
 403		ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
 404		if (ret < 0)
 405			return ret;
 406
 407		desc->mode = ret;
 408
 409		switch (desc->svid) {
 410		case USB_TYPEC_DP_SID:
 411			alt = ucsi_register_displayport(con, override, i, desc);
 412			break;
 413		case USB_TYPEC_NVIDIA_VLINK_SID:
 414			if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
 415				alt = typec_port_register_altmode(con->port,
 416								  desc);
 417			else
 418				alt = ucsi_register_displayport(con, override,
 419								i, desc);
 420			break;
 421		default:
 422			alt = typec_port_register_altmode(con->port, desc);
 423			break;
 424		}
 425
 426		if (IS_ERR(alt)) {
 427			ret = PTR_ERR(alt);
 428			goto err;
 429		}
 430
 431		con->port_altmode[i] = alt;
 432		break;
 433	case UCSI_RECIPIENT_SOP:
 434		i = ucsi_next_altmode(con->partner_altmode);
 435		if (i < 0) {
 436			ret = i;
 437			goto err;
 438		}
 439
 440		ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
 441		if (ret < 0)
 442			return ret;
 443
 444		desc->mode = ret;
 445
 446		alt = typec_partner_register_altmode(con->partner, desc);
 447		if (IS_ERR(alt)) {
 448			ret = PTR_ERR(alt);
 449			goto err;
 450		}
 451
 452		con->partner_altmode[i] = alt;
 453		break;
 454	case UCSI_RECIPIENT_SOP_P:
 455		i = ucsi_next_altmode(con->plug_altmode);
 456		if (i < 0) {
 457			ret = i;
 458			goto err;
 459		}
 460
 461		ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
 462		if (ret < 0)
 463			return ret;
 464
 465		desc->mode = ret;
 466
 467		alt = typec_plug_register_altmode(con->plug, desc);
 468		if (IS_ERR(alt)) {
 469			ret = PTR_ERR(alt);
 470			goto err;
 471		}
 472
 473		con->plug_altmode[i] = alt;
 474		break;
 475	default:
 476		return -EINVAL;
 477	}
 478
 479	trace_ucsi_register_altmode(recipient, alt);
 480
 481	return 0;
 482
 483err:
 484	dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
 485		desc->svid, desc->mode);
 486
 487	return ret;
 488}
 489
 490static int
 491ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
 492{
 493	int max_altmodes = UCSI_MAX_ALTMODES;
 494	struct typec_altmode_desc desc;
 495	struct ucsi_altmode alt;
 496	struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
 497	struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
 498	struct ucsi *ucsi = con->ucsi;
 499	bool multi_dp = false;
 500	u64 command;
 501	int ret;
 502	int len;
 503	int i;
 504	int k = 0;
 505
 506	if (recipient == UCSI_RECIPIENT_CON)
 507		max_altmodes = con->ucsi->cap.num_alt_modes;
 508
 509	memset(orig, 0, sizeof(orig));
 510	memset(updated, 0, sizeof(updated));
 511
 512	/* First get all the alternate modes */
 513	for (i = 0; i < max_altmodes; i++) {
 514		memset(&alt, 0, sizeof(alt));
 515		command = UCSI_GET_ALTERNATE_MODES;
 516		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
 517		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
 518		command |= UCSI_GET_ALTMODE_OFFSET(i);
 519		len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
 520		/*
 521		 * We are collecting all altmodes first and then registering.
 522		 * Some type-C device will return zero length data beyond last
 523		 * alternate modes. We should not return if length is zero.
 524		 */
 525		if (len < 0)
 526			return len;
 527
 528		/* We got all altmodes, now break out and register them */
 529		if (!len || !alt.svid)
 530			break;
 531
 532		orig[k].mid = alt.mid;
 533		orig[k].svid = alt.svid;
 534		k++;
 535	}
 536	/*
 537	 * Update the original altmode table as some ppms may report
 538	 * multiple DP altmodes.
 539	 */
 540	multi_dp = ucsi->ops->update_altmodes(ucsi, recipient, orig, updated);
 541
 542	/* now register altmodes */
 543	for (i = 0; i < max_altmodes; i++) {
 544		memset(&desc, 0, sizeof(desc));
 545		if (multi_dp) {
 546			desc.svid = updated[i].svid;
 547			desc.vdo = updated[i].mid;
 548		} else {
 549			desc.svid = orig[i].svid;
 550			desc.vdo = orig[i].mid;
 551		}
 552		desc.roles = TYPEC_PORT_DRD;
 553
 554		if (!desc.svid)
 555			return 0;
 556
 557		ret = ucsi_register_altmode(con, &desc, recipient);
 558		if (ret)
 559			return ret;
 560	}
 561
 562	return 0;
 563}
 564
 565static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
 566{
 567	int max_altmodes = UCSI_MAX_ALTMODES;
 568	struct typec_altmode_desc desc;
 569	struct ucsi_altmode alt[2];
 570	u64 command;
 571	int num;
 572	int ret;
 573	int len;
 574	int j;
 575	int i;
 576
 577	if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
 578		return 0;
 579
 580	if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
 581		return 0;
 582
 583	if (con->ucsi->ops->update_altmodes)
 584		return ucsi_register_altmodes_nvidia(con, recipient);
 585
 586	if (recipient == UCSI_RECIPIENT_CON)
 587		max_altmodes = con->ucsi->cap.num_alt_modes;
 588
 589	for (i = 0; i < max_altmodes;) {
 590		memset(alt, 0, sizeof(alt));
 591		command = UCSI_GET_ALTERNATE_MODES;
 592		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
 593		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
 594		command |= UCSI_GET_ALTMODE_OFFSET(i);
 595		len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
 596		if (len == -EBUSY)
 597			continue;
 598		if (len <= 0)
 599			return len;
 600
 601		/*
 602		 * This code is requesting one alt mode at a time, but some PPMs
 603		 * may still return two. If that happens both alt modes need be
 604		 * registered and the offset for the next alt mode has to be
 605		 * incremented.
 606		 */
 607		num = len / sizeof(alt[0]);
 608		i += num;
 609
 610		for (j = 0; j < num; j++) {
 611			if (!alt[j].svid)
 612				return 0;
 613
 614			memset(&desc, 0, sizeof(desc));
 615			desc.vdo = alt[j].mid;
 616			desc.svid = alt[j].svid;
 617			desc.roles = TYPEC_PORT_DRD;
 618
 619			ret = ucsi_register_altmode(con, &desc, recipient);
 620			if (ret)
 621				return ret;
 622		}
 623	}
 624
 625	return 0;
 626}
 627
 628static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
 629{
 630	const struct typec_altmode *pdev;
 631	struct typec_altmode **adev;
 632	int i = 0;
 633
 634	switch (recipient) {
 635	case UCSI_RECIPIENT_CON:
 636		adev = con->port_altmode;
 637		break;
 638	case UCSI_RECIPIENT_SOP:
 639		adev = con->partner_altmode;
 640		break;
 641	case UCSI_RECIPIENT_SOP_P:
 642		adev = con->plug_altmode;
 643		break;
 644	default:
 645		return;
 646	}
 647
 648	while (adev[i]) {
 649		if (recipient == UCSI_RECIPIENT_SOP &&
 650		    (adev[i]->svid == USB_TYPEC_DP_SID ||
 651			(adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
 652			adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
 653			pdev = typec_altmode_get_partner(adev[i]);
 654			ucsi_displayport_remove_partner((void *)pdev);
 655		}
 656		typec_unregister_altmode(adev[i]);
 657		adev[i++] = NULL;
 658	}
 659}
 660
 661static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack)
 662{
 663	u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
 664	size_t size = min(sizeof(con->status),
 665			  UCSI_MAX_DATA_LENGTH(con->ucsi));
 666	int ret;
 667
 668	ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, conn_ack);
 669
 670	return ret < 0 ? ret : 0;
 671}
 672
 673static int ucsi_read_pdos(struct ucsi_connector *con,
 674			  enum typec_role role, int is_partner,
 675			  u32 *pdos, int offset, int num_pdos)
 676{
 677	struct ucsi *ucsi = con->ucsi;
 678	u64 command;
 679	int ret;
 680
 681	if (is_partner &&
 682	    ucsi->quirks & UCSI_NO_PARTNER_PDOS &&
 683	    (UCSI_CONSTAT(con, PWR_DIR) || !is_source(role)))
 684		return 0;
 685
 686	command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
 687	command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
 688	command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
 689	command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
 690	command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
 691	ret = ucsi_send_command(ucsi, command, pdos + offset,
 692				num_pdos * sizeof(u32));
 693	if (ret < 0 && ret != -ETIMEDOUT)
 694		dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
 695
 696	return ret;
 697}
 698
 699static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
 700			 int is_partner, u32 *pdos)
 701{
 702	struct ucsi *ucsi = con->ucsi;
 703	u8 num_pdos;
 704	int ret;
 705
 706	if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
 707		return 0;
 708
 709	/* UCSI max payload means only getting at most 4 PDOs at a time */
 710	ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
 711	if (ret < 0)
 712		return ret;
 713
 714	num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
 715	if (num_pdos < UCSI_MAX_PDOS)
 716		return num_pdos;
 717
 718	/* get the remaining PDOs, if any */
 719	ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
 720			     PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
 721	if (ret < 0)
 722		return ret;
 723
 724	return ret / sizeof(u32) + num_pdos;
 725}
 726
 727static int ucsi_get_src_pdos(struct ucsi_connector *con)
 728{
 729	int ret;
 730
 731	ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
 732	if (ret < 0)
 733		return ret;
 734
 735	con->num_pdos = ret;
 736
 737	ucsi_port_psy_changed(con);
 738
 739	return ret;
 740}
 741
 742static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
 743								enum typec_role role,
 744								bool is_partner)
 745{
 746	struct usb_power_delivery_capabilities_desc pd_caps;
 747	int ret;
 748
 749	ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
 750	if (ret <= 0)
 751		return ERR_PTR(ret);
 752
 753	if (ret < PDO_MAX_OBJECTS)
 754		pd_caps.pdo[ret] = 0;
 755
 756	pd_caps.role = role;
 757
 758	return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
 759							&pd_caps);
 760}
 761
 762static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient,
 763			       size_t bytes, void *data, u8 type)
 764{
 765	size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi));
 766	u64 command;
 767	u8 offset;
 768	int ret;
 769
 770	for (offset = 0; offset < bytes; offset += len) {
 771		len = min(len, bytes - offset);
 772
 773		command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num);
 774		command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
 775		command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
 776		command |= UCSI_GET_PD_MESSAGE_BYTES(len);
 777		command |= UCSI_GET_PD_MESSAGE_TYPE(type);
 778
 779		ret = ucsi_send_command(con->ucsi, command, data + offset, len);
 780		if (ret < 0)
 781			return ret;
 782	}
 783
 784	return 0;
 785}
 786
 787static int ucsi_get_partner_identity(struct ucsi_connector *con)
 788{
 789	u32 vdo[7] = {};
 790	int ret;
 791
 792	ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo,
 793				  UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
 794	if (ret < 0)
 795		return ret;
 796
 797	/* VDM Header is not part of struct usb_pd_identity, so dropping it. */
 798	con->partner_identity = *(struct usb_pd_identity *)&vdo[1];
 799
 800	ret = typec_partner_set_identity(con->partner);
 801	if (ret < 0)
 802		dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret);
 803
 804	return ret;
 805}
 806
 807static int ucsi_get_cable_identity(struct ucsi_connector *con)
 808{
 809	u32 vdo[7] = {};
 810	int ret;
 811
 812	ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo,
 813				  UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
 814	if (ret < 0)
 815		return ret;
 816
 817	con->cable_identity = *(struct usb_pd_identity *)&vdo[1];
 818
 819	ret = typec_cable_set_identity(con->cable);
 820	if (ret < 0)
 821		dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret);
 822
 823	return ret;
 824}
 825
 826static int ucsi_check_altmodes(struct ucsi_connector *con)
 827{
 828	int ret, num_partner_am;
 829
 830	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
 831	if (ret && ret != -ETIMEDOUT)
 832		dev_err(con->ucsi->dev,
 833			"con%d: failed to register partner alt modes (%d)\n",
 834			con->num, ret);
 835
 836	/* Ignoring the errors in this case. */
 837	if (con->partner_altmode[0]) {
 838		num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
 839		typec_partner_set_num_altmodes(con->partner, num_partner_am);
 840		ucsi_altmode_update_active(con);
 841		return 0;
 842	} else {
 843		typec_partner_set_num_altmodes(con->partner, 0);
 844	}
 845
 846	return ret;
 847}
 848
 849static void ucsi_register_device_pdos(struct ucsi_connector *con)
 850{
 851	struct ucsi *ucsi = con->ucsi;
 852	struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
 853	struct usb_power_delivery_capabilities *pd_cap;
 854
 855	if (con->pd)
 856		return;
 857
 858	con->pd = usb_power_delivery_register(ucsi->dev, &desc);
 859
 860	pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
 861	if (!IS_ERR(pd_cap))
 862		con->port_source_caps = pd_cap;
 863
 864	pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
 865	if (!IS_ERR(pd_cap))
 866		con->port_sink_caps = pd_cap;
 867
 868	typec_port_set_usb_power_delivery(con->port, con->pd);
 869}
 870
 871static int ucsi_register_partner_pdos(struct ucsi_connector *con)
 872{
 873	struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
 874	struct usb_power_delivery_capabilities *cap;
 875
 876	if (con->partner_pd)
 877		return 0;
 878
 879	con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
 880	if (IS_ERR(con->partner_pd))
 881		return PTR_ERR(con->partner_pd);
 882
 883	cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
 884	if (IS_ERR(cap))
 885	    return PTR_ERR(cap);
 886
 887	con->partner_source_caps = cap;
 888
 889	cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
 890	if (IS_ERR(cap))
 891	    return PTR_ERR(cap);
 892
 893	con->partner_sink_caps = cap;
 894
 895	return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
 896}
 897
 898static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
 899{
 900	usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
 901	con->partner_sink_caps = NULL;
 902	usb_power_delivery_unregister_capabilities(con->partner_source_caps);
 903	con->partner_source_caps = NULL;
 904	usb_power_delivery_unregister(con->partner_pd);
 905	con->partner_pd = NULL;
 906}
 907
 908static int ucsi_register_plug(struct ucsi_connector *con)
 909{
 910	struct typec_plug *plug;
 911	struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
 912
 913	plug = typec_register_plug(con->cable, &desc);
 914	if (IS_ERR(plug)) {
 915		dev_err(con->ucsi->dev,
 916			"con%d: failed to register plug (%ld)\n", con->num,
 917			PTR_ERR(plug));
 918		return PTR_ERR(plug);
 919	}
 920
 921	con->plug = plug;
 922	return 0;
 923}
 924
 925static void ucsi_unregister_plug(struct ucsi_connector *con)
 926{
 927	if (!con->plug)
 928		return;
 929
 930	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
 931	typec_unregister_plug(con->plug);
 932	con->plug = NULL;
 933}
 934
 935static int ucsi_register_cable(struct ucsi_connector *con)
 936{
 937	struct ucsi_cable_property cable_prop;
 938	struct typec_cable *cable;
 939	struct typec_cable_desc desc = {};
 940	u64 command;
 941	int ret;
 942
 943	command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
 944	ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop));
 945	if (ret < 0) {
 946		dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
 947		return ret;
 948	}
 949
 950	switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) {
 951	case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
 952		desc.type = USB_PLUG_TYPE_A;
 953		break;
 954	case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
 955		desc.type = USB_PLUG_TYPE_B;
 956		break;
 957	case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
 958		desc.type = USB_PLUG_TYPE_C;
 959		break;
 960	default:
 961		desc.type = USB_PLUG_NONE;
 962		break;
 963	}
 964
 965	if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
 966		desc.identity = &con->cable_identity;
 967	desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags);
 968
 969	if (con->ucsi->version >= UCSI_VERSION_2_1)
 970		desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags);
 971
 972	cable = typec_register_cable(con->port, &desc);
 973	if (IS_ERR(cable)) {
 974		dev_err(con->ucsi->dev,
 975			"con%d: failed to register cable (%ld)\n", con->num,
 976			PTR_ERR(cable));
 977		return PTR_ERR(cable);
 978	}
 979
 980	con->cable = cable;
 981	return 0;
 982}
 983
 984static void ucsi_unregister_cable(struct ucsi_connector *con)
 985{
 986	if (!con->cable)
 987		return;
 988
 989	ucsi_unregister_plug(con);
 990	typec_unregister_cable(con->cable);
 991	memset(&con->cable_identity, 0, sizeof(con->cable_identity));
 992	con->cable = NULL;
 993}
 994
 995static int ucsi_check_connector_capability(struct ucsi_connector *con)
 996{
 997	u64 pd_revision;
 998	u64 command;
 999	int ret;
1000
1001	if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1)
1002		return 0;
1003
1004	command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
1005	ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
1006	if (ret < 0) {
1007		dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1008		return ret;
1009	}
1010
1011	pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1012	typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision));
1013
1014	return ret;
1015}
1016
1017static void ucsi_orientation(struct ucsi_connector *con)
1018{
1019	if (con->ucsi->version < UCSI_VERSION_2_0)
1020		return;
1021
1022	if (!UCSI_CONSTAT(con, CONNECTED)) {
1023		typec_set_orientation(con->port, TYPEC_ORIENTATION_NONE);
1024		return;
1025	}
1026
1027	switch (UCSI_CONSTAT(con, ORIENTATION)) {
1028	case UCSI_CONSTAT_ORIENTATION_NORMAL:
1029		typec_set_orientation(con->port, TYPEC_ORIENTATION_NORMAL);
1030		break;
1031	case UCSI_CONSTAT_ORIENTATION_REVERSE:
1032		typec_set_orientation(con->port, TYPEC_ORIENTATION_REVERSE);
1033		break;
1034	default:
1035		break;
1036	}
1037}
1038
1039static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
1040{
1041	switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
1042	case UCSI_CONSTAT_PWR_OPMODE_PD:
1043		con->rdo = UCSI_CONSTAT(con, RDO);
1044		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1045		ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1046		ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1047		ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1048		ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1049		break;
1050	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1051		con->rdo = 0;
1052		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1053		ucsi_port_psy_changed(con);
1054		break;
1055	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1056		con->rdo = 0;
1057		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1058		ucsi_port_psy_changed(con);
1059		break;
1060	default:
1061		con->rdo = 0;
1062		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1063		ucsi_port_psy_changed(con);
1064		break;
1065	}
1066}
1067
1068static int ucsi_register_partner(struct ucsi_connector *con)
1069{
1070	u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE);
1071	struct typec_partner_desc desc;
1072	struct typec_partner *partner;
1073
1074	if (con->partner)
1075		return 0;
1076
1077	memset(&desc, 0, sizeof(desc));
1078
1079	switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1080	case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1081		desc.accessory = TYPEC_ACCESSORY_DEBUG;
1082		break;
1083	case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1084		desc.accessory = TYPEC_ACCESSORY_AUDIO;
1085		break;
1086	default:
1087		break;
1088	}
1089
1090	if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1091		ucsi_register_device_pdos(con);
1092
1093	if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1094		desc.identity = &con->partner_identity;
1095	desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1096
1097	if (con->ucsi->version >= UCSI_VERSION_2_1) {
1098		u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1099		desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision);
1100	}
1101
1102	partner = typec_register_partner(con->port, &desc);
1103	if (IS_ERR(partner)) {
1104		dev_err(con->ucsi->dev,
1105			"con%d: failed to register partner (%ld)\n", con->num,
1106			PTR_ERR(partner));
1107		return PTR_ERR(partner);
1108	}
1109
1110	con->partner = partner;
1111
1112	if (con->ucsi->version >= UCSI_VERSION_3_0 &&
1113	    UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4))
1114		typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1115	else if (con->ucsi->version >= UCSI_VERSION_2_0 &&
1116		 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3))
1117		typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1118
1119	return 0;
1120}
1121
1122static void ucsi_unregister_partner(struct ucsi_connector *con)
1123{
1124	if (!con->partner)
1125		return;
1126
1127	typec_set_mode(con->port, TYPEC_STATE_SAFE);
1128
1129	typec_partner_set_usb_power_delivery(con->partner, NULL);
1130	ucsi_unregister_partner_pdos(con);
1131	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1132	ucsi_unregister_cable(con);
1133	typec_unregister_partner(con->partner);
1134	memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1135	con->partner = NULL;
1136}
1137
1138static void ucsi_partner_change(struct ucsi_connector *con)
1139{
1140	enum usb_role u_role = USB_ROLE_NONE;
1141	int ret;
1142
1143	switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1144	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1145	case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1146		u_role = USB_ROLE_HOST;
1147		fallthrough;
1148	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1149		typec_set_data_role(con->port, TYPEC_HOST);
1150		break;
1151	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1152		u_role = USB_ROLE_DEVICE;
1153		typec_set_data_role(con->port, TYPEC_DEVICE);
1154		break;
1155	default:
1156		break;
1157	}
1158
1159	if (UCSI_CONSTAT(con, CONNECTED)) {
1160		switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1161		case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1162			typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1163			break;
1164		case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1165			typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1166			break;
1167		default:
1168			if (UCSI_CONSTAT(con, PARTNER_FLAG_USB))
1169				typec_set_mode(con->port, TYPEC_STATE_USB);
1170		}
1171	}
1172
1173	/* Only notify USB controller if partner supports USB data */
1174	if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1175		u_role = USB_ROLE_NONE;
1176
1177	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1178	if (ret)
1179		dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1180			con->num, u_role);
1181}
1182
1183static int ucsi_check_connection(struct ucsi_connector *con)
1184{
1185	u8 prev_state = UCSI_CONSTAT(con, CONNECTED);
1186	int ret;
1187
1188	ret = ucsi_get_connector_status(con, false);
1189	if (ret) {
1190		dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1191		return ret;
1192	}
1193
1194	if (UCSI_CONSTAT(con, CONNECTED)) {
1195		if (prev_state)
1196			return 0;
1197		ucsi_register_partner(con);
1198		ucsi_pwr_opmode_change(con);
1199		ucsi_partner_change(con);
1200	} else {
1201		ucsi_partner_change(con);
1202		ucsi_port_psy_changed(con);
1203		ucsi_unregister_partner(con);
1204	}
1205
1206	return 0;
1207}
1208
1209static int ucsi_check_cable(struct ucsi_connector *con)
1210{
1211	int ret, num_plug_am;
1212
1213	if (con->cable)
1214		return 0;
1215
1216	ret = ucsi_register_cable(con);
1217	if (ret < 0)
1218		return ret;
1219
1220	if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1221		ret = ucsi_get_cable_identity(con);
1222		if (ret < 0)
1223			return ret;
1224	}
1225
1226	if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1227		ret = ucsi_register_plug(con);
1228		if (ret < 0)
1229			return ret;
1230
1231		ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1232		if (ret < 0)
1233			return ret;
1234
1235		if (con->plug_altmode[0]) {
1236			num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1237			typec_plug_set_num_altmodes(con->plug, num_plug_am);
1238		} else {
1239			typec_plug_set_num_altmodes(con->plug, 0);
1240		}
1241	}
1242
1243	return 0;
1244}
1245
1246static void ucsi_handle_connector_change(struct work_struct *work)
1247{
1248	struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1249						  work);
1250	struct ucsi *ucsi = con->ucsi;
1251	u8 curr_scale, volt_scale;
1252	enum typec_role role;
1253	u16 change;
1254	int ret;
1255	u32 val;
1256
1257	mutex_lock(&con->lock);
1258
1259	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1260		dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n",
1261			     __func__);
1262
1263	ret = ucsi_get_connector_status(con, true);
1264	if (ret) {
1265		dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1266			__func__, ret);
1267		clear_bit(EVENT_PENDING, &con->ucsi->flags);
1268		goto out_unlock;
1269	}
1270
1271	trace_ucsi_connector_change(con->num, con);
1272
1273	if (ucsi->ops->connector_status)
1274		ucsi->ops->connector_status(con);
1275
1276	change = UCSI_CONSTAT(con, CHANGE);
1277	role = UCSI_CONSTAT(con, PWR_DIR);
1278
1279	if (change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1280		typec_set_pwr_role(con->port, role);
1281		ucsi_port_psy_changed(con);
1282
1283		/* Complete pending power role swap */
1284		if (!completion_done(&con->complete))
1285			complete(&con->complete);
1286	}
1287
1288	if (change & UCSI_CONSTAT_CONNECT_CHANGE) {
1289		typec_set_pwr_role(con->port, role);
1290		ucsi_port_psy_changed(con);
1291		ucsi_partner_change(con);
1292		ucsi_orientation(con);
1293
1294		if (UCSI_CONSTAT(con, CONNECTED)) {
1295			ucsi_register_partner(con);
1296			ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1297			if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1298				ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1299			if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1300				ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1301
1302			if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1303				ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1304				ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1305			}
1306		} else {
1307			ucsi_unregister_partner(con);
1308		}
1309	}
1310
1311	if (change & (UCSI_CONSTAT_POWER_OPMODE_CHANGE | UCSI_CONSTAT_POWER_LEVEL_CHANGE))
1312		ucsi_pwr_opmode_change(con);
1313
1314	if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) {
1315		ucsi_partner_change(con);
1316
1317		/* Complete pending data role swap */
1318		if (!completion_done(&con->complete))
1319			complete(&con->complete);
1320	}
1321
1322	if (change & UCSI_CONSTAT_CAM_CHANGE)
1323		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
1324
1325	if (change & (UCSI_CONSTAT_BC_CHANGE | UCSI_CONSTAT_SINK_PATH_CHANGE))
1326		ucsi_port_psy_changed(con);
1327
1328	if (con->ucsi->version >= UCSI_VERSION_2_1 &&
1329	    UCSI_CONSTAT(con, PWR_READING_READY_V2_1)) {
1330		curr_scale = UCSI_CONSTAT(con, CURRENT_SCALE_V2_1);
1331		volt_scale = UCSI_CONSTAT(con, VOLTAGE_SCALE_V2_1);
1332
1333		val = UCSI_CONSTAT(con, PEAK_CURRENT_V2_1);
1334		con->peak_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val;
1335
1336		val = UCSI_CONSTAT(con, AVG_CURRENT_V2_1);
1337		con->avg_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val;
1338
1339		val = UCSI_CONSTAT(con, VBUS_VOLTAGE_V2_1);
1340		con->vbus_voltage = UCSI_CONSTAT_VOLT_SCALE_MULT * volt_scale * val;
1341	}
1342
1343out_unlock:
1344	mutex_unlock(&con->lock);
1345}
1346
1347/**
1348 * ucsi_connector_change - Process Connector Change Event
1349 * @ucsi: UCSI Interface
1350 * @num: Connector number
1351 */
1352void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1353{
1354	struct ucsi_connector *con = &ucsi->connector[num - 1];
1355
1356	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1357		dev_dbg(ucsi->dev, "Early connector change event\n");
1358		return;
1359	}
1360
1361	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1362		schedule_work(&con->work);
1363}
1364EXPORT_SYMBOL_GPL(ucsi_connector_change);
1365
1366/* -------------------------------------------------------------------------- */
1367
1368/*
1369 * Hard Reset bit field was defined with value 1 in UCSI spec version 1.0.
1370 * Starting with spec version 1.1, Hard Reset bit field was removed from the
1371 * CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect,
1372 * the value to pass in to the command for a Hard Reset is different depending
1373 * on the supported UCSI version by the LPM.
1374 *
1375 * For performing a Data Reset on LPMs supporting version 2.0 and greater,
1376 * this function needs to be called with the second argument set to 0.
1377 */
1378static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1379{
1380	u64 command;
1381
1382	command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1383
1384	if (con->ucsi->version < UCSI_VERSION_1_1)
1385		command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0;
1386	else if (con->ucsi->version >= UCSI_VERSION_2_0)
1387		command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0;
1388
1389	return ucsi_send_command(con->ucsi, command, NULL, 0);
1390}
1391
1392static int ucsi_reset_ppm(struct ucsi *ucsi)
1393{
1394	u64 command;
1395	unsigned long tmo;
1396	u32 cci;
1397	int ret;
1398
1399	mutex_lock(&ucsi->ppm_lock);
1400
1401	ret = ucsi->ops->poll_cci(ucsi, &cci);
1402	if (ret < 0)
1403		goto out;
1404
1405	/*
1406	 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1407	 * the flag before we start another reset. Send a
1408	 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1409	 * Ignore a timeout and try the reset anyway if this fails.
1410	 */
1411	if (cci & UCSI_CCI_RESET_COMPLETE) {
1412		command = UCSI_SET_NOTIFICATION_ENABLE;
1413		ret = ucsi->ops->async_control(ucsi, command);
1414		if (ret < 0)
1415			goto out;
1416
1417		tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1418		do {
1419			ret = ucsi->ops->poll_cci(ucsi, &cci);
1420			if (ret < 0)
1421				goto out;
1422			if (cci & UCSI_CCI_COMMAND_COMPLETE)
1423				break;
1424			if (time_is_before_jiffies(tmo))
1425				break;
1426			msleep(20);
1427		} while (1);
1428
1429		WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1430	}
1431
1432	command = UCSI_PPM_RESET;
1433	ret = ucsi->ops->async_control(ucsi, command);
1434	if (ret < 0)
1435		goto out;
1436
1437	tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1438
1439	do {
1440		if (time_is_before_jiffies(tmo)) {
1441			ret = -ETIMEDOUT;
1442			goto out;
1443		}
1444
1445		/* Give the PPM time to process a reset before reading CCI */
1446		msleep(20);
1447
1448		ret = ucsi->ops->poll_cci(ucsi, &cci);
1449		if (ret)
1450			goto out;
1451
1452		/* If the PPM is still doing something else, reset it again. */
1453		if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1454			ret = ucsi->ops->async_control(ucsi, command);
1455			if (ret < 0)
1456				goto out;
1457		}
1458
1459	} while (!(cci & UCSI_CCI_RESET_COMPLETE));
1460
1461out:
1462	mutex_unlock(&ucsi->ppm_lock);
1463	return ret;
1464}
1465
1466static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1467{
1468	int ret;
1469
1470	ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1471	if (ret == -ETIMEDOUT) {
1472		u64 c;
1473
1474		/* PPM most likely stopped responding. Resetting everything. */
1475		ucsi_reset_ppm(con->ucsi);
1476
1477		c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1478		ucsi_send_command(con->ucsi, c, NULL, 0);
1479
1480		ucsi_reset_connector(con, true);
1481	}
1482
1483	return ret;
1484}
1485
1486static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1487{
1488	struct ucsi_connector *con = typec_get_drvdata(port);
1489	u8 partner_type;
1490	u64 command;
1491	int ret = 0;
1492
1493	mutex_lock(&con->lock);
1494
1495	if (!con->partner) {
1496		ret = -ENOTCONN;
1497		goto out_unlock;
1498	}
1499
1500	partner_type = UCSI_CONSTAT(con, PARTNER_TYPE);
1501	if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1502	     role == TYPEC_DEVICE) ||
1503	    (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1504	     role == TYPEC_HOST))
1505		goto out_unlock;
1506
1507	reinit_completion(&con->complete);
1508
1509	command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1510	command |= UCSI_SET_UOR_ROLE(role);
1511	command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1512	ret = ucsi_role_cmd(con, command);
1513	if (ret < 0)
1514		goto out_unlock;
1515
1516	mutex_unlock(&con->lock);
1517
1518	if (!wait_for_completion_timeout(&con->complete,
1519					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1520		return -ETIMEDOUT;
1521
1522	return 0;
1523
1524out_unlock:
1525	mutex_unlock(&con->lock);
1526
1527	return ret;
1528}
1529
1530static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1531{
1532	struct ucsi_connector *con = typec_get_drvdata(port);
1533	enum typec_role cur_role;
1534	u64 command;
1535	int ret = 0;
1536
1537	mutex_lock(&con->lock);
1538
1539	if (!con->partner) {
1540		ret = -ENOTCONN;
1541		goto out_unlock;
1542	}
1543
1544	cur_role = UCSI_CONSTAT(con, PWR_DIR);
1545
1546	if (cur_role == role)
1547		goto out_unlock;
1548
1549	reinit_completion(&con->complete);
1550
1551	command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1552	command |= UCSI_SET_PDR_ROLE(role);
1553	command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1554	ret = ucsi_role_cmd(con, command);
1555	if (ret < 0)
1556		goto out_unlock;
1557
1558	mutex_unlock(&con->lock);
1559
1560	if (!wait_for_completion_timeout(&con->complete,
1561					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1562		return -ETIMEDOUT;
1563
1564	mutex_lock(&con->lock);
1565
1566	/* Something has gone wrong while swapping the role */
1567	if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
1568		ucsi_reset_connector(con, true);
1569		ret = -EPROTO;
1570	}
1571
1572out_unlock:
1573	mutex_unlock(&con->lock);
1574
1575	return ret;
1576}
1577
1578static const struct typec_operations ucsi_ops = {
1579	.dr_set = ucsi_dr_swap,
1580	.pr_set = ucsi_pr_swap
1581};
1582
1583/* Caller must call fwnode_handle_put() after use */
1584static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1585{
1586	struct fwnode_handle *fwnode;
1587	int i = 1;
1588
1589	device_for_each_child_node(con->ucsi->dev, fwnode)
1590		if (i++ == con->num)
1591			return fwnode;
1592	return NULL;
1593}
1594
1595static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1596{
1597	struct typec_capability *cap = &con->typec_cap;
1598	enum typec_accessory *accessory = cap->accessory;
1599	enum usb_role u_role = USB_ROLE_NONE;
1600	u64 command;
1601	char *name;
1602	int ret;
1603
1604	name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1605	if (!name)
1606		return -ENOMEM;
1607
1608	con->wq = create_singlethread_workqueue(name);
1609	kfree(name);
1610	if (!con->wq)
1611		return -ENOMEM;
1612
1613	INIT_WORK(&con->work, ucsi_handle_connector_change);
1614	init_completion(&con->complete);
1615	mutex_init(&con->lock);
1616	INIT_LIST_HEAD(&con->partner_tasks);
1617	con->ucsi = ucsi;
1618
1619	cap->fwnode = ucsi_find_fwnode(con);
1620	con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1621	if (IS_ERR(con->usb_role_sw))
1622		return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1623			"con%d: failed to get usb role switch\n", con->num);
1624
1625	/* Delay other interactions with the con until registration is complete */
1626	mutex_lock(&con->lock);
1627
1628	/* Get connector capability */
1629	command = UCSI_GET_CONNECTOR_CAPABILITY;
1630	command |= UCSI_CONNECTOR_NUMBER(con->num);
1631	ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1632	if (ret < 0)
1633		goto out_unlock;
1634
1635	if (UCSI_CONCAP(con, OPMODE_DRP))
1636		cap->data = TYPEC_PORT_DRD;
1637	else if (UCSI_CONCAP(con, OPMODE_DFP))
1638		cap->data = TYPEC_PORT_DFP;
1639	else if (UCSI_CONCAP(con, OPMODE_UFP))
1640		cap->data = TYPEC_PORT_UFP;
1641
1642	if (UCSI_CONCAP(con, PROVIDER) && UCSI_CONCAP(con, CONSUMER))
1643		cap->type = TYPEC_PORT_DRP;
1644	else if (UCSI_CONCAP(con, PROVIDER))
1645		cap->type = TYPEC_PORT_SRC;
1646	else if (UCSI_CONCAP(con, CONSUMER))
1647		cap->type = TYPEC_PORT_SNK;
1648
1649	cap->revision = ucsi->cap.typec_version;
1650	cap->pd_revision = ucsi->cap.pd_version;
1651	cap->svdm_version = SVDM_VER_2_0;
1652	cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1653
1654	if (UCSI_CONCAP(con, OPMODE_AUDIO_ACCESSORY))
1655		*accessory++ = TYPEC_ACCESSORY_AUDIO;
1656	if (UCSI_CONCAP(con, OPMODE_DEBUG_ACCESSORY))
1657		*accessory = TYPEC_ACCESSORY_DEBUG;
1658
1659	if (UCSI_CONCAP_USB2_SUPPORT(con))
1660		cap->usb_capability |= USB_CAPABILITY_USB2;
1661	if (UCSI_CONCAP_USB3_SUPPORT(con))
1662		cap->usb_capability |= USB_CAPABILITY_USB3;
1663	if (UCSI_CONCAP_USB4_SUPPORT(con))
1664		cap->usb_capability |= USB_CAPABILITY_USB4;
1665
1666	cap->driver_data = con;
1667	cap->ops = &ucsi_ops;
1668
1669	if (ucsi->version >= UCSI_VERSION_2_0)
1670		con->typec_cap.orientation_aware = true;
1671
1672	if (ucsi->ops->update_connector)
1673		ucsi->ops->update_connector(con);
1674
1675	ret = ucsi_register_port_psy(con);
1676	if (ret)
1677		goto out;
1678
1679	/* Register the connector */
1680	con->port = typec_register_port(ucsi->dev, cap);
1681	if (IS_ERR(con->port)) {
1682		ret = PTR_ERR(con->port);
1683		goto out;
1684	}
1685
1686	if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS))
1687		ucsi_register_device_pdos(con);
1688
1689	/* Alternate modes */
1690	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1691	if (ret) {
1692		dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1693			con->num);
1694		goto out;
1695	}
1696
1697	/* Get the status */
1698	ret = ucsi_get_connector_status(con, false);
1699	if (ret) {
1700		dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1701		goto out;
1702	}
1703
1704	if (ucsi->ops->connector_status)
1705		ucsi->ops->connector_status(con);
1706
1707	switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1708	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1709	case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1710		u_role = USB_ROLE_HOST;
1711		fallthrough;
1712	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1713		typec_set_data_role(con->port, TYPEC_HOST);
1714		break;
1715	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1716		u_role = USB_ROLE_DEVICE;
1717		typec_set_data_role(con->port, TYPEC_DEVICE);
1718		break;
1719	default:
1720		break;
1721	}
1722
1723	/* Check if there is already something connected */
1724	if (UCSI_CONSTAT(con, CONNECTED)) {
1725		typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR));
1726		ucsi_register_partner(con);
1727		ucsi_pwr_opmode_change(con);
1728		ucsi_orientation(con);
1729		ucsi_port_psy_changed(con);
1730		if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1731			ucsi_get_partner_identity(con);
1732		if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1733			ucsi_check_cable(con);
1734	}
1735
1736	/* Only notify USB controller if partner supports USB data */
1737	if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1738		u_role = USB_ROLE_NONE;
1739
1740	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1741	if (ret) {
1742		dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1743			con->num, u_role);
1744		ret = 0;
1745	}
1746
1747	if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1748		ucsi_register_device_pdos(con);
1749		ucsi_get_src_pdos(con);
1750		ucsi_check_altmodes(con);
1751		ucsi_check_connector_capability(con);
1752	}
1753
1754	trace_ucsi_register_port(con->num, con);
1755
1756out:
1757	fwnode_handle_put(cap->fwnode);
1758out_unlock:
1759	mutex_unlock(&con->lock);
1760
1761	if (ret && con->wq) {
1762		destroy_workqueue(con->wq);
1763		con->wq = NULL;
1764	}
1765
1766	return ret;
1767}
1768
1769static u64 ucsi_get_supported_notifications(struct ucsi *ucsi)
1770{
1771	u16 features = ucsi->cap.features;
1772	u64 ntfy = UCSI_ENABLE_NTFY_ALL;
1773
1774	if (!(features & UCSI_CAP_ALT_MODE_DETAILS))
1775		ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE;
1776
1777	if (!(features & UCSI_CAP_PDO_DETAILS))
1778		ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE |
1779			  UCSI_ENABLE_NTFY_CAP_CHANGE);
1780
1781	if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS))
1782		ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE;
1783
1784	if (!(features & UCSI_CAP_PD_RESET))
1785		ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE;
1786
1787	if (ucsi->version <= UCSI_VERSION_1_2)
1788		return ntfy;
1789
1790	ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE;
1791
1792	if (features & UCSI_CAP_GET_ATTENTION_VDO)
1793		ntfy |= UCSI_ENABLE_NTFY_ATTENTION;
1794
1795	if (features & UCSI_CAP_FW_UPDATE_REQUEST)
1796		ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ;
1797
1798	if (features & UCSI_CAP_SECURITY_REQUEST)
1799		ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER;
1800
1801	if (features & UCSI_CAP_SET_RETIMER_MODE)
1802		ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE;
1803
1804	return ntfy;
1805}
1806
1807/**
1808 * ucsi_init - Initialize UCSI interface
1809 * @ucsi: UCSI to be initialized
1810 *
1811 * Registers all ports @ucsi has and enables all notification events.
1812 */
1813static int ucsi_init(struct ucsi *ucsi)
1814{
1815	struct ucsi_connector *con, *connector;
1816	u64 command, ntfy;
1817	u32 cci;
1818	int ret;
1819	int i;
1820
1821	/* Reset the PPM */
1822	ret = ucsi_reset_ppm(ucsi);
1823	if (ret) {
1824		dev_err(ucsi->dev, "failed to reset PPM!\n");
1825		goto err;
1826	}
1827
1828	/* Enable basic notifications */
1829	ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1830	command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1831	ret = ucsi_send_command(ucsi, command, NULL, 0);
1832	if (ret < 0)
1833		goto err_reset;
1834
1835	/* Get PPM capabilities */
1836	command = UCSI_GET_CAPABILITY;
1837	ret = ucsi_send_command(ucsi, command, &ucsi->cap,
1838				BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE));
1839	if (ret < 0)
1840		goto err_reset;
1841
1842	if (!ucsi->cap.num_connectors) {
1843		ret = -ENODEV;
1844		goto err_reset;
1845	}
1846	/* Check if reserved bit set. This is out of spec but happens in buggy FW */
1847	if (ucsi->cap.num_connectors & 0x80) {
1848		dev_warn(ucsi->dev, "UCSI: Invalid num_connectors %d. Likely buggy FW\n",
1849			 ucsi->cap.num_connectors);
1850		ucsi->cap.num_connectors &= 0x7f; // clear bit and carry on
1851	}
1852
1853	/* Allocate the connectors. Released in ucsi_unregister() */
1854	connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1855	if (!connector) {
1856		ret = -ENOMEM;
1857		goto err_reset;
1858	}
1859
1860	/* Register all connectors */
1861	for (i = 0; i < ucsi->cap.num_connectors; i++) {
1862		connector[i].num = i + 1;
1863		ret = ucsi_register_port(ucsi, &connector[i]);
1864		if (ret)
1865			goto err_unregister;
1866	}
1867
1868	/* Enable all supported notifications */
1869	ntfy = ucsi_get_supported_notifications(ucsi);
1870	command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1871	ret = ucsi_send_command(ucsi, command, NULL, 0);
1872	if (ret < 0)
1873		goto err_unregister;
1874
1875	ucsi->connector = connector;
1876	ucsi->ntfy = ntfy;
1877
1878	mutex_lock(&ucsi->ppm_lock);
1879	ret = ucsi->ops->read_cci(ucsi, &cci);
1880	mutex_unlock(&ucsi->ppm_lock);
1881	if (ret)
1882		return ret;
1883	if (UCSI_CCI_CONNECTOR(cci))
1884		ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1885
1886	return 0;
1887
1888err_unregister:
1889	for (con = connector; con->port; con++) {
1890		if (con->wq)
1891			destroy_workqueue(con->wq);
1892		ucsi_unregister_partner(con);
1893		ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1894		ucsi_unregister_port_psy(con);
1895
1896		usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1897		con->port_sink_caps = NULL;
1898		usb_power_delivery_unregister_capabilities(con->port_source_caps);
1899		con->port_source_caps = NULL;
1900		usb_power_delivery_unregister(con->pd);
1901		con->pd = NULL;
1902		typec_unregister_port(con->port);
1903		con->port = NULL;
1904	}
1905	kfree(connector);
1906err_reset:
1907	memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1908	ucsi_reset_ppm(ucsi);
1909err:
1910	return ret;
1911}
1912
1913static void ucsi_resume_work(struct work_struct *work)
1914{
1915	struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1916	struct ucsi_connector *con;
1917	u64 command;
1918	int ret;
1919
1920	/* Restore UCSI notification enable mask after system resume */
1921	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1922	ret = ucsi_send_command(ucsi, command, NULL, 0);
1923	if (ret < 0) {
1924		dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1925		return;
1926	}
1927
1928	for (con = ucsi->connector; con->port; con++) {
1929		mutex_lock(&con->lock);
1930		ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1931		mutex_unlock(&con->lock);
1932	}
1933}
1934
1935int ucsi_resume(struct ucsi *ucsi)
1936{
1937	if (ucsi->connector)
1938		queue_work(system_long_wq, &ucsi->resume_work);
1939	return 0;
1940}
1941EXPORT_SYMBOL_GPL(ucsi_resume);
1942
1943static void ucsi_init_work(struct work_struct *work)
1944{
1945	struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1946	int ret;
1947
1948	ret = ucsi_init(ucsi);
1949	if (ret)
1950		dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1951
1952	if (ret == -EPROBE_DEFER) {
1953		if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1954			dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1955			return;
1956		}
1957
1958		queue_delayed_work(system_long_wq, &ucsi->work,
1959				   UCSI_ROLE_SWITCH_INTERVAL);
1960	}
1961}
1962
1963/**
1964 * ucsi_get_drvdata - Return private driver data pointer
1965 * @ucsi: UCSI interface
1966 */
1967void *ucsi_get_drvdata(struct ucsi *ucsi)
1968{
1969	return ucsi->driver_data;
1970}
1971EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1972
1973/**
1974 * ucsi_set_drvdata - Assign private driver data pointer
1975 * @ucsi: UCSI interface
1976 * @data: Private data pointer
1977 */
1978void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1979{
1980	ucsi->driver_data = data;
1981}
1982EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1983
1984/**
1985 * ucsi_con_mutex_lock - Acquire the connector mutex
1986 * @con: The connector interface to lock
1987 *
1988 * Returns true on success, false if the connector is disconnected
1989 */
1990bool ucsi_con_mutex_lock(struct ucsi_connector *con)
1991{
1992	bool mutex_locked = false;
1993	bool connected = true;
1994
1995	while (connected && !mutex_locked) {
1996		mutex_locked = mutex_trylock(&con->lock) != 0;
1997		connected = UCSI_CONSTAT(con, CONNECTED);
1998		if (connected && !mutex_locked)
1999			msleep(20);
2000	}
2001
2002	connected = connected && con->partner;
2003	if (!connected && mutex_locked)
2004		mutex_unlock(&con->lock);
2005
2006	return connected;
2007}
2008
2009/**
2010 * ucsi_con_mutex_unlock - Release the connector mutex
2011 * @con: The connector interface to unlock
2012 */
2013void ucsi_con_mutex_unlock(struct ucsi_connector *con)
2014{
2015	mutex_unlock(&con->lock);
2016}
2017
2018/**
2019 * ucsi_create - Allocate UCSI instance
2020 * @dev: Device interface to the PPM (Platform Policy Manager)
2021 * @ops: I/O routines
2022 */
2023struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
2024{
2025	struct ucsi *ucsi;
2026
2027	if (!ops ||
2028	    !ops->read_version || !ops->read_cci || !ops->poll_cci ||
2029	    !ops->read_message_in || !ops->sync_control || !ops->async_control)
2030		return ERR_PTR(-EINVAL);
2031
2032	ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
2033	if (!ucsi)
2034		return ERR_PTR(-ENOMEM);
2035
2036	INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
2037	INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
2038	mutex_init(&ucsi->ppm_lock);
2039	init_completion(&ucsi->complete);
2040	ucsi->dev = dev;
2041	ucsi->ops = ops;
2042
2043	return ucsi;
2044}
2045EXPORT_SYMBOL_GPL(ucsi_create);
2046
2047/**
2048 * ucsi_destroy - Free UCSI instance
2049 * @ucsi: UCSI instance to be freed
2050 */
2051void ucsi_destroy(struct ucsi *ucsi)
2052{
2053	ucsi_debugfs_unregister(ucsi);
2054	kfree(ucsi);
2055}
2056EXPORT_SYMBOL_GPL(ucsi_destroy);
2057
2058/**
2059 * ucsi_register - Register UCSI interface
2060 * @ucsi: UCSI instance
2061 */
2062int ucsi_register(struct ucsi *ucsi)
2063{
2064	int ret;
2065
2066	ret = ucsi->ops->read_version(ucsi, &ucsi->version);
2067	if (ret)
2068		return ret;
2069
2070	if (!ucsi->version)
2071		return -ENODEV;
2072
2073	/*
2074	 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
2075	 * N = sub-minor version).
2076	 */
2077	dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
2078		UCSI_BCD_GET_MAJOR(ucsi->version),
2079		UCSI_BCD_GET_MINOR(ucsi->version),
2080		UCSI_BCD_GET_SUBMINOR(ucsi->version));
2081
2082	queue_delayed_work(system_long_wq, &ucsi->work, 0);
2083
2084	ucsi_debugfs_register(ucsi);
2085	return 0;
2086}
2087EXPORT_SYMBOL_GPL(ucsi_register);
2088
2089/**
2090 * ucsi_unregister - Unregister UCSI interface
2091 * @ucsi: UCSI interface to be unregistered
2092 *
2093 * Unregister UCSI interface that was created with ucsi_register().
2094 */
2095void ucsi_unregister(struct ucsi *ucsi)
2096{
2097	u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
2098	int i;
2099
2100	/* Make sure that we are not in the middle of driver initialization */
2101	cancel_delayed_work_sync(&ucsi->work);
2102	cancel_work_sync(&ucsi->resume_work);
2103
2104	/* Disable notifications */
2105	ucsi->ops->async_control(ucsi, cmd);
2106
2107	if (!ucsi->connector)
2108		return;
2109
2110	for (i = 0; i < ucsi->cap.num_connectors; i++) {
2111		cancel_work_sync(&ucsi->connector[i].work);
2112
2113		if (ucsi->connector[i].wq) {
2114			struct ucsi_work *uwork;
2115
2116			mutex_lock(&ucsi->connector[i].lock);
2117			/*
2118			 * queue delayed items immediately so they can execute
2119			 * and free themselves before the wq is destroyed
2120			 */
2121			list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
2122				mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
2123			mutex_unlock(&ucsi->connector[i].lock);
2124			destroy_workqueue(ucsi->connector[i].wq);
2125		}
2126
2127		ucsi_unregister_partner(&ucsi->connector[i]);
2128		ucsi_unregister_altmodes(&ucsi->connector[i],
2129					 UCSI_RECIPIENT_CON);
2130		ucsi_unregister_port_psy(&ucsi->connector[i]);
2131
2132		usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
2133		ucsi->connector[i].port_sink_caps = NULL;
2134		usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
2135		ucsi->connector[i].port_source_caps = NULL;
2136		usb_power_delivery_unregister(ucsi->connector[i].pd);
2137		ucsi->connector[i].pd = NULL;
2138		typec_unregister_port(ucsi->connector[i].port);
2139	}
2140
2141	kfree(ucsi->connector);
2142}
2143EXPORT_SYMBOL_GPL(ucsi_unregister);
2144
2145static int __init ucsi_module_init(void)
2146{
2147	ucsi_debugfs_init();
2148	return 0;
2149}
2150module_init(ucsi_module_init);
2151
2152static void __exit ucsi_module_exit(void)
2153{
2154	ucsi_debugfs_exit();
2155}
2156module_exit(ucsi_module_exit);
2157
2158MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2159MODULE_LICENSE("GPL v2");
2160MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
~/ Read-only file preview.