2 * Driver for the TAOS evaluation modules
3 * These devices include an I2C master which can be controlled over the
6 * Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
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.
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.
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
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>
31 #define TAOS_BUFFER_SIZE 63
33 #define TAOS_STATE_INIT 0
34 #define TAOS_STATE_IDLE 1
35 #define TAOS_STATE_EOFF 2
36 #define TAOS_STATE_RECV 3
38 #define TAOS_CMD_RESET 0x12
39 #define TAOS_CMD_ECHO_ON '+'
40 #define TAOS_CMD_ECHO_OFF '-'
42 static DECLARE_WAIT_QUEUE_HEAD(wq
);
45 struct i2c_adapter adapter
;
46 struct i2c_client
*client
;
48 u8 addr
; /* last used address */
49 unsigned char buffer
[TAOS_BUFFER_SIZE
];
50 unsigned int pos
; /* position inside the buffer */
53 /* TAOS TSL2550 EVM */
54 static struct i2c_board_info tsl2550_info
= {
55 I2C_BOARD_INFO("tsl2550", 0x39),
58 /* Instantiate i2c devices based on the adapter name */
59 static struct i2c_client
*taos_instantiate_device(struct i2c_adapter
*adapter
)
61 if (!strncmp(adapter
->name
, "TAOS TSL2550 EVM", 16)) {
62 dev_info(&adapter
->dev
, "Instantiating device %s at 0x%02x\n",
63 tsl2550_info
.type
, tsl2550_info
.addr
);
64 return i2c_new_device(adapter
, &tsl2550_info
);
70 static 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
)
74 struct serio
*serio
= adapter
->algo_data
;
75 struct taos_data
*taos
= serio_get_drvdata(serio
);
78 /* Encode our transaction. "@" is for the device address, "$" for the
79 SMBus command and "#" for the data. */
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
);
89 if (read_write
== I2C_SMBUS_WRITE
)
90 sprintf(p
, "$#%02X", command
);
94 case I2C_SMBUS_BYTE_DATA
:
95 if (read_write
== I2C_SMBUS_WRITE
)
96 sprintf(p
, "$%02X#%02X", command
, data
->byte
);
98 sprintf(p
, "$%02X", command
);
101 dev_warn(&adapter
->dev
, "Unsupported transaction %d\n", size
);
105 /* Send the transaction to the TAOS EVM */
106 dev_dbg(&adapter
->dev
, "Command buffer: %s\n", taos
->buffer
);
107 for (p
= taos
->buffer
; *p
; p
++)
108 serio_write(serio
, *p
);
112 /* Start the transaction and read the answer */
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
120 dev_err(&adapter
->dev
, "Transaction timeout (pos=%d)\n",
124 dev_dbg(&adapter
->dev
, "Answer buffer: %s\n", taos
->buffer
);
126 /* Interpret the returned string */
127 p
= taos
->buffer
+ 1;
129 if (!strcmp(p
, "NAK"))
132 if (read_write
== I2C_SMBUS_WRITE
) {
133 if (!strcmp(p
, "ACK"))
137 data
->byte
= simple_strtol(p
+ 1, NULL
, 16);
145 static u32
taos_smbus_func(struct i2c_adapter
*adapter
)
147 return I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_SMBUS_BYTE_DATA
;
150 static const struct i2c_algorithm taos_algorithm
= {
151 .smbus_xfer
= taos_smbus_xfer
,
152 .functionality
= taos_smbus_func
,
155 static irqreturn_t
taos_interrupt(struct serio
*serio
, unsigned char data
,
158 struct taos_data
*taos
= serio_get_drvdata(serio
);
160 switch (taos
->state
) {
161 case TAOS_STATE_INIT
:
162 taos
->buffer
[taos
->pos
++] = 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
);
170 case TAOS_STATE_EOFF
:
171 taos
->state
= TAOS_STATE_IDLE
;
172 wake_up_interruptible(&wq
);
174 case TAOS_STATE_RECV
:
175 taos
->buffer
[taos
->pos
++] = data
;
177 taos
->buffer
[taos
->pos
] = '\0';
178 taos
->state
= TAOS_STATE_IDLE
;
179 wake_up_interruptible(&wq
);
187 /* Extract the adapter name from the buffer received after reset.
188 The buffer is modified and a pointer inside the buffer is returned. */
189 static char *taos_adapter_name(char *buffer
)
193 start
= strstr(buffer
, "TAOS ");
197 end
= strchr(start
, '\r');
205 static int taos_connect(struct serio
*serio
, struct serio_driver
*drv
)
207 struct taos_data
*taos
;
208 struct i2c_adapter
*adapter
;
212 taos
= kzalloc(sizeof(struct taos_data
), GFP_KERNEL
);
217 taos
->state
= TAOS_STATE_INIT
;
218 serio_set_drvdata(serio
, taos
);
220 err
= serio_open(serio
, drv
);
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
;
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));
235 if (taos
->state
!= TAOS_STATE_IDLE
) {
237 dev_err(&serio
->dev
, "TAOS EVM reset failed (state=%d, "
238 "pos=%d)\n", taos
->state
, taos
->pos
);
242 name
= taos_adapter_name(taos
->buffer
);
245 dev_err(&serio
->dev
, "TAOS EVM identification failed\n");
248 strlcpy(adapter
->name
, name
, sizeof(adapter
->name
));
250 /* Turn echo off for better performance */
251 taos
->state
= TAOS_STATE_EOFF
;
252 serio_write(serio
, TAOS_CMD_ECHO_OFF
);
254 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
255 msecs_to_jiffies(250));
256 if (taos
->state
!= TAOS_STATE_IDLE
) {
258 dev_err(&serio
->dev
, "TAOS EVM echo off failed "
259 "(state=%d)\n", taos
->state
);
263 err
= i2c_add_adapter(adapter
);
266 dev_info(&serio
->dev
, "Connected to TAOS EVM\n");
268 taos
->client
= taos_instantiate_device(adapter
);
279 static void taos_disconnect(struct serio
*serio
)
281 struct taos_data
*taos
= serio_get_drvdata(serio
);
284 i2c_unregister_device(taos
->client
);
285 i2c_del_adapter(&taos
->adapter
);
289 dev_info(&serio
->dev
, "Disconnected from TAOS EVM\n");
292 static struct serio_device_id taos_serio_ids
[] = {
295 .proto
= SERIO_TAOSEVM
,
301 MODULE_DEVICE_TABLE(serio
, taos_serio_ids
);
303 static struct serio_driver taos_drv
= {
307 .description
= "TAOS evaluation module driver",
308 .id_table
= taos_serio_ids
,
309 .connect
= taos_connect
,
310 .disconnect
= taos_disconnect
,
311 .interrupt
= taos_interrupt
,
314 static int __init
taos_init(void)
316 return serio_register_driver(&taos_drv
);
319 static void __exit
taos_exit(void)
321 serio_unregister_driver(&taos_drv
);
324 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
325 MODULE_DESCRIPTION("TAOS evaluation module driver");
326 MODULE_LICENSE("GPL");
328 module_init(taos_init
);
329 module_exit(taos_exit
);