21 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
26 #include <linux/i2c.h>
29 #include <linux/input.h>
31 #define QT2160_VALID_CHIPID 0x11
33 #define QT2160_CMD_CHIPID 0
34 #define QT2160_CMD_CODEVER 1
35 #define QT2160_CMD_GSTAT 2
36 #define QT2160_CMD_KEYS3 3
37 #define QT2160_CMD_KEYS4 4
38 #define QT2160_CMD_SLIDE 5
39 #define QT2160_CMD_GPIOS 6
40 #define QT2160_CMD_SUBVER 7
41 #define QT2160_CMD_CALIBRATE 10
43 #define QT2160_CYCLE_INTERVAL (2*HZ)
45 static unsigned char qt2160_key2code[] = {
76 "couldn't send request. Returned %d\n", error);
83 "couldn't read registers. Returned %d bytes\n", error);
94 "couldn't send request. Returned %d\n", error);
101 "couldn't read register. Returned %d\n", data);
105 buffer[idx++] =
data;
112 static int qt2160_get_key_matrix(
struct qt2160_data *qt2160)
117 u16 old_matrix, new_matrix;
120 dev_dbg(&client->
dev,
"requesting keys...\n");
129 "could not perform chip read.\n");
134 qt2160->
key_matrix = new_matrix = (regs[2] << 8) | regs[1];
137 for (i = 0; i < 16; ++
i, mask <<= 1) {
138 int keyval = new_matrix &
mask;
140 if ((old_matrix & mask) != keyval) {
141 input_report_key(input, qt2160->
keycodes[i], keyval);
143 i, keyval ?
"pressed" :
"released");
152 static irqreturn_t qt2160_irq(
int irq,
void *_qt2160)
161 spin_unlock_irqrestore(&qt2160->
lock, flags);
166 static void qt2160_schedule_read(
struct qt2160_data *qt2160)
168 spin_lock_irq(&qt2160->
lock);
170 spin_unlock_irq(&qt2160->
lock);
180 qt2160_get_key_matrix(qt2160);
183 qt2160_schedule_read(qt2160);
193 "couldn't send request. Returned %d\n", ret);
200 "couldn't read register. Returned %d\n", ret);
214 "couldn't send request. Returned %d\n", error);
221 "couldn't write data. Returned %d\n", error);
236 dev_err(&client->
dev,
"ID %d not supported\n",
id);
243 dev_err(&client->
dev,
"could not get firmware version\n");
250 dev_err(&client->
dev,
"could not get firmware revision\n");
254 dev_info(&client->
dev,
"AT42QT2160 firmware version %d.%d.%d\n",
255 ver >> 4, ver & 0xf, rev);
264 struct input_dev *
input;
269 error = i2c_check_functionality(client->
adapter,
272 dev_err(&client->
dev,
"%s adapter not supported\n",
277 if (!qt2160_identify(client))
282 input = input_allocate_device();
283 if (!qt2160 || !input) {
284 dev_err(&client->
dev,
"insufficient memory\n");
294 input->name =
"AT42QT2160 Touch Sense Keyboard";
298 input->keycodesize =
sizeof(qt2160->
keycodes[0]);
299 input->keycodemax =
ARRAY_SIZE(qt2160_key2code);
303 for (i = 0; i <
ARRAY_SIZE(qt2160_key2code); i++) {
305 __set_bit(qt2160_key2code[i], input->keybit);
312 dev_err(&client->
dev,
"failed to calibrate device\n");
321 "failed to allocate irq %d\n", client->
irq);
326 error = input_register_device(qt2160->
input);
329 "Failed to register input device\n");
333 i2c_set_clientdata(client, qt2160);
334 qt2160_schedule_read(qt2160);
342 input_free_device(input);
349 struct qt2160_data *qt2160 = i2c_get_clientdata(client);
357 input_unregister_device(qt2160->
input);
376 .id_table = qt2160_idtable,
377 .probe = qt2160_probe,