Skip to content

Commit 13f2ec3

Browse files
lumaggregkh
authored andcommitted
usb: typec: ucsi: simplify command sending API
The sync_write and async_write are used only for writing UCSI commands to the UCSI_CONTROL offsets. Rename sync_write and async_write operations to sync_control and async_control accordingly. Drop the offset and length fields and pass u64 command instead. 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]>
1 parent a7d2fa7 commit 13f2ec3

File tree

7 files changed

+64
-87
lines changed

7 files changed

+64
-87
lines changed

drivers/usb/typec/ucsi/ucsi.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
6060
ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
6161
}
6262

63-
return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
63+
return ucsi->ops->sync_control(ucsi, ctrl);
6464
}
6565

6666
static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
@@ -155,7 +155,7 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
155155
connector_num = 0;
156156
}
157157

158-
ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
158+
ret = ucsi->ops->sync_control(ucsi, cmd);
159159
if (ret)
160160
return ret;
161161

@@ -1350,8 +1350,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)
13501350
*/
13511351
if (cci & UCSI_CCI_RESET_COMPLETE) {
13521352
command = UCSI_SET_NOTIFICATION_ENABLE;
1353-
ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1354-
sizeof(command));
1353+
ret = ucsi->ops->async_control(ucsi, command);
13551354
if (ret < 0)
13561355
goto out;
13571356

@@ -1372,8 +1371,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)
13721371
}
13731372

13741373
command = UCSI_PPM_RESET;
1375-
ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1376-
sizeof(command));
1374+
ret = ucsi->ops->async_control(ucsi, command);
13771375
if (ret < 0)
13781376
goto out;
13791377

@@ -1394,9 +1392,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)
13941392

13951393
/* If the PPM is still doing something else, reset it again. */
13961394
if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1397-
ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1398-
&command,
1399-
sizeof(command));
1395+
ret = ucsi->ops->async_control(ucsi, command);
14001396
if (ret < 0)
14011397
goto out;
14021398
}
@@ -1924,7 +1920,7 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
19241920
{
19251921
struct ucsi *ucsi;
19261922

1927-
if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1923+
if (!ops || !ops->read || !ops->sync_control || !ops->async_control)
19281924
return ERR_PTR(-EINVAL);
19291925

19301926
ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
@@ -2000,7 +1996,7 @@ void ucsi_unregister(struct ucsi *ucsi)
20001996
cancel_work_sync(&ucsi->resume_work);
20011997

20021998
/* Disable notifications */
2003-
ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1999+
ucsi->ops->async_control(ucsi, cmd);
20042000

20052001
if (!ucsi->connector)
20062002
return;

drivers/usb/typec/ucsi/ucsi.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ struct dentry;
5757
/**
5858
* struct ucsi_operations - UCSI I/O operations
5959
* @read: Read operation
60-
* @sync_write: Blocking write operation
61-
* @async_write: Non-blocking write operation
60+
* @sync_control: Blocking control operation
61+
* @async_control: Non-blocking control operation
6262
* @update_altmodes: Squashes duplicate DP altmodes
6363
* @update_connector: Update connector capabilities before registering
6464
* @connector_status: Updates connector status, called holding connector lock
@@ -70,10 +70,8 @@ struct dentry;
7070
struct ucsi_operations {
7171
int (*read)(struct ucsi *ucsi, unsigned int offset,
7272
void *val, size_t val_len);
73-
int (*sync_write)(struct ucsi *ucsi, unsigned int offset,
74-
const void *val, size_t val_len);
75-
int (*async_write)(struct ucsi *ucsi, unsigned int offset,
76-
const void *val, size_t val_len);
73+
int (*sync_control)(struct ucsi *ucsi, u64 command);
74+
int (*async_control)(struct ucsi *ucsi, u64 command);
7775
bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig,
7876
struct ucsi_altmode *updated);
7977
void (*update_connector)(struct ucsi_connector *con);

drivers/usb/typec/ucsi/ucsi_acpi.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,28 @@ static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset,
6161
return 0;
6262
}
6363

64-
static int ucsi_acpi_async_write(struct ucsi *ucsi, unsigned int offset,
65-
const void *val, size_t val_len)
64+
static int ucsi_acpi_async_control(struct ucsi *ucsi, u64 command)
6665
{
6766
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
6867

69-
memcpy(ua->base + offset, val, val_len);
70-
ua->cmd = *(u64 *)val;
68+
memcpy(ua->base + UCSI_CONTROL, &command, sizeof(command));
69+
ua->cmd = command;
7170

7271
return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
7372
}
7473

75-
static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset,
76-
const void *val, size_t val_len)
74+
static int ucsi_acpi_sync_control(struct ucsi *ucsi, u64 command)
7775
{
7876
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
79-
bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI;
77+
bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
8078
int ret;
8179

8280
if (ack)
8381
set_bit(UCSI_ACPI_ACK_PENDING, &ua->flags);
8482
else
8583
set_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags);
8684

87-
ret = ucsi_acpi_async_write(ucsi, offset, val, val_len);
85+
ret = ucsi_acpi_async_control(ucsi, command);
8886
if (ret)
8987
goto out_clear_bit;
9088

@@ -102,8 +100,8 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset,
102100

103101
static const struct ucsi_operations ucsi_acpi_ops = {
104102
.read = ucsi_acpi_read,
105-
.sync_write = ucsi_acpi_sync_write,
106-
.async_write = ucsi_acpi_async_write
103+
.sync_control = ucsi_acpi_sync_control,
104+
.async_control = ucsi_acpi_async_control
107105
};
108106

109107
static int
@@ -125,8 +123,8 @@ ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_
125123

126124
static const struct ucsi_operations ucsi_zenbook_ops = {
127125
.read = ucsi_zenbook_read,
128-
.sync_write = ucsi_acpi_sync_write,
129-
.async_write = ucsi_acpi_async_write
126+
.sync_control = ucsi_acpi_sync_control,
127+
.async_control = ucsi_acpi_async_control
130128
};
131129

132130
static int ucsi_gram_read(struct ucsi *ucsi, unsigned int offset,
@@ -157,13 +155,12 @@ static int ucsi_gram_read(struct ucsi *ucsi, unsigned int offset,
157155
return ret;
158156
}
159157

160-
static int ucsi_gram_sync_write(struct ucsi *ucsi, unsigned int offset,
161-
const void *val, size_t val_len)
158+
static int ucsi_gram_sync_control(struct ucsi *ucsi, u64 command)
162159
{
163160
struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
164161
int ret;
165162

166-
ret = ucsi_acpi_sync_write(ucsi, offset, val, val_len);
163+
ret = ucsi_acpi_sync_control(ucsi, command);
167164
if (ret < 0)
168165
return ret;
169166

@@ -177,8 +174,8 @@ static int ucsi_gram_sync_write(struct ucsi *ucsi, unsigned int offset,
177174

178175
static const struct ucsi_operations ucsi_gram_ops = {
179176
.read = ucsi_gram_read,
180-
.sync_write = ucsi_gram_sync_write,
181-
.async_write = ucsi_acpi_async_write
177+
.sync_control = ucsi_gram_sync_control,
178+
.async_control = ucsi_acpi_async_control
182179
};
183180

184181
static const struct dmi_system_id ucsi_acpi_quirks[] = {

drivers/usb/typec/ucsi/ucsi_ccg.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -610,25 +610,23 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset,
610610
return ret;
611611
}
612612

613-
static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset,
614-
const void *val, size_t val_len)
613+
static int ucsi_ccg_async_control(struct ucsi *ucsi, u64 command)
615614
{
616615
struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
617-
u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset);
616+
u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CONTROL);
618617

619618
/*
620-
* UCSI may read CCI instantly after async_write,
619+
* UCSI may read CCI instantly after async_control,
621620
* clear CCI to avoid caller getting wrong data before we get CCI from ISR
622621
*/
623622
spin_lock(&uc->op_lock);
624623
uc->op_data.cci = 0;
625624
spin_unlock(&uc->op_lock);
626625

627-
return ccg_write(uc, reg, val, val_len);
626+
return ccg_write(uc, reg, (u8 *)&command, sizeof(command));
628627
}
629628

