2 * qt2160.c - Atmel AT42QT2160 Touch Sense Controller
4 * Copyright (C) 2009 Raphael Derosso Pereira <raphaelpereira@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/i2c.h>
27 #include <linux/irq.h>
28 #include <linux/interrupt.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[] = {
46 KEY_0, KEY_1, KEY_2, KEY_3,
47 KEY_4, KEY_5, KEY_6, KEY_7,
48 KEY_8, KEY_9, KEY_A, KEY_B,
49 KEY_C, KEY_D, KEY_E, KEY_F,
53 struct i2c_client *client;
54 struct input_dev *input;
55 struct delayed_work dwork;
56 spinlock_t lock; /* Protects canceling/rescheduling of dwork */
57 unsigned short keycodes[ARRAY_SIZE(qt2160_key2code)];
61 static int qt2160_read_block(struct i2c_client *client,
62 u8 inireg, u8 *buffer, unsigned int count)
67 * Can't use SMBus block data read. Check for I2C functionality to speed
68 * things up whenever possible. Otherwise we will be forced to read
71 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
73 error = i2c_smbus_write_byte(client, inireg + idx);
76 "couldn't send request. Returned %d\n", error);
80 error = i2c_master_recv(client, buffer, count);
83 "couldn't read registers. Returned %d bytes\n", error);
91 error = i2c_smbus_write_byte(client, inireg + idx);
94 "couldn't send request. Returned %d\n", error);
98 data = i2c_smbus_read_byte(client);
100 dev_err(&client->dev,
101 "couldn't read register. Returned %d\n", data);
105 buffer[idx++] = data;
112 static int qt2160_get_key_matrix(struct qt2160_data *qt2160)
114 struct i2c_client *client = qt2160->client;
115 struct input_dev *input = qt2160->input;
117 u16 old_matrix, new_matrix;
120 dev_dbg(&client->dev, "requesting keys...\n");
123 * Read all registers from General Status Register
126 ret = qt2160_read_block(client, QT2160_CMD_GSTAT, regs, 6);
128 dev_err(&client->dev,
129 "could not perform chip read.\n");
133 old_matrix = qt2160->key_matrix;
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);
142 dev_dbg(&client->dev, "key %d %s\n",
143 i, keyval ? "pressed" : "released");
152 static irqreturn_t qt2160_irq(int irq, void *_qt2160)
154 struct qt2160_data *qt2160 = _qt2160;
157 spin_lock_irqsave(&qt2160->lock, flags);
159 mod_delayed_work(system_wq, &qt2160->dwork, 0);
161 spin_unlock_irqrestore(&qt2160->lock, flags);
166 static void qt2160_schedule_read(struct qt2160_data *qt2160)
168 spin_lock_irq(&qt2160->lock);
169 schedule_delayed_work(&qt2160->dwork, QT2160_CYCLE_INTERVAL);
170 spin_unlock_irq(&qt2160->lock);
173 static void qt2160_worker(struct work_struct *work)
175 struct qt2160_data *qt2160 =
176 container_of(work, struct qt2160_data, dwork.work);
178 dev_dbg(&qt2160->client->dev, "worker\n");
180 qt2160_get_key_matrix(qt2160);
182 /* Avoid device lock up by checking every so often */
183 qt2160_schedule_read(qt2160);
186 static int qt2160_read(struct i2c_client *client, u8 reg)
190 ret = i2c_smbus_write_byte(client, reg);
192 dev_err(&client->dev,
193 "couldn't send request. Returned %d\n", ret);
197 ret = i2c_smbus_read_byte(client);
199 dev_err(&client->dev,
200 "couldn't read register. Returned %d\n", ret);
207 static int qt2160_write(struct i2c_client *client, u8 reg, u8 data)
211 ret = i2c_smbus_write_byte_data(client, reg, data);
213 dev_err(&client->dev,
214 "couldn't write data. Returned %d\n", ret);
220 static bool qt2160_identify(struct i2c_client *client)
224 /* Read Chid ID to check if chip is valid */
225 id = qt2160_read(client, QT2160_CMD_CHIPID);
226 if (id != QT2160_VALID_CHIPID) {
227 dev_err(&client->dev, "ID %d not supported\n", id);
231 /* Read chip firmware version */
232 ver = qt2160_read(client, QT2160_CMD_CODEVER);
234 dev_err(&client->dev, "could not get firmware version\n");
238 /* Read chip firmware revision */
239 rev = qt2160_read(client, QT2160_CMD_SUBVER);
241 dev_err(&client->dev, "could not get firmware revision\n");
245 dev_info(&client->dev, "AT42QT2160 firmware version %d.%d.%d\n",
246 ver >> 4, ver & 0xf, rev);
251 static int qt2160_probe(struct i2c_client *client,
252 const struct i2c_device_id *id)
254 struct qt2160_data *qt2160;
255 struct input_dev *input;
259 /* Check functionality */
260 error = i2c_check_functionality(client->adapter,
261 I2C_FUNC_SMBUS_BYTE);
263 dev_err(&client->dev, "%s adapter not supported\n",
264 dev_driver_string(&client->adapter->dev));
268 if (!qt2160_identify(client))
271 /* Chip is valid and active. Allocate structure */
272 qt2160 = kzalloc(sizeof(struct qt2160_data), GFP_KERNEL);
273 input = input_allocate_device();
274 if (!qt2160 || !input) {
275 dev_err(&client->dev, "insufficient memory\n");
280 qt2160->client = client;
281 qt2160->input = input;
282 INIT_DELAYED_WORK(&qt2160->dwork, qt2160_worker);
283 spin_lock_init(&qt2160->lock);
285 input->name = "AT42QT2160 Touch Sense Keyboard";
286 input->id.bustype = BUS_I2C;
288 input->keycode = qt2160->keycodes;
289 input->keycodesize = sizeof(qt2160->keycodes[0]);
290 input->keycodemax = ARRAY_SIZE(qt2160_key2code);
292 __set_bit(EV_KEY, input->evbit);
293 __clear_bit(EV_REP, input->evbit);
294 for (i = 0; i < ARRAY_SIZE(qt2160_key2code); i++) {
295 qt2160->keycodes[i] = qt2160_key2code[i];
296 __set_bit(qt2160_key2code[i], input->keybit);
298 __clear_bit(KEY_RESERVED, input->keybit);
300 /* Calibrate device */
301 error = qt2160_write(client, QT2160_CMD_CALIBRATE, 1);
303 dev_err(&client->dev, "failed to calibrate device\n");
308 error = request_irq(client->irq, qt2160_irq,
309 IRQF_TRIGGER_FALLING, "qt2160", qt2160);
311 dev_err(&client->dev,
312 "failed to allocate irq %d\n", client->irq);
317 error = input_register_device(qt2160->input);
319 dev_err(&client->dev,
320 "Failed to register input device\n");
324 i2c_set_clientdata(client, qt2160);
325 qt2160_schedule_read(qt2160);
331 free_irq(client->irq, qt2160);
333 input_free_device(input);
338 static int qt2160_remove(struct i2c_client *client)
340 struct qt2160_data *qt2160 = i2c_get_clientdata(client);
342 /* Release IRQ so no queue will be scheduled */
344 free_irq(client->irq, qt2160);
346 cancel_delayed_work_sync(&qt2160->dwork);
348 input_unregister_device(qt2160->input);
354 static const struct i2c_device_id qt2160_idtable[] = {
359 MODULE_DEVICE_TABLE(i2c, qt2160_idtable);
361 static struct i2c_driver qt2160_driver = {
364 .owner = THIS_MODULE,
367 .id_table = qt2160_idtable,
368 .probe = qt2160_probe,
369 .remove = qt2160_remove,
372 module_i2c_driver(qt2160_driver);
374 MODULE_AUTHOR("Raphael Derosso Pereira <raphaelpereira@gmail.com>");
375 MODULE_DESCRIPTION("Driver for AT42QT2160 Touch Sensor");
376 MODULE_LICENSE("GPL");