1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip AT42QT1050 QTouch Sensor Controller
5 * Copyright (C) 2019 Pengutronix, Marco Felsch <kernel@pengutronix.de>
7 * Base on AT42QT1070 driver by:
8 * Bo Shen <voice.shen@atmel.com>
9 * Copyright (C) 2011 Atmel
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
18 #include <linux/module.h>
20 #include <linux/regmap.h>
23 #define QT1050_CHIP_ID 0x00
24 #define QT1050_CHIP_ID_VER 0x46
26 /* Firmware version */
27 #define QT1050_FW_VERSION 0x01
29 /* Detection status */
30 #define QT1050_DET_STATUS 0x02
33 #define QT1050_KEY_STATUS 0x03
36 #define QT1050_KEY_SIGNAL_0_MSB 0x06
37 #define QT1050_KEY_SIGNAL_0_LSB 0x07
38 #define QT1050_KEY_SIGNAL_1_MSB 0x08
39 #define QT1050_KEY_SIGNAL_1_LSB 0x09
40 #define QT1050_KEY_SIGNAL_2_MSB 0x0c
41 #define QT1050_KEY_SIGNAL_2_LSB 0x0d
42 #define QT1050_KEY_SIGNAL_3_MSB 0x0e
43 #define QT1050_KEY_SIGNAL_3_LSB 0x0f
44 #define QT1050_KEY_SIGNAL_4_MSB 0x10
45 #define QT1050_KEY_SIGNAL_4_LSB 0x11
48 #define QT1050_REF_DATA_0_MSB 0x14
49 #define QT1050_REF_DATA_0_LSB 0x15
50 #define QT1050_REF_DATA_1_MSB 0x16
51 #define QT1050_REF_DATA_1_LSB 0x17
52 #define QT1050_REF_DATA_2_MSB 0x1a
53 #define QT1050_REF_DATA_2_LSB 0x1b
54 #define QT1050_REF_DATA_3_MSB 0x1c
55 #define QT1050_REF_DATA_3_LSB 0x1d
56 #define QT1050_REF_DATA_4_MSB 0x1e
57 #define QT1050_REF_DATA_4_LSB 0x1f
59 /* Negative threshold level */
60 #define QT1050_NTHR_0 0x21
61 #define QT1050_NTHR_1 0x22
62 #define QT1050_NTHR_2 0x24
63 #define QT1050_NTHR_3 0x25
64 #define QT1050_NTHR_4 0x26
67 #define QT1050_PULSE_SCALE_0 0x28
68 #define QT1050_PULSE_SCALE_1 0x29
69 #define QT1050_PULSE_SCALE_2 0x2b
70 #define QT1050_PULSE_SCALE_3 0x2c
71 #define QT1050_PULSE_SCALE_4 0x2d
73 /* Detection integrator counter / AKS */
74 #define QT1050_DI_AKS_0 0x2f
75 #define QT1050_DI_AKS_1 0x30
76 #define QT1050_DI_AKS_2 0x32
77 #define QT1050_DI_AKS_3 0x33
78 #define QT1050_DI_AKS_4 0x34
80 /* Charge Share Delay */
81 #define QT1050_CSD_0 0x36
82 #define QT1050_CSD_1 0x37
83 #define QT1050_CSD_2 0x39
84 #define QT1050_CSD_3 0x3a
85 #define QT1050_CSD_4 0x3b
88 #define QT1050_LPMODE 0x3d
90 /* Calibration and Reset */
91 #define QT1050_RES_CAL 0x3f
92 #define QT1050_RES_CAL_RESET BIT(7)
93 #define QT1050_RES_CAL_CALIBRATE BIT(1)
95 #define QT1050_MAX_KEYS 5
96 #define QT1050_RESET_TIME 255
98 struct qt1050_key_regs
{
100 unsigned int pulse_scale
;
115 struct i2c_client
*client
;
116 struct input_dev
*input
;
117 struct regmap
*regmap
;
118 struct qt1050_key keys
[QT1050_MAX_KEYS
];
119 unsigned short keycodes
[QT1050_MAX_KEYS
];
124 static const struct qt1050_key_regs qt1050_key_regs_data
[] = {
126 .nthr
= QT1050_NTHR_0
,
127 .pulse_scale
= QT1050_PULSE_SCALE_0
,
128 .di_aks
= QT1050_DI_AKS_0
,
131 .nthr
= QT1050_NTHR_1
,
132 .pulse_scale
= QT1050_PULSE_SCALE_1
,
133 .di_aks
= QT1050_DI_AKS_1
,
136 .nthr
= QT1050_NTHR_2
,
137 .pulse_scale
= QT1050_PULSE_SCALE_2
,
138 .di_aks
= QT1050_DI_AKS_2
,
141 .nthr
= QT1050_NTHR_3
,
142 .pulse_scale
= QT1050_PULSE_SCALE_3
,
143 .di_aks
= QT1050_DI_AKS_3
,
146 .nthr
= QT1050_NTHR_4
,
147 .pulse_scale
= QT1050_PULSE_SCALE_4
,
148 .di_aks
= QT1050_DI_AKS_4
,
153 static bool qt1050_volatile_reg(struct device
*dev
, unsigned int reg
)
156 case QT1050_DET_STATUS
:
157 case QT1050_KEY_STATUS
:
158 case QT1050_KEY_SIGNAL_0_MSB
:
159 case QT1050_KEY_SIGNAL_0_LSB
:
160 case QT1050_KEY_SIGNAL_1_MSB
:
161 case QT1050_KEY_SIGNAL_1_LSB
:
162 case QT1050_KEY_SIGNAL_2_MSB
:
163 case QT1050_KEY_SIGNAL_2_LSB
:
164 case QT1050_KEY_SIGNAL_3_MSB
:
165 case QT1050_KEY_SIGNAL_3_LSB
:
166 case QT1050_KEY_SIGNAL_4_MSB
:
167 case QT1050_KEY_SIGNAL_4_LSB
:
174 static const struct regmap_range qt1050_readable_ranges
[] = {
175 regmap_reg_range(QT1050_CHIP_ID
, QT1050_KEY_STATUS
),
176 regmap_reg_range(QT1050_KEY_SIGNAL_0_MSB
, QT1050_KEY_SIGNAL_1_LSB
),
177 regmap_reg_range(QT1050_KEY_SIGNAL_2_MSB
, QT1050_KEY_SIGNAL_4_LSB
),
178 regmap_reg_range(QT1050_REF_DATA_0_MSB
, QT1050_REF_DATA_1_LSB
),
179 regmap_reg_range(QT1050_REF_DATA_2_MSB
, QT1050_REF_DATA_4_LSB
),
180 regmap_reg_range(QT1050_NTHR_0
, QT1050_NTHR_1
),
181 regmap_reg_range(QT1050_NTHR_2
, QT1050_NTHR_4
),
182 regmap_reg_range(QT1050_PULSE_SCALE_0
, QT1050_PULSE_SCALE_1
),
183 regmap_reg_range(QT1050_PULSE_SCALE_2
, QT1050_PULSE_SCALE_4
),
184 regmap_reg_range(QT1050_DI_AKS_0
, QT1050_DI_AKS_1
),
185 regmap_reg_range(QT1050_DI_AKS_2
, QT1050_DI_AKS_4
),
186 regmap_reg_range(QT1050_CSD_0
, QT1050_CSD_1
),
187 regmap_reg_range(QT1050_CSD_2
, QT1050_RES_CAL
),
190 static const struct regmap_access_table qt1050_readable_table
= {
191 .yes_ranges
= qt1050_readable_ranges
,
192 .n_yes_ranges
= ARRAY_SIZE(qt1050_readable_ranges
),
195 static const struct regmap_range qt1050_writeable_ranges
[] = {
196 regmap_reg_range(QT1050_NTHR_0
, QT1050_NTHR_1
),
197 regmap_reg_range(QT1050_NTHR_2
, QT1050_NTHR_4
),
198 regmap_reg_range(QT1050_PULSE_SCALE_0
, QT1050_PULSE_SCALE_1
),
199 regmap_reg_range(QT1050_PULSE_SCALE_2
, QT1050_PULSE_SCALE_4
),
200 regmap_reg_range(QT1050_DI_AKS_0
, QT1050_DI_AKS_1
),
201 regmap_reg_range(QT1050_DI_AKS_2
, QT1050_DI_AKS_4
),
202 regmap_reg_range(QT1050_CSD_0
, QT1050_CSD_1
),
203 regmap_reg_range(QT1050_CSD_2
, QT1050_RES_CAL
),
206 static const struct regmap_access_table qt1050_writeable_table
= {
207 .yes_ranges
= qt1050_writeable_ranges
,
208 .n_yes_ranges
= ARRAY_SIZE(qt1050_writeable_ranges
),
211 static const struct regmap_config qt1050_regmap_config
= {
214 .max_register
= QT1050_RES_CAL
,
216 .cache_type
= REGCACHE_MAPLE
,
218 .wr_table
= &qt1050_writeable_table
,
219 .rd_table
= &qt1050_readable_table
,
220 .volatile_reg
= qt1050_volatile_reg
,
223 static bool qt1050_identify(struct qt1050_priv
*ts
)
229 err
= regmap_read(ts
->regmap
, QT1050_CHIP_ID
, &val
);
231 dev_err(&ts
->client
->dev
, "Failed to read chip ID: %d\n", err
);
235 if (val
!= QT1050_CHIP_ID_VER
) {
236 dev_err(&ts
->client
->dev
, "ID %d not supported\n", val
);
240 /* Read firmware version */
241 err
= regmap_read(ts
->regmap
, QT1050_FW_VERSION
, &val
);
243 dev_err(&ts
->client
->dev
, "could not read the firmware version\n");
247 dev_info(&ts
->client
->dev
, "AT42QT1050 firmware version %1d.%1d\n",
248 val
>> 4, val
& 0xf);
253 static irqreturn_t
qt1050_irq_threaded(int irq
, void *dev_id
)
255 struct qt1050_priv
*ts
= dev_id
;
256 struct input_dev
*input
= ts
->input
;
257 unsigned long new_keys
, changed
;
261 /* Read the detected status register, thus clearing interrupt */
262 err
= regmap_read(ts
->regmap
, QT1050_DET_STATUS
, &val
);
264 dev_err(&ts
->client
->dev
, "Fail to read detection status: %d\n",
269 /* Read which key changed, keys are not continuous */
270 err
= regmap_read(ts
->regmap
, QT1050_KEY_STATUS
, &val
);
272 dev_err(&ts
->client
->dev
,
273 "Fail to determine the key status: %d\n", err
);
276 new_keys
= (val
& 0x70) >> 2 | (val
& 0x6) >> 1;
277 changed
= ts
->last_keys
^ new_keys
;
278 /* Report registered keys only */
279 changed
&= ts
->reg_keys
;
281 for_each_set_bit(i
, &changed
, QT1050_MAX_KEYS
)
282 input_report_key(input
, ts
->keys
[i
].keycode
,
283 test_bit(i
, &new_keys
));
285 ts
->last_keys
= new_keys
;
291 static const struct qt1050_key_regs
*qt1050_get_key_regs(int key_num
)
293 return &qt1050_key_regs_data
[key_num
];
296 static int qt1050_set_key(struct regmap
*map
, int number
, int on
)
298 const struct qt1050_key_regs
*key_regs
;
300 key_regs
= qt1050_get_key_regs(number
);
302 return regmap_update_bits(map
, key_regs
->di_aks
, 0xfc,
306 static int qt1050_apply_fw_data(struct qt1050_priv
*ts
)
308 struct regmap
*map
= ts
->regmap
;
309 struct qt1050_key
*button
= &ts
->keys
[0];
310 const struct qt1050_key_regs
*key_regs
;
313 /* Disable all keys and enable only the specified ones */
314 for (i
= 0; i
< QT1050_MAX_KEYS
; i
++) {
315 err
= qt1050_set_key(map
, i
, 0);
320 for (i
= 0; i
< QT1050_MAX_KEYS
; i
++, button
++) {
321 /* Keep KEY_RESERVED keys off */
322 if (button
->keycode
== KEY_RESERVED
)
325 err
= qt1050_set_key(map
, button
->num
, 1);
329 key_regs
= qt1050_get_key_regs(button
->num
);
331 err
= regmap_write(map
, key_regs
->pulse_scale
,
332 (button
->samples
<< 4) | (button
->scale
));
335 err
= regmap_write(map
, key_regs
->csd
, button
->charge_delay
);
338 err
= regmap_write(map
, key_regs
->nthr
, button
->thr_cnt
);
346 static int qt1050_parse_fw(struct qt1050_priv
*ts
)
348 struct device
*dev
= &ts
->client
->dev
;
351 nbuttons
= device_get_child_node_count(dev
);
352 if (nbuttons
== 0 || nbuttons
> QT1050_MAX_KEYS
)
355 device_for_each_child_node_scoped(dev
, child
) {
356 struct qt1050_key button
;
358 /* Required properties */
359 if (fwnode_property_read_u32(child
, "linux,code",
361 dev_err(dev
, "Button without keycode\n");
364 if (button
.keycode
>= KEY_MAX
) {
365 dev_err(dev
, "Invalid keycode 0x%x\n",
370 if (fwnode_property_read_u32(child
, "reg",
372 dev_err(dev
, "Button without pad number\n");
375 if (button
.num
< 0 || button
.num
> QT1050_MAX_KEYS
- 1)
378 ts
->reg_keys
|= BIT(button
.num
);
380 /* Optional properties */
381 if (fwnode_property_read_u32(child
,
382 "microchip,pre-charge-time-ns",
383 &button
.charge_delay
)) {
384 button
.charge_delay
= 0;
386 if (button
.charge_delay
% 2500 == 0)
387 button
.charge_delay
=
388 button
.charge_delay
/ 2500;
390 button
.charge_delay
= 0;
393 if (fwnode_property_read_u32(child
, "microchip,average-samples",
397 if (is_power_of_2(button
.samples
))
398 button
.samples
= ilog2(button
.samples
);
403 if (fwnode_property_read_u32(child
, "microchip,average-scaling",
407 if (is_power_of_2(button
.scale
))
408 button
.scale
= ilog2(button
.scale
);
414 if (fwnode_property_read_u32(child
, "microchip,threshold",
418 if (button
.thr_cnt
> 255)
422 ts
->keys
[button
.num
] = button
;
428 static int qt1050_probe(struct i2c_client
*client
)
430 struct qt1050_priv
*ts
;
431 struct input_dev
*input
;
432 struct device
*dev
= &client
->dev
;
434 unsigned int status
, i
;
437 /* Check basic functionality */
438 err
= i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE
);
440 dev_err(&client
->dev
, "%s adapter not supported\n",
441 dev_driver_string(&client
->adapter
->dev
));
446 dev_err(dev
, "assign a irq line to this device\n");
450 ts
= devm_kzalloc(dev
, sizeof(*ts
), GFP_KERNEL
);
454 input
= devm_input_allocate_device(dev
);
458 map
= devm_regmap_init_i2c(client
, &qt1050_regmap_config
);
466 i2c_set_clientdata(client
, ts
);
468 /* Identify the qt1050 chip */
469 if (!qt1050_identify(ts
))
473 err
= qt1050_parse_fw(ts
);
475 dev_err(dev
, "Failed to parse firmware: %d\n", err
);
479 input
->name
= "AT42QT1050 QTouch Sensor";
480 input
->dev
.parent
= &client
->dev
;
481 input
->id
.bustype
= BUS_I2C
;
483 /* Add the keycode */
484 input
->keycode
= ts
->keycodes
;
485 input
->keycodesize
= sizeof(ts
->keycodes
[0]);
486 input
->keycodemax
= QT1050_MAX_KEYS
;
488 __set_bit(EV_KEY
, input
->evbit
);
489 for (i
= 0; i
< QT1050_MAX_KEYS
; i
++) {
490 ts
->keycodes
[i
] = ts
->keys
[i
].keycode
;
491 __set_bit(ts
->keycodes
[i
], input
->keybit
);
494 /* Trigger re-calibration */
495 err
= regmap_update_bits(ts
->regmap
, QT1050_RES_CAL
, 0x7f,
496 QT1050_RES_CAL_CALIBRATE
);
498 dev_err(dev
, "Trigger calibration failed: %d\n", err
);
501 err
= regmap_read_poll_timeout(ts
->regmap
, QT1050_DET_STATUS
, status
,
502 status
>> 7 == 1, 10000, 200000);
504 dev_err(dev
, "Calibration failed: %d\n", err
);
508 /* Soft reset to set defaults */
509 err
= regmap_update_bits(ts
->regmap
, QT1050_RES_CAL
,
510 QT1050_RES_CAL_RESET
, QT1050_RES_CAL_RESET
);
512 dev_err(dev
, "Trigger soft reset failed: %d\n", err
);
515 msleep(QT1050_RESET_TIME
);
518 err
= qt1050_apply_fw_data(ts
);
520 dev_err(dev
, "Failed to set firmware data: %d\n", err
);
524 err
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
525 qt1050_irq_threaded
, IRQF_ONESHOT
,
528 dev_err(&client
->dev
, "Failed to request irq: %d\n", err
);
532 /* Clear #CHANGE line */
533 err
= regmap_read(ts
->regmap
, QT1050_DET_STATUS
, &status
);
535 dev_err(dev
, "Failed to clear #CHANGE line level: %d\n", err
);
539 /* Register the input device */
540 err
= input_register_device(ts
->input
);
542 dev_err(&client
->dev
, "Failed to register input device: %d\n",
550 static int qt1050_suspend(struct device
*dev
)
552 struct i2c_client
*client
= to_i2c_client(dev
);
553 struct qt1050_priv
*ts
= i2c_get_clientdata(client
);
555 disable_irq(client
->irq
);
558 * Set measurement interval to 1s (125 x 8ms) if wakeup is allowed
559 * else turn off. The 1s interval seems to be a good compromise between
560 * low power and response time.
562 return regmap_write(ts
->regmap
, QT1050_LPMODE
,
563 device_may_wakeup(dev
) ? 125 : 0);
566 static int qt1050_resume(struct device
*dev
)
568 struct i2c_client
*client
= to_i2c_client(dev
);
569 struct qt1050_priv
*ts
= i2c_get_clientdata(client
);
571 enable_irq(client
->irq
);
573 /* Set measurement interval back to 16ms (2 x 8ms) */
574 return regmap_write(ts
->regmap
, QT1050_LPMODE
, 2);
577 static DEFINE_SIMPLE_DEV_PM_OPS(qt1050_pm_ops
, qt1050_suspend
, qt1050_resume
);
579 static const struct of_device_id __maybe_unused qt1050_of_match
[] = {
580 { .compatible
= "microchip,qt1050", },
583 MODULE_DEVICE_TABLE(of
, qt1050_of_match
);
585 static struct i2c_driver qt1050_driver
= {
588 .of_match_table
= of_match_ptr(qt1050_of_match
),
589 .pm
= pm_sleep_ptr(&qt1050_pm_ops
),
591 .probe
= qt1050_probe
,
594 module_i2c_driver(qt1050_driver
);
596 MODULE_AUTHOR("Marco Felsch <kernel@pengutronix.de");
597 MODULE_DESCRIPTION("Driver for AT42QT1050 QTouch sensor");
598 MODULE_LICENSE("GPL v2");