Skip to content

Commit

Permalink
usb: typec: ucsi: split read operation
Browse files Browse the repository at this point in the history
The read operation is only used to read fixed data at fixed offsets
(UCSI_VERSION, UCSI_CCI, UCSI_MESSAGE_IN). In some cases drivers apply
offset-specific overrides. Split the read() operation into three
operations, read_version(), read_cci(), read_message_in().

Tested-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
Link: https://lore.kernel.org/r/20240627-ucsi-rework-interface-v4-3-289ddc6874c7@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Dmitry Baryshkov authored and Greg Kroah-Hartman committed Jul 3, 2024
1 parent 13f2ec3 commit 467399d
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 72 deletions.
20 changes: 10 additions & 10 deletions drivers/usb/typec/ucsi/ucsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static int ucsi_read_message_in(struct ucsi *ucsi, void *buf,
if (ucsi->version <= UCSI_VERSION_1_2)
buf_size = clamp(buf_size, 0, 16);

return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size);
return ucsi->ops->read_message_in(ucsi, buf, buf_size);
}

static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
Expand Down Expand Up @@ -159,7 +159,7 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
if (ret)
return ret;

ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
ret = ucsi->ops->read_cci(ucsi, &cci);
if (ret)
return ret;

Expand Down Expand Up @@ -1338,7 +1338,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)

mutex_lock(&ucsi->ppm_lock);

ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
ret = ucsi->ops->read_cci(ucsi, &cci);
if (ret < 0)
goto out;

Expand All @@ -1356,8 +1356,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)

tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
do {
ret = ucsi->ops->read(ucsi, UCSI_CCI,
&cci, sizeof(cci));
ret = ucsi->ops->read_cci(ucsi, &cci);
if (ret < 0)
goto out;
if (cci & UCSI_CCI_COMMAND_COMPLETE)
Expand Down Expand Up @@ -1386,7 +1385,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)
/* Give the PPM time to process a reset before reading CCI */
msleep(20);

ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
ret = ucsi->ops->read_cci(ucsi, &cci);
if (ret)
goto out;

Expand Down Expand Up @@ -1806,7 +1805,7 @@ static int ucsi_init(struct ucsi *ucsi)
ucsi->ntfy = ntfy;

mutex_lock(&ucsi->ppm_lock);
ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
ret = ucsi->ops->read_cci(ucsi, &cci);
mutex_unlock(&ucsi->ppm_lock);
if (ret)
return ret;
Expand Down Expand Up @@ -1920,7 +1919,9 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
{
struct ucsi *ucsi;

if (!ops || !ops->read || !ops->sync_control || !ops->async_control)
if (!ops ||
!ops->read_version || !ops->read_cci || !ops->read_message_in ||
!ops->sync_control || !ops->async_control)
return ERR_PTR(-EINVAL);

ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
Expand Down Expand Up @@ -1956,8 +1957,7 @@ int ucsi_register(struct ucsi *ucsi)
{
int ret;

ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
sizeof(ucsi->version));
ret = ucsi->ops->read_version(ucsi, &ucsi->version);
if (ret)
return ret;

Expand Down
9 changes: 6 additions & 3 deletions drivers/usb/typec/ucsi/ucsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ struct dentry;

/**
* struct ucsi_operations - UCSI I/O operations
* @read: Read operation
* @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
Expand All @@ -68,8 +70,9 @@ struct dentry;
* return immediately after sending the data to the PPM.
*/
struct ucsi_operations {
int (*read)(struct ucsi *ucsi, unsigned int offset,
void *val, size_t val_len);
int (*read_version)(struct ucsi *ucsi, u16 *version);
int (*read_cci)(struct ucsi *ucsi, u32 *cci);
int (*read_message_in)(struct ucsi *ucsi, void *val, size_t val_len);
int (*sync_control)(struct ucsi *ucsi, u64 command);
int (*async_control)(struct ucsi *ucsi, u64 command);
bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig,
Expand Down
72 changes: 57 additions & 15 deletions drivers/usb/typec/ucsi/ucsi_acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
return 0;
}

static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset,
void *val, size_t val_len)
static int ucsi_acpi_read_version(struct ucsi *ucsi, u16 *version)
{
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
int ret;
Expand All @@ -56,7 +55,35 @@ static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset,
if (ret)
return ret;

memcpy(val, ua->base + offset, val_len);
memcpy(version, ua->base + UCSI_VERSION, sizeof(*version));

return 0;
}

