2 * Atmel AT42QT1070 QTouch Sensor Controller
4 * Copyright (C) 2011 Atmel
6 * Authors: Bo Shen <voice.shen@atmel.com>
8 * Base on AT42QT2160 driver by:
9 * Raphael Derosso Pereira <raphaelpereira@gmail.com>
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.
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.
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.
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>
36 /* Address for each register */
38 #define QT1070_CHIP_ID 0x2E
40 #define FW_VERSION 0x01
41 #define QT1070_FW_VERSION 0x15
43 #define DET_STATUS 0x02
45 #define KEY_STATUS 0x03
48 #define CALIBRATE_CMD 0x38
49 #define QT1070_CAL_TIME 200
53 #define QT1070_RESET_TIME 255
55 /* AT42QT1070 support up to 7 keys */
56 static const unsigned short qt1070_key2code
[] = {
57 KEY_0
, KEY_1
, KEY_2
, KEY_3
,
62 struct i2c_client
*client
;
63 struct input_dev
*input
;
65 unsigned short keycodes
[ARRAY_SIZE(qt1070_key2code
)];
69 static int qt1070_read(struct i2c_client
*client
, u8 reg
)
73 ret
= i2c_smbus_read_byte_data(client
, reg
);
76 "can not read register, returned %d\n", ret
);
81 static int qt1070_write(struct i2c_client
*client
, u8 reg
, u8 data
)
85 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
88 "can not write register, returned %d\n", ret
);
93 static bool qt1070_identify(struct i2c_client
*client
)
98 id
= qt1070_read(client
, CHIP_ID
);
99 if (id
!= QT1070_CHIP_ID
) {
100 dev_err(&client
->dev
, "ID %d not supported\n", id
);
104 /* Read firmware version */
105 ver
= qt1070_read(client
, FW_VERSION
);
107 dev_err(&client
->dev
, "could not read the firmware version\n");
111 dev_info(&client
->dev
, "AT42QT1070 firmware version %x\n", ver
);
116 static irqreturn_t
qt1070_interrupt(int irq
, void *dev_id
)
118 struct qt1070_data
*data
= dev_id
;
119 struct i2c_client
*client
= data
->client
;
120 struct input_dev
*input
= data
->input
;
122 u8 new_keys
, keyval
, mask
= 0x01;
124 /* Read the detected status register, thus clearing interrupt */
125 qt1070_read(client
, DET_STATUS
);
127 /* Read which key changed */
128 new_keys
= qt1070_read(client
, KEY_STATUS
);
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
);
138 data
->last_keys
= new_keys
;
142 static int qt1070_probe(struct i2c_client
*client
,
143 const struct i2c_device_id
*id
)
145 struct qt1070_data
*data
;
146 struct input_dev
*input
;
150 err
= i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE
);
152 dev_err(&client
->dev
, "%s adapter not supported\n",
153 dev_driver_string(&client
->adapter
->dev
));
158 dev_err(&client
->dev
, "please assign the irq to this device\n");
162 /* Identify the qt1070 chip */
163 if (!qt1070_identify(client
))
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");
174 data
->client
= client
;
176 data
->irq
= client
->irq
;
178 input
->name
= "AT42QT1070 QTouch Sensor";
179 input
->dev
.parent
= &client
->dev
;
180 input
->id
.bustype
= BUS_I2C
;
182 /* Add the keycode */
183 input
->keycode
= data
->keycodes
;
184 input
->keycodesize
= sizeof(data
->keycodes
[0]);
185 input
->keycodemax
= ARRAY_SIZE(qt1070_key2code
);
187 __set_bit(EV_KEY
, input
->evbit
);
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
);
194 /* Calibrate device */
195 qt1070_write(client
, CALIBRATE_CMD
, 1);
196 msleep(QT1070_CAL_TIME
);
199 qt1070_write(client
, RESET
, 1);
200 msleep(QT1070_RESET_TIME
);
202 err
= request_threaded_irq(client
->irq
, NULL
, qt1070_interrupt
,
203 IRQF_TRIGGER_NONE
| IRQF_ONESHOT
,
204 client
->dev
.driver
->name
, data
);
206 dev_err(&client
->dev
, "fail to request irq\n");
210 /* Register the input device */
211 err
= input_register_device(data
->input
);
213 dev_err(&client
->dev
, "Failed to register input device\n");
217 i2c_set_clientdata(client
, data
);
219 /* Read to clear the chang line */
220 qt1070_read(client
, DET_STATUS
);
225 free_irq(client
->irq
, data
);
227 input_free_device(input
);
232 static int qt1070_remove(struct i2c_client
*client
)
234 struct qt1070_data
*data
= i2c_get_clientdata(client
);
237 free_irq(client
->irq
, data
);
239 input_unregister_device(data
->input
);
245 #ifdef CONFIG_PM_SLEEP
246 static int qt1070_suspend(struct device
*dev
)
248 struct i2c_client
*client
= to_i2c_client(dev
);
249 struct qt1070_data
*data
= i2c_get_clientdata(client
);
251 if (device_may_wakeup(dev
))
252 enable_irq_wake(data
->irq
);
257 static int qt1070_resume(struct device
*dev
)
259 struct i2c_client
*client
= to_i2c_client(dev
);
260 struct qt1070_data
*data
= i2c_get_clientdata(client
);
262 if (device_may_wakeup(dev
))
263 disable_irq_wake(data
->irq
);
269 static SIMPLE_DEV_PM_OPS(qt1070_pm_ops
, qt1070_suspend
, qt1070_resume
);
271 static const struct i2c_device_id qt1070_id
[] = {
275 MODULE_DEVICE_TABLE(i2c
, qt1070_id
);
277 static struct i2c_driver qt1070_driver
= {
280 .owner
= THIS_MODULE
,
281 .pm
= &qt1070_pm_ops
,
283 .id_table
= qt1070_id
,
284 .probe
= qt1070_probe
,
285 .remove
= qt1070_remove
,
288 module_i2c_driver(qt1070_driver
);
290 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
291 MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
292 MODULE_LICENSE("GPL");