1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Atmel AT42QT1070 QTouch Sensor Controller
4 *
5 * Copyright (C) 2011 Atmel
6 *
7 * Authors: Bo Shen <voice.shen@atmel.com>
8 *
9 * Base on AT42QT2160 driver by:
10 * Raphael Derosso Pereira <raphaelpereira@gmail.com>
11 * Copyright (C) 2009
12 */
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>
22
23 /* Address for each register */
24 #define CHIP_ID 0x00
25 #define QT1070_CHIP_ID 0x2E
26
27 #define FW_VERSION 0x01
28 #define QT1070_FW_VERSION 0x15
29
30 #define DET_STATUS 0x02
31
32 #define KEY_STATUS 0x03
33
34 /* Calibrate */
35 #define CALIBRATE_CMD 0x38
36 #define QT1070_CAL_TIME 200
37
38 /* Reset */
39 #define RESET 0x39
40 #define QT1070_RESET_TIME 255
41
42 /* AT42QT1070 support up to 7 keys */
43 static const unsigned short qt1070_key2code[] = {
44 KEY_0, KEY_1, KEY_2, KEY_3,
45 KEY_4, KEY_5, KEY_6,
46 };
47
48 struct qt1070_data {
49 struct i2c_client *client;
50 struct input_dev *input;
51 unsigned int irq;
52 unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
53 u8 last_keys;
54 };
55
qt1070_read(struct i2c_client * client,u8 reg)56 static int qt1070_read(struct i2c_client *client, u8 reg)
57 {
58 int ret;
59
60 ret = i2c_smbus_read_byte_data(client, reg);
61 if (ret < 0)
62 dev_err(&client->dev,
63 "can not read register, returned %d\n", ret);
64
65 return ret;
66 }
67
qt1070_write(struct i2c_client * client,u8 reg,u8 data)68 static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
69 {
70 int ret;
71
72 ret = i2c_smbus_write_byte_data(client, reg, data);
73 if (ret < 0)
74 dev_err(&client->dev,
75 "can not write register, returned %d\n", ret);
76
77 return ret;
78 }
79
qt1070_identify(struct i2c_client * client)80 static bool qt1070_identify(struct i2c_client *client)
81 {
82 int id, ver;
83
84 /* Read Chip ID */
85 id = qt1070_read(client, CHIP_ID);
86 if (id != QT1070_CHIP_ID) {
87 dev_err(&client->dev, "ID %d not supported\n", id);
88 return false;
89 }
90
91 /* Read firmware version */
92 ver = qt1070_read(client, FW_VERSION);
93 if (ver < 0) {
94 dev_err(&client->dev, "could not read the firmware version\n");
95 return false;
96 }
97
98 dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
99
100 return true;
101 }
102
qt1070_interrupt(int irq,void * dev_id)103 static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
104 {
105 struct qt1070_data *data = dev_id;
106 struct i2c_client *client = data->client;
107 struct input_dev *input = data->input;
108 int i;
109 u8 new_keys, keyval, mask = 0x01;
110
111 /* Read the detected status register, thus clearing interrupt */
112 qt1070_read(client, DET_STATUS);
113
114 /* Read which key changed */
115 new_keys = qt1070_read(client, KEY_STATUS);
116
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);
121 mask <<= 1;
122 }
123 input_sync(input);
124
125 data->last_keys = new_keys;
126 return IRQ_HANDLED;
127 }
128
qt1070_probe(struct i2c_client * client)129 static int qt1070_probe(struct i2c_client *client)
130 {
131 struct qt1070_data *data;
132 struct input_dev *input;
133 int i;
134 int err;
135
136 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
137 dev_err(&client->dev, "%s adapter not supported\n",
138 dev_driver_string(&client->adapter->dev));
139 return -ENODEV;
140 }
141
142 if (!client->irq) {
143 dev_err(&client->dev, "please assign the irq to this device\n");
144 return -EINVAL;
145 }
146
147 /* Identify the qt1070 chip */
148 if (!qt1070_identify(client))
149 return -ENODEV;
150
151 data = devm_kzalloc(&client->dev, sizeof(struct qt1070_data),
152 GFP_KERNEL);
153 if (!data)
154 return -ENOMEM;
155
156 input = devm_input_allocate_device(&client->dev);
157 if (!input)
158 return -ENOMEM;
159
160 data->client = client;
161 data->input = input;
162 data->irq = client->irq;
163
164 input->name = "AT42QT1070 QTouch Sensor";
165 input->id.bustype = BUS_I2C;
166
167 /* Add the keycode */
168 input->keycode = data->keycodes;
169 input->keycodesize = sizeof(data->keycodes[0]);
170 input->keycodemax = ARRAY_SIZE(qt1070_key2code);
171
172 __set_bit(EV_KEY, input->evbit);
173
174 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
175 data->keycodes[i] = qt1070_key2code[i];
176 __set_bit(qt1070_key2code[i], input->keybit);
177 }
178
179 /* Calibrate device */
180 qt1070_write(client, CALIBRATE_CMD, 1);
181 msleep(QT1070_CAL_TIME);
182
183 /* Soft reset */
184 qt1070_write(client, RESET, 1);
185 msleep(QT1070_RESET_TIME);
186
187 err = devm_request_threaded_irq(&client->dev, client->irq,
188 NULL, qt1070_interrupt,
189 IRQF_TRIGGER_NONE | IRQF_ONESHOT,
190 client->dev.driver->name, data);
191 if (err) {
192 dev_err(&client->dev, "fail to request irq\n");
193 return err;
194 }
195
196 /* Register the input device */
197 err = input_register_device(data->input);
198 if (err) {
199 dev_err(&client->dev, "Failed to register input device\n");
200 return err;
201 }
202
203 i2c_set_clientdata(client, data);
204
205 /* Read to clear the chang line */
206 qt1070_read(client, DET_STATUS);
207
208 return 0;
209 }
210
qt1070_suspend(struct device * dev)211 static int qt1070_suspend(struct device *dev)
212 {
213 struct i2c_client *client = to_i2c_client(dev);
214 struct qt1070_data *data = i2c_get_clientdata(client);
215
216 if (device_may_wakeup(dev))
217 enable_irq_wake(data->irq);
218
219 return 0;
220 }
221
qt1070_resume(struct device * dev)222 static int qt1070_resume(struct device *dev)
223 {
224 struct i2c_client *client = to_i2c_client(dev);
225 struct qt1070_data *data = i2c_get_clientdata(client);
226
227 if (device_may_wakeup(dev))
228 disable_irq_wake(data->irq);
229
230 return 0;
231 }
232
233 static DEFINE_SIMPLE_DEV_PM_OPS(qt1070_pm_ops, qt1070_suspend, qt1070_resume);
234
235 static const struct i2c_device_id qt1070_id[] = {
236 { .name = "qt1070" },
237 { }
238 };
239 MODULE_DEVICE_TABLE(i2c, qt1070_id);
240
241 #ifdef CONFIG_OF
242 static const struct of_device_id qt1070_of_match[] = {
243 { .compatible = "qt1070", },
244 { },
245 };
246 MODULE_DEVICE_TABLE(of, qt1070_of_match);
247 #endif
248
249 static struct i2c_driver qt1070_driver = {
250 .driver = {
251 .name = "qt1070",
252 .of_match_table = of_match_ptr(qt1070_of_match),
253 .pm = pm_sleep_ptr(&qt1070_pm_ops),
254 },
255 .id_table = qt1070_id,
256 .probe = qt1070_probe,
257 };
258
259 module_i2c_driver(qt1070_driver);
260
261 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
262 MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
263 MODULE_LICENSE("GPL");
264