1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C slave mode testunit
5 * Copyright (C) 2020 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
6 * Copyright (C) 2020 by Renesas Electronics Corporation
9 #include <linux/bitops.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h> /* FIXME: is system_long_wq the best choice? */
17 #define TU_CUR_VERSION 0x01
20 TU_CMD_READ_BYTES = 1, /* save 0 for ABORT, RESET or similar */
22 TU_CMD_SMBUS_BLOCK_PROC_CALL,
38 struct testunit_data {
42 struct i2c_client *client;
43 struct delayed_work worker;
46 static void i2c_slave_testunit_work(struct work_struct *work)
48 struct testunit_data *tu = container_of(work, struct testunit_data, worker.work);
53 msg.addr = I2C_CLIENT_END;
56 switch (tu->regs[TU_REG_CMD]) {
57 case TU_CMD_READ_BYTES:
58 msg.addr = tu->regs[TU_REG_DATAL];
60 msg.len = tu->regs[TU_REG_DATAH];
63 case TU_CMD_HOST_NOTIFY:
67 msgbuf[0] = tu->client->addr;
68 msgbuf[1] = tu->regs[TU_REG_DATAL];
69 msgbuf[2] = tu->regs[TU_REG_DATAH];
76 if (msg.addr != I2C_CLIENT_END) {
77 ret = i2c_transfer(tu->client->adapter, &msg, 1);
78 /* convert '0 msgs transferred' to errno */
79 ret = (ret == 0) ? -EIO : ret;
83 dev_err(&tu->client->dev, "CMD%02X failed (%d)\n", tu->regs[TU_REG_CMD], ret);
85 clear_bit(TU_FLAG_IN_PROCESS, &tu->flags);
88 static int i2c_slave_testunit_slave_cb(struct i2c_client *client,
89 enum i2c_slave_event event, u8 *val)
91 struct testunit_data *tu = i2c_get_clientdata(client);
92 bool is_proc_call = tu->reg_idx == 3 && tu->regs[TU_REG_DATAL] == 1 &&
93 tu->regs[TU_REG_CMD] == TU_CMD_SMBUS_BLOCK_PROC_CALL;
97 case I2C_SLAVE_WRITE_RECEIVED:
98 if (test_bit(TU_FLAG_IN_PROCESS, &tu->flags))
101 if (tu->reg_idx < TU_NUM_REGS)
102 tu->regs[tu->reg_idx] = *val;
106 if (tu->reg_idx <= TU_NUM_REGS)
109 /* TU_REG_CMD always written at this point */
110 if (tu->regs[TU_REG_CMD] >= TU_NUM_CMDS)
116 if (tu->reg_idx == TU_NUM_REGS) {
117 set_bit(TU_FLAG_IN_PROCESS, &tu->flags);
118 queue_delayed_work(system_long_wq, &tu->worker,
119 msecs_to_jiffies(10 * tu->regs[TU_REG_DELAY]));
123 case I2C_SLAVE_WRITE_REQUESTED:
124 memset(tu->regs, 0, TU_NUM_REGS);
128 case I2C_SLAVE_READ_PROCESSED:
129 if (is_proc_call && tu->regs[TU_REG_DATAH])
130 tu->regs[TU_REG_DATAH]--;
133 case I2C_SLAVE_READ_REQUESTED:
134 *val = is_proc_call ? tu->regs[TU_REG_DATAH] : TU_CUR_VERSION;
141 static int i2c_slave_testunit_probe(struct i2c_client *client)
143 struct testunit_data *tu;
145 tu = devm_kzalloc(&client->dev, sizeof(struct testunit_data), GFP_KERNEL);
150 i2c_set_clientdata(client, tu);
151 INIT_DELAYED_WORK(&tu->worker, i2c_slave_testunit_work);
153 return i2c_slave_register(client, i2c_slave_testunit_slave_cb);
156 static void i2c_slave_testunit_remove(struct i2c_client *client)
158 struct testunit_data *tu = i2c_get_clientdata(client);
160 cancel_delayed_work_sync(&tu->worker);
161 i2c_slave_unregister(client);
164 static const struct i2c_device_id i2c_slave_testunit_id[] = {
165 { "slave-testunit", 0 },
168 MODULE_DEVICE_TABLE(i2c, i2c_slave_testunit_id);
170 static struct i2c_driver i2c_slave_testunit_driver = {
172 .name = "i2c-slave-testunit",
174 .probe = i2c_slave_testunit_probe,
175 .remove = i2c_slave_testunit_remove,
176 .id_table = i2c_slave_testunit_id,
178 module_i2c_driver(i2c_slave_testunit_driver);
180 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
181 MODULE_DESCRIPTION("I2C slave mode test unit");
182 MODULE_LICENSE("GPL v2");