Skip to content

Commit

Permalink
usb: typec: ucsi: extract common code for command handling
Browse files Browse the repository at this point in the history
Extract common functions to handle command sending and to handle events
from UCSI. This ensures that all UCSI glue drivers handle the ACKs in
the same way.

The CCG driver used DEV_CMD_PENDING both for internal
firmware-related commands and for UCSI control handling. Leave the
former use case intact.

Tested-by: Heikki Krogerus <[email protected]>
Reviewed-by: Heikki Krogerus <[email protected]>
Signed-off-by: Dmitry Baryshkov <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
lumag authored and gregkh committed Jul 3, 2024
1 parent e1870c1 commit 584e8df
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 203 deletions.
43 changes: 43 additions & 0 deletions drivers/usb/typec/ucsi/ucsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,48 @@
*/
#define UCSI_SWAP_TIMEOUT_MS 5000

void ucsi_notify_common(struct ucsi *ucsi, u32 cci)
{
if (UCSI_CCI_CONNECTOR(cci))
ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));

if (cci & UCSI_CCI_ACK_COMPLETE &&
test_bit(ACK_PENDING, &ucsi->flags))
complete(&ucsi->complete);

if (cci & UCSI_CCI_COMMAND_COMPLETE &&
test_bit(COMMAND_PENDING, &ucsi->flags))
complete(&ucsi->complete);
}
EXPORT_SYMBOL_GPL(ucsi_notify_common);

int ucsi_sync_control_common(struct ucsi *ucsi, u64 command)
{
bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
int ret;

if (ack)
set_bit(ACK_PENDING, &ucsi->flags);
else
set_bit(COMMAND_PENDING, &ucsi->flags);

ret = ucsi->ops->async_control(ucsi, command);
if (ret)
goto out_clear_bit;

if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
ret = -ETIMEDOUT;

out_clear_bit:
if (ack)
clear_bit(ACK_PENDING, &ucsi->flags);
else
clear_bit(COMMAND_PENDING, &ucsi->flags);

return ret;
}
EXPORT_SYMBOL_GPL(ucsi_sync_control_common);

static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
{
u64 ctrl;
Expand Down Expand Up @@ -1919,6 +1961,7 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
mutex_init(&ucsi->ppm_lock);
init_completion(&ucsi->complete);
ucsi->dev = dev;
ucsi->ops = ops;

Expand Down
7 changes: 7 additions & 0 deletions drivers/usb/typec/ucsi/ucsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#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>
Expand Down Expand Up @@ -425,6 +426,9 @@ struct ucsi {
/* PPM communication flags */
unsigned long flags;
#define EVENT_PENDING 0
#define COMMAND_PENDING 1
#define ACK_PENDING 2
struct completion complete;

unsigned long quirks;
#define UCSI_NO_PARTNER_PDOS BIT(0) /* Don't read partner's PDOs */
Expand Down Expand Up @@ -489,6 +493,9 @@ int ucsi_send_command(struct ucsi *ucsi, u64 command,
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);
Expand Down
56 changes: 8 additions & 48 deletions drivers/usb/typec/ucsi/ucsi_acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ struct ucsi_acpi {
struct device *dev;
struct ucsi *ucsi;
void *base;
struct completion complete;
unsigned long flags;
#define UCSI_ACPI_COMMAND_PENDING 1
#define UCSI_ACPI_ACK_PENDING 2
#define UCSI_ACPI_CHECK_BOGUS_EVENT 3
bool check_bogus_event;
guid_t guid;
u64 cmd;
};
Expand Down Expand Up @@ -98,38 +94,11 @@ static int ucsi_acpi_async_control(struct ucsi *ucsi, u64 command)
return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
}

static int ucsi_acpi_sync_control(struct ucsi *ucsi, u64 command)
{
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
int ret;

if (ack)
set_bit(UCSI_ACPI_ACK_PENDING, &ua->flags);
else
set_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags);

ret = ucsi_acpi_async_control(ucsi, command);
if (ret)
goto out_clear_bit;

if (!wait_for_completion_timeout(&ua->complete, 5 * HZ))
ret = -ETIMEDOUT;

out_clear_bit:
if (ack)
clear_bit(UCSI_ACPI_ACK_PENDING, &ua->flags);
else
clear_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags);

return ret;
}

