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),
56 /* Instantiate i2c devices based on the adapter name */
57 static struct i2c_client
*taos_instantiate_device(struct i2c_adapter
*adapter
)
59 if (!strncmp(adapter
->name
, "TAOS TSL2550 EVM", 16)) {
60 dev_info(&adapter
->dev
, "Instantiating device %s at 0x%02x\n",
61 tsl2550_info
.type
, tsl2550_info
.addr
);
62 return i2c_new_device(adapter
, &tsl2550_info
);
68 static int taos_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
69 unsigned short flags
, char read_write
, u8 command
,
70 int size
, union i2c_smbus_data
*data
)
72 struct serio
*serio
= adapter
->algo_data
;
73 struct taos_data
*taos
= serio_get_drvdata(serio
);
76 /* Encode our transaction. "@" is for the device address, "$" for the
77 SMBus command and "#" for the data. */
80 /* The device remembers the last used address, no need to send it
81 again if it's the same */
82 if (addr
!= taos
->addr
)
83 p
+= sprintf(p
, "@%02X", addr
);
87 if (read_write
== I2C_SMBUS_WRITE
)
88 sprintf(p
, "$#%02X", command
);
92 case I2C_SMBUS_BYTE_DATA
:
93 if (read_write
== I2C_SMBUS_WRITE
)
94 sprintf(p
, "$%02X#%02X", command
, data
->byte
);
96 sprintf(p
, "$%02X", command
);
99 dev_dbg(&adapter
->dev
, "Unsupported transaction size %d\n",
104 /* Send the transaction to the TAOS EVM */
105 dev_dbg(&adapter
->dev
, "Command buffer: %s\n", taos
->buffer
);
107 taos
->state
= TAOS_STATE_SEND
;
108 serio_write(serio
, taos
->buffer
[0]);
109 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
110 msecs_to_jiffies(250));
111 if (taos
->state
!= TAOS_STATE_IDLE
) {
112 dev_err(&adapter
->dev
, "Transaction failed "
113 "(state=%d, pos=%d)\n", taos
->state
, taos
->pos
);
119 /* Start the transaction and read the answer */
121 taos
->state
= TAOS_STATE_RECV
;
122 serio_write(serio
, read_write
== I2C_SMBUS_WRITE
? '>' : '<');
123 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
124 msecs_to_jiffies(150));
125 if (taos
->state
!= TAOS_STATE_IDLE
127 dev_err(&adapter
->dev
, "Transaction timeout (pos=%d)\n",
131 dev_dbg(&adapter
->dev
, "Answer buffer: %s\n", taos
->buffer
);
133 /* Interpret the returned string */
134 p
= taos
->buffer
+ 2;
136 if (!strcmp(p
, "NAK"))
139 if (read_write
== I2C_SMBUS_WRITE
) {
140 if (!strcmp(p
, "ACK"))
144 data
->byte
= simple_strtol(p
+ 1, NULL
, 16);
152 static u32
taos_smbus_func(struct i2c_adapter
*adapter
)
154 return I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_SMBUS_BYTE_DATA
;
157 static const struct i2c_algorithm taos_algorithm
= {
158 .smbus_xfer
= taos_smbus_xfer
,
159 .functionality
= taos_smbus_func
,
162 static irqreturn_t
taos_interrupt(struct serio
*serio
, unsigned char data
,
165 struct taos_data
*taos
= serio_get_drvdata(serio
);
167 switch (taos
->state
) {
168 case TAOS_STATE_INIT
:
169 taos
->buffer
[taos
->pos
++] = data
;
171 || taos
->pos
== TAOS_BUFFER_SIZE
- 1) {
172 taos
->buffer
[taos
->pos
] = '\0';
173 taos
->state
= TAOS_STATE_IDLE
;
174 wake_up_interruptible(&wq
);
177 case TAOS_STATE_SEND
:
178 if (taos
->buffer
[++taos
->pos
])
179 serio_write(serio
, taos
->buffer
[taos
->pos
]);
181 taos
->state
= TAOS_STATE_IDLE
;
182 wake_up_interruptible(&wq
);
185 case TAOS_STATE_RECV
:
186 taos
->buffer
[taos
->pos
++] = data
;
188 taos
->buffer
[taos
->pos
] = '\0';
189 taos
->state
= TAOS_STATE_IDLE
;
190 wake_up_interruptible(&wq
);
198 /* Extract the adapter name from the buffer received after reset.
199 The buffer is modified and a pointer inside the buffer is returned. */
200 static char *taos_adapter_name(char *buffer
)
204 start
= strstr(buffer
, "TAOS ");
208 end
= strchr(start
, '\r');
216 static int taos_connect(struct serio
*serio
, struct serio_driver
*drv
)
218 struct taos_data
*taos
;
219 struct i2c_adapter
*adapter
;
223 taos
= kzalloc(sizeof(struct taos_data
), GFP_KERNEL
);
228 taos
->state
= TAOS_STATE_INIT
;
229 serio_set_drvdata(serio
, taos
);
231 err
= serio_open(serio
, drv
);
235 adapter
= &taos
->adapter
;
236 adapter
->owner
= THIS_MODULE
;
237 adapter
->algo
= &taos_algorithm
;
238 adapter
->algo_data
= serio
;
239 adapter
->dev
.parent
= &serio
->dev
;
241 /* Reset the TAOS evaluation module to identify it */
242 serio_write(serio
, TAOS_CMD_RESET
);
243 wait_event_interruptible_timeout(wq
, taos
->state
== TAOS_STATE_IDLE
,
244 msecs_to_jiffies(2000));
246 if (taos
->state
!= TAOS_STATE_IDLE
) {
248 dev_dbg(&serio
->dev
, "TAOS EVM reset failed (state=%d, "
249 "pos=%d)\n", taos
->state
, taos
->pos
);
253 name
= taos_adapter_name(taos
->buffer
);
256 dev_err(&serio
->dev
, "TAOS EVM identification failed\n");
259 strlcpy(adapter
->name
, name
, sizeof(adapter
->name
));
261 err
= i2c_add_adapter(adapter
);
264 dev_dbg(&serio
->dev
, "Connected to TAOS EVM\n");
266 taos
->client
= taos_instantiate_device(adapter
);
272 serio_set_drvdata(serio
, NULL
);
278 static void taos_disconnect(struct serio
*serio
)
280 struct taos_data
*taos
= serio_get_drvdata(serio
);
283 i2c_unregister_device(taos
->client
);
284 i2c_del_adapter(&taos
->adapter
);
286 serio_set_drvdata(serio
, NULL
);
289 dev_dbg(&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
);