linux/drivers/usb/typec/ucsi/ucsi.h

/* SPDX-License-Identifier: GPL-2.0 */

#ifndef __DRIVER_USB_TYPEC_UCSI_H
#define __DRIVER_USB_TYPEC_UCSI_H

#include <linux/bitops.h>
#include <linux/completion.h>
#include <linux/device.h>
#include <linux/power_supply.h>
#include <linux/types.h>
#include <linux/usb/typec.h>
#include <linux/usb/pd.h>
#include <linux/usb/role.h>
#include <asm/unaligned.h>

/* -------------------------------------------------------------------------- */

struct ucsi;
struct ucsi_altmode;
struct ucsi_connector;
struct dentry;

/* UCSI offsets (Bytes) */
#define UCSI_VERSION
#define UCSI_CCI
#define UCSI_CONTROL
#define UCSI_MESSAGE_IN
#define UCSI_MESSAGE_OUT
#define UCSIv2_MESSAGE_OUT

/* UCSI versions */
#define UCSI_VERSION_1_2
#define UCSI_VERSION_2_0
#define UCSI_VERSION_2_1
#define UCSI_VERSION_3_0

#define UCSI_BCD_GET_MAJOR(_v_)
#define UCSI_BCD_GET_MINOR(_v_)
#define UCSI_BCD_GET_SUBMINOR(_v_)

/*
 * Per USB PD 3.2, Section 6.2.1.1.5, the spec revision is represented by 2 bits
 * 0b00 = 1.0, 0b01 = 2.0, 0b10 = 3.0, 0b11 = Reserved, Shall NOT be used.
 */
#define UCSI_SPEC_REVISION_TO_BCD(_v_)

/* Command Status and Connector Change Indication (CCI) bits */
#define UCSI_CCI_CONNECTOR(_c_)
#define UCSI_CCI_LENGTH(_c_)
#define UCSI_CCI_NOT_SUPPORTED
#define UCSI_CCI_CANCEL_COMPLETE
#define UCSI_CCI_RESET_COMPLETE
#define UCSI_CCI_BUSY
#define UCSI_CCI_ACK_COMPLETE
#define UCSI_CCI_ERROR
#define UCSI_CCI_COMMAND_COMPLETE

/**
 * struct ucsi_operations - UCSI I/O operations
 * @read_version: Read implemented UCSI version
 * @read_cci: Read CCI register
 * @read_message_in: Read message data from UCSI
 * @sync_control: Blocking control operation
 * @async_control: Non-blocking control operation
 * @update_altmodes: Squashes duplicate DP altmodes
 * @update_connector: Update connector capabilities before registering
 * @connector_status: Updates connector status, called holding connector lock
 *
 * Read and write routines for UCSI interface. @sync_write must wait for the
 * Command Completion Event from the PPM before returning, and @async_write must
 * return immediately after sending the data to the PPM.
 */
struct ucsi_operations {};

struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops);
void ucsi_destroy(struct ucsi *ucsi);
int ucsi_register(struct ucsi *ucsi);
void ucsi_unregister(struct ucsi *ucsi);
void *ucsi_get_drvdata(struct ucsi *ucsi);
void ucsi_set_drvdata(struct ucsi *ucsi, void *data);

void ucsi_connector_change(struct ucsi *ucsi, u8 num);

/* -------------------------------------------------------------------------- */

/* Commands */
#define UCSI_PPM_RESET
#define UCSI_CANCEL
#define UCSI_CONNECTOR_RESET
#define UCSI_ACK_CC_CI
#define UCSI_SET_NOTIFICATION_ENABLE
#define UCSI_GET_CAPABILITY
#define UCSI_GET_CONNECTOR_CAPABILITY
#define UCSI_SET_UOM
#define UCSI_SET_UOR
#define UCSI_SET_PDM
#define UCSI_SET_PDR
#define UCSI_GET_ALTERNATE_MODES
#define UCSI_GET_CAM_SUPPORTED
#define UCSI_GET_CURRENT_CAM
#define UCSI_SET_NEW_CAM
#define UCSI_GET_PDOS
#define UCSI_GET_CABLE_PROPERTY
#define UCSI_GET_CONNECTOR_STATUS
#define UCSI_GET_ERROR_STATUS
#define UCSI_GET_PD_MESSAGE

#define UCSI_CONNECTOR_NUMBER(_num_)
#define UCSI_COMMAND(_cmd_)

