1a8335c64SWolfram Sang // SPDX-License-Identifier: GPL-2.0-only
2a8335c64SWolfram Sang /*
3a8335c64SWolfram Sang * I2C slave mode testunit
4a8335c64SWolfram Sang *
5a8335c64SWolfram Sang * Copyright (C) 2020 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
6a8335c64SWolfram Sang * Copyright (C) 2020 by Renesas Electronics Corporation
7a8335c64SWolfram Sang */
8a8335c64SWolfram Sang
9a8335c64SWolfram Sang #include <generated/utsrelease.h>
10a8335c64SWolfram Sang #include <linux/bitops.h>
11a8335c64SWolfram Sang #include <linux/completion.h>
12a8335c64SWolfram Sang #include <linux/gpio/consumer.h>
13a8335c64SWolfram Sang #include <linux/i2c.h>
14a8335c64SWolfram Sang #include <linux/init.h>
15a8335c64SWolfram Sang #include <linux/module.h>
16a8335c64SWolfram Sang #include <linux/of.h>
17a8335c64SWolfram Sang #include <linux/slab.h>
18a8335c64SWolfram Sang #include <linux/workqueue.h> /* FIXME: is system_long_wq the best choice? */
19a8335c64SWolfram Sang
20a8335c64SWolfram Sang #define TU_VERSION_MAX_LENGTH 128
21*74b0666fSWolfram Sang
22b39ab96aSWolfram Sang enum testunit_cmds {
23a8335c64SWolfram Sang TU_CMD_READ_BYTES = 1, /* save 0 for ABORT, RESET or similar */
24a8335c64SWolfram Sang TU_CMD_SMBUS_HOST_NOTIFY,
25a8335c64SWolfram Sang TU_CMD_SMBUS_BLOCK_PROC_CALL,
26a8335c64SWolfram Sang TU_CMD_GET_VERSION_WITH_REP_START,
27a8335c64SWolfram Sang TU_CMD_SMBUS_ALERT_REQUEST,
28a8335c64SWolfram Sang TU_NUM_CMDS
29a8335c64SWolfram Sang };
30a8335c64SWolfram Sang
31a8335c64SWolfram Sang enum testunit_regs {
32a8335c64SWolfram Sang TU_REG_CMD,
33a8335c64SWolfram Sang TU_REG_DATAL,
34a8335c64SWolfram Sang TU_REG_DATAH,
35a8335c64SWolfram Sang TU_REG_DELAY,
36a8335c64SWolfram Sang TU_NUM_REGS
37a8335c64SWolfram Sang };
38a8335c64SWolfram Sang
39a8335c64SWolfram Sang enum testunit_flags {
40a8335c64SWolfram Sang TU_FLAG_IN_PROCESS,
41a8335c64SWolfram Sang };
42a8335c64SWolfram Sang
43a8335c64SWolfram Sang struct testunit_data {
44a8335c64SWolfram Sang unsigned long flags;
45a8335c64SWolfram Sang u8 regs[TU_NUM_REGS];
46a8335c64SWolfram Sang u8 reg_idx;
47a8335c64SWolfram Sang u8 read_idx;
48a8335c64SWolfram Sang struct i2c_client *client;
49a8335c64SWolfram Sang struct delayed_work worker;
50a8335c64SWolfram Sang struct gpio_desc *gpio;
51a8335c64SWolfram Sang struct completion alert_done;
52a8335c64SWolfram Sang };
53a8335c64SWolfram Sang
54a8335c64SWolfram Sang static char tu_version_info[] = "v" UTS_RELEASE "\n\0";
55a8335c64SWolfram Sang
i2c_slave_testunit_smbalert_cb(struct i2c_client * client,enum i2c_slave_event event,u8 * val)56a8335c64SWolfram Sang static int i2c_slave_testunit_smbalert_cb(struct i2c_client *client,
57a8335c64SWolfram Sang enum i2c_slave_event event, u8 *val)
58a8335c64SWolfram Sang {
59a8335c64SWolfram Sang struct testunit_data *tu = i2c_get_clientdata(client);
60a8335c64SWolfram Sang
61a8335c64SWolfram Sang switch (event) {
62a8335c64SWolfram Sang case I2C_SLAVE_READ_PROCESSED:
63*74b0666fSWolfram Sang gpiod_set_value(tu->gpio, 0);
64a8335c64SWolfram Sang fallthrough;
65a8335c64SWolfram Sang case I2C_SLAVE_READ_REQUESTED:
66a8335c64SWolfram Sang *val = tu->regs[TU_REG_DATAL];
67a8335c64SWolfram Sang break;
68a8335c64SWolfram Sang
69a8335c64SWolfram Sang case I2C_SLAVE_STOP:
70a8335c64SWolfram Sang complete(&tu->alert_done);
71a8335c64SWolfram Sang break;
72a8335c64SWolfram Sang
73a8335c64SWolfram Sang case I2C_SLAVE_WRITE_REQUESTED:
74a8335c64SWolfram Sang case I2C_SLAVE_WRITE_RECEIVED:
75a8335c64SWolfram Sang return -EOPNOTSUPP;
76a8335c64SWolfram Sang }
77a8335c64SWolfram Sang
78a8335c64SWolfram Sang return 0;
79a8335c64SWolfram Sang }
80a8335c64SWolfram Sang
i2c_slave_testunit_slave_cb(struct i2c_client * client,enum i2c_slave_event event,u8 * val)81a8335c64SWolfram Sang static int i2c_slave_testunit_slave_cb(struct i2c_client *client,
82a8335c64SWolfram Sang enum i2c_slave_event event, u8 *val)
83a8335c64SWolfram Sang {
84a8335c64SWolfram Sang struct testunit_data *tu = i2c_get_clientdata(client);
85a8335c64SWolfram Sang bool is_proc_call = tu->reg_idx == 3 && tu->regs[TU_REG_DATAL] == 1 &&
86a8335c64SWolfram Sang tu->regs[TU_REG_CMD] == TU_CMD_SMBUS_BLOCK_PROC_CALL;
87a8335c64SWolfram Sang bool is_get_version = tu->reg_idx == 3 &&
88a8335c64SWolfram Sang tu->regs[TU_REG_CMD] == TU_CMD_GET_VERSION_WITH_REP_START;
89a8335c64SWolfram Sang int ret = 0;
90a8335c64SWolfram Sang
91a8335c64SWolfram Sang switch (event) {
92b39ab96aSWolfram Sang case I2C_SLAVE_WRITE_REQUESTED:
93b39ab96aSWolfram Sang if (test_bit(TU_FLAG_IN_PROCESS, &tu->flags))
94a8335c64SWolfram Sang return -EBUSY;
95a8335c64SWolfram Sang
96a8335c64SWolfram Sang memset(tu->regs, 0, TU_NUM_REGS);
97a8335c64SWolfram Sang tu->reg_idx = 0;
98a8335c64SWolfram Sang tu->read_idx = 0;
99a8335c64SWolfram Sang break;
100a8335c64SWolfram Sang
101a8335c64SWolfram Sang case I2C_SLAVE_WRITE_RECEIVED:
102a8335c64SWolfram Sang if (test_bit(TU_FLAG_IN_PROCESS, &tu->flags))
103a8335c64SWolfram Sang return -EBUSY;
104a8335c64SWolfram Sang
105a8335c64SWolfram Sang if (tu->reg_idx < TU_NUM_REGS)
106a8335c64SWolfram Sang tu->regs[tu->reg_idx] = *val;
107a8335c64SWolfram Sang else
108a8335c64SWolfram Sang ret = -EMSGSIZE;
109a8335c64SWolfram Sang
110a8335c64SWolfram Sang if (tu->reg_idx <= TU_NUM_REGS)
111a8335c64SWolfram Sang tu->reg_idx++;
112a8335c64SWolfram Sang
113a8335c64SWolfram Sang /* TU_REG_CMD always written at this point */
114a8335c64SWolfram Sang if (tu->regs[TU_REG_CMD] >= TU_NUM_CMDS)
115a8335c64SWolfram Sang ret = -EINVAL;
116a8335c64SWolfram Sang
117a8335c64SWolfram Sang break;
118a8335c64SWolfram Sang
119a8335c64SWolfram Sang case I2C_SLAVE_STOP:
120a8335c64SWolfram Sang if (tu->reg_idx == TU_NUM_REGS) {
121119736c7SWolfram Sang set_bit(TU_FLAG_IN_PROCESS, &tu->flags);
122119736c7SWolfram Sang queue_delayed_work(system_long_wq, &tu->worker,
123119736c7SWolfram Sang msecs_to_jiffies(10 * tu->regs[TU_REG_DELAY]));
124119736c7SWolfram Sang }
125119736c7SWolfram Sang
126119736c7SWolfram Sang /*
127119736c7SWolfram Sang * Reset reg_idx to avoid that work gets queued again in case of
128c422b6a6SWolfram Sang * STOP after a following read message. But do not clear TU regs
129a8335c64SWolfram Sang * here because we still need them in the workqueue!
130a8335c64SWolfram Sang */
131c116deafSWolfram Sang tu->reg_idx = 0;
132c116deafSWolfram Sang break;
133c116deafSWolfram Sang
134b39ab96aSWolfram Sang case I2C_SLAVE_READ_PROCESSED:
135a8335c64SWolfram Sang /* Advance until we reach the NUL character */
136a8335c64SWolfram Sang if (is_get_version && tu_version_info[tu->read_idx] != 0)
137a8335c64SWolfram Sang tu->read_idx++;
138a8335c64SWolfram Sang else if (is_proc_call && tu->regs[TU_REG_DATAH])
139b39ab96aSWolfram Sang tu->regs[TU_REG_DATAH]--;
140b39ab96aSWolfram Sang
141b39ab96aSWolfram Sang fallthrough;
142b39ab96aSWolfram Sang
143b39ab96aSWolfram Sang case I2C_SLAVE_READ_REQUESTED:
144b39ab96aSWolfram Sang if (is_get_version)
145a8335c64SWolfram Sang *val = tu_version_info[tu->read_idx];
146a8335c64SWolfram Sang else if (is_proc_call)
147a8335c64SWolfram Sang *val = tu->regs[TU_REG_DATAH];
148a8335c64SWolfram Sang else
149a8335c64SWolfram Sang *val = test_bit(TU_FLAG_IN_PROCESS, &tu->flags) ?
150a8335c64SWolfram Sang tu->regs[TU_REG_CMD] : 0;
151a8335c64SWolfram Sang break;
152a8335c64SWolfram Sang }
153a8335c64SWolfram Sang
154a8335c64SWolfram Sang return ret;
155a8335c64SWolfram Sang }
156a8335c64SWolfram Sang
i2c_slave_testunit_work(struct work_struct * work)157a8335c64SWolfram Sang static void i2c_slave_testunit_work(struct work_struct *work)
158a8335c64SWolfram Sang {
159a8335c64SWolfram Sang struct testunit_data *tu = container_of(work, struct testunit_data, worker.work);
160a8335c64SWolfram Sang unsigned long time_left;
161a8335c64SWolfram Sang struct i2c_msg msg;
162a8335c64SWolfram Sang u8 msgbuf[256];
163a8335c64SWolfram Sang u16 orig_addr;
164a8335c64SWolfram Sang int ret = 0;
165a8335c64SWolfram Sang
166ed5c2f5fSUwe Kleine-König msg.addr = I2C_CLIENT_END;
167a8335c64SWolfram Sang msg.buf = msgbuf;
168a8335c64SWolfram Sang
169a8335c64SWolfram Sang switch (tu->regs[TU_REG_CMD]) {
170a8335c64SWolfram Sang case TU_CMD_READ_BYTES:
171a8335c64SWolfram Sang msg.addr = tu->regs[TU_REG_DATAL];
172a8335c64SWolfram Sang msg.flags = I2C_M_RD;
173a8335c64SWolfram Sang msg.len = tu->regs[TU_REG_DATAH];
174a8335c64SWolfram Sang break;
175d08ed106SUwe Kleine-König
176a8335c64SWolfram Sang case TU_CMD_SMBUS_HOST_NOTIFY:
177a8335c64SWolfram Sang msg.addr = 0x08;
178a8335c64SWolfram Sang msg.flags = 0;
179a8335c64SWolfram Sang msg.len = 3;
180a8335c64SWolfram Sang msgbuf[0] = tu->client->addr;
181a8335c64SWolfram Sang msgbuf[1] = tu->regs[TU_REG_DATAL];
182a8335c64SWolfram Sang msgbuf[2] = tu->regs[TU_REG_DATAH];
183a8335c64SWolfram Sang break;
184834a9dc4SUwe Kleine-König
185a8335c64SWolfram Sang case TU_CMD_SMBUS_ALERT_REQUEST:
186a8335c64SWolfram Sang i2c_slave_unregister(tu->client);
187a8335c64SWolfram Sang orig_addr = tu->client->addr;
188a8335c64SWolfram Sang tu->client->addr = 0x0c;
189a8335c64SWolfram Sang ret = i2c_slave_register(tu->client, i2c_slave_testunit_smbalert_cb);
190a8335c64SWolfram Sang if (ret)
191a8335c64SWolfram Sang goto out_smbalert;
192a8335c64SWolfram Sang
193 reinit_completion(&tu->alert_done);
194 gpiod_set_value(tu->gpio, 1);
195 time_left = wait_for_completion_timeout(&tu->alert_done, HZ);
196 if (!time_left)
197 ret = -ETIMEDOUT;
198
199 i2c_slave_unregister(tu->client);
200 out_smbalert:
201 tu->client->addr = orig_addr;
202 i2c_slave_register(tu->client, i2c_slave_testunit_slave_cb);
203 break;
204
205 default:
206 break;
207 }
208
209 if (msg.addr != I2C_CLIENT_END) {
210 ret = i2c_transfer(tu->client->adapter, &msg, 1);
211 /* convert '0 msgs transferred' to errno */
212 ret = (ret == 0) ? -EIO : ret;
213 }
214
215 if (ret < 0)
216 dev_err(&tu->client->dev, "CMD%02X failed (%d)\n", tu->regs[TU_REG_CMD], ret);
217
218 clear_bit(TU_FLAG_IN_PROCESS, &tu->flags);
219 }
220
i2c_slave_testunit_probe(struct i2c_client * client)221 static int i2c_slave_testunit_probe(struct i2c_client *client)
222 {
223 struct testunit_data *tu;
224
225 tu = devm_kzalloc(&client->dev, sizeof(struct testunit_data), GFP_KERNEL);
226 if (!tu)
227 return -ENOMEM;
228
229 tu->client = client;
230 i2c_set_clientdata(client, tu);
231 init_completion(&tu->alert_done);
232 INIT_DELAYED_WORK(&tu->worker, i2c_slave_testunit_work);
233
234 tu->gpio = devm_gpiod_get_index_optional(&client->dev, NULL, 0, GPIOD_OUT_LOW);
235 if (gpiod_cansleep(tu->gpio)) {
236 dev_err(&client->dev, "GPIO access which may sleep is not allowed\n");
237 return -EDEADLK;
238 }
239
240 if (sizeof(tu_version_info) > TU_VERSION_MAX_LENGTH)
241 tu_version_info[TU_VERSION_MAX_LENGTH - 1] = 0;
242
243 return i2c_slave_register(client, i2c_slave_testunit_slave_cb);
244 };
245
i2c_slave_testunit_remove(struct i2c_client * client)246 static void i2c_slave_testunit_remove(struct i2c_client *client)
247 {
248 struct testunit_data *tu = i2c_get_clientdata(client);
249
250 cancel_delayed_work_sync(&tu->worker);
251 i2c_slave_unregister(client);
252 }
253
254 static const struct i2c_device_id i2c_slave_testunit_id[] = {
255 { "slave-testunit" },
256 { }
257 };
258 MODULE_DEVICE_TABLE(i2c, i2c_slave_testunit_id);
259
260 static struct i2c_driver i2c_slave_testunit_driver = {
261 .driver = {
262 .name = "i2c-slave-testunit",
263 },
264 .probe = i2c_slave_testunit_probe,
265 .remove = i2c_slave_testunit_remove,
266 .id_table = i2c_slave_testunit_id,
267 };
268 module_i2c_driver(i2c_slave_testunit_driver);
269
270 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
271 MODULE_DESCRIPTION("I2C slave mode test unit");
272 MODULE_LICENSE("GPL v2");
273