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