qt1070.c 6.2 KB

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