qt2160.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/i2c.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/input.h>
  29. #define QT2160_VALID_CHIPID 0x11
  30. #define QT2160_CMD_CHIPID 0
  31. #define QT2160_CMD_CODEVER 1
  32. #define QT2160_CMD_GSTAT 2
  33. #define QT2160_CMD_KEYS3 3
  34. #define QT2160_CMD_KEYS4 4
  35. #define QT2160_CMD_SLIDE 5
  36. #define QT2160_CMD_GPIOS 6
  37. #define QT2160_CMD_SUBVER 7
  38. #define QT2160_CMD_CALIBRATE 10
  39. #define QT2160_CYCLE_INTERVAL (2*HZ)
  40. static unsigned char qt2160_key2code[] = {
  41. KEY_0, KEY_1, KEY_2, KEY_3,
  42. KEY_4, KEY_5, KEY_6, KEY_7,
  43. KEY_8, KEY_9, KEY_A, KEY_B,
  44. KEY_C, KEY_D, KEY_E, KEY_F,
  45. };
  46. struct qt2160_data {
  47. struct i2c_client *client;
  48. struct input_dev *input;
  49. struct delayed_work dwork;
  50. spinlock_t lock; /* Protects canceling/rescheduling of dwork */
  51. unsigned short keycodes[ARRAY_SIZE(qt2160_key2code)];
  52. u16 key_matrix;
  53. };
  54. static int qt2160_read_block(struct i2c_client *client,
  55. u8 inireg, u8 *buffer, unsigned int count)
  56. {
  57. int error, idx = 0;
  58. /*
  59. * Can't use SMBus block data read. Check for I2C functionality to speed
  60. * things up whenever possible. Otherwise we will be forced to read
  61. * sequentially.
  62. */
  63. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  64. error = i2c_smbus_write_byte(client, inireg + idx);
  65. if (error) {
  66. dev_err(&client->dev,
  67. "couldn't send request. Returned %d\n", error);
  68. return error;
  69. }
  70. error = i2c_master_recv(client, buffer, count);
  71. if (error != count) {
  72. dev_err(&client->dev,
  73. "couldn't read registers. Returned %d bytes\n", error);
  74. return error;
  75. }
  76. } else {
  77. while (count--) {
  78. int data;
  79. error = i2c_smbus_write_byte(client, inireg + idx);
  80. if (error) {
  81. dev_err(&client->dev,
  82. "couldn't send request. Returned %d\n", error);
  83. return error;
  84. }
  85. data = i2c_smbus_read_byte(client);
  86. if (data < 0) {
  87. dev_err(&client->dev,
  88. "couldn't read register. Returned %d\n", data);
  89. return data;
  90. }
  91. buffer[idx++] = data;
  92. }
  93. }
  94. return 0;
  95. }
  96. static int qt2160_get_key_matrix(struct qt2160_data *qt2160)
  97. {
  98. struct i2c_client *client = qt2160->client;
  99. struct input_dev *input = qt2160->input;
  100. u8 regs[6];
  101. u16 old_matrix, new_matrix;
  102. int ret, i, mask;
  103. dev_dbg(&client->dev, "requesting keys...\n");
  104. /*
  105. * Read all registers from General Status Register
  106. * to GPIOs register
  107. */
  108. ret = qt2160_read_block(client, QT2160_CMD_GSTAT, regs, 6);
  109. if (ret) {
  110. dev_err(&client->dev,
  111. "could not perform chip read.\n");
  112. return ret;
  113. }
  114. old_matrix = qt2160->key_matrix;
  115. qt2160->key_matrix = new_matrix = (regs[2] << 8) | regs[1];
  116. mask = 0x01;
  117. for (i = 0; i < 16; ++i, mask <<= 1) {
  118. int keyval = new_matrix & mask;
  119. if ((old_matrix & mask) != keyval) {
  120. input_report_key(input, qt2160->keycodes[i], keyval);
  121. dev_dbg(&client->dev, "key %d %s\n",
  122. i, keyval ? "pressed" : "released");
  123. }
  124. }
  125. input_sync(input);
  126. return 0;
  127. }
  128. static irqreturn_t qt2160_irq(int irq, void *_qt2160)
  129. {
  130. struct qt2160_data *qt2160 = _qt2160;
  131. unsigned long flags;
  132. spin_lock_irqsave(&qt2160->lock, flags);
  133. __cancel_delayed_work(&qt2160->dwork);
  134. schedule_delayed_work(&qt2160->dwork, 0);
  135. spin_unlock_irqrestore(&qt2160->lock, flags);
  136. return IRQ_HANDLED;
  137. }
  138. static void qt2160_schedule_read(struct qt2160_data *qt2160)
  139. {
  140. spin_lock_irq(&qt2160->lock);
  141. schedule_delayed_work(&qt2160->dwork, QT2160_CYCLE_INTERVAL);
  142. spin_unlock_irq(&qt2160->lock);
  143. }
  144. static void qt2160_worker(struct work_struct *work)
  145. {
  146. struct qt2160_data *qt2160 =
  147. container_of(work, struct qt2160_data, dwork.work);
  148. dev_dbg(&qt2160->client->dev, "worker\n");
  149. qt2160_get_key_matrix(qt2160);
  150. /* Avoid device lock up by checking every so often */
  151. qt2160_schedule_read(qt2160);
  152. }
  153. static int __devinit qt2160_read(struct i2c_client *client, u8 reg)
  154. {
  155. int ret;
  156. ret = i2c_smbus_write_byte(client, reg);
  157. if (ret) {
  158. dev_err(&client->dev,
  159. "couldn't send request. Returned %d\n", ret);
  160. return ret;
  161. }
  162. ret = i2c_smbus_read_byte(client);
  163. if (ret < 0) {
  164. dev_err(&client->dev,
  165. "couldn't read register. Returned %d\n", ret);
  166. return ret;
  167. }
  168. return ret;
  169. }
  170. static int __devinit qt2160_write(struct i2c_client *client, u8 reg, u8 data)
  171. {
  172. int error;
  173. error = i2c_smbus_write_byte(client, reg);
  174. if (error) {
  175. dev_err(&client->dev,
  176. "couldn't send request. Returned %d\n", error);
  177. return error;
  178. }
  179. error = i2c_smbus_write_byte(client, data);
  180. if (error) {
  181. dev_err(&client->dev,
  182. "couldn't write data. Returned %d\n", error);
  183. return error;
  184. }
  185. return error;
  186. }
  187. static bool __devinit qt2160_identify(struct i2c_client *client)
  188. {
  189. int id, ver, rev;
  190. /* Read Chid ID to check if chip is valid */
  191. id = qt2160_read(client, QT2160_CMD_CHIPID);
  192. if (id != QT2160_VALID_CHIPID) {
  193. dev_err(&client->dev, "ID %d not supported\n", id);
  194. return false;
  195. }
  196. /* Read chip firmware version */
  197. ver = qt2160_read(client, QT2160_CMD_CODEVER);
  198. if (ver < 0) {
  199. dev_err(&client->dev, "could not get firmware version\n");
  200. return false;
  201. }
  202. /* Read chip firmware revision */
  203. rev = qt2160_read(client, QT2160_CMD_SUBVER);
  204. if (rev < 0) {
  205. dev_err(&client->dev, "could not get firmware revision\n");
  206. return false;
  207. }
  208. dev_info(&client->dev, "AT42QT2160 firmware version %d.%d.%d\n",
  209. ver >> 4, ver & 0xf, rev);
  210. return true;
  211. }
  212. static int __devinit qt2160_probe(struct i2c_client *client,
  213. const struct i2c_device_id *id)
  214. {
  215. struct qt2160_data *qt2160;
  216. struct input_dev *input;
  217. int i;
  218. int error;
  219. /* Check functionality */
  220. error = i2c_check_functionality(client->adapter,
  221. I2C_FUNC_SMBUS_BYTE);
  222. if (!error) {
  223. dev_err(&client->dev, "%s adapter not supported\n",
  224. dev_driver_string(&client->adapter->dev));
  225. return -ENODEV;
  226. }
  227. if (!qt2160_identify(client))
  228. return -ENODEV;
  229. /* Chip is valid and active. Allocate structure */
  230. qt2160 = kzalloc(sizeof(struct qt2160_data), GFP_KERNEL);
  231. input = input_allocate_device();
  232. if (!qt2160 || !input) {
  233. dev_err(&client->dev, "insufficient memory\n");
  234. error = -ENOMEM;
  235. goto err_free_mem;
  236. }
  237. qt2160->client = client;
  238. qt2160->input = input;
  239. INIT_DELAYED_WORK(&qt2160->dwork, qt2160_worker);
  240. spin_lock_init(&qt2160->lock);
  241. input->name = "AT42QT2160 Touch Sense Keyboard";
  242. input->id.bustype = BUS_I2C;
  243. input->keycode = qt2160->keycodes;
  244. input->keycodesize = sizeof(qt2160->keycodes[0]);
  245. input->keycodemax = ARRAY_SIZE(qt2160_key2code);
  246. __set_bit(EV_KEY, input->evbit);
  247. __clear_bit(EV_REP, input->evbit);
  248. for (i = 0; i < ARRAY_SIZE(qt2160_key2code); i++) {
  249. qt2160->keycodes[i] = qt2160_key2code[i];
  250. __set_bit(qt2160_key2code[i], input->keybit);
  251. }
  252. __clear_bit(KEY_RESERVED, input->keybit);
  253. /* Calibrate device */
  254. error = qt2160_write(client, QT2160_CMD_CALIBRATE, 1);
  255. if (error) {
  256. dev_err(&client->dev, "failed to calibrate device\n");
  257. goto err_free_mem;
  258. }
  259. if (client->irq) {
  260. error = request_irq(client->irq, qt2160_irq,
  261. IRQF_TRIGGER_FALLING, "qt2160", qt2160);
  262. if (error) {
  263. dev_err(&client->dev,
  264. "failed to allocate irq %d\n", client->irq);
  265. goto err_free_mem;
  266. }
  267. }
  268. error = input_register_device(qt2160->input);
  269. if (error) {
  270. dev_err(&client->dev,
  271. "Failed to register input device\n");
  272. goto err_free_irq;
  273. }
  274. i2c_set_clientdata(client, qt2160);
  275. qt2160_schedule_read(qt2160);
  276. return 0;
  277. err_free_irq:
  278. if (client->irq)
  279. free_irq(client->irq, qt2160);
  280. err_free_mem:
  281. input_free_device(input);
  282. kfree(qt2160);
  283. return error;
  284. }
  285. static int __devexit qt2160_remove(struct i2c_client *client)
  286. {
  287. struct qt2160_data *qt2160 = i2c_get_clientdata(client);
  288. /* Release IRQ so no queue will be scheduled */
  289. if (client->irq)
  290. free_irq(client->irq, qt2160);
  291. cancel_delayed_work_sync(&qt2160->dwork);
  292. input_unregister_device(qt2160->input);
  293. kfree(qt2160);
  294. i2c_set_clientdata(client, NULL);
  295. return 0;
  296. }
  297. static struct i2c_device_id qt2160_idtable[] = {
  298. { "qt2160", 0, },
  299. { }
  300. };
  301. MODULE_DEVICE_TABLE(i2c, qt2160_idtable);
  302. static struct i2c_driver qt2160_driver = {
  303. .driver = {
  304. .name = "qt2160",
  305. .owner = THIS_MODULE,
  306. },
  307. .id_table = qt2160_idtable,
  308. .probe = qt2160_probe,
  309. .remove = __devexit_p(qt2160_remove),
  310. };
  311. static int __init qt2160_init(void)
  312. {
  313. return i2c_add_driver(&qt2160_driver);
  314. }
  315. module_init(qt2160_init);
  316. static void __exit qt2160_cleanup(void)
  317. {
  318. i2c_del_driver(&qt2160_driver);
  319. }
  320. module_exit(qt2160_cleanup);
  321. MODULE_AUTHOR("Raphael Derosso Pereira <raphaelpereira@gmail.com>");
  322. MODULE_DESCRIPTION("Driver for AT42QT2160 Touch Sensor");
  323. MODULE_LICENSE("GPL");