#define UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(_cmd_)
#define UCSI_DEFAULT_GET_CONNECTOR_NUMBER(_cmd_)

/* CONNECTOR_RESET command bits */
#define UCSI_CONNECTOR_RESET_HARD

/* ACK_CC_CI bits */
#define UCSI_ACK_CONNECTOR_CHANGE
#define UCSI_ACK_COMMAND_COMPLETE

/* SET_NOTIFICATION_ENABLE command bits */
#define UCSI_ENABLE_NTFY_CMD_COMPLETE
#define UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE
#define UCSI_ENABLE_NTFY_PWR_OPMODE_CHANGE
#define UCSI_ENABLE_NTFY_ATTENTION
#define UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ
#define UCSI_ENABLE_NTFY_CAP_CHANGE
#define UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE
#define UCSI_ENABLE_NTFY_PD_RESET_COMPLETE
#define UCSI_ENABLE_NTFY_CAM_CHANGE
#define UCSI_ENABLE_NTFY_BAT_STATUS_CHANGE
#define UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER
#define UCSI_ENABLE_NTFY_PARTNER_CHANGE
#define UCSI_ENABLE_NTFY_PWR_DIR_CHANGE
#define UCSI_ENABLE_NTFY_SET_RETIMER_MODE
#define UCSI_ENABLE_NTFY_CONNECTOR_CHANGE
#define UCSI_ENABLE_NTFY_ERROR
#define UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE
#define UCSI_ENABLE_NTFY_ALL

/* SET_UOR command bits */
#define UCSI_SET_UOR_ROLE(_r_)
#define UCSI_SET_UOR_ACCEPT_ROLE_SWAPS

/* SET_PDF command bits */
#define UCSI_SET_PDR_ROLE(_r_)
#define UCSI_SET_PDR_ACCEPT_ROLE_SWAPS

/* GET_ALTERNATE_MODES command bits */
#define UCSI_ALTMODE_RECIPIENT(_r_)
#define UCSI_GET_ALTMODE_RECIPIENT(_r_)
#define UCSI_RECIPIENT_CON
#define UCSI_RECIPIENT_SOP
#define UCSI_RECIPIENT_SOP_P
#define UCSI_RECIPIENT_SOP_PP
#define UCSI_GET_ALTMODE_CONNECTOR_NUMBER(_r_)
#define UCSI_ALTMODE_OFFSET(_r_)
#define UCSI_GET_ALTMODE_OFFSET(_r_)
#define UCSI_GET_ALTMODE_NUM_ALTMODES(_r_)

/* GET_PDOS command bits */
#define UCSI_GET_PDOS_PARTNER_PDO(_r_)
#define UCSI_GET_PDOS_PDO_OFFSET(_r_)
#define UCSI_GET_PDOS_NUM_PDOS(_r_)
#define UCSI_MAX_PDOS
#define UCSI_GET_PDOS_SRC_PDOS

/* GET_PD_MESSAGE command bits */
#define UCSI_GET_PD_MESSAGE_RECIPIENT(_r_)
#define UCSI_GET_PD_MESSAGE_OFFSET(_r_)
#define UCSI_GET_PD_MESSAGE_BYTES(_r_)
#define UCSI_GET_PD_MESSAGE_TYPE(_r_)
#define UCSI_GET_PD_MESSAGE_TYPE_SNK_CAP_EXT
#define UCSI_GET_PD_MESSAGE_TYPE_SRC_CAP_EXT
#define UCSI_GET_PD_MESSAGE_TYPE_BAT_CAP
#define UCSI_GET_PD_MESSAGE_TYPE_BAT_STAT
#define UCSI_GET_PD_MESSAGE_TYPE_IDENTITY
#define UCSI_GET_PD_MESSAGE_TYPE_REVISION

/* -------------------------------------------------------------------------- */

/* Error information returned by PPM in response to GET_ERROR_STATUS command. */
#define UCSI_ERROR_UNREGONIZED_CMD
#define UCSI_ERROR_INVALID_CON_NUM
#define UCSI_ERROR_INVALID_CMD_ARGUMENT
#define UCSI_ERROR_INCOMPATIBLE_PARTNER
#define UCSI_ERROR_CC_COMMUNICATION_ERR
#define UCSI_ERROR_DEAD_BATTERY
#define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL
#define UCSI_ERROR_OVERCURRENT
#define UCSI_ERROR_UNDEFINED
#define UCSI_ERROR_PARTNER_REJECTED_SWAP
#define UCSI_ERROR_HARD_RESET
#define UCSI_ERROR_PPM_POLICY_CONFLICT
#define UCSI_ERROR_SWAP_REJECTED
#define UCSI_ERROR_REVERSE_CURRENT_PROTECTION
#define UCSI_ERROR_SET_SINK_PATH_REJECTED