static const struct ucsi_operations ucsi_acpi_ops = {
.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,
.sync_control = ucsi_sync_control_common,
.async_control = ucsi_acpi_async_control
};

Expand Down Expand Up @@ -165,7 +134,7 @@ static const struct ucsi_operations ucsi_zenbook_ops = {
.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,
.sync_control = ucsi_sync_control_common,
.async_control = ucsi_acpi_async_control
};

Expand All @@ -182,14 +151,14 @@ static int ucsi_gram_read_message_in(struct ucsi *ucsi, void *val, size_t val_le
return ret;

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

/* Clear the bogus change */
if (status->change == bogus_change)
status->change = 0;

clear_bit(UCSI_ACPI_CHECK_BOGUS_EVENT, &ua->flags);
ua->check_bogus_event = false;
}

return ret;
Expand All @@ -200,14 +169,14 @@ static int ucsi_gram_sync_control(struct ucsi *ucsi, u64 command)
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
int ret;

ret = ucsi_acpi_sync_control(ucsi, command);
ret = ucsi_sync_control_common(ucsi, command);
if (ret < 0)
return ret;

if (UCSI_COMMAND(ua->cmd) == UCSI_GET_PDOS &&
ua->cmd & UCSI_GET_PDOS_PARTNER_PDO(1) &&
ua->cmd & UCSI_GET_PDOS_SRC_PDOS)
set_bit(UCSI_ACPI_CHECK_BOGUS_EVENT, &ua->flags);
ua->check_bogus_event = true;

return ret;
}
Expand Down Expand Up @@ -249,15 +218,7 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
if (ret)
return;

if (UCSI_CCI_CONNECTOR(cci))
ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci));

if (cci & UCSI_CCI_ACK_COMPLETE &&
test_bit(UCSI_ACPI_ACK_PENDING, &ua->flags))
complete(&ua->complete);
if (cci & UCSI_CCI_COMMAND_COMPLETE &&
test_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags))
complete(&ua->complete);
ucsi_notify_common(ua->ucsi, cci);
}

static int ucsi_acpi_probe(struct platform_device *pdev)
Expand Down Expand Up @@ -291,7 +252,6 @@ static int ucsi_acpi_probe(struct platform_device *pdev)
if (ret)
return ret;

init_completion(&ua->complete);
ua->dev = &pdev->dev;

id = dmi_first_match(ucsi_acpi_quirks);
Expand Down
21 changes: 3 additions & 18 deletions drivers/usb/typec/ucsi/ucsi_ccg.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ struct ucsi_ccg {
u16 fw_build;
struct work_struct pm_work;

struct completion complete;

u64 last_cmd_sent;
bool has_multiple_dp;
struct ucsi_ccg_altmode orig[UCSI_MAX_ALTMODES];
Expand Down Expand Up @@ -637,7 +635,6 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command)

mutex_lock(&uc->lock);
pm_runtime_get_sync(uc->dev);
set_bit(DEV_CMD_PENDING, &uc->flags);

uc->last_cmd_sent = command;

Expand All @@ -649,15 +646,8 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command)
ucsi_ccg_update_set_new_cam_cmd(uc, con, &command);
}

ret = ucsi_ccg_async_control(ucsi, command);
if (ret)
goto err_clear_bit;

if (!wait_for_completion_timeout(&uc->complete, msecs_to_jiffies(5000)))
ret = -ETIMEDOUT;
ret = ucsi_sync_control_common(ucsi, command);

err_clear_bit:
clear_bit(DEV_CMD_PENDING, &uc->flags);
pm_runtime_put_sync(uc->dev);
mutex_unlock(&uc->lock);

Expand Down Expand Up @@ -694,9 +684,6 @@ static irqreturn_t ccg_irq_handler(int irq, void *data)
if (ret)
goto err_clear_irq;

if (UCSI_CCI_CONNECTOR(cci))
ucsi_connector_change(uc->ucsi, UCSI_CCI_CONNECTOR(cci));

