1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Atmel AT42QT1070 QTouch Sensor Controller
5 * Copyright (C) 2011 Atmel
9 * Base on AT42QT2160 driver by:
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>
23 /* Address for each register */
25 #define QT1070_CHIP_ID 0x2E
27 #define FW_VERSION 0x01
28 #define QT1070_FW_VERSION 0x15
30 #define DET_STATUS 0x02
32 #define KEY_STATUS 0x03
35 #define CALIBRATE_CMD 0x38
36 #define QT1070_CAL_TIME 200
40 #define QT1070_RESET_TIME 255
42 /* AT42QT1070 support up to 7 keys */
43 static const unsigned short qt1070_key2code[] = {
44 KEY_0, KEY_1, KEY_2, KEY_3,
49 struct i2c_client *client;
50 struct input_dev *input;
52 unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
56 static int qt1070_read(struct i2c_client *client, u8 reg)
60 ret = i2c_smbus_read_byte_data(client, reg);
63 "can not read register, returned %d\n", ret);
68 static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
72 ret = i2c_smbus_write_byte_data(client, reg, data);
75 "can not write register, returned %d\n", ret);
80 static bool qt1070_identify(struct i2c_client *client)
85 id = qt1070_read(client, CHIP_ID);
86 if (id != QT1070_CHIP_ID) {
87 dev_err(&client->dev, "ID %d not supported\n", id);
91 /* Read firmware version */
92 ver = qt1070_read(client, FW_VERSION);
94 dev_err(&client->dev, "could not read the firmware version\n");
98 dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
103 static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
105 struct qt1070_data *data = dev_id;
106 struct i2c_client *client = data->client;
107 struct input_dev *input = data->input;
109 u8 new_keys, keyval, mask = 0x01;
111 /* Read the detected status register, thus clearing interrupt */
112 qt1070_read(client, DET_STATUS);
114 /* Read which key changed */
115 new_keys = qt1070_read(client, KEY_STATUS);
117 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
118 keyval = new_keys & mask;
119 if ((data->last_keys & mask) != keyval)
120 input_report_key(input, data->keycodes[i], keyval);
125 data->last_keys = new_keys;
129 static int qt1070_probe(struct i2c_client *client,
130 const struct i2c_device_id *id)
132 struct qt1070_data *data;
133 struct input_dev *input;
137 err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
139 dev_err(&client->dev, "%s adapter not supported\n",
140 dev_driver_string(&client->adapter->dev));
145 dev_err(&client->dev, "please assign the irq to this device\n");
149 /* Identify the qt1070 chip */
150 if (!qt1070_identify(client))
153 data = kzalloc(sizeof(struct qt1070_data), GFP_KERNEL);
154 input = input_allocate_device();
155 if (!data || !input) {
156 dev_err(&client->dev, "insufficient memory\n");
161 data->client = client;
163 data->irq = client->irq;
165 input->name = "AT42QT1070 QTouch Sensor";
166 input->dev.parent = &client->dev;
167 input->id.bustype = BUS_I2C;
169 /* Add the keycode */
170 input->keycode = data->keycodes;
171 input->keycodesize = sizeof(data->keycodes[0]);
172 input->keycodemax = ARRAY_SIZE(qt1070_key2code);
174 __set_bit(EV_KEY, input->evbit);
176 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
177 data->keycodes[i] = qt1070_key2code[i];
178 __set_bit(qt1070_key2code[i], input->keybit);
181 /* Calibrate device */
182 qt1070_write(client, CALIBRATE_CMD, 1);
183 msleep(QT1070_CAL_TIME);
186 qt1070_write(client, RESET, 1);
187 msleep(QT1070_RESET_TIME);
189 err = request_threaded_irq(client->irq, NULL, qt1070_interrupt,
190 IRQF_TRIGGER_NONE | IRQF_ONESHOT,
191 client->dev.driver->name, data);
193 dev_err(&client->dev, "fail to request irq\n");
197 /* Register the input device */
198 err = input_register_device(data->input);
200 dev_err(&client->dev, "Failed to register input device\n");
204 i2c_set_clientdata(client, data);
206 /* Read to clear the chang line */
207 qt1070_read(client, DET_STATUS);
212 free_irq(client->irq, data);
214 input_free_device(input);
219 static int qt1070_remove(struct i2c_client *client)
221 struct qt1070_data *data = i2c_get_clientdata(client);
224 free_irq(client->irq, data);
226 input_unregister_device(data->input);
232 #ifdef CONFIG_PM_SLEEP
233 static int qt1070_suspend(struct device *dev)
235 struct i2c_client *client = to_i2c_client(dev);
236 struct qt1070_data *data = i2c_get_clientdata(client);
238 if (device_may_wakeup(dev))
239 enable_irq_wake(data->irq);
244 static int qt1070_resume(struct device *dev)
246 struct i2c_client *client = to_i2c_client(dev);
247 struct qt1070_data *data = i2c_get_clientdata(client);
249 if (device_may_wakeup(dev))
250 disable_irq_wake(data->irq);
256 static SIMPLE_DEV_PM_OPS(qt1070_pm_ops, qt1070_suspend, qt1070_resume);
258 static const struct i2c_device_id qt1070_id[] = {
262 MODULE_DEVICE_TABLE(i2c, qt1070_id);
265 static const struct of_device_id qt1070_of_match[] = {
266 { .compatible = "qt1070", },
269 MODULE_DEVICE_TABLE(of, qt1070_of_match);
272 static struct i2c_driver qt1070_driver = {
275 .of_match_table = of_match_ptr(qt1070_of_match),
276 .pm = &qt1070_pm_ops,
278 .id_table = qt1070_id,
279 .probe = qt1070_probe,
280 .remove = qt1070_remove,
283 module_i2c_driver(qt1070_driver);
286 MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
287 MODULE_LICENSE("GPL");