Loading...
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 */
43static const unsigned short qt1070_key2code[] = {
44 KEY_0, KEY_1, KEY_2, KEY_3,
45 KEY_4, KEY_5, KEY_6,
46};
47
48struct 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
56static 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
68static 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
80static 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
103static 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
129static 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 err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
137 if (!err) {
138 dev_err(&client->dev, "%s adapter not supported\n",
139 dev_driver_string(&client->adapter->dev));
140 return -ENODEV;
141 }
142
143 if (!client->irq) {
144 dev_err(&client->dev, "please assign the irq to this device\n");
145 return -EINVAL;
146 }
147
148 /* Identify the qt1070 chip */
149 if (!qt1070_identify(client))
150 return -ENODEV;
151
152 data = kzalloc(sizeof(struct qt1070_data), GFP_KERNEL);
153 input = input_allocate_device();
154 if (!data || !input) {
155 dev_err(&client->dev, "insufficient memory\n");
156 err = -ENOMEM;
157 goto err_free_mem;
158 }
159
160 data->client = client;
161 data->input = input;
162 data->irq = client->irq;
163
164 input->name = "AT42QT1070 QTouch Sensor";
165 input->dev.parent = &client->dev;
166 input->id.bustype = BUS_I2C;
167
168 /* Add the keycode */
169 input->keycode = data->keycodes;
170 input->keycodesize = sizeof(data->keycodes[0]);
171 input->keycodemax = ARRAY_SIZE(qt1070_key2code);
172
173 __set_bit(EV_KEY, input->evbit);
174
175 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
176 data->keycodes[i] = qt1070_key2code[i];
177 __set_bit(qt1070_key2code[i], input->keybit);
178 }
179
180 /* Calibrate device */
181 qt1070_write(client, CALIBRATE_CMD, 1);
182 msleep(QT1070_CAL_TIME);
183
184 /* Soft reset */
185 qt1070_write(client, RESET, 1);
186 msleep(QT1070_RESET_TIME);
187
188 err = request_threaded_irq(client->irq, 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 goto err_free_mem;
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 goto err_free_irq;
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
210err_free_irq:
211 free_irq(client->irq, data);
212err_free_mem:
213 input_free_device(input);
214 kfree(data);
215 return err;
216}
217
218static void qt1070_remove(struct i2c_client *client)
219{
220 struct qt1070_data *data = i2c_get_clientdata(client);
221
222 /* Release IRQ */
223 free_irq(client->irq, data);
224
225 input_unregister_device(data->input);
226 kfree(data);
227}
228
229static int qt1070_suspend(struct device *dev)
230{
231 struct i2c_client *client = to_i2c_client(dev);
232 struct qt1070_data *data = i2c_get_clientdata(client);
233
234 if (device_may_wakeup(dev))
235 enable_irq_wake(data->irq);
236
237 return 0;
238}
239
240static int qt1070_resume(struct device *dev)
241{
242 struct i2c_client *client = to_i2c_client(dev);
243 struct qt1070_data *data = i2c_get_clientdata(client);
244
245 if (device_may_wakeup(dev))
246 disable_irq_wake(data->irq);
247
248 return 0;
249}
250
251static DEFINE_SIMPLE_DEV_PM_OPS(qt1070_pm_ops, qt1070_suspend, qt1070_resume);
252
253static const struct i2c_device_id qt1070_id[] = {
254 { "qt1070", 0 },
255 { },
256};
257MODULE_DEVICE_TABLE(i2c, qt1070_id);
258
259#ifdef CONFIG_OF
260static const struct of_device_id qt1070_of_match[] = {
261 { .compatible = "qt1070", },
262 { },
263};
264MODULE_DEVICE_TABLE(of, qt1070_of_match);
265#endif
266
267static struct i2c_driver qt1070_driver = {
268 .driver = {
269 .name = "qt1070",
270 .of_match_table = of_match_ptr(qt1070_of_match),
271 .pm = pm_sleep_ptr(&qt1070_pm_ops),
272 },
273 .id_table = qt1070_id,
274 .probe_new = qt1070_probe,
275 .remove = qt1070_remove,
276};
277
278module_i2c_driver(qt1070_driver);
279
280MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
281MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
282MODULE_LICENSE("GPL");
1/*
2 * Atmel AT42QT1070 QTouch Sensor Controller
3 *
4 * Copyright (C) 2011 Atmel
5 *
6 * Authors: Bo Shen <voice.shen@atmel.com>
7 *
8 * Base on AT42QT2160 driver by:
9 * Raphael Derosso Pereira <raphaelpereira@gmail.com>
10 * Copyright (C) 2009
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/i2c.h>
29#include <linux/input.h>
30#include <linux/slab.h>
31#include <linux/irq.h>
32#include <linux/interrupt.h>
33#include <linux/jiffies.h>
34#include <linux/delay.h>
35
36/* Address for each register */
37#define CHIP_ID 0x00
38#define QT1070_CHIP_ID 0x2E
39
40#define FW_VERSION 0x01
41#define QT1070_FW_VERSION 0x15
42
43#define DET_STATUS 0x02
44
45#define KEY_STATUS 0x03
46
47/* Calibrate */
48#define CALIBRATE_CMD 0x38
49#define QT1070_CAL_TIME 200
50
51/* Reset */
52#define RESET 0x39
53#define QT1070_RESET_TIME 255
54
55/* AT42QT1070 support up to 7 keys */
56static const unsigned short qt1070_key2code[] = {
57 KEY_0, KEY_1, KEY_2, KEY_3,
58 KEY_4, KEY_5, KEY_6,
59};
60
61struct qt1070_data {
62 struct i2c_client *client;
63 struct input_dev *input;
64 unsigned int irq;
65 unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
66 u8 last_keys;
67};
68
69static int qt1070_read(struct i2c_client *client, u8 reg)
70{
71 int ret;
72
73 ret = i2c_smbus_read_byte_data(client, reg);
74 if (ret < 0)
75 dev_err(&client->dev,
76 "can not read register, returned %d\n", ret);
77
78 return ret;
79}
80
81static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
82{
83 int ret;
84
85 ret = i2c_smbus_write_byte_data(client, reg, data);
86 if (ret < 0)
87 dev_err(&client->dev,
88 "can not write register, returned %d\n", ret);
89
90 return ret;
91}
92
93static bool qt1070_identify(struct i2c_client *client)
94{
95 int id, ver;
96
97 /* Read Chip ID */
98 id = qt1070_read(client, CHIP_ID);
99 if (id != QT1070_CHIP_ID) {
100 dev_err(&client->dev, "ID %d not supported\n", id);
101 return false;
102 }
103
104 /* Read firmware version */
105 ver = qt1070_read(client, FW_VERSION);
106 if (ver < 0) {
107 dev_err(&client->dev, "could not read the firmware version\n");
108 return false;
109 }
110
111 dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
112
113 return true;
114}
115
116static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
117{
118 struct qt1070_data *data = dev_id;
119 struct i2c_client *client = data->client;
120 struct input_dev *input = data->input;
121 int i;
122 u8 new_keys, keyval, mask = 0x01;
123
124 /* Read the detected status register, thus clearing interrupt */
125 qt1070_read(client, DET_STATUS);
126
127 /* Read which key changed */
128 new_keys = qt1070_read(client, KEY_STATUS);
129
130 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
131 keyval = new_keys & mask;
132 if ((data->last_keys & mask) != keyval)
133 input_report_key(input, data->keycodes[i], keyval);
134 mask <<= 1;
135 }
136 input_sync(input);
137
138 data->last_keys = new_keys;
139 return IRQ_HANDLED;
140}
141
142static int qt1070_probe(struct i2c_client *client,
143 const struct i2c_device_id *id)
144{
145 struct qt1070_data *data;
146 struct input_dev *input;
147 int i;
148 int err;
149
150 err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
151 if (!err) {
152 dev_err(&client->dev, "%s adapter not supported\n",
153 dev_driver_string(&client->adapter->dev));
154 return -ENODEV;
155 }
156
157 if (!client->irq) {
158 dev_err(&client->dev, "please assign the irq to this device\n");
159 return -EINVAL;
160 }
161
162 /* Identify the qt1070 chip */
163 if (!qt1070_identify(client))
164 return -ENODEV;
165
166 data = kzalloc(sizeof(struct qt1070_data), GFP_KERNEL);
167 input = input_allocate_device();
168 if (!data || !input) {
169 dev_err(&client->dev, "insufficient memory\n");
170 err = -ENOMEM;
171 goto err_free_mem;
172 }
173
174 data->client = client;
175 data->input = input;
176 data->irq = client->irq;
177
178 input->name = "AT42QT1070 QTouch Sensor";
179 input->dev.parent = &client->dev;
180 input->id.bustype = BUS_I2C;
181
182 /* Add the keycode */
183 input->keycode = data->keycodes;
184 input->keycodesize = sizeof(data->keycodes[0]);
185 input->keycodemax = ARRAY_SIZE(qt1070_key2code);
186
187 __set_bit(EV_KEY, input->evbit);
188
189 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
190 data->keycodes[i] = qt1070_key2code[i];
191 __set_bit(qt1070_key2code[i], input->keybit);
192 }
193
194 /* Calibrate device */
195 qt1070_write(client, CALIBRATE_CMD, 1);
196 msleep(QT1070_CAL_TIME);
197
198 /* Soft reset */
199 qt1070_write(client, RESET, 1);
200 msleep(QT1070_RESET_TIME);
201
202 err = request_threaded_irq(client->irq, NULL, qt1070_interrupt,
203 IRQF_TRIGGER_NONE | IRQF_ONESHOT,
204 client->dev.driver->name, data);
205 if (err) {
206 dev_err(&client->dev, "fail to request irq\n");
207 goto err_free_mem;
208 }
209
210 /* Register the input device */
211 err = input_register_device(data->input);
212 if (err) {
213 dev_err(&client->dev, "Failed to register input device\n");
214 goto err_free_irq;
215 }
216
217 i2c_set_clientdata(client, data);
218
219 /* Read to clear the chang line */
220 qt1070_read(client, DET_STATUS);
221
222 return 0;
223
224err_free_irq:
225 free_irq(client->irq, data);
226err_free_mem:
227 input_free_device(input);
228 kfree(data);
229 return err;
230}
231
232static int qt1070_remove(struct i2c_client *client)
233{
234 struct qt1070_data *data = i2c_get_clientdata(client);
235
236 /* Release IRQ */
237 free_irq(client->irq, data);
238
239 input_unregister_device(data->input);
240 kfree(data);
241
242 return 0;
243}
244
245#ifdef CONFIG_PM_SLEEP
246static int qt1070_suspend(struct device *dev)
247{
248 struct i2c_client *client = to_i2c_client(dev);
249 struct qt1070_data *data = i2c_get_clientdata(client);
250
251 if (device_may_wakeup(dev))
252 enable_irq_wake(data->irq);
253
254 return 0;
255}
256
257static int qt1070_resume(struct device *dev)
258{
259 struct i2c_client *client = to_i2c_client(dev);
260 struct qt1070_data *data = i2c_get_clientdata(client);
261
262 if (device_may_wakeup(dev))
263 disable_irq_wake(data->irq);
264
265 return 0;
266}
267#endif
268
269static SIMPLE_DEV_PM_OPS(qt1070_pm_ops, qt1070_suspend, qt1070_resume);
270
271static const struct i2c_device_id qt1070_id[] = {
272 { "qt1070", 0 },
273 { },
274};
275MODULE_DEVICE_TABLE(i2c, qt1070_id);
276
277static struct i2c_driver qt1070_driver = {
278 .driver = {
279 .name = "qt1070",
280 .owner = THIS_MODULE,
281 .pm = &qt1070_pm_ops,
282 },
283 .id_table = qt1070_id,
284 .probe = qt1070_probe,
285 .remove = qt1070_remove,
286};
287
288module_i2c_driver(qt1070_driver);
289
290MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
291MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
292MODULE_LICENSE("GPL");