630-
static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset,
631-
const void *val, size_t val_len)
629+
static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command)
632630
{
633631
struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
634632
struct ucsi_connector *con;
@@ -639,19 +637,17 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset,
639637
pm_runtime_get_sync(uc->dev);
640638
set_bit(DEV_CMD_PENDING, &uc->flags);
641639

642-
if (offset == UCSI_CONTROL && val_len == sizeof(uc->last_cmd_sent)) {
643-
uc->last_cmd_sent = *(u64 *)val;
640+
uc->last_cmd_sent = command;
644641

645-
if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_SET_NEW_CAM &&
646-
uc->has_multiple_dp) {
647-
con_index = (uc->last_cmd_sent >> 16) &
648-
UCSI_CMD_CONNECTOR_MASK;
649-
con = &uc->ucsi->connector[con_index - 1];
650-
ucsi_ccg_update_set_new_cam_cmd(uc, con, (u64 *)val);
651-
}
642+
if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_SET_NEW_CAM &&
643+
uc->has_multiple_dp) {
644+
con_index = (uc->last_cmd_sent >> 16) &
645+
UCSI_CMD_CONNECTOR_MASK;
646+
con = &uc->ucsi->connector[con_index - 1];
647+
ucsi_ccg_update_set_new_cam_cmd(uc, con, &command);
652648
}
653649

654-
ret = ucsi_ccg_async_write(ucsi, offset, val, val_len);
650+
ret = ucsi_ccg_async_control(ucsi, command);
655651
if (ret)
656652
goto err_clear_bit;
657653

@@ -668,8 +664,8 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset,
668664

