qt1070.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Atmel AT42QT1070 QTouch Sensor Controller
  3. *
  4. * Copyright (C) 2011 Atmel
  5. *
  6. * Authors: Bo Shen <voice.shen@atmel.com>
  7. *
  8. * Base on AT42QT2160 driver by:
  9. * Raphael Derosso Pereira <raphaelpereira@gmail.com>
  10. * Copyright (C) 2009
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/i2c.h>
  30. #include <linux/input.h>
  31. #include <linux/slab.h>
  32. #include <linux/irq.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/delay.h>
  36. /* Address for each register */
  37. #define CHIP_ID 0x00
  38. #define QT1070_CHIP_ID 0x2E
  39. #define FW_VERSION 0x01
  40. #define QT1070_FW_VERSION 0x15
  41. #define DET_STATUS 0x02
  42. #define KEY_STATUS 0x03
  43. /* Calibrate */
  44. #define CALIBRATE_CMD 0x38
  45. #define QT1070_CAL_TIME 200
  46. /* Reset */
  47. #define RESET 0x39
  48. #define QT1070_RESET_TIME 255
  49. /* AT42QT1070 support up to 7 keys */
  50. static const unsigned short qt1070_key2code[] = {
  51. KEY_0, KEY_1, KEY_2, KEY_3,
  52. KEY_4, KEY_5, KEY_6,
  53. };
  54. struct qt1070_data {
  55. struct i2c_client *client;
  56. struct input_dev *input;
  57. unsigned int irq;
  58. unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
  59. u8 last_keys;
  60. };
  61. static int qt1070_read(struct i2c_client *client, u8 reg)
  62. {
  63. int ret;
  64. ret = i2c_smbus_read_byte_data(client, reg);
  65. if (ret < 0)
  66. dev_err(&client->dev,
  67. "can not read register, returned %d\n", ret);
  68. return ret;
  69. }
  70. static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
  71. {
  72. int ret;
  73. ret = i2c_smbus_write_byte_data(client, reg, data);
  74. if (ret < 0)
  75. dev_err(&client->dev,
  76. "can not write register, returned %d\n", ret);
  77. return ret;
  78. }
  79. static bool __devinit qt1070_identify(struct i2c_client *client)
  80. {
  81. int id, ver;
  82. /* Read Chip ID */
  83. id = qt1070_read(client, CHIP_ID);
  84. if (id != QT1070_CHIP_ID) {
  85. dev_err(&client->dev, "ID %d not supported\n", id);
  86. return false;
  87. }
  88. /* Read firmware version */
  89. ver = qt1070_read(client, FW_VERSION);
  90. if (ver < 0) {
  91. dev_err(&client->dev, "could not read the firmware version\n");
  92. return false;
  93. }
  94. dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
  95. return true;
  96. }
  97. static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
  98. {
  99. struct qt1070_data *data = dev_id;
  100. struct i2c_client *client = data->client;
  101. struct input_dev *input = data->input;
  102. int i;
  103. u8 new_keys, keyval, mask = 0x01;
  104. /* Read the detected status register, thus clearing interrupt */
  105. qt1070_read(client, DET_STATUS);
  106. /* Read which key changed */
  107. new_keys = qt1070_read(client, KEY_STATUS);
  108. for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
  109. keyval = new_keys & mask;
  110. if ((data->last_keys & mask) != keyval)
  111. input_report_key(input, data->keycodes[i], keyval);
  112. mask <<= 1;
  113. }
  114. input_sync(input);
  115. data->last_keys = new_keys;
  116. return IRQ_HANDLED;
  117. }
  118. static int __devinit qt1070_probe(struct i2c_client *client,
  119. const struct i2c_device_id *id)
  120. {
  121. struct qt1070_data *data;
  122. struct input_dev *input;
  123. int i;
  124. int err;
  125. err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
  126. if (!err) {
  127. dev_err(&client->dev, "%s adapter not supported\n",
  128. dev_driver_string(&client->adapter->dev));
  129. return -ENODEV;
  130. }
  131. if (!client->irq) {
  132. dev_err(&client->dev, "please assign the irq to this device\n");
  133. return -EINVAL;
  134. }
  135. /* Identify the qt1070 chip */
  136. if (!qt1070_identify(client))
  137. return -ENODEV;
  138. data = kzalloc(sizeof(struct qt1070_data), GFP_KERNEL);
  139. input = input_allocate_device();
  140. if (!data || !input) {
  141. dev_err(&client->dev, "insufficient memory\n");
  142. err = -ENOMEM;
  143. goto err_free_mem;
  144. }
  145. data->client = client;
  146. data->input = input;
  147. data->irq = client->irq;
  148. input->name = "AT42QT1070 QTouch Sensor";
  149. input->dev.parent = &client->dev;
  150. input->id.bustype = BUS_I2C;
  151. /* Add the keycode */
  152. input->keycode = data->keycodes;
  153. input->keycodesize = sizeof(data->keycodes[0]);
  154. input->keycodemax = ARRAY_SIZE(qt1070_key2code);
  155. __set_bit(EV_KEY, input->evbit);
  156. for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
  157. data->keycodes[i] = qt1070_key2code[i];
  158. __set_bit(qt1070_key2code[i], input->keybit);
  159. }
  160. /* Calibrate device */
  161. qt1070_write(client, CALIBRATE_CMD, 1);
  162. msleep(QT1070_CAL_TIME);
  163. /* Soft reset */
  164. qt1070_write(client, RESET, 1);
  165. msleep(QT1070_RESET_TIME);
  166. err = request_threaded_irq(client->irq, NULL, qt1070_interrupt,
  167. IRQF_TRIGGER_NONE, client->dev.driver->name, data);
  168. if (err) {
  169. dev_err(&client->dev, "fail to request irq\n");
  170. goto err_free_mem;
  171. }
  172. /* Register the input device */
  173. err = input_register_device(data->input);
  174. if (err) {
  175. dev_err(&client->dev, "Failed to register input device\n");
  176. goto err_free_irq;
  177. }
  178. i2c_set_clientdata(client, data);
  179. /* Read to clear the chang line */
  180. qt1070_read(client, DET_STATUS);
  181. return 0;
  182. err_free_irq:
  183. free_irq(client->irq, data);
  184. err_free_mem:
  185. input_free_device(input);
  186. kfree(data);
  187. return err;
  188. }
  189. static int __devexit qt1070_remove(struct i2c_client *client)
  190. {
  191. struct qt1070_data *data = i2c_get_clientdata(client);
  192. /* Release IRQ */
  193. free_irq(client->irq, data);
  194. input_unregister_device(data->input);
  195. kfree(data);
  196. i2c_set_clientdata(client, NULL);
  197. return 0;
  198. }
  199. static const struct i2c_device_id qt1070_id[] = {
  200. { "qt1070", 0 },
  201. { },
  202. };
  203. static struct i2c_driver qt1070_driver = {
  204. .driver = {
  205. .name = "qt1070",
  206. .owner = THIS_MODULE,
  207. },
  208. .id_table = qt1070_id,
  209. .probe = qt1070_probe,
  210. .remove = __devexit_p(qt1070_remove),
  211. };
  212. static int __init qt1070_init(void)
  213. {
  214. return i2c_add_driver(&qt1070_driver);
  215. }
  216. module_init(qt1070_init);
  217. static void __exit qt1070_exit(void)
  218. {
  219. i2c_del_driver(&qt1070_driver);
  220. }
  221. module_exit(qt1070_exit);
  222. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  223. MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
  224. MODULE_LICENSE("GPL");