qt1070.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/i2c.h>
  29. #include <linux/input.h>
  30. #include <linux/slab.h>
  31. #include <linux/irq.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/delay.h>
  35. /* Address for each register */
  36. #define CHIP_ID 0x00
  37. #define QT1070_CHIP_ID 0x2E
  38. #define FW_VERSION 0x01
  39. #define QT1070_FW_VERSION 0x15
  40. #define DET_STATUS 0x02
  41. #define KEY_STATUS 0x03
  42. /* Calibrate */
  43. #define CALIBRATE_CMD 0x38
  44. #define QT1070_CAL_TIME 200
  45. /* Reset */
  46. #define RESET 0x39
  47. #define QT1070_RESET_TIME 255
  48. /* AT42QT1070 support up to 7 keys */
  49. static const unsigned short qt1070_key2code[] = {
  50. KEY_0, KEY_1, KEY_2, KEY_3,
  51. KEY_4, KEY_5, KEY_6,
  52. };
  53. struct qt1070_data {
  54. struct i2c_client *client;
  55. struct input_dev *input;
  56. unsigned int irq;
  57. unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
  58. u8 last_keys;
  59. };
  60. static int qt1070_read(struct i2c_client *client, u8 reg)
  61. {
  62. int ret;
  63. ret = i2c_smbus_read_byte_data(client, reg);
  64. if (ret < 0)
  65. dev_err(&client->dev,
  66. "can not read register, returned %d\n", ret);
  67. return ret;
  68. }
  69. static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
  70. {
  71. int ret;
  72. ret = i2c_smbus_write_byte_data(client, reg, data);
  73. if (ret < 0)
  74. dev_err(&client->dev,
  75. "can not write register, returned %d\n", ret);
  76. return ret;
  77. }
  78. static bool qt1070_identify(struct i2c_client *client)
  79. {
  80. int id, ver;
  81. /* Read Chip ID */
  82. id = qt1070_read(client, CHIP_ID);
  83. if (id != QT1070_CHIP_ID) {
  84. dev_err(&client->dev, "ID %d not supported\n", id);
  85. return false;
  86. }
  87. /* Read firmware version */
  88. ver = qt1070_read(client, FW_VERSION);
  89. if (ver < 0) {
  90. dev_err(&client->dev, "could not read the firmware version\n");
  91. return false;
  92. }
  93. dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
  94. return true;
  95. }
  96. static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
  97. {
  98. struct qt1070_data *data = dev_id;
  99. struct i2c_client *client = data->client;
  100. struct input_dev *input = data->input;
  101. int i;
  102. u8 new_keys, keyval, mask = 0x01;
  103. /* Read the detected status register, thus clearing interrupt */
  104. qt1070_read(client, DET_STATUS);
  105. /* Read which key changed */
  106. new_keys = qt1070_read(client, KEY_STATUS);
  107. for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
  108. keyval = new_keys & mask;
  109. if ((data->last_keys & mask) != keyval)
  110. input_report_key(input, data->keycodes[i], keyval);
  111. mask <<= 1;
  112. }
  113. input_sync(input);
  114. data->last_keys = new_keys;
  115. return IRQ_HANDLED;
  116. }
  117. static int qt1070_probe(struct i2c_client *client,
  118. const struct i2c_device_id *id)
  119. {
  120. struct qt1070_data *data;
  121. struct input_dev *input;
  122. int i;
  123. int err;
  124. err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
  125. if (!err) {
  126. dev_err(&client->dev, "%s adapter not supported\n",
  127. dev_driver_string(&client->adapter->dev));
  128. return -ENODEV;
  129. }
  130. if (!client->irq) {
  131. dev_err(&client->dev, "please assign the irq to this device\n");
  132. return -EINVAL;
  133. }
  134. /* Identify the qt1070 chip */
  135. if (!qt1070_identify(client))
  136. return -ENODEV;
  137. data = kzalloc(sizeof(struct qt1070_data), GFP_KERNEL);
  138. input = input_allocate_device();
  139. if (!data || !input) {
  140. dev_err(&client->dev, "insufficient memory\n");
  141. err = -ENOMEM;
  142. goto err_free_mem;
  143. }
  144. data->client = client;
  145. data->input = input;
  146. data->irq = client->irq;
  147. input->name = "AT42QT1070 QTouch Sensor";
  148. input->dev.parent = &client->dev;
  149. input->id.bustype = BUS_I2C;
  150. /* Add the keycode */
  151. input->keycode = data->keycodes;
  152. input->keycodesize = sizeof(data->keycodes[0]);
  153. input->keycodemax = ARRAY_SIZE(qt1070_key2code);
  154. __set_bit(EV_KEY, input->evbit);
  155. for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
  156. data->keycodes[i] = qt1070_key2code[i];
  157. __set_bit(qt1070_key2code[i], input->keybit);
  158. }
  159. /* Calibrate device */
  160. qt1070_write(client, CALIBRATE_CMD, 1);
  161. msleep(QT1070_CAL_TIME);
  162. /* Soft reset */
  163. qt1070_write(client, RESET, 1);
  164. msleep(QT1070_RESET_TIME);
  165. err = request_threaded_irq(client->irq, NULL, qt1070_interrupt,
  166. IRQF_TRIGGER_NONE | IRQF_ONESHOT,
  167. 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 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. return 0;
  197. }
  198. #ifdef CONFIG_PM_SLEEP
  199. static int qt1070_suspend(struct device *dev)
  200. {
  201. struct i2c_client *client = to_i2c_client(dev);
  202. struct qt1070_data *data = i2c_get_clientdata(client);
  203. if (device_may_wakeup(dev))
  204. enable_irq_wake(data->irq);
  205. return 0;
  206. }
  207. static int qt1070_resume(struct device *dev)
  208. {
  209. struct i2c_client *client = to_i2c_client(dev);
  210. struct qt1070_data *data = i2c_get_clientdata(client);
  211. if (device_may_wakeup(dev))
  212. disable_irq_wake(data->irq);
  213. return 0;
  214. }
  215. #endif
  216. static SIMPLE_DEV_PM_OPS(qt1070_pm_ops, qt1070_suspend, qt1070_resume);
  217. static const struct i2c_device_id qt1070_id[] = {
  218. { "qt1070", 0 },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(i2c, qt1070_id);
  222. #ifdef CONFIG_OF
  223. static const struct of_device_id qt1070_of_match[] = {
  224. { .compatible = "qt1070", },
  225. { },
  226. };
  227. MODULE_DEVICE_TABLE(of, qt1070_of_match);
  228. #endif
  229. static struct i2c_driver qt1070_driver = {
  230. .driver = {
  231. .name = "qt1070",
  232. .of_match_table = of_match_ptr(qt1070_of_match),
  233. .pm = &qt1070_pm_ops,
  234. },
  235. .id_table = qt1070_id,
  236. .probe = qt1070_probe,
  237. .remove = qt1070_remove,
  238. };
  239. module_i2c_driver(qt1070_driver);
  240. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  241. MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
  242. MODULE_LICENSE("GPL");