1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the TAOS evaluation modules
4 * These devices include an I2C master which can be controlled over the
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/interrupt.h>
14 #include <linux/input.h>
15 #include <linux/serio.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
19 #define TAOS_BUFFER_SIZE 63
21 #define TAOS_STATE_INIT 0
22 #define TAOS_STATE_IDLE 1
23 #define TAOS_STATE_EOFF 2
24 #define TAOS_STATE_RECV 3
26 #define TAOS_CMD_RESET 0x12
27 #define TAOS_CMD_ECHO_ON '+'
28 #define TAOS_CMD_ECHO_OFF '-'
30 static DECLARE_WAIT_QUEUE_HEAD(wq);
33 struct i2c_adapter adapter;
34 struct i2c_client *client;
36 u8 addr; /* last used address */
37 unsigned char buffer[TAOS_BUFFER_SIZE];
38 unsigned int pos; /* position inside the buffer */
41 /* TAOS TSL2550 EVM */
42 static const struct i2c_board_info tsl2550_info = {
43 I2C_BOARD_INFO("tsl2550", 0x39),
46 /* Instantiate i2c devices based on the adapter name */
47 static struct i2c_client *taos_instantiate_device(struct i2c_adapter *adapter)
49 if (!strncmp(adapter->name, "TAOS TSL2550 EVM", 16)) {
50 dev_info(&adapter->dev, "Instantiating device %s at 0x%02x\n",
51 tsl2550_info.type, tsl2550_info.addr);
52 return i2c_new_client_device(adapter, &tsl2550_info);
55 return ERR_PTR(-ENODEV);
58 static int taos_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
59 unsigned short flags, char read_write, u8 command,
60 int size, union i2c_smbus_data *data)
62 struct serio *serio = adapter->algo_data;
63 struct taos_data *taos = serio_get_drvdata(serio);
66 /* Encode our transaction. "@" is for the device address, "$" for the
67 SMBus command and "#" for the data. */
70 /* The device remembers the last used address, no need to send it
71 again if it's the same */
72 if (addr != taos->addr)
73 p += sprintf(p, "@%02X", addr);
77 if (read_write == I2C_SMBUS_WRITE)
78 sprintf(p, "$#%02X", command);
82 case I2C_SMBUS_BYTE_DATA:
83 if (read_write == I2C_SMBUS_WRITE)
84 sprintf(p, "$%02X#%02X", command, data->byte);
86 sprintf(p, "$%02X", command);
89 dev_warn(&adapter->dev, "Unsupported transaction %d\n", size);
93 /* Send the transaction to the TAOS EVM */
94 dev_dbg(&adapter->dev, "Command buffer: %s\n", taos->buffer);
95 for (p = taos->buffer; *p; p++)
96 serio_write(serio, *p);
100 /* Start the transaction and read the answer */
102 taos->state = TAOS_STATE_RECV;
103 serio_write(serio, read_write == I2C_SMBUS_WRITE ? '>' : '<');
104 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
105 msecs_to_jiffies(150));
106 if (taos->state != TAOS_STATE_IDLE
108 dev_err(&adapter->dev, "Transaction timeout (pos=%d)\n",
112 dev_dbg(&adapter->dev, "Answer buffer: %s\n", taos->buffer);
114 /* Interpret the returned string */
115 p = taos->buffer + 1;
117 if (!strcmp(p, "NAK"))
120 if (read_write == I2C_SMBUS_WRITE) {
121 if (!strcmp(p, "ACK"))
126 * Voluntarily dropping error code of kstrtou8 since all
127 * error code that it could return are invalid according
128 * to Documentation/i2c/fault-codes.rst.
130 if (kstrtou8(p + 1, 16, &data->byte))
139 static u32 taos_smbus_func(struct i2c_adapter *adapter)
141 return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA;
144 static const struct i2c_algorithm taos_algorithm = {
145 .smbus_xfer = taos_smbus_xfer,
146 .functionality = taos_smbus_func,
149 static irqreturn_t taos_interrupt(struct serio *serio, unsigned char data,
152 struct taos_data *taos = serio_get_drvdata(serio);
154 switch (taos->state) {
155 case TAOS_STATE_INIT:
156 taos->buffer[taos->pos++] = data;
158 || taos->pos == TAOS_BUFFER_SIZE - 1) {
159 taos->buffer[taos->pos] = '\0';
160 taos->state = TAOS_STATE_IDLE;
161 wake_up_interruptible(&wq);
164 case TAOS_STATE_EOFF:
165 taos->state = TAOS_STATE_IDLE;
166 wake_up_interruptible(&wq);
168 case TAOS_STATE_RECV:
169 taos->buffer[taos->pos++] = data;
171 taos->buffer[taos->pos] = '\0';
172 taos->state = TAOS_STATE_IDLE;
173 wake_up_interruptible(&wq);
181 /* Extract the adapter name from the buffer received after reset.
182 The buffer is modified and a pointer inside the buffer is returned. */
183 static char *taos_adapter_name(char *buffer)
187 start = strstr(buffer, "TAOS ");
191 end = strchr(start, '\r');
199 static int taos_connect(struct serio *serio, struct serio_driver *drv)
201 struct taos_data *taos;
202 struct i2c_adapter *adapter;
206 taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
211 taos->state = TAOS_STATE_INIT;
212 serio_set_drvdata(serio, taos);
214 err = serio_open(serio, drv);
218 adapter = &taos->adapter;
219 adapter->owner = THIS_MODULE;
220 adapter->algo = &taos_algorithm;
221 adapter->algo_data = serio;
222 adapter->dev.parent = &serio->dev;
224 /* Reset the TAOS evaluation module to identify it */
225 serio_write(serio, TAOS_CMD_RESET);
226 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
227 msecs_to_jiffies(2000));
229 if (taos->state != TAOS_STATE_IDLE) {
231 dev_err(&serio->dev, "TAOS EVM reset failed (state=%d, "
232 "pos=%d)\n", taos->state, taos->pos);
236 name = taos_adapter_name(taos->buffer);
239 dev_err(&serio->dev, "TAOS EVM identification failed\n");
242 strlcpy(adapter->name, name, sizeof(adapter->name));
244 /* Turn echo off for better performance */
245 taos->state = TAOS_STATE_EOFF;
246 serio_write(serio, TAOS_CMD_ECHO_OFF);
248 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
249 msecs_to_jiffies(250));
250 if (taos->state != TAOS_STATE_IDLE) {
252 dev_err(&serio->dev, "TAOS EVM echo off failed "
253 "(state=%d)\n", taos->state);
257 err = i2c_add_adapter(adapter);
260 dev_info(&serio->dev, "Connected to TAOS EVM\n");
262 taos->client = taos_instantiate_device(adapter);
273 static void taos_disconnect(struct serio *serio)
275 struct taos_data *taos = serio_get_drvdata(serio);
277 i2c_unregister_device(taos->client);
278 i2c_del_adapter(&taos->adapter);
282 dev_info(&serio->dev, "Disconnected from TAOS EVM\n");
285 static const struct serio_device_id taos_serio_ids[] = {
288 .proto = SERIO_TAOSEVM,
294 MODULE_DEVICE_TABLE(serio, taos_serio_ids);
296 static struct serio_driver taos_drv = {
300 .description = "TAOS evaluation module driver",
301 .id_table = taos_serio_ids,
302 .connect = taos_connect,
303 .disconnect = taos_disconnect,
304 .interrupt = taos_interrupt,
307 module_serio_driver(taos_drv);
310 MODULE_DESCRIPTION("TAOS evaluation module driver");
311 MODULE_LICENSE("GPL");