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_SEND 2
36 #define TAOS_STATE_RECV 3
38 #define TAOS_CMD_RESET 0x12
40 static DECLARE_WAIT_QUEUE_HEAD(wq
);
43 struct i2c_adapter adapter
;
44 struct i2c_client
*client
;
46 u8 addr
; /* last used address */
47 unsigned char buffer
[TAOS_BUFFER_SIZE
];
48 unsigned int pos
; /* position inside the buffer */
51 /* TAOS TSL2550 EVM */
52 static struct i2c_board_info tsl2550_info
= {
53 I2C_BOARD_INFO("tsl2550", 0x39),
57 /* Instantiate i2c devices based on the adapter name */
58 static struct i2c_client
*taos_instantiate_device(struct i2c_adapter
*adapter
)
60 if (!strncmp(adapter
->name
, "TAOS TSL2550 EVM", 16)) {
61 dev_info(&adapter
->dev
, "Instantiating device %s at 0x%02x\n",
62 tsl2550_info
.driver_name
, tsl2550_info
.addr
);
63 return i2c_new_device(adapter
, &tsl2550_info
);
69 static int taos_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
70 unsigned short flags
, char read_write
, u8 command
,
71 int size
, union i2c_smbus_data
*data
)
73 struct serio
*serio
= adapter
->algo_data
;
74 struct taos_data
*taos
= serio_get_drvdata(serio
);
77 /* Encode our transaction. "@" is for the device address, "$" for the
78 SMBus command and "#" for the data. */
81 /* The device remembers the last used address, no need to send it
82 again if it's the same */
83 if (addr
!= taos
->addr
)
84 p
+= sprintf(p
, "@%02X", addr
);
88 if (read_write
== I2C_SMBUS_WRITE
)
89 sprintf(p
, "$#%02X", command
);
93 case I2C_SMBUS_BYTE_DATA
:
94 if (read_write
== I2C_SMBUS_WRITE
)
95 sprintf(p
, "$%02X#%02X", command
, data
->byte
);
97 sprintf(p
, "$%02X", command
);
100 dev_dbg(&adapter
->dev
, "Unsupported transaction size %d\n",
105 /* Send the transaction to the TAOS EVM */
106 dev_dbg(&adapter
->dev
, "Command buffer: %s\n", taos
->buffer
);
108 taos
->state
= TAOS_STATE_SEND
;
109 serio_write(serio
, taos
->buffer
[0]);
110 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
111 msecs_to_jiffies(250));
112 if (taos
->state
!= TAOS_STATE_IDLE
) {
113 dev_err(&adapter
->dev
, "Transaction failed "
114 "(state=%d, pos=%d)\n", taos
->state
, taos
->pos
);
120 /* Start the transaction and read the answer */
122 taos
->state
= TAOS_STATE_RECV
;
123 serio_write(serio
, read_write
== I2C_SMBUS_WRITE
? '>' : '<');
124 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
125 msecs_to_jiffies(150));
126 if (taos
->state
!= TAOS_STATE_IDLE
128 dev_err(&adapter
->dev
, "Transaction timeout (pos=%d)\n",
132 dev_dbg(&adapter
->dev
, "Answer buffer: %s\n", taos
->buffer
);
134 /* Interpret the returned string */
135 p
= taos
->buffer
+ 2;
137 if (!strcmp(p
, "NAK"))
140 if (read_write
== I2C_SMBUS_WRITE
) {
141 if (!strcmp(p
, "ACK"))
145 data
->byte
= simple_strtol(p
+ 1, NULL
, 16);
153 static u32
taos_smbus_func(struct i2c_adapter
*adapter
)
155 return I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_SMBUS_BYTE_DATA
;
158 static const struct i2c_algorithm taos_algorithm
= {
159 .smbus_xfer
= taos_smbus_xfer
,
160 .functionality
= taos_smbus_func
,
163 static irqreturn_t
taos_interrupt(struct serio
*serio
, unsigned char data
,
166 struct taos_data
*taos
= serio_get_drvdata(serio
);
168 switch (taos
->state
) {
169 case TAOS_STATE_INIT
:
170 taos
->buffer
[taos
->pos
++] = data
;
172 || taos
->pos
== TAOS_BUFFER_SIZE
- 1) {
173 taos
->buffer
[taos
->pos
] = '\0';
174 taos
->state
= TAOS_STATE_IDLE
;
175 wake_up_interruptible(&wq
);
178 case TAOS_STATE_SEND
:
179 if (taos
->buffer
[++taos
->pos
])
180 serio_write(serio
, taos
->buffer
[taos
->pos
]);
182 taos
->state
= TAOS_STATE_IDLE
;
183 wake_up_interruptible(&wq
);
186 case TAOS_STATE_RECV
:
187 taos
->buffer
[taos
->pos
++] = data
;
189 taos
->buffer
[taos
->pos
] = '\0';
190 taos
->state
= TAOS_STATE_IDLE
;
191 wake_up_interruptible(&wq
);
199 /* Extract the adapter name from the buffer received after reset.
200 The buffer is modified and a pointer inside the buffer is returned. */
201 static char *taos_adapter_name(char *buffer
)
205 start
= strstr(buffer
, "TAOS ");
209 end
= strchr(start
, '\r');
217 static int taos_connect(struct serio
*serio
, struct serio_driver
*drv
)
219 struct taos_data
*taos
;
220 struct i2c_adapter
*adapter
;
224 taos
= kzalloc(sizeof(struct taos_data
), GFP_KERNEL
);
229 taos
->state
= TAOS_STATE_INIT
;
230 serio_set_drvdata(serio
, taos
);
232 err
= serio_open(serio
, drv
);
236 adapter
= &taos
->adapter
;
237 adapter
->owner
= THIS_MODULE
;
238 adapter
->algo
= &taos_algorithm
;
239 adapter
->algo_data
= serio
;
240 adapter
->dev
.parent
= &serio
->dev
;
242 /* Reset the TAOS evaluation module to identify it */
243 serio_write(serio
, TAOS_CMD_RESET
);
244 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
245 msecs_to_jiffies(2000));
247 if (taos
->state
!= TAOS_STATE_IDLE
) {
249 dev_dbg(&serio
->dev
, "TAOS EVM reset failed (state=%d, "
250 "pos=%d)\n", taos
->state
, taos
->pos
);
254 name
= taos_adapter_name(taos
->buffer
);
257 dev_err(&serio
->dev
, "TAOS EVM identification failed\n");
260 strlcpy(adapter
->name
, name
, sizeof(adapter
->name
));
262 err
= i2c_add_adapter(adapter
);
265 dev_dbg(&serio
->dev
, "Connected to TAOS EVM\n");
267 taos
->client
= taos_instantiate_device(adapter
);
273 serio_set_drvdata(serio
, NULL
);
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
);
287 serio_set_drvdata(serio
, NULL
);
290 dev_dbg(&serio
->dev
, "Disconnected from TAOS EVM\n");
293 static struct serio_device_id taos_serio_ids
[] = {
296 .proto
= SERIO_TAOSEVM
,
302 MODULE_DEVICE_TABLE(serio
, taos_serio_ids
);
304 static struct serio_driver taos_drv
= {
308 .description
= "TAOS evaluation module driver",
309 .id_table
= taos_serio_ids
,
310 .connect
= taos_connect
,
311 .disconnect
= taos_disconnect
,
312 .interrupt
= taos_interrupt
,
315 static int __init
taos_init(void)
317 return serio_register_driver(&taos_drv
);
320 static void __exit
taos_exit(void)
322 serio_unregister_driver(&taos_drv
);
325 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
326 MODULE_DESCRIPTION("TAOS evaluation module driver");
327 MODULE_LICENSE("GPL");
329 module_init(taos_init
);
330 module_exit(taos_exit
);