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