1 // SPDX-License-Identifier: GPL-2.0-only
3 * Atmel Atmegaxx Capacitive Touch Button Driver
5 * Copyright (C) 2016 Google, inc.
9 * It's irrelevant that the HW used to develop captouch driver is based
10 * on Atmega88PA part and uses QtouchADC parts for sensing touch.
11 * Calling this driver "captouch" is an arbitrary way to distinguish
12 * the protocol this driver supported by other atmel/qtouch drivers.
14 * Captouch driver supports a newer/different version of the I2C
15 * registers/commands than the qt1070.c driver.
16 * Don't let the similarity of the general driver structure fool you.
18 * For raw i2c access from userspace, use i2cset/i2cget
19 * to poke at /dev/i2c-N devices.
22 #include <linux/device.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/i2c.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
31 /* Maximum number of buttons supported */
32 #define MAX_NUM_OF_BUTTONS 8
35 #define REG_KEY1_THRESHOLD 0x02
36 #define REG_KEY2_THRESHOLD 0x03
37 #define REG_KEY3_THRESHOLD 0x04
38 #define REG_KEY4_THRESHOLD 0x05
40 #define REG_KEY1_REF_H 0x20
41 #define REG_KEY1_REF_L 0x21
42 #define REG_KEY2_REF_H 0x22
43 #define REG_KEY2_REF_L 0x23
44 #define REG_KEY3_REF_H 0x24
45 #define REG_KEY3_REF_L 0x25
46 #define REG_KEY4_REF_H 0x26
47 #define REG_KEY4_REF_L 0x27
49 #define REG_KEY1_DLT_H 0x30
50 #define REG_KEY1_DLT_L 0x31
51 #define REG_KEY2_DLT_H 0x32
52 #define REG_KEY2_DLT_L 0x33
53 #define REG_KEY3_DLT_H 0x34
54 #define REG_KEY3_DLT_L 0x35
55 #define REG_KEY4_DLT_H 0x36
56 #define REG_KEY4_DLT_L 0x37
58 #define REG_KEY_STATE 0x3C
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
68 struct atmel_captouch_device {
69 struct i2c_client *client;
70 struct input_dev *input;
72 u32 keycodes[MAX_NUM_OF_BUTTONS];
74 u8 xfer_buf[8] ____cacheline_aligned;
78 * Read from I2C slave device
79 * The protocol is that the client has to provide both the register address
80 * and the length, and while reading back the device would prepend the data
81 * with address and length for verification.
83 static int atmel_read(struct atmel_captouch_device *capdev,
84 u8 reg, u8 *data, size_t len)
86 struct i2c_client *client = capdev->client;
87 struct device *dev = &client->dev;
88 struct i2c_msg msg[2];
91 if (len > sizeof(capdev->xfer_buf) - 2)
94 capdev->xfer_buf[0] = reg;
95 capdev->xfer_buf[1] = len;
97 msg[0].addr = client->addr;
99 msg[0].buf = capdev->xfer_buf;
102 msg[1].addr = client->addr;
103 msg[1].flags = I2C_M_RD;
104 msg[1].buf = capdev->xfer_buf;
105 msg[1].len = len + 2;
107 err = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
108 if (err != ARRAY_SIZE(msg))
109 return err < 0 ? err : -EIO;
111 if (capdev->xfer_buf[0] != reg) {
113 "I2C read error: register address does not match (%#02x vs %02x)\n",
114 capdev->xfer_buf[0], reg);
118 memcpy(data, &capdev->xfer_buf[2], len);
124 * Handle interrupt and report the key changes to the input system.
125 * Multi-touch can be supported; however, it really depends on whether
126 * the device can multi-touch.
128 static irqreturn_t atmel_captouch_isr(int irq, void *data)
130 struct atmel_captouch_device *capdev = data;
131 struct device *dev = &capdev->client->dev;
137 error = atmel_read(capdev, REG_KEY_STATE, &new_btn, 1);
139 dev_err(dev, "failed to read button state: %d\n", error);
143 dev_dbg(dev, "%s: button state %#02x\n", __func__, new_btn);
145 changed_btn = new_btn ^ capdev->prev_btn;
146 capdev->prev_btn = new_btn;
148 for (i = 0; i < capdev->num_btn; i++) {
149 if (changed_btn & BIT(i))
150 input_report_key(capdev->input,
155 input_sync(capdev->input);
162 * Probe function to setup the device, input system and interrupt
164 static int atmel_captouch_probe(struct i2c_client *client,
165 const struct i2c_device_id *id)
167 struct atmel_captouch_device *capdev;
168 struct device *dev = &client->dev;
169 struct device_node *node;
173 if (!i2c_check_functionality(client->adapter,
174 I2C_FUNC_SMBUS_BYTE_DATA |
175 I2C_FUNC_SMBUS_WORD_DATA |
176 I2C_FUNC_SMBUS_I2C_BLOCK)) {
177 dev_err(dev, "needed i2c functionality is not supported\n");
181 capdev = devm_kzalloc(dev, sizeof(*capdev), GFP_KERNEL);
185 capdev->client = client;
187 err = atmel_read(capdev, REG_KEY_STATE,
188 &capdev->prev_btn, sizeof(capdev->prev_btn));
190 dev_err(dev, "failed to read initial button state: %d\n", err);
194 capdev->input = devm_input_allocate_device(dev);
195 if (!capdev->input) {
196 dev_err(dev, "failed to allocate input device\n");
200 capdev->input->id.bustype = BUS_I2C;
201 capdev->input->id.product = 0x880A;
202 capdev->input->id.version = 0;
203 capdev->input->name = "ATMegaXX Capacitive Button Controller";
204 __set_bit(EV_KEY, capdev->input->evbit);
208 dev_err(dev, "failed to find matching node in device tree\n");
212 if (of_property_read_bool(node, "autorepeat"))
213 __set_bit(EV_REP, capdev->input->evbit);
215 capdev->num_btn = of_property_count_u32_elems(node, "linux,keymap");
216 if (capdev->num_btn > MAX_NUM_OF_BUTTONS)
217 capdev->num_btn = MAX_NUM_OF_BUTTONS;
219 err = of_property_read_u32_array(node, "linux,keycodes",
224 "failed to read linux,keycode property: %d\n", err);
228 for (i = 0; i < capdev->num_btn; i++)
229 __set_bit(capdev->keycodes[i], capdev->input->keybit);
231 capdev->input->keycode = capdev->keycodes;
232 capdev->input->keycodesize = sizeof(capdev->keycodes[0]);
233 capdev->input->keycodemax = capdev->num_btn;
235 err = input_register_device(capdev->input);
239 err = devm_request_threaded_irq(dev, client->irq,
240 NULL, atmel_captouch_isr,
242 "atmel_captouch", capdev);
244 dev_err(dev, "failed to request irq %d: %d\n",
253 static const struct of_device_id atmel_captouch_of_id[] = {
255 .compatible = "atmel,captouch",
259 MODULE_DEVICE_TABLE(of, atmel_captouch_of_id);
262 static const struct i2c_device_id atmel_captouch_id[] = {
263 { "atmel_captouch", 0 },
266 MODULE_DEVICE_TABLE(i2c, atmel_captouch_id);
268 static struct i2c_driver atmel_captouch_driver = {
269 .probe = atmel_captouch_probe,
270 .id_table = atmel_captouch_id,
272 .name = "atmel_captouch",
273 .of_match_table = of_match_ptr(atmel_captouch_of_id),
276 module_i2c_driver(atmel_captouch_driver);
278 /* Module information */
279 MODULE_AUTHOR("Hung-yu Wu <hywu@google.com>");
280 MODULE_DESCRIPTION("Atmel ATmegaXX Capacitance Touch Sensor I2C Driver");
281 MODULE_LICENSE("GPL v2");