Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v3.15
  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");
v3.5.6
  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/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>
 36
 37/* Address for each register */
 38#define CHIP_ID            0x00
 39#define QT1070_CHIP_ID     0x2E
 40
 41#define FW_VERSION         0x01
 42#define QT1070_FW_VERSION  0x15
 43
 44#define DET_STATUS         0x02
 45
 46#define KEY_STATUS         0x03
 47
 48/* Calibrate */
 49#define CALIBRATE_CMD      0x38
 50#define QT1070_CAL_TIME    200
 51
 52/* Reset */
 53#define RESET              0x39
 54#define QT1070_RESET_TIME  255
 55
 56/* AT42QT1070 support up to 7 keys */
 57static const unsigned short qt1070_key2code[] = {
 58	KEY_0, KEY_1, KEY_2, KEY_3,
 59	KEY_4, KEY_5, KEY_6,
 60};
 61
 62struct qt1070_data {
 63	struct i2c_client *client;
 64	struct input_dev *input;
 65	unsigned int irq;
 66	unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
 67	u8 last_keys;
 68};
 69
 70static int qt1070_read(struct i2c_client *client, u8 reg)
 71{
 72	int ret;
 73
 74	ret = i2c_smbus_read_byte_data(client, reg);
 75	if (ret < 0)
 76		dev_err(&client->dev,
 77			"can not read register, returned %d\n", ret);
 78
 79	return ret;
 80}
 81
 82static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
 83{
 84	int ret;
 85
 86	ret = i2c_smbus_write_byte_data(client, reg, data);
 87	if (ret < 0)
 88		dev_err(&client->dev,
 89			"can not write register, returned %d\n", ret);
 90
 91	return ret;
 92}
 93
 94static bool __devinit qt1070_identify(struct i2c_client *client)
 95{
 96	int id, ver;
 97
 98	/* Read Chip ID */
 99	id = qt1070_read(client, CHIP_ID);
100	if (id != QT1070_CHIP_ID) {
101		dev_err(&client->dev, "ID %d not supported\n", id);
102		return false;
103	}
104
105	/* Read firmware version */
106	ver = qt1070_read(client, FW_VERSION);
107	if (ver < 0) {
108		dev_err(&client->dev, "could not read the firmware version\n");
109		return false;
110	}
111
112	dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
113
114	return true;
115}
116
117static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
118{
119	struct qt1070_data *data = dev_id;
120	struct i2c_client *client = data->client;
121	struct input_dev *input = data->input;
122	int i;
123	u8 new_keys, keyval, mask = 0x01;
124
125	/* Read the detected status register, thus clearing interrupt */
126	qt1070_read(client, DET_STATUS);
127
128	/* Read which key changed */
129	new_keys = qt1070_read(client, KEY_STATUS);
130
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);
135		mask <<= 1;
136	}
137	input_sync(input);
138
139	data->last_keys = new_keys;
140	return IRQ_HANDLED;
141}
142
143static int __devinit qt1070_probe(struct i2c_client *client,
144				const struct i2c_device_id *id)
145{
146	struct qt1070_data *data;
147	struct input_dev *input;
148	int i;
149	int err;
150
151	err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
152	if (!err) {
153		dev_err(&client->dev, "%s adapter not supported\n",
154			dev_driver_string(&client->adapter->dev));
155		return -ENODEV;
156	}
157
158	if (!client->irq) {
159		dev_err(&client->dev, "please assign the irq to this device\n");
160		return -EINVAL;
161	}
162
163	/* Identify the qt1070 chip */
164	if (!qt1070_identify(client))
165		return -ENODEV;
166
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");
171		err = -ENOMEM;
172		goto err_free_mem;
173	}
174
175	data->client = client;
176	data->input = input;
177	data->irq = client->irq;
178
179	input->name = "AT42QT1070 QTouch Sensor";
180	input->dev.parent = &client->dev;
181	input->id.bustype = BUS_I2C;
182
183	/* Add the keycode */
184	input->keycode = data->keycodes;
185	input->keycodesize = sizeof(data->keycodes[0]);
186	input->keycodemax = ARRAY_SIZE(qt1070_key2code);
187
188	__set_bit(EV_KEY, input->evbit);
189
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);
193	}
194
195	/* Calibrate device */
196	qt1070_write(client, CALIBRATE_CMD, 1);
197	msleep(QT1070_CAL_TIME);
198
199	/* Soft reset */
200	qt1070_write(client, RESET, 1);
201	msleep(QT1070_RESET_TIME);
202
203	err = request_threaded_irq(client->irq, NULL, qt1070_interrupt,
204				   IRQF_TRIGGER_NONE | IRQF_ONESHOT,
205				   client->dev.driver->name, data);
206	if (err) {
207		dev_err(&client->dev, "fail to request irq\n");
208		goto err_free_mem;
209	}
210
211	/* Register the input device */
212	err = input_register_device(data->input);
213	if (err) {
214		dev_err(&client->dev, "Failed to register input device\n");
215		goto err_free_irq;
216	}
217
218	i2c_set_clientdata(client, data);
219
220	/* Read to clear the chang line */
221	qt1070_read(client, DET_STATUS);
222
223	return 0;
224
225err_free_irq:
226	free_irq(client->irq, data);
227err_free_mem:
228	input_free_device(input);
229	kfree(data);
230	return err;
231}
232
233static int __devexit qt1070_remove(struct i2c_client *client)
234{
235	struct qt1070_data *data = i2c_get_clientdata(client);
236
237	/* Release IRQ */
238	free_irq(client->irq, data);
239
240	input_unregister_device(data->input);
241	kfree(data);
242
243	return 0;
244}
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246static const struct i2c_device_id qt1070_id[] = {
247	{ "qt1070", 0 },
248	{ },
249};
250MODULE_DEVICE_TABLE(i2c, qt1070_id);
251
252static struct i2c_driver qt1070_driver = {
253	.driver	= {
254		.name	= "qt1070",
255		.owner	= THIS_MODULE,
 
256	},
257	.id_table	= qt1070_id,
258	.probe		= qt1070_probe,
259	.remove		= __devexit_p(qt1070_remove),
260};
261
262module_i2c_driver(qt1070_driver);
263
264MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
265MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
266MODULE_LICENSE("GPL");