cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

ucsi.c (33455B)


      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		5000
     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
     39static int ucsi_acknowledge_command(struct ucsi *ucsi)
     40{
     41	u64 ctrl;
     42
     43	ctrl = UCSI_ACK_CC_CI;
     44	ctrl |= UCSI_ACK_COMMAND_COMPLETE;
     45
     46	return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
     47}
     48
     49static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
     50{
     51	u64 ctrl;
     52
     53	ctrl = UCSI_ACK_CC_CI;
     54	ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
     55
     56	return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
     57}
     58
     59static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
     60
     61static int ucsi_read_error(struct ucsi *ucsi)
     62{
     63	u16 error;
     64	int ret;
     65
     66	/* Acknowledge the command that failed */
     67	ret = ucsi_acknowledge_command(ucsi);
     68	if (ret)
     69		return ret;
     70
     71	ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
     72	if (ret < 0)
     73		return ret;
     74
     75	ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
     76	if (ret)
     77		return ret;
     78
     79	switch (error) {
     80	case UCSI_ERROR_INCOMPATIBLE_PARTNER:
     81		return -EOPNOTSUPP;
     82	case UCSI_ERROR_CC_COMMUNICATION_ERR:
     83		return -ECOMM;
     84	case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
     85		return -EPROTO;
     86	case UCSI_ERROR_DEAD_BATTERY:
     87		dev_warn(ucsi->dev, "Dead battery condition!\n");
     88		return -EPERM;
     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);
     93		return -EINVAL;
     94	case UCSI_ERROR_OVERCURRENT:
     95		dev_warn(ucsi->dev, "Overcurrent condition\n");
     96		break;
     97	case UCSI_ERROR_PARTNER_REJECTED_SWAP:
     98		dev_warn(ucsi->dev, "Partner rejected swap\n");
     99		break;
    100	case UCSI_ERROR_HARD_RESET:
    101		dev_warn(ucsi->dev, "Hard reset occurred\n");
    102		break;
    103	case UCSI_ERROR_PPM_POLICY_CONFLICT:
    104		dev_warn(ucsi->dev, "PPM Policy conflict\n");
    105		break;
    106	case UCSI_ERROR_SWAP_REJECTED:
    107		dev_warn(ucsi->dev, "Swap rejected\n");
    108		break;
    109	case UCSI_ERROR_UNDEFINED:
    110	default:
    111		dev_err(ucsi->dev, "unknown error %u\n", error);
    112		break;
    113	}
    114
    115	return -EIO;
    116}
    117
    118static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
    119{
    120	u32 cci;
    121	int ret;
    122
    123	ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
    124	if (ret)
    125		return ret;
    126
    127	ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
    128	if (ret)
    129		return ret;
    130
    131	if (cci & UCSI_CCI_BUSY) {
    132		ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
    133		return -EBUSY;
    134	}
    135
    136	if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
    137		return -EIO;
    138
    139	if (cci & UCSI_CCI_NOT_SUPPORTED)
    140		return -EOPNOTSUPP;
    141
    142	if (cci & UCSI_CCI_ERROR) {
    143		if (cmd == UCSI_GET_ERROR_STATUS)
    144			return -EIO;
    145		return ucsi_read_error(ucsi);
    146	}
    147
    148	return UCSI_CCI_LENGTH(cci);
    149}
    150
    151int ucsi_send_command(struct ucsi *ucsi, u64 command,
    152		      void *data, size_t size)
    153{
    154	u8 length;
    155	int ret;
    156
    157	mutex_lock(&ucsi->ppm_lock);
    158
    159	ret = ucsi_exec_command(ucsi, command);
    160	if (ret < 0)
    161		goto out;
    162
    163	length = ret;
    164
    165	if (data) {
    166		ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
    167		if (ret)
    168			goto out;
    169	}
    170
    171	ret = ucsi_acknowledge_command(ucsi);
    172	if (ret)
    173		goto out;
    174
    175	ret = length;
    176out:
    177	mutex_unlock(&ucsi->ppm_lock);
    178	return ret;
    179}
    180EXPORT_SYMBOL_GPL(ucsi_send_command);
    181
    182int ucsi_resume(struct ucsi *ucsi)
    183{
    184	u64 command;
    185
    186	/* Restore UCSI notification enable mask after system resume */
    187	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
    188
    189	return ucsi_send_command(ucsi, command, NULL, 0);
    190}
    191EXPORT_SYMBOL_GPL(ucsi_resume);
    192/* -------------------------------------------------------------------------- */
    193
    194struct ucsi_work {
    195	struct delayed_work work;
    196	unsigned long delay;
    197	unsigned int count;
    198	struct ucsi_connector *con;
    199	int (*cb)(struct ucsi_connector *);
    200};
    201
    202static void ucsi_poll_worker(struct work_struct *work)
    203{
    204	struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
    205	struct ucsi_connector *con = uwork->con;
    206	int ret;
    207
    208	mutex_lock(&con->lock);
    209
    210	if (!con->partner) {
    211		mutex_unlock(&con->lock);
    212		kfree(uwork);
    213		return;
    214	}
    215
    216	ret = uwork->cb(con);
    217
    218	if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
    219		queue_delayed_work(con->wq, &uwork->work, uwork->delay);
    220	else
    221		kfree(uwork);
    222
    223	mutex_unlock(&con->lock);
    224}
    225
    226static int ucsi_partner_task(struct ucsi_connector *con,
    227			     int (*cb)(struct ucsi_connector *),
    228			     int retries, unsigned long delay)
    229{
    230	struct ucsi_work *uwork;
    231
    232	if (!con->partner)
    233		return 0;
    234
    235	uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
    236	if (!uwork)
    237		return -ENOMEM;
    238
    239	INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
    240	uwork->count = retries;
    241	uwork->delay = delay;
    242	uwork->con = con;
    243	uwork->cb = cb;
    244
    245	queue_delayed_work(con->wq, &uwork->work, delay);
    246
    247	return 0;
    248}
    249
    250/* -------------------------------------------------------------------------- */
    251
    252void ucsi_altmode_update_active(struct ucsi_connector *con)
    253{
    254	const struct typec_altmode *altmode = NULL;
    255	u64 command;
    256	int ret;
    257	u8 cur;
    258	int i;
    259
    260	command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
    261	ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
    262	if (ret < 0) {
    263		if (con->ucsi->version > 0x0100) {
    264			dev_err(con->ucsi->dev,
    265				"GET_CURRENT_CAM command failed\n");
    266			return;
    267		}
    268		cur = 0xff;
    269	}
    270
    271	if (cur < UCSI_MAX_ALTMODES)
    272		altmode = typec_altmode_get_partner(con->port_altmode[cur]);
    273
    274	for (i = 0; con->partner_altmode[i]; i++)
    275		typec_altmode_update_active(con->partner_altmode[i],
    276					    con->partner_altmode[i] == altmode);
    277}
    278
    279static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
    280{
    281	u8 mode = 1;
    282	int i;
    283
    284	for (i = 0; alt[i]; i++) {
    285		if (i > MODE_DISCOVERY_MAX)
    286			return -ERANGE;
    287
    288		if (alt[i]->svid == svid)
    289			mode++;
    290	}
    291
    292	return mode;
    293}
    294
    295static int ucsi_next_altmode(struct typec_altmode **alt)
    296{
    297	int i = 0;
    298
    299	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
    300		if (!alt[i])
    301			return i;
    302
    303	return -ENOENT;
    304}
    305
    306static int ucsi_get_num_altmode(struct typec_altmode **alt)
    307{
    308	int i;
    309
    310	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
    311		if (!alt[i])
    312			break;
    313
    314	return i;
    315}
    316
    317static int ucsi_register_altmode(struct ucsi_connector *con,
    318				 struct typec_altmode_desc *desc,
    319				 u8 recipient)
    320{
    321	struct typec_altmode *alt;
    322	bool override;
    323	int ret;
    324	int i;
    325
    326	override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
    327
    328	switch (recipient) {
    329	case UCSI_RECIPIENT_CON:
    330		i = ucsi_next_altmode(con->port_altmode);
    331		if (i < 0) {
    332			ret = i;
    333			goto err;
    334		}
    335
    336		ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
    337		if (ret < 0)
    338			return ret;
    339
    340		desc->mode = ret;
    341
    342		switch (desc->svid) {
    343		case USB_TYPEC_DP_SID:
    344			alt = ucsi_register_displayport(con, override, i, desc);
    345			break;
    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,
    349								  desc);
    350			else
    351				alt = ucsi_register_displayport(con, override,
    352								i, desc);
    353			break;
    354		default:
    355			alt = typec_port_register_altmode(con->port, desc);
    356			break;
    357		}
    358
    359		if (IS_ERR(alt)) {
    360			ret = PTR_ERR(alt);
    361			goto err;
    362		}
    363
    364		con->port_altmode[i] = alt;
    365		break;
    366	case UCSI_RECIPIENT_SOP:
    367		i = ucsi_next_altmode(con->partner_altmode);
    368		if (i < 0) {
    369			ret = i;
    370			goto err;
    371		}
    372
    373		ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
    374		if (ret < 0)
    375			return ret;
    376
    377		desc->mode = ret;
    378
    379		alt = typec_partner_register_altmode(con->partner, desc);
    380		if (IS_ERR(alt)) {
    381			ret = PTR_ERR(alt);
    382			goto err;
    383		}
    384
    385		con->partner_altmode[i] = alt;
    386		break;
    387	default:
    388		return -EINVAL;
    389	}
    390
    391	trace_ucsi_register_altmode(recipient, alt);
    392
    393	return 0;
    394
    395err:
    396	dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
    397		desc->svid, desc->mode);
    398
    399	return ret;
    400}
    401
    402static int
    403ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
    404{
    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;
    412	u64 command;
    413	int ret;
    414	int len;
    415	int i;
    416	int k = 0;
    417
    418	if (recipient == UCSI_RECIPIENT_CON)
    419		max_altmodes = con->ucsi->cap.num_alt_modes;
    420
    421	memset(orig, 0, sizeof(orig));
    422	memset(updated, 0, sizeof(updated));
    423
    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));
    432		/*
    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.
    436		 */
    437		if (len < 0)
    438			return len;
    439
    440		/* We got all altmodes, now break out and register them */
    441		if (!len || !alt.svid)
    442			break;
    443
    444		orig[k].mid = alt.mid;
    445		orig[k].svid = alt.svid;
    446		k++;
    447	}
    448	/*
    449	 * Update the original altmode table as some ppms may report
    450	 * multiple DP altmodes.
    451	 */
    452	if (recipient == UCSI_RECIPIENT_CON)
    453		multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
    454
    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;
    461		} else {
    462			desc.svid = orig[i].svid;
    463			desc.vdo = orig[i].mid;
    464		}
    465		desc.roles = TYPEC_PORT_DRD;
    466
    467		if (!desc.svid)
    468			return 0;
    469
    470		ret = ucsi_register_altmode(con, &desc, recipient);
    471		if (ret)
    472			return ret;
    473	}
    474
    475	return 0;
    476}
    477
    478static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
    479{
    480	int max_altmodes = UCSI_MAX_ALTMODES;
    481	struct typec_altmode_desc desc;
    482	struct ucsi_altmode alt[2];
    483	u64 command;
    484	int num;
    485	int ret;
    486	int len;
    487	int j;
    488	int i;
    489
    490	if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
    491		return 0;
    492
    493	if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
    494		return 0;
    495
    496	if (con->ucsi->ops->update_altmodes)
    497		return ucsi_register_altmodes_nvidia(con, recipient);
    498
    499	if (recipient == UCSI_RECIPIENT_CON)
    500		max_altmodes = con->ucsi->cap.num_alt_modes;
    501
    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));
    509		if (len == -EBUSY)
    510			continue;
    511		if (len <= 0)
    512			return len;
    513
    514		/*
    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
    518		 * incremented.
    519		 */
    520		num = len / sizeof(alt[0]);
    521		i += num;
    522
    523		for (j = 0; j < num; j++) {
    524			if (!alt[j].svid)
    525				return 0;
    526
    527			memset(&desc, 0, sizeof(desc));
    528			desc.vdo = alt[j].mid;
    529			desc.svid = alt[j].svid;
    530			desc.roles = TYPEC_PORT_DRD;
    531
    532			ret = ucsi_register_altmode(con, &desc, recipient);
    533			if (ret)
    534				return ret;
    535		}
    536	}
    537
    538	return 0;
    539}
    540
    541static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
    542{
    543	const struct typec_altmode *pdev;
    544	struct typec_altmode **adev;
    545	int i = 0;
    546
    547	switch (recipient) {
    548	case UCSI_RECIPIENT_CON:
    549		adev = con->port_altmode;
    550		break;
    551	case UCSI_RECIPIENT_SOP:
    552		adev = con->partner_altmode;
    553		break;
    554	default:
    555		return;
    556	}
    557
    558	while (adev[i]) {
    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);
    565		}
    566		typec_unregister_altmode(adev[i]);
    567		adev[i++] = NULL;
    568	}
    569}
    570
    571static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
    572			 u32 *pdos, int offset, int num_pdos)
    573{
    574	struct ucsi *ucsi = con->ucsi;
    575	u64 command;
    576	int ret;
    577
    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");
    589
    590	return ret;
    591}
    592
    593static int ucsi_get_src_pdos(struct ucsi_connector *con)
    594{
    595	int ret;
    596
    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);
    599	if (ret < 0)
    600		return ret;
    601
    602	con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
    603	if (con->num_pdos < UCSI_MAX_PDOS)
    604		return 0;
    605
    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);
    609	if (ret < 0)
    610		return ret;
    611
    612	con->num_pdos += ret / sizeof(u32);
    613
    614	ucsi_port_psy_changed(con);
    615
    616	return 0;
    617}
    618
    619static int ucsi_check_altmodes(struct ucsi_connector *con)
    620{
    621	int ret, num_partner_am;
    622
    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",
    627			con->num, ret);
    628
    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);
    635		return 0;
    636	}
    637
    638	return ret;
    639}
    640
    641static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
    642{
    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);
    649		break;
    650	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
    651		con->rdo = 0;
    652		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
    653		break;
    654	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
    655		con->rdo = 0;
    656		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
    657		break;
    658	default:
    659		con->rdo = 0;
    660		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
    661		break;
    662	}
    663}
    664
    665static int ucsi_register_partner(struct ucsi_connector *con)
    666{
    667	u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
    668	struct typec_partner_desc desc;
    669	struct typec_partner *partner;
    670
    671	if (con->partner)
    672		return 0;
    673
    674	memset(&desc, 0, sizeof(desc));
    675
    676	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
    677	case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
    678		desc.accessory = TYPEC_ACCESSORY_DEBUG;
    679		break;
    680	case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
    681		desc.accessory = TYPEC_ACCESSORY_AUDIO;
    682		break;
    683	default:
    684		break;
    685	}
    686
    687	desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
    688
    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,
    693			PTR_ERR(partner));
    694		return PTR_ERR(partner);
    695	}
    696
    697	con->partner = partner;
    698
    699	return 0;
    700}
    701
    702static void ucsi_unregister_partner(struct ucsi_connector *con)
    703{
    704	if (!con->partner)
    705		return;
    706
    707	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
    708	typec_unregister_partner(con->partner);
    709	con->partner = NULL;
    710}
    711
    712static void ucsi_partner_change(struct ucsi_connector *con)
    713{
    714	enum usb_role u_role = USB_ROLE_NONE;
    715	int ret;
    716
    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;
    721		fallthrough;
    722	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
    723		typec_set_data_role(con->port, TYPEC_HOST);
    724		break;
    725	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
    726		u_role = USB_ROLE_DEVICE;
    727		typec_set_data_role(con->port, TYPEC_DEVICE);
    728		break;
    729	default:
    730		break;
    731	}
    732
    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;
    736
    737	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
    738	if (ret)
    739		dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
    740			con->num, u_role);
    741}
    742
    743static int ucsi_check_connection(struct ucsi_connector *con)
    744{
    745	u64 command;
    746	int ret;
    747
    748	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
    749	ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
    750	if (ret < 0) {
    751		dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
    752		return ret;
    753	}
    754
    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);
    759	} else {
    760		ucsi_partner_change(con);
    761		ucsi_port_psy_changed(con);
    762		ucsi_unregister_partner(con);
    763	}
    764
    765	return 0;
    766}
    767
    768static void ucsi_handle_connector_change(struct work_struct *work)
    769{
    770	struct ucsi_connector *con = container_of(work, struct ucsi_connector,
    771						  work);
    772	struct ucsi *ucsi = con->ucsi;
    773	enum typec_role role;
    774	u64 command;
    775	int ret;
    776
    777	mutex_lock(&con->lock);
    778
    779	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
    780	ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
    781	if (ret < 0) {
    782		dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
    783			__func__, ret);
    784		goto out_unlock;
    785	}
    786
    787	trace_ucsi_connector_change(con->num, &con->status);
    788
    789	role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
    790
    791	if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
    792		typec_set_pwr_role(con->port, role);
    793
    794		/* Complete pending power role swap */
    795		if (!completion_done(&con->complete))
    796			complete(&con->complete);
    797	}
    798
    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);
    803
    804		if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
    805			ucsi_register_partner(con);
    806			ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
    807		} else {
    808			ucsi_unregister_partner(con);
    809		}
    810	}
    811
    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);
    815
    816	if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
    817		ucsi_partner_change(con);
    818
    819		/* Complete pending data role swap */
    820		if (!completion_done(&con->complete))
    821			complete(&con->complete);
    822	}
    823
    824	if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
    825		ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
    826
    827	clear_bit(EVENT_PENDING, &con->ucsi->flags);
    828
    829	ret = ucsi_acknowledge_connector_change(ucsi);
    830	if (ret)
    831		dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
    832
    833out_unlock:
    834	mutex_unlock(&con->lock);
    835}
    836
    837/**
    838 * ucsi_connector_change - Process Connector Change Event
    839 * @ucsi: UCSI Interface
    840 * @num: Connector number
    841 */
    842void ucsi_connector_change(struct ucsi *ucsi, u8 num)
    843{
    844	struct ucsi_connector *con = &ucsi->connector[num - 1];
    845
    846	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
    847		dev_dbg(ucsi->dev, "Bogus connector change event\n");
    848		return;
    849	}
    850
    851	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
    852		schedule_work(&con->work);
    853}
    854EXPORT_SYMBOL_GPL(ucsi_connector_change);
    855
    856/* -------------------------------------------------------------------------- */
    857
    858static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
    859{
    860	u64 command;
    861
    862	command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
    863	command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
    864
    865	return ucsi_send_command(con->ucsi, command, NULL, 0);
    866}
    867
    868static int ucsi_reset_ppm(struct ucsi *ucsi)
    869{
    870	u64 command = UCSI_PPM_RESET;
    871	unsigned long tmo;
    872	u32 cci;
    873	int ret;
    874
    875	mutex_lock(&ucsi->ppm_lock);
    876
    877	ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
    878				     sizeof(command));
    879	if (ret < 0)
    880		goto out;
    881
    882	tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
    883
    884	do {
    885		if (time_is_before_jiffies(tmo)) {
    886			ret = -ETIMEDOUT;
    887			goto out;
    888		}
    889
    890		ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
    891		if (ret)
    892			goto out;
    893
    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,
    897						     &command,
    898						     sizeof(command));
    899			if (ret < 0)
    900				goto out;
    901		}
    902
    903		msleep(20);
    904	} while (!(cci & UCSI_CCI_RESET_COMPLETE));
    905
    906out:
    907	mutex_unlock(&ucsi->ppm_lock);
    908	return ret;
    909}
    910
    911static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
    912{
    913	int ret;
    914
    915	ret = ucsi_send_command(con->ucsi, command, NULL, 0);
    916	if (ret == -ETIMEDOUT) {
    917		u64 c;
    918
    919		/* PPM most likely stopped responding. Resetting everything. */
    920		ucsi_reset_ppm(con->ucsi);
    921
    922		c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
    923		ucsi_send_command(con->ucsi, c, NULL, 0);
    924
    925		ucsi_reset_connector(con, true);
    926	}
    927
    928	return ret;
    929}
    930
    931static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
    932{
    933	struct ucsi_connector *con = typec_get_drvdata(port);
    934	u8 partner_type;
    935	u64 command;
    936	int ret = 0;
    937
    938	mutex_lock(&con->lock);
    939
    940	if (!con->partner) {
    941		ret = -ENOTCONN;
    942		goto out_unlock;
    943	}
    944
    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 &&
    949	     role == TYPEC_HOST))
    950		goto out_unlock;
    951
    952	reinit_completion(&con->complete);
    953
    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);
    958	if (ret < 0)
    959		goto out_unlock;
    960
    961	mutex_unlock(&con->lock);
    962
    963	if (!wait_for_completion_timeout(&con->complete,
    964					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
    965		return -ETIMEDOUT;
    966
    967	return 0;
    968
    969out_unlock:
    970	mutex_unlock(&con->lock);
    971
    972	return ret;
    973}
    974
    975static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
    976{
    977	struct ucsi_connector *con = typec_get_drvdata(port);
    978	enum typec_role cur_role;
    979	u64 command;
    980	int ret = 0;
    981
    982	mutex_lock(&con->lock);
    983
    984	if (!con->partner) {
    985		ret = -ENOTCONN;
    986		goto out_unlock;
    987	}
    988
    989	cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
    990
    991	if (cur_role == role)
    992		goto out_unlock;
    993
    994	reinit_completion(&con->complete);
    995
    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);
   1000	if (ret < 0)
   1001		goto out_unlock;
   1002
   1003	mutex_unlock(&con->lock);
   1004
   1005	if (!wait_for_completion_timeout(&con->complete,
   1006					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
   1007		return -ETIMEDOUT;
   1008
   1009	mutex_lock(&con->lock);
   1010
   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);
   1015		ret = -EPROTO;
   1016	}
   1017
   1018out_unlock:
   1019	mutex_unlock(&con->lock);
   1020
   1021	return ret;
   1022}
   1023
   1024static const struct typec_operations ucsi_ops = {
   1025	.dr_set = ucsi_dr_swap,
   1026	.pr_set = ucsi_pr_swap
   1027};
   1028
   1029/* Caller must call fwnode_handle_put() after use */
   1030static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
   1031{
   1032	struct fwnode_handle *fwnode;
   1033	int i = 1;
   1034
   1035	device_for_each_child_node(con->ucsi->dev, fwnode)
   1036		if (i++ == con->num)
   1037			return fwnode;
   1038	return NULL;
   1039}
   1040
   1041static int ucsi_register_port(struct ucsi *ucsi, int index)
   1042{
   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;
   1047	u64 command;
   1048	char *name;
   1049	int ret;
   1050
   1051	name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
   1052	if (!name)
   1053		return -ENOMEM;
   1054
   1055	con->wq = create_singlethread_workqueue(name);
   1056	kfree(name);
   1057	if (!con->wq)
   1058		return -ENOMEM;
   1059
   1060	INIT_WORK(&con->work, ucsi_handle_connector_change);
   1061	init_completion(&con->complete);
   1062	mutex_init(&con->lock);
   1063	con->num = index + 1;
   1064	con->ucsi = ucsi;
   1065
   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",
   1070			con->num);
   1071		return PTR_ERR(con->usb_role_sw);
   1072	}
   1073
   1074	/* Delay other interactions with the con until registration is complete */
   1075	mutex_lock(&con->lock);
   1076
   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));
   1081	if (ret < 0)
   1082		goto out_unlock;
   1083
   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;
   1090
   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;
   1098
   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;
   1103
   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;
   1108
   1109	cap->driver_data = con;
   1110	cap->ops = &ucsi_ops;
   1111
   1112	ret = ucsi_register_port_psy(con);
   1113	if (ret)
   1114		goto out;
   1115
   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);
   1120		goto out;
   1121	}
   1122
   1123	/* Alternate modes */
   1124	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
   1125	if (ret) {
   1126		dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
   1127			con->num);
   1128		goto out;
   1129	}
   1130
   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));
   1134	if (ret < 0) {
   1135		dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
   1136		ret = 0;
   1137		goto out;
   1138	}
   1139	ret = 0; /* ucsi_send_command() returns length on success */
   1140
   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;
   1145		fallthrough;
   1146	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
   1147		typec_set_data_role(con->port, TYPEC_HOST);
   1148		break;
   1149	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
   1150		u_role = USB_ROLE_DEVICE;
   1151		typec_set_data_role(con->port, TYPEC_DEVICE);
   1152		break;
   1153	default:
   1154		break;
   1155	}
   1156
   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);
   1164	}
   1165
   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;
   1169
   1170	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
   1171	if (ret) {
   1172		dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
   1173			con->num, u_role);
   1174		ret = 0;
   1175	}
   1176
   1177	if (con->partner &&
   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);
   1182	}
   1183
   1184	trace_ucsi_register_port(con->num, &con->status);
   1185
   1186out:
   1187	fwnode_handle_put(cap->fwnode);
   1188out_unlock:
   1189	mutex_unlock(&con->lock);
   1190
   1191	if (ret && con->wq) {
   1192		destroy_workqueue(con->wq);
   1193		con->wq = NULL;
   1194	}
   1195
   1196	return ret;
   1197}
   1198
   1199static void ucsi_unregister_connectors(struct ucsi *ucsi)
   1200{
   1201	struct ucsi_connector *con;
   1202	int i;
   1203
   1204	if (!ucsi->connector)
   1205		return;
   1206
   1207	for (i = 0; i < ucsi->cap.num_connectors; i++) {
   1208		con = &ucsi->connector[i];
   1209
   1210		if (!con->wq)
   1211			break;
   1212
   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);
   1219	}
   1220
   1221	kfree(ucsi->connector);
   1222	ucsi->connector = NULL;
   1223}
   1224
   1225/**
   1226 * ucsi_init - Initialize UCSI interface
   1227 * @ucsi: UCSI to be initialized
   1228 *
   1229 * Registers all ports @ucsi has and enables all notification events.
   1230 */
   1231static int ucsi_init(struct ucsi *ucsi)
   1232{
   1233	u64 command;
   1234	int ret;
   1235	int i;
   1236
   1237	/* Reset the PPM */
   1238	ret = ucsi_reset_ppm(ucsi);
   1239	if (ret) {
   1240		dev_err(ucsi->dev, "failed to reset PPM!\n");
   1241		goto err;
   1242	}
   1243
   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);
   1248	if (ret < 0)
   1249		goto err_reset;
   1250
   1251	/* Get PPM capabilities */
   1252	command = UCSI_GET_CAPABILITY;
   1253	ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
   1254	if (ret < 0)
   1255		goto err_reset;
   1256
   1257	if (!ucsi->cap.num_connectors) {
   1258		ret = -ENODEV;
   1259		goto err_reset;
   1260	}
   1261
   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) {
   1266		ret = -ENOMEM;
   1267		goto err_reset;
   1268	}
   1269
   1270	/* Register all connectors */
   1271	for (i = 0; i < ucsi->cap.num_connectors; i++) {
   1272		ret = ucsi_register_port(ucsi, i);
   1273		if (ret)
   1274			goto err_unregister;
   1275	}
   1276
   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);
   1281	if (ret < 0)
   1282		goto err_unregister;
   1283
   1284	return 0;
   1285
   1286err_unregister:
   1287	ucsi_unregister_connectors(ucsi);
   1288
   1289err_reset:
   1290	memset(&ucsi->cap, 0, sizeof(ucsi->cap));
   1291	ucsi_reset_ppm(ucsi);
   1292err:
   1293	return ret;
   1294}
   1295
   1296static void ucsi_init_work(struct work_struct *work)
   1297{
   1298	struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
   1299	int ret;
   1300
   1301	ret = ucsi_init(ucsi);
   1302	if (ret)
   1303		dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
   1304
   1305	if (ret == -EPROBE_DEFER) {
   1306		if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
   1307			return;
   1308
   1309		queue_delayed_work(system_long_wq, &ucsi->work,
   1310				   UCSI_ROLE_SWITCH_INTERVAL);
   1311	}
   1312}
   1313
   1314/**
   1315 * ucsi_get_drvdata - Return private driver data pointer
   1316 * @ucsi: UCSI interface
   1317 */
   1318void *ucsi_get_drvdata(struct ucsi *ucsi)
   1319{
   1320	return ucsi->driver_data;
   1321}
   1322EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
   1323
   1324/**
   1325 * ucsi_set_drvdata - Assign private driver data pointer
   1326 * @ucsi: UCSI interface
   1327 * @data: Private data pointer
   1328 */
   1329void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
   1330{
   1331	ucsi->driver_data = data;
   1332}
   1333EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
   1334
   1335/**
   1336 * ucsi_create - Allocate UCSI instance
   1337 * @dev: Device interface to the PPM (Platform Policy Manager)
   1338 * @ops: I/O routines
   1339 */
   1340struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
   1341{
   1342	struct ucsi *ucsi;
   1343
   1344	if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
   1345		return ERR_PTR(-EINVAL);
   1346
   1347	ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
   1348	if (!ucsi)
   1349		return ERR_PTR(-ENOMEM);
   1350
   1351	INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
   1352	mutex_init(&ucsi->ppm_lock);
   1353	ucsi->dev = dev;
   1354	ucsi->ops = ops;
   1355
   1356	return ucsi;
   1357}
   1358EXPORT_SYMBOL_GPL(ucsi_create);
   1359
   1360/**
   1361 * ucsi_destroy - Free UCSI instance
   1362 * @ucsi: UCSI instance to be freed
   1363 */
   1364void ucsi_destroy(struct ucsi *ucsi)
   1365{
   1366	kfree(ucsi);
   1367}
   1368EXPORT_SYMBOL_GPL(ucsi_destroy);
   1369
   1370/**
   1371 * ucsi_register - Register UCSI interface
   1372 * @ucsi: UCSI instance
   1373 */
   1374int ucsi_register(struct ucsi *ucsi)
   1375{
   1376	int ret;
   1377
   1378	ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
   1379			      sizeof(ucsi->version));
   1380	if (ret)
   1381		return ret;
   1382
   1383	if (!ucsi->version)
   1384		return -ENODEV;
   1385
   1386	queue_delayed_work(system_long_wq, &ucsi->work, 0);
   1387
   1388	return 0;
   1389}
   1390EXPORT_SYMBOL_GPL(ucsi_register);
   1391
   1392/**
   1393 * ucsi_unregister - Unregister UCSI interface
   1394 * @ucsi: UCSI interface to be unregistered
   1395 *
   1396 * Unregister UCSI interface that was created with ucsi_register().
   1397 */
   1398void ucsi_unregister(struct ucsi *ucsi)
   1399{
   1400	u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
   1401
   1402	/* Make sure that we are not in the middle of driver initialization */
   1403	cancel_delayed_work_sync(&ucsi->work);
   1404
   1405	/* Disable notifications */
   1406	ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
   1407
   1408	ucsi_unregister_connectors(ucsi);
   1409}
   1410EXPORT_SYMBOL_GPL(ucsi_unregister);
   1411
   1412MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
   1413MODULE_LICENSE("GPL v2");
   1414MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");