669665
static const struct ucsi_operations ucsi_ccg_ops = {
670666
.read = ucsi_ccg_read,
671-
.sync_write = ucsi_ccg_sync_write,
672-
.async_write = ucsi_ccg_async_write,
667+
.sync_control = ucsi_ccg_sync_control,
668+
.async_control = ucsi_ccg_async_control,
673669
.update_altmodes = ucsi_ccg_update_altmodes
674670
};
675671

drivers/usb/typec/ucsi/ucsi_glink.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,19 @@ static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned i
143143
return 0;
144144
}
145145

146-
static int pmic_glink_ucsi_async_write(struct ucsi *__ucsi, unsigned int offset,
147-
const void *val, size_t val_len)
146+
static int pmic_glink_ucsi_async_control(struct ucsi *__ucsi, u64 command)
148147
{
149148
struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi);
150149
int ret;
151150

152151
mutex_lock(&ucsi->lock);
153-
ret = pmic_glink_ucsi_locked_write(ucsi, offset, val, val_len);
152+
ret = pmic_glink_ucsi_locked_write(ucsi, UCSI_CONTROL, &command, sizeof(command));
154153
mutex_unlock(&ucsi->lock);
155154

156155
return ret;
157156
}
158157

159-
static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset,
160-
const void *val, size_t val_len)
158+
static int pmic_glink_ucsi_sync_control(struct ucsi *__ucsi, u64 command)
161159
{
162160
struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi);
163161
unsigned long left;
@@ -169,7 +167,7 @@ static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset,
169167
ucsi->sync_val = 0;
170168
reinit_completion(&ucsi->sync_ack);
171169
ucsi->sync_pending = true;
172-
ret = pmic_glink_ucsi_locked_write(ucsi, offset, val, val_len);
170+
ret = pmic_glink_ucsi_locked_write(ucsi, UCSI_CONTROL, &command, sizeof(command));
173171
mutex_unlock(&ucsi->lock);
174172

175173
left = wait_for_completion_timeout(&ucsi->sync_ack, 5 * HZ);
@@ -217,8 +215,8 @@ static void pmic_glink_ucsi_connector_status(struct ucsi_connector *con)
217215

218216
static const struct ucsi_operations pmic_glink_ucsi_ops = {
219217
.read = pmic_glink_ucsi_read,
220-
.sync_write = pmic_glink_ucsi_sync_write,
221-
.async_write = pmic_glink_ucsi_async_write,
218+
.sync_control = pmic_glink_ucsi_sync_control,
219+
.async_control = pmic_glink_ucsi_async_control,
222220
.update_connector = pmic_glink_ucsi_update_connector,
223221
.connector_status = pmic_glink_ucsi_connector_status,
224222
};

drivers/usb/typec/ucsi/ucsi_stm32g0.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val,
359359
return 0;
360360
}
361361

362-
static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, const void *val,
363-
size_t len)
362+
static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command)
364363
{
365364
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
366365
struct i2c_client *client = g0->client;
@@ -373,39 +372,38 @@ static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, cons
373372
unsigned char *buf;
374373
int ret;
375374

376-
buf = kmalloc(len + 1, GFP_KERNEL);
375+
buf = kmalloc(sizeof(command) + 1, GFP_KERNEL);
377376
if (!buf)
378377
return -ENOMEM;
379378

380-
buf[0] = offset;
381-
memcpy(&buf[1], val, len);
382-
msg[0].len = len + 1;
379+
buf[0] = UCSI_CONTROL;
380+
memcpy(&buf[1], &command, sizeof(command));
381+
msg[0].len = sizeof(command) + 1;
383382
msg[0].buf = buf;
384383

385384
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
386385
kfree(buf);
387386
if (ret != ARRAY_SIZE(msg)) {
388-
dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, offset, ret);
387+
dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, UCSI_CONTROL, ret);
389388

390389
return ret < 0 ? ret : -EIO;
391390
}
392391

393392
return 0;
394393
}
395394

396-
static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const void *val,
397-
size_t len)
395+
static int ucsi_stm32g0_sync_control(struct ucsi *ucsi, u64 command)
398396
{
399397
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
400-
bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI;
398+
bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
401399
int ret;
402400

403401
if (ack)
404402
set_bit(ACK_PENDING, &g0->flags);
405403
else
406404
set_bit(COMMAND_PENDING, &g0->flags);
407405

408-
ret = ucsi_stm32g0_async_write(ucsi, offset, val, len);
406+
ret = ucsi_stm32g0_async_control(ucsi, command);
409407
if (ret)
410408
goto out_clear_bit;
411409

@@ -449,8 +447,8 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data)
449447

450448
static const struct ucsi_operations ucsi_stm32g0_ops = {
451449
.read = ucsi_stm32g0_read,
452-
.sync_write = ucsi_stm32g0_sync_write,
453-
.async_write = ucsi_stm32g0_async_write,
450+
.sync_control = ucsi_stm32g0_sync_control,
451+
.async_control = ucsi_stm32g0_async_control,
454452
};
455453

456454
static int ucsi_stm32g0_register(struct ucsi *ucsi)

0 commit comments

Comments
 (0)