atmel_captouch.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Atmel Atmegaxx Capacitive Touch Button Driver
  4. *
  5. * Copyright (C) 2016 Google, inc.
  6. */
  7. /*
  8. * It's irrelevant that the HW used to develop captouch driver is based
  9. * on Atmega88PA part and uses QtouchADC parts for sensing touch.
  10. * Calling this driver "captouch" is an arbitrary way to distinguish
  11. * the protocol this driver supported by other atmel/qtouch drivers.
  12. *
  13. * Captouch driver supports a newer/different version of the I2C
  14. * registers/commands than the qt1070.c driver.
  15. * Don't let the similarity of the general driver structure fool you.
  16. *
  17. * For raw i2c access from userspace, use i2cset/i2cget
  18. * to poke at /dev/i2c-N devices.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/input.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/slab.h>
  28. /* Maximum number of buttons supported */
  29. #define MAX_NUM_OF_BUTTONS 8
  30. /* Registers */
  31. #define REG_KEY1_THRESHOLD 0x02
  32. #define REG_KEY2_THRESHOLD 0x03
  33. #define REG_KEY3_THRESHOLD 0x04
  34. #define REG_KEY4_THRESHOLD 0x05
  35. #define REG_KEY1_REF_H 0x20
  36. #define REG_KEY1_REF_L 0x21
  37. #define REG_KEY2_REF_H 0x22
  38. #define REG_KEY2_REF_L 0x23
  39. #define REG_KEY3_REF_H 0x24
  40. #define REG_KEY3_REF_L 0x25
  41. #define REG_KEY4_REF_H 0x26
  42. #define REG_KEY4_REF_L 0x27
  43. #define REG_KEY1_DLT_H 0x30
  44. #define REG_KEY1_DLT_L 0x31
  45. #define REG_KEY2_DLT_H 0x32
  46. #define REG_KEY2_DLT_L 0x33
  47. #define REG_KEY3_DLT_H 0x34
  48. #define REG_KEY3_DLT_L 0x35
  49. #define REG_KEY4_DLT_H 0x36
  50. #define REG_KEY4_DLT_L 0x37
  51. #define REG_KEY_STATE 0x3C
  52. /*
  53. * @i2c_client: I2C slave device client pointer
  54. * @input: Input device pointer
  55. * @num_btn: Number of buttons
  56. * @keycodes: map of button# to KeyCode
  57. * @prev_btn: Previous key state to detect button "press" or "release"
  58. * @xfer_buf: I2C transfer buffer
  59. */
  60. struct atmel_captouch_device {
  61. struct i2c_client *client;
  62. struct input_dev *input;
  63. u32 num_btn;
  64. u32 keycodes[MAX_NUM_OF_BUTTONS];
  65. u8 prev_btn;
  66. u8 xfer_buf[8] ____cacheline_aligned;
  67. };
  68. /*
  69. * Read from I2C slave device
  70. * The protocol is that the client has to provide both the register address
  71. * and the length, and while reading back the device would prepend the data
  72. * with address and length for verification.
  73. */
  74. static int atmel_read(struct atmel_captouch_device *capdev,
  75. u8 reg, u8 *data, size_t len)
  76. {
  77. struct i2c_client *client = capdev->client;
  78. struct device *dev = &client->dev;
  79. struct i2c_msg msg[2];
  80. int err;
  81. if (len > sizeof(capdev->xfer_buf) - 2)
  82. return -EINVAL;
  83. capdev->xfer_buf[0] = reg;
  84. capdev->xfer_buf[1] = len;
  85. msg[0].addr = client->addr;
  86. msg[0].flags = 0;
  87. msg[0].buf = capdev->xfer_buf;
  88. msg[0].len = 2;
  89. msg[1].addr = client->addr;
  90. msg[1].flags = I2C_M_RD;
  91. msg[1].buf = capdev->xfer_buf;
  92. msg[1].len = len + 2;
  93. err = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  94. if (err != ARRAY_SIZE(msg))
  95. return err < 0 ? err : -EIO;
  96. if (capdev->xfer_buf[0] != reg) {
  97. dev_err(dev,
  98. "I2C read error: register address does not match (%#02x vs %02x)\n",
  99. capdev->xfer_buf[0], reg);
  100. return -ECOMM;
  101. }
  102. memcpy(data, &capdev->xfer_buf[2], len);
  103. return 0;
  104. }
  105. /*
  106. * Handle interrupt and report the key changes to the input system.
  107. * Multi-touch can be supported; however, it really depends on whether
  108. * the device can multi-touch.
  109. */
  110. static irqreturn_t atmel_captouch_isr(int irq, void *data)
  111. {
  112. struct atmel_captouch_device *capdev = data;
  113. struct device *dev = &capdev->client->dev;
  114. int error;
  115. int i;
  116. u8 new_btn;
  117. u8 changed_btn;
  118. error = atmel_read(capdev, REG_KEY_STATE, &new_btn, 1);
  119. if (error) {
  120. dev_err(dev, "failed to read button state: %d\n", error);
  121. goto out;
  122. }
  123. dev_dbg(dev, "%s: button state %#02x\n", __func__, new_btn);
  124. changed_btn = new_btn ^ capdev->prev_btn;
  125. capdev->prev_btn = new_btn;
  126. for (i = 0; i < capdev->num_btn; i++) {
  127. if (changed_btn & BIT(i))
  128. input_report_key(capdev->input,
  129. capdev->keycodes[i],
  130. new_btn & BIT(i));
  131. }
  132. input_sync(capdev->input);
  133. out:
  134. return IRQ_HANDLED;
  135. }
  136. /*
  137. * Probe function to setup the device, input system and interrupt
  138. */
  139. static int atmel_captouch_probe(struct i2c_client *client)
  140. {
  141. struct atmel_captouch_device *capdev;
  142. struct device *dev = &client->dev;
  143. struct device_node *node;
  144. int i;
  145. int err;
  146. if (!i2c_check_functionality(client->adapter,
  147. I2C_FUNC_SMBUS_BYTE_DATA |
  148. I2C_FUNC_SMBUS_WORD_DATA |
  149. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  150. dev_err(dev, "needed i2c functionality is not supported\n");
  151. return -EINVAL;
  152. }
  153. capdev = devm_kzalloc(dev, sizeof(*capdev), GFP_KERNEL);
  154. if (!capdev)
  155. return -ENOMEM;
  156. capdev->client = client;
  157. err = atmel_read(capdev, REG_KEY_STATE,
  158. &capdev->prev_btn, sizeof(capdev->prev_btn));
  159. if (err) {
  160. dev_err(dev, "failed to read initial button state: %d\n", err);
  161. return err;
  162. }
  163. capdev->input = devm_input_allocate_device(dev);
  164. if (!capdev->input) {
  165. dev_err(dev, "failed to allocate input device\n");
  166. return -ENOMEM;
  167. }
  168. capdev->input->id.bustype = BUS_I2C;
  169. capdev->input->id.product = 0x880A;
  170. capdev->input->id.version = 0;
  171. capdev->input->name = "ATMegaXX Capacitive Button Controller";
  172. __set_bit(EV_KEY, capdev->input->evbit);
  173. node = dev->of_node;
  174. if (!node) {
  175. dev_err(dev, "failed to find matching node in device tree\n");
  176. return -EINVAL;
  177. }
  178. if (of_property_read_bool(node, "autorepeat"))
  179. __set_bit(EV_REP, capdev->input->evbit);
  180. capdev->num_btn = of_property_count_u32_elems(node, "linux,keymap");
  181. if (capdev->num_btn > MAX_NUM_OF_BUTTONS)
  182. capdev->num_btn = MAX_NUM_OF_BUTTONS;
  183. err = of_property_read_u32_array(node, "linux,keycodes",
  184. capdev->keycodes,
  185. capdev->num_btn);
  186. if (err) {
  187. dev_err(dev,
  188. "failed to read linux,keycode property: %d\n", err);
  189. return err;
  190. }
  191. for (i = 0; i < capdev->num_btn; i++)
  192. __set_bit(capdev->keycodes[i], capdev->input->keybit);
  193. capdev->input->keycode = capdev->keycodes;
  194. capdev->input->keycodesize = sizeof(capdev->keycodes[0]);
  195. capdev->input->keycodemax = capdev->num_btn;
  196. err = input_register_device(capdev->input);
  197. if (err)
  198. return err;
  199. err = devm_request_threaded_irq(dev, client->irq,
  200. NULL, atmel_captouch_isr,
  201. IRQF_ONESHOT,
  202. "atmel_captouch", capdev);
  203. if (err) {
  204. dev_err(dev, "failed to request irq %d: %d\n",
  205. client->irq, err);
  206. return err;
  207. }
  208. return 0;
  209. }
  210. static const struct of_device_id atmel_captouch_of_id[] = {
  211. {
  212. .compatible = "atmel,captouch",
  213. },
  214. { /* sentinel */ }
  215. };
  216. MODULE_DEVICE_TABLE(of, atmel_captouch_of_id);
  217. static const struct i2c_device_id atmel_captouch_id[] = {
  218. { "atmel_captouch" },
  219. { }
  220. };
  221. MODULE_DEVICE_TABLE(i2c, atmel_captouch_id);
  222. static struct i2c_driver atmel_captouch_driver = {
  223. .probe = atmel_captouch_probe,
  224. .id_table = atmel_captouch_id,
  225. .driver = {
  226. .name = "atmel_captouch",
  227. .of_match_table = atmel_captouch_of_id,
  228. },
  229. };
  230. module_i2c_driver(atmel_captouch_driver);
  231. /* Module information */
  232. MODULE_AUTHOR("Hung-yu Wu <hywu@google.com>");
  233. MODULE_DESCRIPTION("Atmel ATmegaXX Capacitance Touch Sensor I2C Driver");
  234. MODULE_LICENSE("GPL v2");