atmel_captouch.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. const struct i2c_device_id *id)
  141. {
  142. struct atmel_captouch_device *capdev;
  143. struct device *dev = &client->dev;
  144. struct device_node *node;
  145. int i;
  146. int err;
  147. if (!i2c_check_functionality(client->adapter,
  148. I2C_FUNC_SMBUS_BYTE_DATA |
  149. I2C_FUNC_SMBUS_WORD_DATA |
  150. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  151. dev_err(dev, "needed i2c functionality is not supported\n");
  152. return -EINVAL;
  153. }
  154. capdev = devm_kzalloc(dev, sizeof(*capdev), GFP_KERNEL);
  155. if (!capdev)
  156. return -ENOMEM;
  157. capdev->client = client;
  158. err = atmel_read(capdev, REG_KEY_STATE,
  159. &capdev->prev_btn, sizeof(capdev->prev_btn));
  160. if (err) {
  161. dev_err(dev, "failed to read initial button state: %d\n", err);
  162. return err;
  163. }
  164. capdev->input = devm_input_allocate_device(dev);
  165. if (!capdev->input) {
  166. dev_err(dev, "failed to allocate input device\n");
  167. return -ENOMEM;
  168. }
  169. capdev->input->id.bustype = BUS_I2C;
  170. capdev->input->id.product = 0x880A;
  171. capdev->input->id.version = 0;
  172. capdev->input->name = "ATMegaXX Capacitive Button Controller";
  173. __set_bit(EV_KEY, capdev->input->evbit);
  174. node = dev->of_node;
  175. if (!node) {
  176. dev_err(dev, "failed to find matching node in device tree\n");
  177. return -EINVAL;
  178. }
  179. if (of_property_read_bool(node, "autorepeat"))
  180. __set_bit(EV_REP, capdev->input->evbit);
  181. capdev->num_btn = of_property_count_u32_elems(node, "linux,keymap");
  182. if (capdev->num_btn > MAX_NUM_OF_BUTTONS)
  183. capdev->num_btn = MAX_NUM_OF_BUTTONS;
  184. err = of_property_read_u32_array(node, "linux,keycodes",
  185. capdev->keycodes,
  186. capdev->num_btn);
  187. if (err) {
  188. dev_err(dev,
  189. "failed to read linux,keycode property: %d\n", err);
  190. return err;
  191. }
  192. for (i = 0; i < capdev->num_btn; i++)
  193. __set_bit(capdev->keycodes[i], capdev->input->keybit);
  194. capdev->input->keycode = capdev->keycodes;
  195. capdev->input->keycodesize = sizeof(capdev->keycodes[0]);
  196. capdev->input->keycodemax = capdev->num_btn;
  197. err = input_register_device(capdev->input);
  198. if (err)
  199. return err;
  200. err = devm_request_threaded_irq(dev, client->irq,
  201. NULL, atmel_captouch_isr,
  202. IRQF_ONESHOT,
  203. "atmel_captouch", capdev);
  204. if (err) {
  205. dev_err(dev, "failed to request irq %d: %d\n",
  206. client->irq, err);
  207. return err;
  208. }
  209. return 0;
  210. }
  211. #ifdef CONFIG_OF
  212. static const struct of_device_id atmel_captouch_of_id[] = {
  213. {
  214. .compatible = "atmel,captouch",
  215. },
  216. { /* sentinel */ }
  217. };
  218. MODULE_DEVICE_TABLE(of, atmel_captouch_of_id);
  219. #endif
  220. static const struct i2c_device_id atmel_captouch_id[] = {
  221. { "atmel_captouch", 0 },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(i2c, atmel_captouch_id);
  225. static struct i2c_driver atmel_captouch_driver = {
  226. .probe = atmel_captouch_probe,
  227. .id_table = atmel_captouch_id,
  228. .driver = {
  229. .name = "atmel_captouch",
  230. .of_match_table = of_match_ptr(atmel_captouch_of_id),
  231. },
  232. };
  233. module_i2c_driver(atmel_captouch_driver);
  234. /* Module information */
  235. MODULE_AUTHOR("Hung-yu Wu <[email protected]>");
  236. MODULE_DESCRIPTION("Atmel ATmegaXX Capacitance Touch Sensor I2C Driver");
  237. MODULE_LICENSE("GPL v2");