1 /* 2 * qt2160.c - Atmel AT42QT2160 Touch Sense Controller 3 * 4 * Copyright (C) 2009 Raphael Derosso Pereira <raphaelpereira@gmail.com> 5 * 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. 10 * 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. 15 * 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. 19 */ 20 21 #include <linux/kernel.h> 22 #include <linux/init.h> 23 #include <linux/leds.h> 24 #include <linux/module.h> 25 #include <linux/slab.h> 26 #include <linux/jiffies.h> 27 #include <linux/i2c.h> 28 #include <linux/irq.h> 29 #include <linux/interrupt.h> 30 #include <linux/input.h> 31 32 #define QT2160_VALID_CHIPID 0x11 33 34 #define QT2160_CMD_CHIPID 0 35 #define QT2160_CMD_CODEVER 1 36 #define QT2160_CMD_GSTAT 2 37 #define QT2160_CMD_KEYS3 3 38 #define QT2160_CMD_KEYS4 4 39 #define QT2160_CMD_SLIDE 5 40 #define QT2160_CMD_GPIOS 6 41 #define QT2160_CMD_SUBVER 7 42 #define QT2160_CMD_CALIBRATE 10 43 #define QT2160_CMD_DRIVE_X 70 44 #define QT2160_CMD_PWMEN_X 74 45 #define QT2160_CMD_PWM_DUTY 76 46 47 #define QT2160_NUM_LEDS_X 8 48 49 #define QT2160_CYCLE_INTERVAL (2*HZ) 50 51 static unsigned char qt2160_key2code[] = { 52 KEY_0, KEY_1, KEY_2, KEY_3, 53 KEY_4, KEY_5, KEY_6, KEY_7, 54 KEY_8, KEY_9, KEY_A, KEY_B, 55 KEY_C, KEY_D, KEY_E, KEY_F, 56 }; 57 58 #ifdef CONFIG_LEDS_CLASS 59 struct qt2160_led { 60 struct qt2160_data *qt2160; 61 struct led_classdev cdev; 62 struct work_struct work; 63 char name[32]; 64 int id; 65 enum led_brightness new_brightness; 66 }; 67 #endif 68 69 struct qt2160_data { 70 struct i2c_client *client; 71 struct input_dev *input; 72 struct delayed_work dwork; 73 spinlock_t lock; /* Protects canceling/rescheduling of dwork */ 74 unsigned short keycodes[ARRAY_SIZE(qt2160_key2code)]; 75 u16 key_matrix; 76 #ifdef CONFIG_LEDS_CLASS 77 struct qt2160_led leds[QT2160_NUM_LEDS_X]; 78 struct mutex led_lock; 79 #endif 80 }; 81 82 static int qt2160_read(struct i2c_client *client, u8 reg); 83 static int qt2160_write(struct i2c_client *client, u8 reg, u8 data); 84 85 #ifdef CONFIG_LEDS_CLASS 86 87 static void qt2160_led_work(struct work_struct *work) 88 { 89 struct qt2160_led *led = container_of(work, struct qt2160_led, work); 90 struct qt2160_data *qt2160 = led->qt2160; 91 struct i2c_client *client = qt2160->client; 92 int value = led->new_brightness; 93 u32 drive, pwmen; 94 95 mutex_lock(&qt2160->led_lock); 96 97 drive = qt2160_read(client, QT2160_CMD_DRIVE_X); 98 pwmen = qt2160_read(client, QT2160_CMD_PWMEN_X); 99 if (value != LED_OFF) { 100 drive |= (1 << led->id); 101 pwmen |= (1 << led->id); 102 103 } else { 104 drive &= ~(1 << led->id); 105 pwmen &= ~(1 << led->id); 106 } 107 qt2160_write(client, QT2160_CMD_DRIVE_X, drive); 108 qt2160_write(client, QT2160_CMD_PWMEN_X, pwmen); 109 110 /* 111 * Changing this register will change the brightness 112 * of every LED in the qt2160. It's a HW limitation. 113 */ 114 if (value != LED_OFF) 115 qt2160_write(client, QT2160_CMD_PWM_DUTY, value); 116 117 mutex_unlock(&qt2160->led_lock); 118 } 119 120 static void qt2160_led_set(struct led_classdev *cdev, 121 enum led_brightness value) 122 { 123 struct qt2160_led *led = container_of(cdev, struct qt2160_led, cdev); 124 125 led->new_brightness = value; 126 schedule_work(&led->work); 127 } 128 129 #endif /* CONFIG_LEDS_CLASS */ 130 131 static int qt2160_read_block(struct i2c_client *client, 132 u8 inireg, u8 *buffer, unsigned int count) 133 { 134 int error, idx = 0; 135 136 /* 137 * Can't use SMBus block data read. Check for I2C functionality to speed 138 * things up whenever possible. Otherwise we will be forced to read 139 * sequentially. 140 */ 141 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 142 143 error = i2c_smbus_write_byte(client, inireg + idx); 144 if (error) { 145 dev_err(&client->dev, 146 "couldn't send request. Returned %d\n", error); 147 return error; 148 } 149 150 error = i2c_master_recv(client, buffer, count); 151 if (error != count) { 152 dev_err(&client->dev, 153 "couldn't read registers. Returned %d bytes\n", error); 154 return error; 155 } 156 } else { 157 158 while (count--) { 159 int data; 160 161 error = i2c_smbus_write_byte(client, inireg + idx); 162 if (error) { 163 dev_err(&client->dev, 164 "couldn't send request. Returned %d\n", error); 165 return error; 166 } 167 168 data = i2c_smbus_read_byte(client); 169 if (data < 0) { 170 dev_err(&client->dev, 171 "couldn't read register. Returned %d\n", data); 172 return data; 173 } 174 175 buffer[idx++] = data; 176 } 177 } 178 179 return 0; 180 } 181 182 static int qt2160_get_key_matrix(struct qt2160_data *qt2160) 183 { 184 struct i2c_client *client = qt2160->client; 185 struct input_dev *input = qt2160->input; 186 u8 regs[6]; 187 u16 old_matrix, new_matrix; 188 int ret, i, mask; 189 190 dev_dbg(&client->dev, "requesting keys...\n"); 191 192 /* 193 * Read all registers from General Status Register 194 * to GPIOs register 195 */ 196 ret = qt2160_read_block(client, QT2160_CMD_GSTAT, regs, 6); 197 if (ret) { 198 dev_err(&client->dev, 199 "could not perform chip read.\n"); 200 return ret; 201 } 202 203 old_matrix = qt2160->key_matrix; 204 qt2160->key_matrix = new_matrix = (regs[2] << 8) | regs[1]; 205 206 mask = 0x01; 207 for (i = 0; i < 16; ++i, mask <<= 1) { 208 int keyval = new_matrix & mask; 209 210 if ((old_matrix & mask) != keyval) { 211 input_report_key(input, qt2160->keycodes[i], keyval); 212 dev_dbg(&client->dev, "key %d %s\n", 213 i, keyval ? "pressed" : "released"); 214 } 215 } 216 217 input_sync(input); 218 219 return 0; 220 } 221 222 static irqreturn_t qt2160_irq(int irq, void *_qt2160) 223 { 224 struct qt2160_data *qt2160 = _qt2160; 225 unsigned long flags; 226 227 spin_lock_irqsave(&qt2160->lock, flags); 228 229 mod_delayed_work(system_wq, &qt2160->dwork, 0); 230 231 spin_unlock_irqrestore(&qt2160->lock, flags); 232 233 return IRQ_HANDLED; 234 } 235 236 static void qt2160_schedule_read(struct qt2160_data *qt2160) 237 { 238 spin_lock_irq(&qt2160->lock); 239 schedule_delayed_work(&qt2160->dwork, QT2160_CYCLE_INTERVAL); 240 spin_unlock_irq(&qt2160->lock); 241 } 242 243 static void qt2160_worker(struct work_struct *work) 244 { 245 struct qt2160_data *qt2160 = 246 container_of(work, struct qt2160_data, dwork.work); 247 248 dev_dbg(&qt2160->client->dev, "worker\n"); 249 250 qt2160_get_key_matrix(qt2160); 251 252 /* Avoid device lock up by checking every so often */ 253 qt2160_schedule_read(qt2160); 254 } 255 256 static int qt2160_read(struct i2c_client *client, u8 reg) 257 { 258 int ret; 259 260 ret = i2c_smbus_write_byte(client, reg); 261 if (ret) { 262 dev_err(&client->dev, 263 "couldn't send request. Returned %d\n", ret); 264 return ret; 265 } 266 267 ret = i2c_smbus_read_byte(client); 268 if (ret < 0) { 269 dev_err(&client->dev, 270 "couldn't read register. Returned %d\n", ret); 271 return ret; 272 } 273 274 return ret; 275 } 276 277 static int qt2160_write(struct i2c_client *client, u8 reg, u8 data) 278 { 279 int ret; 280 281 ret = i2c_smbus_write_byte_data(client, reg, data); 282 if (ret < 0) 283 dev_err(&client->dev, 284 "couldn't write data. Returned %d\n", ret); 285 286 return ret; 287 } 288 289 #ifdef CONFIG_LEDS_CLASS 290 291 static int qt2160_register_leds(struct qt2160_data *qt2160) 292 { 293 struct i2c_client *client = qt2160->client; 294 int ret; 295 int i; 296 297 mutex_init(&qt2160->led_lock); 298 299 for (i = 0; i < QT2160_NUM_LEDS_X; i++) { 300 struct qt2160_led *led = &qt2160->leds[i]; 301 302 snprintf(led->name, sizeof(led->name), "qt2160:x%d", i); 303 led->cdev.name = led->name; 304 led->cdev.brightness_set = qt2160_led_set; 305 led->cdev.brightness = LED_OFF; 306 led->id = i; 307 led->qt2160 = qt2160; 308 309 INIT_WORK(&led->work, qt2160_led_work); 310 311 ret = led_classdev_register(&client->dev, &led->cdev); 312 if (ret < 0) 313 return ret; 314 } 315 316 /* Tur off LEDs */ 317 qt2160_write(client, QT2160_CMD_DRIVE_X, 0); 318 qt2160_write(client, QT2160_CMD_PWMEN_X, 0); 319 qt2160_write(client, QT2160_CMD_PWM_DUTY, 0); 320 321 return 0; 322 } 323 324 static void qt2160_unregister_leds(struct qt2160_data *qt2160) 325 { 326 int i; 327 328 for (i = 0; i < QT2160_NUM_LEDS_X; i++) { 329 led_classdev_unregister(&qt2160->leds[i].cdev); 330 cancel_work_sync(&qt2160->leds[i].work); 331 } 332 } 333 334 #else 335 336 static inline int qt2160_register_leds(struct qt2160_data *qt2160) 337 { 338 return 0; 339 } 340 341 static inline void qt2160_unregister_leds(struct qt2160_data *qt2160) 342 { 343 } 344 345 #endif 346 347 static bool qt2160_identify(struct i2c_client *client) 348 { 349 int id, ver, rev; 350 351 /* Read Chid ID to check if chip is valid */ 352 id = qt2160_read(client, QT2160_CMD_CHIPID); 353 if (id != QT2160_VALID_CHIPID) { 354 dev_err(&client->dev, "ID %d not supported\n", id); 355 return false; 356 } 357 358 /* Read chip firmware version */ 359 ver = qt2160_read(client, QT2160_CMD_CODEVER); 360 if (ver < 0) { 361 dev_err(&client->dev, "could not get firmware version\n"); 362 return false; 363 } 364 365 /* Read chip firmware revision */ 366 rev = qt2160_read(client, QT2160_CMD_SUBVER); 367 if (rev < 0) { 368 dev_err(&client->dev, "could not get firmware revision\n"); 369 return false; 370 } 371 372 dev_info(&client->dev, "AT42QT2160 firmware version %d.%d.%d\n", 373 ver >> 4, ver & 0xf, rev); 374 375 return true; 376 } 377 378 static int qt2160_probe(struct i2c_client *client, 379 const struct i2c_device_id *id) 380 { 381 struct qt2160_data *qt2160; 382 struct input_dev *input; 383 int i; 384 int error; 385 386 /* Check functionality */ 387 error = i2c_check_functionality(client->adapter, 388 I2C_FUNC_SMBUS_BYTE); 389 if (!error) { 390 dev_err(&client->dev, "%s adapter not supported\n", 391 dev_driver_string(&client->adapter->dev)); 392 return -ENODEV; 393 } 394 395 if (!qt2160_identify(client)) 396 return -ENODEV; 397 398 /* Chip is valid and active. Allocate structure */ 399 qt2160 = kzalloc(sizeof(struct qt2160_data), GFP_KERNEL); 400 input = input_allocate_device(); 401 if (!qt2160 || !input) { 402 dev_err(&client->dev, "insufficient memory\n"); 403 error = -ENOMEM; 404 goto err_free_mem; 405 } 406 407 qt2160->client = client; 408 qt2160->input = input; 409 INIT_DELAYED_WORK(&qt2160->dwork, qt2160_worker); 410 spin_lock_init(&qt2160->lock); 411 412 input->name = "AT42QT2160 Touch Sense Keyboard"; 413 input->id.bustype = BUS_I2C; 414 415 input->keycode = qt2160->keycodes; 416 input->keycodesize = sizeof(qt2160->keycodes[0]); 417 input->keycodemax = ARRAY_SIZE(qt2160_key2code); 418 419 __set_bit(EV_KEY, input->evbit); 420 __clear_bit(EV_REP, input->evbit); 421 for (i = 0; i < ARRAY_SIZE(qt2160_key2code); i++) { 422 qt2160->keycodes[i] = qt2160_key2code[i]; 423 __set_bit(qt2160_key2code[i], input->keybit); 424 } 425 __clear_bit(KEY_RESERVED, input->keybit); 426 427 /* Calibrate device */ 428 error = qt2160_write(client, QT2160_CMD_CALIBRATE, 1); 429 if (error) { 430 dev_err(&client->dev, "failed to calibrate device\n"); 431 goto err_free_mem; 432 } 433 434 if (client->irq) { 435 error = request_irq(client->irq, qt2160_irq, 436 IRQF_TRIGGER_FALLING, "qt2160", qt2160); 437 if (error) { 438 dev_err(&client->dev, 439 "failed to allocate irq %d\n", client->irq); 440 goto err_free_mem; 441 } 442 } 443 444 error = qt2160_register_leds(qt2160); 445 if (error) { 446 dev_err(&client->dev, "Failed to register leds\n"); 447 goto err_free_irq; 448 } 449 450 error = input_register_device(qt2160->input); 451 if (error) { 452 dev_err(&client->dev, 453 "Failed to register input device\n"); 454 goto err_unregister_leds; 455 } 456 457 i2c_set_clientdata(client, qt2160); 458 qt2160_schedule_read(qt2160); 459 460 return 0; 461 462 err_unregister_leds: 463 qt2160_unregister_leds(qt2160); 464 err_free_irq: 465 if (client->irq) 466 free_irq(client->irq, qt2160); 467 err_free_mem: 468 input_free_device(input); 469 kfree(qt2160); 470 return error; 471 } 472 473 static int qt2160_remove(struct i2c_client *client) 474 { 475 struct qt2160_data *qt2160 = i2c_get_clientdata(client); 476 477 qt2160_unregister_leds(qt2160); 478 479 /* Release IRQ so no queue will be scheduled */ 480 if (client->irq) 481 free_irq(client->irq, qt2160); 482 483 cancel_delayed_work_sync(&qt2160->dwork); 484 485 input_unregister_device(qt2160->input); 486 kfree(qt2160); 487 488 return 0; 489 } 490 491 static const struct i2c_device_id qt2160_idtable[] = { 492 { "qt2160", 0, }, 493 { } 494 }; 495 496 MODULE_DEVICE_TABLE(i2c, qt2160_idtable); 497 498 static struct i2c_driver qt2160_driver = { 499 .driver = { 500 .name = "qt2160", 501 .owner = THIS_MODULE, 502 }, 503 504 .id_table = qt2160_idtable, 505 .probe = qt2160_probe, 506 .remove = qt2160_remove, 507 }; 508 509 module_i2c_driver(qt2160_driver); 510 511 MODULE_AUTHOR("Raphael Derosso Pereira <raphaelpereira@gmail.com>"); 512 MODULE_DESCRIPTION("Driver for AT42QT2160 Touch Sensor"); 513 MODULE_LICENSE("GPL"); 514