Skip to content

Commit 6b21470

Browse files
author
Wolfram Sang
committed
i2c: testunit: add command to support versioning and test rep_start
For some devices, it is essential that controllers handle repeated start correctly and do not replace it with a stop/start combination. This addition helps to test that because it will only return a version string if repeated start is done properly. Signed-off-by: Wolfram Sang <[email protected]>
1 parent faf3c10 commit 6b21470

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

Documentation/i2c/slave-testunit-backend.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,41 @@ later)::
133133

134134
# i2ctransfer -y 0 w3@0x30 3 1 0x10 r?
135135
0x10 0x0f 0x0e 0x0d 0x0c 0x0b 0x0a 0x09 0x08 0x07 0x06 0x05 0x04 0x03 0x02 0x01 0x00
136+
137+
0x04 GET_VERSION_WITH_REP_START
138+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139+
140+
.. list-table::
141+
:header-rows: 1
142+
143+
* - CMD
144+
- DATAL
145+
- DATAH
146+
- DELAY
147+
148+
* - 0x04
149+
- currently unused
150+
- currently unused
151+
- leave out, partial command!
152+
153+
Partial command. After sending this command, the testunit will reply to a read
154+
message with a NUL terminated version string based on UTS_RELEASE. The first
155+
character is always a 'v' and the length of the version string is at maximum
156+
128 bytes. However, it will only respond if the read message is connected to
157+
the write message via repeated start. If your controller driver handles
158+
repeated start correctly, this will work::
159+
160+
# i2ctransfer -y 0 w3@0x30 4 0 0 r128
161+
0x76 0x36 0x2e 0x31 0x31 0x2e 0x30 0x2d 0x72 0x63 0x31 0x2d 0x30 0x30 0x30 0x30 ...
162+
163+
If you have i2c-tools 4.4 or later, you can print out the data right away::
164+
165+
# i2ctransfer -y -b 0 w3@0x30 4 0 0 r128
166+
v6.11.0-rc1-00009-gd37a1b4d3fd0
167+
168+
STOP/START combinations between the two messages will *not* work because they
169+
are not equivalent to a REPEATED START. As an example, this returns just the
170+
default response::
171+
172+
# i2cset -y 0 0x30 4 0 0 i; i2cget -y 0 0x30
173+
0x01

drivers/i2c/i2c-slave-testunit.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Copyright (C) 2020 by Renesas Electronics Corporation
77
*/
88

9+
#include <generated/utsrelease.h>
910
#include <linux/bitops.h>
1011
#include <linux/i2c.h>
1112
#include <linux/init.h>
@@ -15,11 +16,13 @@
1516
#include <linux/workqueue.h> /* FIXME: is system_long_wq the best choice? */
1617

1718
#define TU_CUR_VERSION 0x01
19+
#define TU_VERSION_MAX_LENGTH 128
1820

1921
enum testunit_cmds {
2022
TU_CMD_READ_BYTES = 1, /* save 0 for ABORT, RESET or similar */
2123
TU_CMD_SMBUS_HOST_NOTIFY,
2224
TU_CMD_SMBUS_BLOCK_PROC_CALL,
25+
TU_CMD_GET_VERSION_WITH_REP_START,
2326
TU_NUM_CMDS
2427
};
2528

@@ -39,10 +42,13 @@ struct testunit_data {
3942
unsigned long flags;
4043
u8 regs[TU_NUM_REGS];
4144
u8 reg_idx;
45+
u8 read_idx;
4246
struct i2c_client *client;
4347
struct delayed_work worker;
4448
};
4549

50+
static char tu_version_info[] = "v" UTS_RELEASE "\n\0";
51+
4652
static void i2c_slave_testunit_work(struct work_struct *work)
4753
{
4854
struct testunit_data *tu = container_of(work, struct testunit_data, worker.work);
@@ -91,6 +97,8 @@ static int i2c_slave_testunit_slave_cb(struct i2c_client *client,
9197
struct testunit_data *tu = i2c_get_clientdata(client);
9298
bool is_proc_call = tu->reg_idx == 3 && tu->regs[TU_REG_DATAL] == 1 &&
9399
tu->regs[TU_REG_CMD] == TU_CMD_SMBUS_BLOCK_PROC_CALL;
100+
bool is_get_version = tu->reg_idx == 3 &&
101+
tu->regs[TU_REG_CMD] == TU_CMD_GET_VERSION_WITH_REP_START;
94102
int ret = 0;
95103

96104
switch (event) {
@@ -100,6 +108,7 @@ static int i2c_slave_testunit_slave_cb(struct i2c_client *client,
100108

101109
memset(tu->regs, 0, TU_NUM_REGS);
102110
tu->reg_idx = 0;
111+
tu->read_idx = 0;
103112
break;
104113

105114
case I2C_SLAVE_WRITE_RECEIVED:
@@ -136,12 +145,21 @@ static int i2c_slave_testunit_slave_cb(struct i2c_client *client,
136145
break;
137146

138147
case I2C_SLAVE_READ_PROCESSED:
139-
if (is_proc_call && tu->regs[TU_REG_DATAH])
148+
/* Advance until we reach the NUL character */
149+
if (is_get_version && tu_version_info[tu->read_idx] != 0)
150+
tu->read_idx++;
151+
else if (is_proc_call && tu->regs[TU_REG_DATAH])
140152
tu->regs[TU_REG_DATAH]--;
153+
141154
fallthrough;
142155

143156
case I2C_SLAVE_READ_REQUESTED:
144-
*val = is_proc_call ? tu->regs[TU_REG_DATAH] : TU_CUR_VERSION;
157+
if (is_get_version)
158+
*val = tu_version_info[tu->read_idx];
159+
else if (is_proc_call)
160+
*val = tu->regs[TU_REG_DATAH];
161+
else
162+
*val = TU_CUR_VERSION;
145163
break;
146164
}
147165

@@ -160,6 +178,9 @@ static int i2c_slave_testunit_probe(struct i2c_client *client)
160178
i2c_set_clientdata(client, tu);
161179
INIT_DELAYED_WORK(&tu->worker, i2c_slave_testunit_work);
162180

181+
if (sizeof(tu_version_info) > TU_VERSION_MAX_LENGTH)
182+
tu_version_info[TU_VERSION_MAX_LENGTH - 1] = 0;
183+
163184
return i2c_slave_register(client, i2c_slave_testunit_slave_cb);
164185
};
165186

0 commit comments

Comments
 (0)