1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * qt2160.c - Atmel AT42QT2160 Touch Sense Controller
5 * Copyright (C) 2009 Raphael Derosso Pereira <raphaelpereira@gmail.com>
8 #include <linux/kernel.h>
9 #include <linux/leds.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/i2c.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/input.h>
18 #define QT2160_VALID_CHIPID 0x11
20 #define QT2160_CMD_CHIPID 0
21 #define QT2160_CMD_CODEVER 1
22 #define QT2160_CMD_GSTAT 2
23 #define QT2160_CMD_KEYS3 3
24 #define QT2160_CMD_KEYS4 4
25 #define QT2160_CMD_SLIDE 5
26 #define QT2160_CMD_GPIOS 6
27 #define QT2160_CMD_SUBVER 7
28 #define QT2160_CMD_CALIBRATE 10
29 #define QT2160_CMD_DRIVE_X 70
30 #define QT2160_CMD_PWMEN_X 74
31 #define QT2160_CMD_PWM_DUTY 76
33 #define QT2160_NUM_LEDS_X 8
35 #define QT2160_CYCLE_INTERVAL 2000 /* msec - 2 sec */
37 static unsigned char qt2160_key2code
[] = {
38 KEY_0
, KEY_1
, KEY_2
, KEY_3
,
39 KEY_4
, KEY_5
, KEY_6
, KEY_7
,
40 KEY_8
, KEY_9
, KEY_A
, KEY_B
,
41 KEY_C
, KEY_D
, KEY_E
, KEY_F
,
44 #ifdef CONFIG_LEDS_CLASS
46 struct qt2160_data
*qt2160
;
47 struct led_classdev cdev
;
50 enum led_brightness brightness
;
55 struct i2c_client
*client
;
56 struct input_dev
*input
;
57 unsigned short keycodes
[ARRAY_SIZE(qt2160_key2code
)];
59 #ifdef CONFIG_LEDS_CLASS
60 struct qt2160_led leds
[QT2160_NUM_LEDS_X
];
64 static int qt2160_read(struct i2c_client
*client
, u8 reg
);
65 static int qt2160_write(struct i2c_client
*client
, u8 reg
, u8 data
);
67 #ifdef CONFIG_LEDS_CLASS
69 static int qt2160_led_set(struct led_classdev
*cdev
,
70 enum led_brightness value
)
72 struct qt2160_led
*led
= container_of(cdev
, struct qt2160_led
, cdev
);
73 struct qt2160_data
*qt2160
= led
->qt2160
;
74 struct i2c_client
*client
= qt2160
->client
;
77 if (value
!= led
->brightness
) {
78 drive
= qt2160_read(client
, QT2160_CMD_DRIVE_X
);
79 pwmen
= qt2160_read(client
, QT2160_CMD_PWMEN_X
);
80 if (value
!= LED_OFF
) {
81 drive
|= BIT(led
->id
);
82 pwmen
|= BIT(led
->id
);
85 drive
&= ~BIT(led
->id
);
86 pwmen
&= ~BIT(led
->id
);
88 qt2160_write(client
, QT2160_CMD_DRIVE_X
, drive
);
89 qt2160_write(client
, QT2160_CMD_PWMEN_X
, pwmen
);
92 * Changing this register will change the brightness
93 * of every LED in the qt2160. It's a HW limitation.
96 qt2160_write(client
, QT2160_CMD_PWM_DUTY
, value
);
98 led
->brightness
= value
;
104 #endif /* CONFIG_LEDS_CLASS */
106 static int qt2160_read_block(struct i2c_client
*client
,
107 u8 inireg
, u8
*buffer
, unsigned int count
)
112 * Can't use SMBus block data read. Check for I2C functionality to speed
113 * things up whenever possible. Otherwise we will be forced to read
116 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
118 error
= i2c_smbus_write_byte(client
, inireg
+ idx
);
120 dev_err(&client
->dev
,
121 "couldn't send request. Returned %d\n", error
);
125 error
= i2c_master_recv(client
, buffer
, count
);
126 if (error
!= count
) {
127 dev_err(&client
->dev
,
128 "couldn't read registers. Returned %d bytes\n", error
);
136 error
= i2c_smbus_write_byte(client
, inireg
+ idx
);
138 dev_err(&client
->dev
,
139 "couldn't send request. Returned %d\n", error
);
143 data
= i2c_smbus_read_byte(client
);
145 dev_err(&client
->dev
,
146 "couldn't read register. Returned %d\n", data
);
150 buffer
[idx
++] = data
;
157 static void qt2160_get_key_matrix(struct input_dev
*input
)
159 struct qt2160_data
*qt2160
= input_get_drvdata(input
);
160 struct i2c_client
*client
= qt2160
->client
;
162 u16 old_matrix
, new_matrix
;
165 dev_dbg(&client
->dev
, "requesting keys...\n");
168 * Read all registers from General Status Register
171 ret
= qt2160_read_block(client
, QT2160_CMD_GSTAT
, regs
, 6);
173 dev_err(&client
->dev
,
174 "could not perform chip read.\n");
178 old_matrix
= qt2160
->key_matrix
;
179 qt2160
->key_matrix
= new_matrix
= (regs
[2] << 8) | regs
[1];
182 for (i
= 0; i
< 16; ++i
, mask
<<= 1) {
183 int keyval
= new_matrix
& mask
;
185 if ((old_matrix
& mask
) != keyval
) {
186 input_report_key(input
, qt2160
->keycodes
[i
], keyval
);
187 dev_dbg(&client
->dev
, "key %d %s\n",
188 i
, keyval
? "pressed" : "released");
195 static irqreturn_t
qt2160_irq(int irq
, void *data
)
197 struct input_dev
*input
= data
;
199 qt2160_get_key_matrix(input
);
204 static int qt2160_read(struct i2c_client
*client
, u8 reg
)
208 ret
= i2c_smbus_write_byte(client
, reg
);
210 dev_err(&client
->dev
,
211 "couldn't send request. Returned %d\n", ret
);
215 ret
= i2c_smbus_read_byte(client
);
217 dev_err(&client
->dev
,
218 "couldn't read register. Returned %d\n", ret
);
225 static int qt2160_write(struct i2c_client
*client
, u8 reg
, u8 data
)
229 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
231 dev_err(&client
->dev
,
232 "couldn't write data. Returned %d\n", ret
);
237 #ifdef CONFIG_LEDS_CLASS
239 static int qt2160_register_leds(struct qt2160_data
*qt2160
)
241 struct i2c_client
*client
= qt2160
->client
;
245 for (i
= 0; i
< QT2160_NUM_LEDS_X
; i
++) {
246 struct qt2160_led
*led
= &qt2160
->leds
[i
];
248 snprintf(led
->name
, sizeof(led
->name
), "qt2160:x%d", i
);
249 led
->cdev
.name
= led
->name
;
250 led
->cdev
.brightness_set_blocking
= qt2160_led_set
;
251 led
->cdev
.brightness
= LED_OFF
;
253 led
->qt2160
= qt2160
;
255 error
= devm_led_classdev_register(&client
->dev
, &led
->cdev
);
261 qt2160_write(client
, QT2160_CMD_DRIVE_X
, 0);
262 qt2160_write(client
, QT2160_CMD_PWMEN_X
, 0);
263 qt2160_write(client
, QT2160_CMD_PWM_DUTY
, 0);
270 static inline int qt2160_register_leds(struct qt2160_data
*qt2160
)
277 static bool qt2160_identify(struct i2c_client
*client
)
281 /* Read Chid ID to check if chip is valid */
282 id
= qt2160_read(client
, QT2160_CMD_CHIPID
);
283 if (id
!= QT2160_VALID_CHIPID
) {
284 dev_err(&client
->dev
, "ID %d not supported\n", id
);
288 /* Read chip firmware version */
289 ver
= qt2160_read(client
, QT2160_CMD_CODEVER
);
291 dev_err(&client
->dev
, "could not get firmware version\n");
295 /* Read chip firmware revision */
296 rev
= qt2160_read(client
, QT2160_CMD_SUBVER
);
298 dev_err(&client
->dev
, "could not get firmware revision\n");
302 dev_info(&client
->dev
, "AT42QT2160 firmware version %d.%d.%d\n",
303 ver
>> 4, ver
& 0xf, rev
);
308 static int qt2160_probe(struct i2c_client
*client
)
310 struct qt2160_data
*qt2160
;
311 struct input_dev
*input
;
315 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE
)) {
316 dev_err(&client
->dev
, "%s adapter not supported\n",
317 dev_driver_string(&client
->adapter
->dev
));
321 if (!qt2160_identify(client
))
324 /* Chip is valid and active. Allocate structure */
325 qt2160
= devm_kzalloc(&client
->dev
, sizeof(*qt2160
), GFP_KERNEL
);
329 input
= devm_input_allocate_device(&client
->dev
);
333 qt2160
->client
= client
;
334 qt2160
->input
= input
;
336 input
->name
= "AT42QT2160 Touch Sense Keyboard";
337 input
->id
.bustype
= BUS_I2C
;
339 input
->keycode
= qt2160
->keycodes
;
340 input
->keycodesize
= sizeof(qt2160
->keycodes
[0]);
341 input
->keycodemax
= ARRAY_SIZE(qt2160_key2code
);
343 __set_bit(EV_KEY
, input
->evbit
);
344 __clear_bit(EV_REP
, input
->evbit
);
345 for (i
= 0; i
< ARRAY_SIZE(qt2160_key2code
); i
++) {
346 qt2160
->keycodes
[i
] = qt2160_key2code
[i
];
347 __set_bit(qt2160_key2code
[i
], input
->keybit
);
349 __clear_bit(KEY_RESERVED
, input
->keybit
);
351 input_set_drvdata(input
, qt2160
);
353 /* Calibrate device */
354 error
= qt2160_write(client
, QT2160_CMD_CALIBRATE
, 1);
356 dev_err(&client
->dev
, "failed to calibrate device\n");
361 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
366 dev_err(&client
->dev
,
367 "failed to allocate irq %d\n", client
->irq
);
371 error
= input_setup_polling(input
, qt2160_get_key_matrix
);
373 dev_err(&client
->dev
, "Failed to setup polling\n");
376 input_set_poll_interval(input
, QT2160_CYCLE_INTERVAL
);
379 error
= qt2160_register_leds(qt2160
);
381 dev_err(&client
->dev
, "Failed to register leds\n");
385 error
= input_register_device(qt2160
->input
);
387 dev_err(&client
->dev
,
388 "Failed to register input device\n");
395 static const struct i2c_device_id qt2160_idtable
[] = {
400 MODULE_DEVICE_TABLE(i2c
, qt2160_idtable
);
402 static struct i2c_driver qt2160_driver
= {
407 .id_table
= qt2160_idtable
,
408 .probe
= qt2160_probe
,
411 module_i2c_driver(qt2160_driver
);
413 MODULE_AUTHOR("Raphael Derosso Pereira <raphaelpereira@gmail.com>");
414 MODULE_DESCRIPTION("Driver for AT42QT2160 Touch Sensor");
415 MODULE_LICENSE("GPL");