static int ucsi_acpi_read_cci(struct ucsi *ucsi, u32 *cci)
{
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
int ret;

ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
if (ret)
return ret;

memcpy(cci, ua->base + UCSI_CCI, sizeof(*cci));

return 0;
}

static int ucsi_acpi_read_message_in(struct ucsi *ucsi, void *val, size_t val_len)
{
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
int ret;

ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
if (ret)
return ret;

memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len);

return 0;
}
Expand Down Expand Up @@ -99,50 +126,63 @@ static int ucsi_acpi_sync_control(struct ucsi *ucsi, u64 command)
}

static const struct ucsi_operations ucsi_acpi_ops = {
.read = ucsi_acpi_read,
.read_version = ucsi_acpi_read_version,
.read_cci = ucsi_acpi_read_cci,
.read_message_in = ucsi_acpi_read_message_in,
.sync_control = ucsi_acpi_sync_control,
.async_control = ucsi_acpi_async_control
};

static int
ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len)
ucsi_zenbook_read_cci(struct ucsi *ucsi, u32 *cci)
{
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
int ret;

if (offset == UCSI_VERSION || UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) {
if (UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) {
ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
if (ret)
return ret;
}

memcpy(val, ua->base + offset, val_len);
memcpy(cci, ua->base + UCSI_CCI, sizeof(*cci));

return 0;
}

static int
ucsi_zenbook_read_message_in(struct ucsi *ucsi, void *val, size_t val_len)
{
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);

/* UCSI_MESSAGE_IN is never read for PPM_RESET, return stored data */
memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len);

return 0;
}

static const struct ucsi_operations ucsi_zenbook_ops = {
.read = ucsi_zenbook_read,
.read_version = ucsi_acpi_read_version,
.read_cci = ucsi_zenbook_read_cci,
.read_message_in = ucsi_zenbook_read_message_in,
.sync_control = ucsi_acpi_sync_control,
.async_control = ucsi_acpi_async_control
};

static int ucsi_gram_read(struct ucsi *ucsi, unsigned int offset,
void *val, size_t val_len)
static int ucsi_gram_read_message_in(struct ucsi *ucsi, void *val, size_t val_len)
{
u16 bogus_change = UCSI_CONSTAT_POWER_LEVEL_CHANGE |
UCSI_CONSTAT_PDOS_CHANGE;
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
struct ucsi_connector_status *status;
int ret;

ret = ucsi_acpi_read(ucsi, offset, val, val_len);
ret = ucsi_acpi_read_message_in(ucsi, val, val_len);
if (ret < 0)
return ret;

if (UCSI_COMMAND(ua->cmd) == UCSI_GET_CONNECTOR_STATUS &&
test_bit(UCSI_ACPI_CHECK_BOGUS_EVENT, &ua->flags) &&
offset == UCSI_MESSAGE_IN) {
test_bit(UCSI_ACPI_CHECK_BOGUS_EVENT, &ua->flags)) {
status = (struct ucsi_connector_status *)val;

/* Clear the bogus change */
Expand Down Expand Up @@ -173,7 +213,9 @@ static int ucsi_gram_sync_control(struct ucsi *ucsi, u64 command)
}

static const struct ucsi_operations ucsi_gram_ops = {
.read = ucsi_gram_read,
.read_version = ucsi_acpi_read_version,
.read_cci = ucsi_acpi_read_cci,
.read_message_in = ucsi_gram_read_message_in,
.sync_control = ucsi_gram_sync_control,
.async_control = ucsi_acpi_async_control
};
Expand Down Expand Up @@ -203,7 +245,7 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
u32 cci;
int ret;

ret = ua->ucsi->ops->read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci));
ret = ua->ucsi->ops->read_cci(ua->ucsi, &cci);
if (ret)
return;

Expand Down
50 changes: 27 additions & 23 deletions drivers/usb/typec/ucsi/ucsi_ccg.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,32 +556,34 @@ static void ucsi_ccg_nvidia_altmode(struct ucsi_ccg *uc,
}
}

static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset,
void *val, size_t val_len)
static int ucsi_ccg_read_version(struct ucsi *ucsi, u16 *version)
{
struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset);
struct ucsi_capability *cap;
struct ucsi_altmode *alt;
int ret = 0;
u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_VERSION);

if (offset == UCSI_CCI) {
spin_lock(&uc->op_lock);
memcpy(val, &(uc->op_data).cci, val_len);
spin_unlock(&uc->op_lock);
} else if (offset == UCSI_MESSAGE_IN) {
spin_lock(&uc->op_lock);
memcpy(val, &(uc->op_data).message_in, val_len);
spin_unlock(&uc->op_lock);
} else {
ret = ccg_read(uc, reg, val, val_len);
}
return ccg_read(uc, reg, (u8 *)version, sizeof(*version));
}

