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/init.h>
29 #include <linux/i2c.h>
30 #include <linux/input.h>
31 #include <linux/slab.h>
32 #include <linux/irq.h>
33 #include <linux/interrupt.h>
34 #include <linux/jiffies.h>
35 #include <linux/delay.h>
37 /* Address for each register */
39 #define QT1070_CHIP_ID 0x2E
41 #define FW_VERSION 0x01
42 #define QT1070_FW_VERSION 0x15
44 #define DET_STATUS 0x02
46 #define KEY_STATUS 0x03
49 #define CALIBRATE_CMD 0x38
50 #define QT1070_CAL_TIME 200
54 #define QT1070_RESET_TIME 255
56 /* AT42QT1070 support up to 7 keys */
57 static const unsigned short qt1070_key2code
[] = {
58 KEY_0
, KEY_1
, KEY_2
, KEY_3
,
63 struct i2c_client
*client
;
64 struct input_dev
*input
;
66 unsigned short keycodes
[ARRAY_SIZE(qt1070_key2code
)];
70 static int qt1070_read(struct i2c_client
*client
, u8 reg
)
74 ret
= i2c_smbus_read_byte_data(client
, reg
);
77 "can not read register, returned %d\n", ret
);
82 static int qt1070_write(struct i2c_client
*client
, u8 reg
, u8 data
)
86 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
89 "can not write register, returned %d\n", ret
);
94 static bool __devinit
qt1070_identify(struct i2c_client
*client
)
99 id
= qt1070_read(client
, CHIP_ID
);
100 if (id
!= QT1070_CHIP_ID
) {
101 dev_err(&client
->dev
, "ID %d not supported\n", id
);
105 /* Read firmware version */
106 ver
= qt1070_read(client
, FW_VERSION
);
108 dev_err(&client
->dev
, "could not read the firmware version\n");
112 dev_info(&client
->dev
, "AT42QT1070 firmware version %x\n", ver
);
117 static irqreturn_t
qt1070_interrupt(int irq
, void *dev_id
)
119 struct qt1070_data
*data
= dev_id
;
120 struct i2c_client
*client
= data
->client
;
121 struct input_dev
*input
= data
->input
;
123 u8 new_keys
, keyval
, mask
= 0x01;
125 /* Read the detected status register, thus clearing interrupt */
126 qt1070_read(client
, DET_STATUS
);
128 /* Read which key changed */
129 new_keys
= qt1070_read(client
, KEY_STATUS
);
131 for (i
= 0; i
< ARRAY_SIZE(qt1070_key2code
); i
++) {
132 keyval
= new_keys
& mask
;
133 if ((data
->last_keys
& mask
) != keyval
)
134 input_report_key(input
, data
->keycodes
[i
], keyval
);
139 data
->last_keys
= new_keys
;
143 static int __devinit
qt1070_probe(struct i2c_client
*client
,
144 const struct i2c_device_id
*id
)
146 struct qt1070_data
*data
;
147 struct input_dev
*input
;
151 err
= i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE
);
153 dev_err(&client
->dev
, "%s adapter not supported\n",
154 dev_driver_string(&client
->adapter
->dev
));
159 dev_err(&client
->dev
, "please assign the irq to this device\n");
163 /* Identify the qt1070 chip */
164 if (!qt1070_identify(client
))
167 data
= kzalloc(sizeof(struct qt1070_data
), GFP_KERNEL
);
168 input
= input_allocate_device();
169 if (!data
|| !input
) {
170 dev_err(&client
->dev
, "insufficient memory\n");
175 data
->client
= client
;
177 data
->irq
= client
->irq
;
179 input
->name
= "AT42QT1070 QTouch Sensor";
180 input
->dev
.parent
= &client
->dev
;
181 input
->id
.bustype
= BUS_I2C
;
183 /* Add the keycode */
184 input
->keycode
= data
->keycodes
;
185 input
->keycodesize
= sizeof(data
->keycodes
[0]);
186 input
->keycodemax
= ARRAY_SIZE(qt1070_key2code
);
188 __set_bit(EV_KEY
, input
->evbit
);
190 for (i
= 0; i
< ARRAY_SIZE(qt1070_key2code
); i
++) {
191 data
->keycodes
[i
] = qt1070_key2code
[i
];
192 __set_bit(qt1070_key2code
[i
], input
->keybit
);
195 /* Calibrate device */
196 qt1070_write(client
, CALIBRATE_CMD
, 1);
197 msleep(QT1070_CAL_TIME
);
200 qt1070_write(client
, RESET
, 1);
201 msleep(QT1070_RESET_TIME
);
203 err
= request_threaded_irq(client
->irq
, NULL
, qt1070_interrupt
,
204 IRQF_TRIGGER_NONE
, 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 __devexit
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 static const struct i2c_device_id qt1070_id
[] = {
249 MODULE_DEVICE_TABLE(i2c
, qt1070_id
);
251 static struct i2c_driver qt1070_driver
= {
254 .owner
= THIS_MODULE
,
256 .id_table
= qt1070_id
,
257 .probe
= qt1070_probe
,
258 .remove
= __devexit_p(qt1070_remove
),
261 module_i2c_driver(qt1070_driver
);
263 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
264 MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
265 MODULE_LICENSE("GPL");