blob: 6ffa56e0851777981af053420d69df68fc7c9d1b [file] [log] [blame]
Jean Delvareb9cdad72007-07-12 14:12:31 +02001/*
2 * Driver for the TAOS evaluation modules
3 * These devices include an I2C master which can be controlled over the
4 * serial port.
5 *
6 * Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/delay.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/input.h>
27#include <linux/serio.h>
28#include <linux/init.h>
29#include <linux/i2c.h>
30
31#define TAOS_BUFFER_SIZE 63
32
33#define TAOS_STATE_INIT 0
34#define TAOS_STATE_IDLE 1
Jean Delvare27693ce2009-09-18 22:45:43 +020035#define TAOS_STATE_EOFF 2
Jean Delvareb9cdad72007-07-12 14:12:31 +020036#define TAOS_STATE_RECV 3
37
38#define TAOS_CMD_RESET 0x12
Jean Delvare27693ce2009-09-18 22:45:43 +020039#define TAOS_CMD_ECHO_ON '+'
40#define TAOS_CMD_ECHO_OFF '-'
Jean Delvareb9cdad72007-07-12 14:12:31 +020041
42static DECLARE_WAIT_QUEUE_HEAD(wq);
43
44struct taos_data {
45 struct i2c_adapter adapter;
46 struct i2c_client *client;
47 int state;
48 u8 addr; /* last used address */
49 unsigned char buffer[TAOS_BUFFER_SIZE];
50 unsigned int pos; /* position inside the buffer */
51};
52
53/* TAOS TSL2550 EVM */
54static struct i2c_board_info tsl2550_info = {
55 I2C_BOARD_INFO("tsl2550", 0x39),
Jean Delvareb9cdad72007-07-12 14:12:31 +020056};
57
58/* Instantiate i2c devices based on the adapter name */
59static struct i2c_client *taos_instantiate_device(struct i2c_adapter *adapter)
60{
61 if (!strncmp(adapter->name, "TAOS TSL2550 EVM", 16)) {
62 dev_info(&adapter->dev, "Instantiating device %s at 0x%02x\n",
Jean Delvare3760f732008-04-29 23:11:40 +020063 tsl2550_info.type, tsl2550_info.addr);
Jean Delvareb9cdad72007-07-12 14:12:31 +020064 return i2c_new_device(adapter, &tsl2550_info);
65 }
66
67 return NULL;
68}
69
70static int taos_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
71 unsigned short flags, char read_write, u8 command,
72 int size, union i2c_smbus_data *data)
73{
74 struct serio *serio = adapter->algo_data;
75 struct taos_data *taos = serio_get_drvdata(serio);
76 char *p;
77
78 /* Encode our transaction. "@" is for the device address, "$" for the
79 SMBus command and "#" for the data. */
80 p = taos->buffer;
81
82 /* The device remembers the last used address, no need to send it
83 again if it's the same */
84 if (addr != taos->addr)
85 p += sprintf(p, "@%02X", addr);
86
87 switch (size) {
88 case I2C_SMBUS_BYTE:
89 if (read_write == I2C_SMBUS_WRITE)
90 sprintf(p, "$#%02X", command);
91 else
92 sprintf(p, "$");
93 break;
94 case I2C_SMBUS_BYTE_DATA:
95 if (read_write == I2C_SMBUS_WRITE)
96 sprintf(p, "$%02X#%02X", command, data->byte);
97 else
98 sprintf(p, "$%02X", command);
99 break;
100 default:
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200101 dev_warn(&adapter->dev, "Unsupported transaction %d\n", size);
102 return -EOPNOTSUPP;
Jean Delvareb9cdad72007-07-12 14:12:31 +0200103 }
104
105 /* Send the transaction to the TAOS EVM */
106 dev_dbg(&adapter->dev, "Command buffer: %s\n", taos->buffer);
Jean Delvare27693ce2009-09-18 22:45:43 +0200107 for (p = taos->buffer; *p; p++)
108 serio_write(serio, *p);
109
Jean Delvareb9cdad72007-07-12 14:12:31 +0200110 taos->addr = addr;
111
112 /* Start the transaction and read the answer */
113 taos->pos = 0;
114 taos->state = TAOS_STATE_RECV;
115 serio_write(serio, read_write == I2C_SMBUS_WRITE ? '>' : '<');
116 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
117 msecs_to_jiffies(150));
118 if (taos->state != TAOS_STATE_IDLE
Jean Delvare27693ce2009-09-18 22:45:43 +0200119 || taos->pos != 5) {
Jean Delvareb9cdad72007-07-12 14:12:31 +0200120 dev_err(&adapter->dev, "Transaction timeout (pos=%d)\n",
121 taos->pos);
122 return -EIO;
123 }
124 dev_dbg(&adapter->dev, "Answer buffer: %s\n", taos->buffer);
125
126 /* Interpret the returned string */
Jean Delvare27693ce2009-09-18 22:45:43 +0200127 p = taos->buffer + 1;
Jean Delvareb9cdad72007-07-12 14:12:31 +0200128 p[3] = '\0';
129 if (!strcmp(p, "NAK"))
130 return -ENODEV;
131
132 if (read_write == I2C_SMBUS_WRITE) {
133 if (!strcmp(p, "ACK"))
134 return 0;
135 } else {
136 if (p[0] == 'x') {
137 data->byte = simple_strtol(p + 1, NULL, 16);
138 return 0;
139 }
140 }
141
142 return -EIO;
143}
144
145static u32 taos_smbus_func(struct i2c_adapter *adapter)
146{
147 return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA;
148}
149
150static const struct i2c_algorithm taos_algorithm = {
151 .smbus_xfer = taos_smbus_xfer,
152 .functionality = taos_smbus_func,
153};
154
155static irqreturn_t taos_interrupt(struct serio *serio, unsigned char data,
156 unsigned int flags)
157{
158 struct taos_data *taos = serio_get_drvdata(serio);
159
160 switch (taos->state) {
161 case TAOS_STATE_INIT:
162 taos->buffer[taos->pos++] = data;
163 if (data == ':'
164 || taos->pos == TAOS_BUFFER_SIZE - 1) {
165 taos->buffer[taos->pos] = '\0';
166 taos->state = TAOS_STATE_IDLE;
167 wake_up_interruptible(&wq);
168 }
169 break;
Jean Delvare27693ce2009-09-18 22:45:43 +0200170 case TAOS_STATE_EOFF:
171 taos->state = TAOS_STATE_IDLE;
172 wake_up_interruptible(&wq);
Jean Delvareb9cdad72007-07-12 14:12:31 +0200173 break;
174 case TAOS_STATE_RECV:
175 taos->buffer[taos->pos++] = data;
176 if (data == ']') {
177 taos->buffer[taos->pos] = '\0';
178 taos->state = TAOS_STATE_IDLE;
179 wake_up_interruptible(&wq);
180 }
181 break;
182 }
183
184 return IRQ_HANDLED;
185}
186
187/* Extract the adapter name from the buffer received after reset.
188 The buffer is modified and a pointer inside the buffer is returned. */
189static char *taos_adapter_name(char *buffer)
190{
191 char *start, *end;
192
193 start = strstr(buffer, "TAOS ");
194 if (!start)
195 return NULL;
196
197 end = strchr(start, '\r');
198 if (!end)
199 return NULL;
200 *end = '\0';
201
202 return start;
203}
204
205static int taos_connect(struct serio *serio, struct serio_driver *drv)
206{
207 struct taos_data *taos;
208 struct i2c_adapter *adapter;
209 char *name;
210 int err;
211
212 taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
213 if (!taos) {
214 err = -ENOMEM;
215 goto exit;
216 }
217 taos->state = TAOS_STATE_INIT;
218 serio_set_drvdata(serio, taos);
219
220 err = serio_open(serio, drv);
221 if (err)
222 goto exit_kfree;
223
224 adapter = &taos->adapter;
225 adapter->owner = THIS_MODULE;
226 adapter->algo = &taos_algorithm;
227 adapter->algo_data = serio;
228 adapter->dev.parent = &serio->dev;
229
230 /* Reset the TAOS evaluation module to identify it */
231 serio_write(serio, TAOS_CMD_RESET);
232 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
233 msecs_to_jiffies(2000));
234
235 if (taos->state != TAOS_STATE_IDLE) {
236 err = -ENODEV;
Jean Delvare9b640f22011-06-29 11:36:10 +0200237 dev_err(&serio->dev, "TAOS EVM reset failed (state=%d, "
Jean Delvareb9cdad72007-07-12 14:12:31 +0200238 "pos=%d)\n", taos->state, taos->pos);
239 goto exit_close;
240 }
241
242 name = taos_adapter_name(taos->buffer);
243 if (!name) {
244 err = -ENODEV;
245 dev_err(&serio->dev, "TAOS EVM identification failed\n");
246 goto exit_close;
247 }
248 strlcpy(adapter->name, name, sizeof(adapter->name));
249
Jean Delvare27693ce2009-09-18 22:45:43 +0200250 /* Turn echo off for better performance */
251 taos->state = TAOS_STATE_EOFF;
252 serio_write(serio, TAOS_CMD_ECHO_OFF);
253
254 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
255 msecs_to_jiffies(250));
256 if (taos->state != TAOS_STATE_IDLE) {
257 err = -ENODEV;
Jean Delvare9b640f22011-06-29 11:36:10 +0200258 dev_err(&serio->dev, "TAOS EVM echo off failed "
Jean Delvare27693ce2009-09-18 22:45:43 +0200259 "(state=%d)\n", taos->state);
260 goto exit_close;
261 }
262
Jean Delvareb9cdad72007-07-12 14:12:31 +0200263 err = i2c_add_adapter(adapter);
264 if (err)
265 goto exit_close;
Jean Delvare9b640f22011-06-29 11:36:10 +0200266 dev_info(&serio->dev, "Connected to TAOS EVM\n");
Jean Delvareb9cdad72007-07-12 14:12:31 +0200267
268 taos->client = taos_instantiate_device(adapter);
269 return 0;
270
271 exit_close:
272 serio_close(serio);
273 exit_kfree:
Jean Delvareb9cdad72007-07-12 14:12:31 +0200274 kfree(taos);
275 exit:
276 return err;
277}
278
279static void taos_disconnect(struct serio *serio)
280{
281 struct taos_data *taos = serio_get_drvdata(serio);
282
283 if (taos->client)
284 i2c_unregister_device(taos->client);
285 i2c_del_adapter(&taos->adapter);
286 serio_close(serio);
Jean Delvareb9cdad72007-07-12 14:12:31 +0200287 kfree(taos);
288
Jean Delvare9b640f22011-06-29 11:36:10 +0200289 dev_info(&serio->dev, "Disconnected from TAOS EVM\n");
Jean Delvareb9cdad72007-07-12 14:12:31 +0200290}
291
292static struct serio_device_id taos_serio_ids[] = {
293 {
294 .type = SERIO_RS232,
295 .proto = SERIO_TAOSEVM,
296 .id = SERIO_ANY,
297 .extra = SERIO_ANY,
298 },
299 { 0 }
300};
301MODULE_DEVICE_TABLE(serio, taos_serio_ids);
302
303static struct serio_driver taos_drv = {
304 .driver = {
305 .name = "taos-evm",
306 },
307 .description = "TAOS evaluation module driver",
308 .id_table = taos_serio_ids,
309 .connect = taos_connect,
310 .disconnect = taos_disconnect,
311 .interrupt = taos_interrupt,
312};
313
314static int __init taos_init(void)
315{
316 return serio_register_driver(&taos_drv);
317}
318
319static void __exit taos_exit(void)
320{
321 serio_unregister_driver(&taos_drv);
322}
323
324MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
325MODULE_DESCRIPTION("TAOS evaluation module driver");
326MODULE_LICENSE("GPL");
327
328module_init(taos_init);
329module_exit(taos_exit);