if (ret)
return ret;
static int ucsi_ccg_read_cci(struct ucsi *ucsi, u32 *cci)
{
struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);

if (offset != UCSI_MESSAGE_IN)
return ret;
spin_lock(&uc->op_lock);
*cci = uc->op_data.cci;
spin_unlock(&uc->op_lock);

return 0;
}

static int ucsi_ccg_read_message_in(struct ucsi *ucsi, void *val, size_t val_len)
{
struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
struct ucsi_capability *cap;
struct ucsi_altmode *alt;

spin_lock(&uc->op_lock);
memcpy(val, uc->op_data.message_in, val_len);
spin_unlock(&uc->op_lock);

switch (UCSI_COMMAND(uc->last_cmd_sent)) {
case UCSI_GET_CURRENT_CAM:
Expand All @@ -607,7 +609,7 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset,
}
uc->last_cmd_sent = 0;

return ret;
return 0;
}

static int ucsi_ccg_async_control(struct ucsi *ucsi, u64 command)
Expand Down Expand Up @@ -663,7 +665,9 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command)
}

static const struct ucsi_operations ucsi_ccg_ops = {
.read = ucsi_ccg_read,
.read_version = ucsi_ccg_read_version,
.read_cci = ucsi_ccg_read_cci,
.read_message_in = ucsi_ccg_read_message_in,
.sync_control = ucsi_ccg_sync_control,
.async_control = ucsi_ccg_async_control,
.update_altmodes = ucsi_ccg_update_altmodes
Expand Down
19 changes: 18 additions & 1 deletion drivers/usb/typec/ucsi/ucsi_glink.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset,
return ret;
}

static int pmic_glink_ucsi_read_version(struct ucsi *ucsi, u16 *version)
{
return pmic_glink_ucsi_read(ucsi, UCSI_VERSION, version, sizeof(*version));
}

static int pmic_glink_ucsi_read_cci(struct ucsi *ucsi, u32 *cci)
{
return pmic_glink_ucsi_read(ucsi, UCSI_CCI, cci, sizeof(*cci));
}

static int pmic_glink_ucsi_read_message_in(struct ucsi *ucsi, void *val, size_t val_len)
{
return pmic_glink_ucsi_read(ucsi, UCSI_MESSAGE_IN, val, val_len);
}

static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset,
const void *val, size_t val_len)
{
Expand Down Expand Up @@ -214,7 +229,9 @@ static void pmic_glink_ucsi_connector_status(struct ucsi_connector *con)
}

static const struct ucsi_operations pmic_glink_ucsi_ops = {
.read = pmic_glink_ucsi_read,
.read_version = pmic_glink_ucsi_read_version,
.read_cci = pmic_glink_ucsi_read_cci,
.read_message_in = pmic_glink_ucsi_read_message_in,
.sync_control = pmic_glink_ucsi_sync_control,
.async_control = pmic_glink_ucsi_async_control,
.update_connector = pmic_glink_ucsi_update_connector,
Expand Down
19 changes: 18 additions & 1 deletion drivers/usb/typec/ucsi/ucsi_stm32g0.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val,
return 0;
}

static int ucsi_stm32g0_read_version(struct ucsi *ucsi, u16 *version)
{
return ucsi_stm32g0_read(ucsi, UCSI_VERSION, version, sizeof(*version));
}

static int ucsi_stm32g0_read_cci(struct ucsi *ucsi, u32 *cci)
{
return ucsi_stm32g0_read(ucsi, UCSI_CCI, cci, sizeof(*cci));
}

static int ucsi_stm32g0_read_message_in(struct ucsi *ucsi, void *val, size_t len)
{
return ucsi_stm32g0_read(ucsi, UCSI_MESSAGE_IN, val, len);
}

static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command)
{
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
Expand Down Expand Up @@ -446,7 +461,9 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data)
}

static const struct ucsi_operations ucsi_stm32g0_ops = {
.read = ucsi_stm32g0_read,
.read_version = ucsi_stm32g0_read_version,
.read_cci = ucsi_stm32g0_read_cci,
.read_message_in = ucsi_stm32g0_read_message_in,
.sync_control = ucsi_stm32g0_sync_control,
.async_control = ucsi_stm32g0_async_control,
};
Expand Down
Loading

0 comments on commit 467399d

Please sign in to comment.