PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / input / keyboard / qt2160.c
blob819b22897c1371050a4064ba8bf81d6d7bfd1e3d
1 /*
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/leds.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
42 #define QT2160_CMD_DRIVE_X 70
43 #define QT2160_CMD_PWMEN_X 74
44 #define QT2160_CMD_PWM_DUTY 76
46 #define QT2160_NUM_LEDS_X 8
48 #define QT2160_CYCLE_INTERVAL (2*HZ)
50 static unsigned char qt2160_key2code[] = {
51 KEY_0, KEY_1, KEY_2, KEY_3,
52 KEY_4, KEY_5, KEY_6, KEY_7,
53 KEY_8, KEY_9, KEY_A, KEY_B,
54 KEY_C, KEY_D, KEY_E, KEY_F,
57 #ifdef CONFIG_LEDS_CLASS
58 struct qt2160_led {
59 struct qt2160_data *qt2160;
60 struct led_classdev cdev;
61 struct work_struct work;
62 char name[32];
63 int id;
64 enum led_brightness new_brightness;
66 #endif
68 struct qt2160_data {
69 struct i2c_client *client;
70 struct input_dev *input;
71 struct delayed_work dwork;
72 spinlock_t lock; /* Protects canceling/rescheduling of dwork */
73 unsigned short keycodes[ARRAY_SIZE(qt2160_key2code)];
74 u16 key_matrix;
75 #ifdef CONFIG_LEDS_CLASS
76 struct qt2160_led leds[QT2160_NUM_LEDS_X];
77 struct mutex led_lock;
78 #endif
81 static int qt2160_read(struct i2c_client *client, u8 reg);
82 static int qt2160_write(struct i2c_client *client, u8 reg, u8 data);
84 #ifdef CONFIG_LEDS_CLASS
86 static void qt2160_led_work(struct work_struct *work)
88 struct qt2160_led *led = container_of(work, struct qt2160_led, work);
89 struct qt2160_data *qt2160 = led->qt2160;
90 struct i2c_client *client = qt2160->client;
91 int value = led->new_brightness;
92 u32 drive, pwmen;
94 mutex_lock(&qt2160->led_lock);
96 drive = qt2160_read(client, QT2160_CMD_DRIVE_X);
97 pwmen = qt2160_read(client, QT2160_CMD_PWMEN_X);
98 if (value != LED_OFF) {
99 drive |= (1 << led->id);
100 pwmen |= (1 << led->id);
102 } else {
103 drive &= ~(1 << led->id);
104 pwmen &= ~(1 << led->id);
106 qt2160_write(client, QT2160_CMD_DRIVE_X, drive);
107 qt2160_write(client, QT2160_CMD_PWMEN_X, pwmen);
110 * Changing this register will change the brightness
111 * of every LED in the qt2160. It's a HW limitation.
113 if (value != LED_OFF)
114 qt2160_write(client, QT2160_CMD_PWM_DUTY, value);
116 mutex_unlock(&qt2160->led_lock);
119 static void qt2160_led_set(struct led_classdev *cdev,
120 enum led_brightness value)
122 struct qt2160_led *led = container_of(cdev, struct qt2160_led, cdev);
124 led->new_brightness = value;
125 schedule_work(&led->work);
128 #endif /* CONFIG_LEDS_CLASS */
130 static int qt2160_read_block(struct i2c_client *client,
131 u8 inireg, u8 *buffer, unsigned int count)
133 int error, idx = 0;
136 * Can't use SMBus block data read. Check for I2C functionality to speed
137 * things up whenever possible. Otherwise we will be forced to read
138 * sequentially.
140 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
142 error = i2c_smbus_write_byte(client, inireg + idx);
143 if (error) {
144 dev_err(&client->dev,
145 "couldn't send request. Returned %d\n", error);
146 return error;
149 error = i2c_master_recv(client, buffer, count);
150 if (error != count) {
151 dev_err(&client->dev,
152 "couldn't read registers. Returned %d bytes\n", error);
153 return error;
155 } else {
157 while (count--) {
158 int data;
160 error = i2c_smbus_write_byte(client, inireg + idx);
161 if (error) {
162 dev_err(&client->dev,
163 "couldn't send request. Returned %d\n", error);
164 return error;
167 data = i2c_smbus_read_byte(client);
168 if (data < 0) {
169 dev_err(&client->dev,
170 "couldn't read register. Returned %d\n", data);
171 return data;
174 buffer[idx++] = data;
178 return 0;
181 static int qt2160_get_key_matrix(struct qt2160_data *qt2160)
183 struct i2c_client *client = qt2160->client;
184 struct input_dev *input = qt2160->input;
185 u8 regs[6];
186 u16 old_matrix, new_matrix;
187 int ret, i, mask;
189 dev_dbg(&client->dev, "requesting keys...\n");
192 * Read all registers from General Status Register
193 * to GPIOs register
195 ret = qt2160_read_block(client, QT2160_CMD_GSTAT, regs, 6);
196 if (ret) {
197 dev_err(&client->dev,
198 "could not perform chip read.\n");
199 return ret;
202 old_matrix = qt2160->key_matrix;
203 qt2160->key_matrix = new_matrix = (regs[2] << 8) | regs[1];
205 mask = 0x01;
206 for (i = 0; i < 16; ++i, mask <<= 1) {
207 int keyval = new_matrix & mask;
209 if ((old_matrix & mask) != keyval) {
210 input_report_key(input, qt2160->keycodes[i], keyval);
211 dev_dbg(&client->dev, "key %d %s\n",
212 i, keyval ? "pressed" : "released");
216 input_sync(input);
218 return 0;
221 static irqreturn_t qt2160_irq(int irq, void *_qt2160)
223 struct qt2160_data *qt2160 = _qt2160;
224 unsigned long flags;
226 spin_lock_irqsave(&qt2160->lock, flags);
228 mod_delayed_work(system_wq, &qt2160->dwork, 0);
230 spin_unlock_irqrestore(&qt2160->lock, flags);
232 return IRQ_HANDLED;
235 static void qt2160_schedule_read(struct qt2160_data *qt2160)
237 spin_lock_irq(&qt2160->lock);
238 schedule_delayed_work(&qt2160->dwork, QT2160_CYCLE_INTERVAL);
239 spin_unlock_irq(&qt2160->lock);
242 static void qt2160_worker(struct work_struct *work)
244 struct qt2160_data *qt2160 =
245 container_of(work, struct qt2160_data, dwork.work);
247 dev_dbg(&qt2160->client->dev, "worker\n");
249 qt2160_get_key_matrix(qt2160);
251 /* Avoid device lock up by checking every so often */
252 qt2160_schedule_read(qt2160);
255 static int qt2160_read(struct i2c_client *client, u8 reg)
257 int ret;
259 ret = i2c_smbus_write_byte(client, reg);
260 if (ret) {
261 dev_err(&client->dev,
262 "couldn't send request. Returned %d\n", ret);
263 return ret;
266 ret = i2c_smbus_read_byte(client);
267 if (ret < 0) {
268 dev_err(&client->dev,
269 "couldn't read register. Returned %d\n", ret);
270 return ret;
273 return ret;
276 static int qt2160_write(struct i2c_client *client, u8 reg, u8 data)
278 int ret;
280 ret = i2c_smbus_write_byte_data(client, reg, data);
281 if (ret < 0)
282 dev_err(&client->dev,
283 "couldn't write data. Returned %d\n", ret);
285 return ret;
288 #ifdef CONFIG_LEDS_CLASS
290 static int qt2160_register_leds(struct qt2160_data *qt2160)
292 struct i2c_client *client = qt2160->client;
293 int ret;
294 int i;
296 mutex_init(&qt2160->led_lock);
298 for (i = 0; i < QT2160_NUM_LEDS_X; i++) {
299 struct qt2160_led *led = &qt2160->leds[i];
301 snprintf(led->name, sizeof(led->name), "qt2160:x%d", i);
302 led->cdev.name = led->name;
303 led->cdev.brightness_set = qt2160_led_set;
304 led->cdev.brightness = LED_OFF;
305 led->id = i;
306 led->qt2160 = qt2160;
308 INIT_WORK(&led->work, qt2160_led_work);
310 ret = led_classdev_register(&client->dev, &led->cdev);
311 if (ret < 0)
312 return ret;
315 /* Tur off LEDs */
316 qt2160_write(client, QT2160_CMD_DRIVE_X, 0);
317 qt2160_write(client, QT2160_CMD_PWMEN_X, 0);
318 qt2160_write(client, QT2160_CMD_PWM_DUTY, 0);
320 return 0;
323 static void qt2160_unregister_leds(struct qt2160_data *qt2160)
325 int i;
327 for (i = 0; i < QT2160_NUM_LEDS_X; i++) {
328 led_classdev_unregister(&qt2160->leds[i].cdev);
329 cancel_work_sync(&qt2160->leds[i].work);
333 #else
335 static inline int qt2160_register_leds(struct qt2160_data *qt2160)
337 return 0;
340 static inline void qt2160_unregister_leds(struct qt2160_data *qt2160)
344 #endif
346 static bool qt2160_identify(struct i2c_client *client)
348 int id, ver, rev;
350 /* Read Chid ID to check if chip is valid */
351 id = qt2160_read(client, QT2160_CMD_CHIPID);
352 if (id != QT2160_VALID_CHIPID) {
353 dev_err(&client->dev, "ID %d not supported\n", id);
354 return false;
357 /* Read chip firmware version */
358 ver = qt2160_read(client, QT2160_CMD_CODEVER);
359 if (ver < 0) {
360 dev_err(&client->dev, "could not get firmware version\n");
361 return false;
364 /* Read chip firmware revision */
365 rev = qt2160_read(client, QT2160_CMD_SUBVER);
366 if (rev < 0) {
367 dev_err(&client->dev, "could not get firmware revision\n");
368 return false;
371 dev_info(&client->dev, "AT42QT2160 firmware version %d.%d.%d\n",
372 ver >> 4, ver & 0xf, rev);
374 return true;
377 static int qt2160_probe(struct i2c_client *client,
378 const struct i2c_device_id *id)
380 struct qt2160_data *qt2160;
381 struct input_dev *input;
382 int i;
383 int error;
385 /* Check functionality */
386 error = i2c_check_functionality(client->adapter,
387 I2C_FUNC_SMBUS_BYTE);
388 if (!error) {
389 dev_err(&client->dev, "%s adapter not supported\n",
390 dev_driver_string(&client->adapter->dev));
391 return -ENODEV;
394 if (!qt2160_identify(client))
395 return -ENODEV;
397 /* Chip is valid and active. Allocate structure */
398 qt2160 = kzalloc(sizeof(struct qt2160_data), GFP_KERNEL);
399 input = input_allocate_device();
400 if (!qt2160 || !input) {
401 dev_err(&client->dev, "insufficient memory\n");
402 error = -ENOMEM;
403 goto err_free_mem;
406 qt2160->client = client;
407 qt2160->input = input;
408 INIT_DELAYED_WORK(&qt2160->dwork, qt2160_worker);
409 spin_lock_init(&qt2160->lock);
411 input->name = "AT42QT2160 Touch Sense Keyboard";
412 input->id.bustype = BUS_I2C;
414 input->keycode = qt2160->keycodes;
415 input->keycodesize = sizeof(qt2160->keycodes[0]);
416 input->keycodemax = ARRAY_SIZE(qt2160_key2code);
418 __set_bit(EV_KEY, input->evbit);
419 __clear_bit(EV_REP, input->evbit);
420 for (i = 0; i < ARRAY_SIZE(qt2160_key2code); i++) {
421 qt2160->keycodes[i] = qt2160_key2code[i];
422 __set_bit(qt2160_key2code[i], input->keybit);
424 __clear_bit(KEY_RESERVED, input->keybit);
426 /* Calibrate device */
427 error = qt2160_write(client, QT2160_CMD_CALIBRATE, 1);
428 if (error) {
429 dev_err(&client->dev, "failed to calibrate device\n");
430 goto err_free_mem;
433 if (client->irq) {
434 error = request_irq(client->irq, qt2160_irq,
435 IRQF_TRIGGER_FALLING, "qt2160", qt2160);
436 if (error) {
437 dev_err(&client->dev,
438 "failed to allocate irq %d\n", client->irq);
439 goto err_free_mem;
443 error = qt2160_register_leds(qt2160);
444 if (error) {
445 dev_err(&client->dev, "Failed to register leds\n");
446 goto err_free_irq;
449 error = input_register_device(qt2160->input);
450 if (error) {
451 dev_err(&client->dev,
452 "Failed to register input device\n");
453 goto err_unregister_leds;
456 i2c_set_clientdata(client, qt2160);
457 qt2160_schedule_read(qt2160);
459 return 0;
461 err_unregister_leds:
462 qt2160_unregister_leds(qt2160);
463 err_free_irq:
464 if (client->irq)
465 free_irq(client->irq, qt2160);
466 err_free_mem:
467 input_free_device(input);
468 kfree(qt2160);
469 return error;
472 static int qt2160_remove(struct i2c_client *client)
474 struct qt2160_data *qt2160 = i2c_get_clientdata(client);
476 qt2160_unregister_leds(qt2160);
478 /* Release IRQ so no queue will be scheduled */
479 if (client->irq)
480 free_irq(client->irq, qt2160);
482 cancel_delayed_work_sync(&qt2160->dwork);
484 input_unregister_device(qt2160->input);
485 kfree(qt2160);
487 return 0;
490 static const struct i2c_device_id qt2160_idtable[] = {
491 { "qt2160", 0, },
495 MODULE_DEVICE_TABLE(i2c, qt2160_idtable);
497 static struct i2c_driver qt2160_driver = {
498 .driver = {
499 .name = "qt2160",
500 .owner = THIS_MODULE,
503 .id_table = qt2160_idtable,
504 .probe = qt2160_probe,
505 .remove = qt2160_remove,
508 module_i2c_driver(qt2160_driver);
510 MODULE_AUTHOR("Raphael Derosso Pereira <raphaelpereira@gmail.com>");
511 MODULE_DESCRIPTION("Driver for AT42QT2160 Touch Sensor");
512 MODULE_LICENSE("GPL");