#define UCSI_SET_NEW_CAM_ENTER(x)
#define UCSI_SET_NEW_CAM_GET_AM(x)
#define UCSI_SET_NEW_CAM_AM_MASK
#define UCSI_SET_NEW_CAM_SET_AM(x)
#define UCSI_CMD_CONNECTOR_MASK

/* Data structure filled by PPM in response to GET_CAPABILITY command. */
struct ucsi_capability {} __packed;

/* Data structure filled by PPM in response to GET_CONNECTOR_CAPABILITY cmd. */
struct ucsi_connector_capability {} __packed;

struct ucsi_altmode {} __packed;

/* Data structure filled by PPM in response to GET_CABLE_PROPERTY command. */
struct ucsi_cable_property {} __packed;

/* Data structure filled by PPM in response to GET_CONNECTOR_STATUS command. */
struct ucsi_connector_status {} __packed;

/*
 * Data structure filled by PPM in response to GET_PD_MESSAGE command with the
 * Response Message Type set to Discover Identity Response.
 */
struct ucsi_pd_message_disc_id {} __packed;

/* -------------------------------------------------------------------------- */

struct ucsi_debugfs_entry {};

struct ucsi {};

#define UCSI_MAX_SVID
#define UCSI_MAX_ALTMODES

#define UCSI_TYPEC_VSAFE5V
#define UCSI_TYPEC_1_5_CURRENT
#define UCSI_TYPEC_3_0_CURRENT

struct ucsi_connector {};

int ucsi_send_command(struct ucsi *ucsi, u64 command,
		      void *retval, size_t size);

void ucsi_altmode_update_active(struct ucsi_connector *con);
int ucsi_resume(struct ucsi *ucsi);

void ucsi_notify_common(struct ucsi *ucsi, u32 cci);
int ucsi_sync_control_common(struct ucsi *ucsi, u64 command);

#if IS_ENABLED(CONFIG_POWER_SUPPLY)
int ucsi_register_port_psy(struct ucsi_connector *con);
void ucsi_unregister_port_psy(struct ucsi_connector *con);
void ucsi_port_psy_changed(struct ucsi_connector *con);
#else
static inline int ucsi_register_port_psy(struct ucsi_connector *con) { return 0; }
static inline void ucsi_unregister_port_psy(struct ucsi_connector *con) { }
static inline void ucsi_port_psy_changed(struct ucsi_connector *con) { }
#endif /* CONFIG_POWER_SUPPLY */

#if IS_ENABLED(CONFIG_TYPEC_DP_ALTMODE)
struct typec_altmode *
ucsi_register_displayport(struct ucsi_connector *con,
			  bool override, int offset,
			  struct typec_altmode_desc *desc);

void ucsi_displayport_remove_partner(struct typec_altmode *adev);

#else
static inline struct typec_altmode *
ucsi_register_displayport(struct ucsi_connector *con,
			  bool override, int offset,
			  struct typec_altmode_desc *desc)
{
	return typec_port_register_altmode(con->port, desc);
}

static inline void
ucsi_displayport_remove_partner(struct typec_altmode *adev) { }
#endif /* CONFIG_TYPEC_DP_ALTMODE */

#ifdef CONFIG_DEBUG_FS
void ucsi_debugfs_init(void);
void ucsi_debugfs_exit(void);
void ucsi_debugfs_register(struct ucsi *ucsi);
void ucsi_debugfs_unregister(struct ucsi *ucsi);
#else
static inline void ucsi_debugfs_init(void) { }
static inline void ucsi_debugfs_exit(void) { }
static inline void ucsi_debugfs_register(struct ucsi *ucsi) { }
static inline void ucsi_debugfs_unregister(struct ucsi *ucsi) { }
#endif /* CONFIG_DEBUG_FS */

/*
 * NVIDIA VirtualLink (svid 0x955) has two altmode. VirtualLink
 * DP mode with vdo=0x1 and NVIDIA test mode with vdo=0x3
 */
#define USB_TYPEC_NVIDIA_VLINK_DP_VDO
#define USB_TYPEC_NVIDIA_VLINK_DBG_VDO

#endif /* __DRIVER_USB_TYPEC_UCSI_H */