/*
* As per CCGx UCSI interface guide, copy CCI and MESSAGE_IN
* to the OpRegion before clear the UCSI interrupt
Expand All @@ -708,9 +695,8 @@ static irqreturn_t ccg_irq_handler(int irq, void *data)
err_clear_irq:
ccg_write(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg));

if (!ret && test_bit(DEV_CMD_PENDING, &uc->flags) &&
cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
complete(&uc->complete);
if (!ret)
ucsi_notify_common(uc->ucsi, cci);

return IRQ_HANDLED;
}
Expand Down Expand Up @@ -1429,7 +1415,6 @@ static int ucsi_ccg_probe(struct i2c_client *client)
uc->client = client;
uc->irq = client->irq;
mutex_init(&uc->lock);
init_completion(&uc->complete);
INIT_WORK(&uc->work, ccg_update_firmware);
INIT_WORK(&uc->pm_work, ccg_pm_workaround_work);

Expand Down
47 changes: 2 additions & 45 deletions drivers/usb/typec/ucsi/ucsi_glink.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ struct pmic_glink_ucsi {
struct ucsi *ucsi;
struct completion read_ack;
struct completion write_ack;
struct completion sync_ack;
bool sync_pending;
struct mutex lock; /* protects concurrent access to PMIC Glink interface */

int sync_val;

struct work_struct notify_work;
struct work_struct register_work;

Expand Down Expand Up @@ -170,35 +166,6 @@ static int pmic_glink_ucsi_async_control(struct ucsi *__ucsi, u64 command)
return ret;
}

static int pmic_glink_ucsi_sync_control(struct ucsi *__ucsi, u64 command)
{
struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi);
unsigned long left;
int ret;

/* TOFIX: Downstream forces recipient to CON when UCSI_GET_ALTERNATE_MODES command */

mutex_lock(&ucsi->lock);
ucsi->sync_val = 0;
reinit_completion(&ucsi->sync_ack);
ucsi->sync_pending = true;
ret = pmic_glink_ucsi_locked_write(ucsi, UCSI_CONTROL, &command, sizeof(command));
mutex_unlock(&ucsi->lock);

left = wait_for_completion_timeout(&ucsi->sync_ack, 5 * HZ);
if (!left) {
dev_err(ucsi->dev, "timeout waiting for UCSI sync write response\n");
/* return 0 here and let core UCSI code handle the CCI_BUSY */
ret = 0;
} else if (ucsi->sync_val) {
dev_err(ucsi->dev, "sync write returned: %d\n", ucsi->sync_val);
}

ucsi->sync_pending = false;

return ret;
}

static void pmic_glink_ucsi_update_connector(struct ucsi_connector *con)
{
struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(con->ucsi);
Expand Down Expand Up @@ -232,7 +199,7 @@ static const struct ucsi_operations pmic_glink_ucsi_ops = {
.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,
.sync_control = ucsi_sync_control_common,
.async_control = pmic_glink_ucsi_async_control,
.update_connector = pmic_glink_ucsi_update_connector,
.connector_status = pmic_glink_ucsi_connector_status,
Expand All @@ -256,14 +223,12 @@ static void pmic_glink_ucsi_write_ack(struct pmic_glink_ucsi *ucsi, const void *
if (resp->ret_code)
return;

ucsi->sync_val = resp->ret_code;
complete(&ucsi->write_ack);
}

static void pmic_glink_ucsi_notify(struct work_struct *work)
{
struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, notify_work);
unsigned int con_num;
u32 cci;
int ret;

Expand All @@ -273,14 +238,7 @@ static void pmic_glink_ucsi_notify(struct work_struct *work)
return;
}

con_num = UCSI_CCI_CONNECTOR(cci);
if (con_num)
ucsi_connector_change(ucsi->ucsi, con_num);

if (ucsi->sync_pending &&
(cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))) {
complete(&ucsi->sync_ack);
}
ucsi_notify_common(ucsi->ucsi, cci);
}

static void pmic_glink_ucsi_register(struct work_struct *work)
Expand Down Expand Up @@ -362,7 +320,6 @@ static int pmic_glink_ucsi_probe(struct auxiliary_device *adev,
INIT_WORK(&ucsi->register_work, pmic_glink_ucsi_register);
init_completion(&ucsi->read_ack);
init_completion(&ucsi->write_ack);
init_completion(&ucsi->sync_ack);
mutex_init(&ucsi->lock);

ucsi->ucsi = ucsi_create(dev, &pmic_glink_ucsi_ops);
Expand Down
Loading

0 comments on commit 584e8df

Please sign in to comment.