atmel_captouch.c 7.5 KB

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