xref: /linux/drivers/input/keyboard/qt1050.c (revision b48543c451c30387b53ee6e202dda8d5303f6268)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Microchip AT42QT1050 QTouch Sensor Controller
4  *
5  *  Copyright (C) 2019 Pengutronix, Marco Felsch <kernel@pengutronix.de>
6  *
7  *  Base on AT42QT1070 driver by:
8  *  Bo Shen <voice.shen@atmel.com>
9  *  Copyright (C) 2011 Atmel
10  */
11 
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>
19 #include <linux/of.h>
20 #include <linux/regmap.h>
21 
22 /* Chip ID */
23 #define QT1050_CHIP_ID		0x00
24 #define QT1050_CHIP_ID_VER	0x46
25 
26 /* Firmware version */
27 #define QT1050_FW_VERSION	0x01
28 
29 /* Detection status */
30 #define QT1050_DET_STATUS	0x02
31 
32 /* Key status */
33 #define QT1050_KEY_STATUS	0x03
34 
35 /* Key Signals */
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
46 
47 /* Reference data */
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
58 
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
65 
66 /* Pulse / Scale  */
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
72 
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
79 
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
86 
87 /* Low Power Mode */
88 #define QT1050_LPMODE		0x3d
89 
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)
94 
95 #define QT1050_MAX_KEYS		5
96 #define QT1050_RESET_TIME	255
97 
98 struct qt1050_key_regs {
99 	unsigned int nthr;
100 	unsigned int pulse_scale;
101 	unsigned int di_aks;
102 	unsigned int csd;
103 };
104 
105 struct qt1050_key {
106 	u32 num;
107 	u32 charge_delay;
108 	u32 thr_cnt;
109 	u32 samples;
110 	u32 scale;
111 	u32 keycode;
112 };
113 
114 struct qt1050_priv {
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];
120 	u8			reg_keys;
121 	u8			last_keys;
122 };
123 
124 static const struct qt1050_key_regs qt1050_key_regs_data[] = {
125 	{
126 		.nthr = QT1050_NTHR_0,
127 		.pulse_scale = QT1050_PULSE_SCALE_0,
128 		.di_aks = QT1050_DI_AKS_0,
129 		.csd = QT1050_CSD_0,
130 	}, {
131 		.nthr = QT1050_NTHR_1,
132 		.pulse_scale = QT1050_PULSE_SCALE_1,
133 		.di_aks = QT1050_DI_AKS_1,
134 		.csd = QT1050_CSD_1,
135 	}, {
136 		.nthr = QT1050_NTHR_2,
137 		.pulse_scale = QT1050_PULSE_SCALE_2,
138 		.di_aks = QT1050_DI_AKS_2,
139 		.csd = QT1050_CSD_2,
140 	}, {
141 		.nthr = QT1050_NTHR_3,
142 		.pulse_scale = QT1050_PULSE_SCALE_3,
143 		.di_aks = QT1050_DI_AKS_3,
144 		.csd = QT1050_CSD_3,
145 	}, {
146 		.nthr = QT1050_NTHR_4,
147 		.pulse_scale = QT1050_PULSE_SCALE_4,
148 		.di_aks = QT1050_DI_AKS_4,
149 		.csd = QT1050_CSD_4,
150 	}
151 };
152 
153 static bool qt1050_volatile_reg(struct device *dev, unsigned int reg)
154 {
155 	switch (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:
168 		return true;
169 	default:
170 		return false;
171 	}
172 }
173 
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),
188 };
189 
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),
193 };
194 
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),
204 };
205 
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),
209 };
210 
211 static const struct regmap_config qt1050_regmap_config = {
212 	.reg_bits = 8,
213 	.val_bits = 8,
214 	.max_register = QT1050_RES_CAL,
215 
216 	.cache_type = REGCACHE_MAPLE,
217 
218 	.wr_table = &qt1050_writeable_table,
219 	.rd_table = &qt1050_readable_table,
220 	.volatile_reg = qt1050_volatile_reg,
221 };
222 
223 static bool qt1050_identify(struct qt1050_priv *ts)
224 {
225 	unsigned int val;
226 	int err;
227 
228 	/* Read Chip ID */
229 	err = regmap_read(ts->regmap, QT1050_CHIP_ID, &val);
230 	if (err) {
231 		dev_err(&ts->client->dev, "Failed to read chip ID: %d\n", err);
232 		return false;
233 	}
234 
235 	if (val != QT1050_CHIP_ID_VER) {
236 		dev_err(&ts->client->dev, "ID %d not supported\n", val);
237 		return false;
238 	}
239 
240 	/* Read firmware version */
241 	err = regmap_read(ts->regmap, QT1050_FW_VERSION, &val);
242 	if (err) {
243 		dev_err(&ts->client->dev, "could not read the firmware version\n");
244 		return false;
245 	}
246 
247 	dev_info(&ts->client->dev, "AT42QT1050 firmware version %1d.%1d\n",
248 		 val >> 4, val & 0xf);
249 
250 	return true;
251 }
252 
253 static irqreturn_t qt1050_irq_threaded(int irq, void *dev_id)
254 {
255 	struct qt1050_priv *ts = dev_id;
256 	struct input_dev *input = ts->input;
257 	unsigned long new_keys, changed;
258 	unsigned int val;
259 	int i, err;
260 
261 	/* Read the detected status register, thus clearing interrupt */
262 	err = regmap_read(ts->regmap, QT1050_DET_STATUS, &val);
263 	if (err) {
264 		dev_err(&ts->client->dev, "Fail to read detection status: %d\n",
265 			err);
266 		return IRQ_NONE;
267 	}
268 
269 	/* Read which key changed, keys are not continuous */
270 	err = regmap_read(ts->regmap, QT1050_KEY_STATUS, &val);
271 	if (err) {
272 		dev_err(&ts->client->dev,
273 			"Fail to determine the key status: %d\n", err);
274 		return IRQ_NONE;
275 	}
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;
280 
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));
284 
285 	ts->last_keys = new_keys;
286 	input_sync(input);
287 
288 	return IRQ_HANDLED;
289 }
290 
291 static const struct qt1050_key_regs *qt1050_get_key_regs(int key_num)
292 {
293 	return &qt1050_key_regs_data[key_num];
294 }
295 
296 static int qt1050_set_key(struct regmap *map, int number, int on)
297 {
298 	const struct qt1050_key_regs *key_regs;
299 
300 	key_regs = qt1050_get_key_regs(number);
301 
302 	return regmap_update_bits(map, key_regs->di_aks, 0xfc,
303 				  on ? BIT(4) : 0x00);
304 }
305 
306 static int qt1050_apply_fw_data(struct qt1050_priv *ts)
307 {
308 	struct regmap *map = ts->regmap;
309 	struct qt1050_key *button = &ts->keys[0];
310 	const struct qt1050_key_regs *key_regs;
311 	int i, err;
312 
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);
316 		if (err)
317 			return err;
318 	}
319 
320 	for (i = 0; i < QT1050_MAX_KEYS; i++, button++) {
321 		/* Keep KEY_RESERVED keys off */
322 		if (button->keycode == KEY_RESERVED)
323 			continue;
324 
325 		err = qt1050_set_key(map, button->num, 1);
326 		if (err)
327 			return err;
328 
329 		key_regs = qt1050_get_key_regs(button->num);
330 
331 		err = regmap_write(map, key_regs->pulse_scale,
332 				   (button->samples << 4) | (button->scale));
333 		if (err)
334 			return err;
335 		err = regmap_write(map, key_regs->csd, button->charge_delay);
336 		if (err)
337 			return err;
338 		err = regmap_write(map, key_regs->nthr, button->thr_cnt);
339 		if (err)
340 			return err;
341 	}
342 
343 	return 0;
344 }
345 
346 static int qt1050_parse_fw(struct qt1050_priv *ts)
347 {
348 	struct device *dev = &ts->client->dev;
349 	struct fwnode_handle *child;
350 	int nbuttons;
351 
352 	nbuttons = device_get_child_node_count(dev);
353 	if (nbuttons == 0 || nbuttons > QT1050_MAX_KEYS)
354 		return -ENODEV;
355 
356 	device_for_each_child_node(dev, child) {
357 		struct qt1050_key button;
358 
359 		/* Required properties */
360 		if (fwnode_property_read_u32(child, "linux,code",
361 					     &button.keycode)) {
362 			dev_err(dev, "Button without keycode\n");
363 			goto err;
364 		}
365 		if (button.keycode >= KEY_MAX) {
366 			dev_err(dev, "Invalid keycode 0x%x\n",
367 				button.keycode);
368 			goto err;
369 		}
370 
371 		if (fwnode_property_read_u32(child, "reg",
372 					     &button.num)) {
373 			dev_err(dev, "Button without pad number\n");
374 			goto err;
375 		}
376 		if (button.num < 0 || button.num > QT1050_MAX_KEYS - 1)
377 			goto err;
378 
379 		ts->reg_keys |= BIT(button.num);
380 
381 		/* Optional properties */
382 		if (fwnode_property_read_u32(child,
383 					     "microchip,pre-charge-time-ns",
384 					     &button.charge_delay)) {
385 			button.charge_delay = 0;
386 		} else {
387 			if (button.charge_delay % 2500 == 0)
388 				button.charge_delay =
389 					button.charge_delay / 2500;
390 			else
391 				button.charge_delay = 0;
392 		}
393 
394 		if (fwnode_property_read_u32(child, "microchip,average-samples",
395 					 &button.samples)) {
396 			button.samples = 0;
397 		} else {
398 			if (is_power_of_2(button.samples))
399 				button.samples = ilog2(button.samples);
400 			else
401 				button.samples = 0;
402 		}
403 
404 		if (fwnode_property_read_u32(child, "microchip,average-scaling",
405 					     &button.scale)) {
406 			button.scale = 0;
407 		} else {
408 			if (is_power_of_2(button.scale))
409 				button.scale = ilog2(button.scale);
410 			else
411 				button.scale = 0;
412 
413 		}
414 
415 		if (fwnode_property_read_u32(child, "microchip,threshold",
416 					 &button.thr_cnt)) {
417 			button.thr_cnt = 20;
418 		} else {
419 			if (button.thr_cnt > 255)
420 				button.thr_cnt = 20;
421 		}
422 
423 		ts->keys[button.num] = button;
424 	}
425 
426 	return 0;
427 
428 err:
429 	fwnode_handle_put(child);
430 	return -EINVAL;
431 }
432 
433 static int qt1050_probe(struct i2c_client *client)
434 {
435 	struct qt1050_priv *ts;
436 	struct input_dev *input;
437 	struct device *dev = &client->dev;
438 	struct regmap *map;
439 	unsigned int status, i;
440 	int err;
441 
442 	/* Check basic functionality */
443 	err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
444 	if (!err) {
445 		dev_err(&client->dev, "%s adapter not supported\n",
446 			dev_driver_string(&client->adapter->dev));
447 		return -ENODEV;
448 	}
449 
450 	if (!client->irq) {
451 		dev_err(dev, "assign a irq line to this device\n");
452 		return -EINVAL;
453 	}
454 
455 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
456 	if (!ts)
457 		return -ENOMEM;
458 
459 	input = devm_input_allocate_device(dev);
460 	if (!input)
461 		return -ENOMEM;
462 
463 	map = devm_regmap_init_i2c(client, &qt1050_regmap_config);
464 	if (IS_ERR(map))
465 		return PTR_ERR(map);
466 
467 	ts->client = client;
468 	ts->input = input;
469 	ts->regmap = map;
470 
471 	i2c_set_clientdata(client, ts);
472 
473 	/* Identify the qt1050 chip */
474 	if (!qt1050_identify(ts))
475 		return -ENODEV;
476 
477 	/* Get pdata */
478 	err = qt1050_parse_fw(ts);
479 	if (err) {
480 		dev_err(dev, "Failed to parse firmware: %d\n", err);
481 		return err;
482 	}
483 
484 	input->name = "AT42QT1050 QTouch Sensor";
485 	input->dev.parent = &client->dev;
486 	input->id.bustype = BUS_I2C;
487 
488 	/* Add the keycode */
489 	input->keycode = ts->keycodes;
490 	input->keycodesize = sizeof(ts->keycodes[0]);
491 	input->keycodemax = QT1050_MAX_KEYS;
492 
493 	__set_bit(EV_KEY, input->evbit);
494 	for (i = 0; i < QT1050_MAX_KEYS; i++) {
495 		ts->keycodes[i] = ts->keys[i].keycode;
496 		__set_bit(ts->keycodes[i], input->keybit);
497 	}
498 
499 	/* Trigger re-calibration */
500 	err = regmap_update_bits(ts->regmap, QT1050_RES_CAL, 0x7f,
501 				 QT1050_RES_CAL_CALIBRATE);
502 	if (err) {
503 		dev_err(dev, "Trigger calibration failed: %d\n", err);
504 		return err;
505 	}
506 	err = regmap_read_poll_timeout(ts->regmap, QT1050_DET_STATUS, status,
507 				 status >> 7 == 1, 10000, 200000);
508 	if (err) {
509 		dev_err(dev, "Calibration failed: %d\n", err);
510 		return err;
511 	}
512 
513 	/* Soft reset to set defaults */
514 	err = regmap_update_bits(ts->regmap, QT1050_RES_CAL,
515 				 QT1050_RES_CAL_RESET, QT1050_RES_CAL_RESET);
516 	if (err) {
517 		dev_err(dev, "Trigger soft reset failed: %d\n", err);
518 		return err;
519 	}
520 	msleep(QT1050_RESET_TIME);
521 
522 	/* Set pdata */
523 	err = qt1050_apply_fw_data(ts);
524 	if (err) {
525 		dev_err(dev, "Failed to set firmware data: %d\n", err);
526 		return err;
527 	}
528 
529 	err = devm_request_threaded_irq(dev, client->irq, NULL,
530 					qt1050_irq_threaded, IRQF_ONESHOT,
531 					"qt1050", ts);
532 	if (err) {
533 		dev_err(&client->dev, "Failed to request irq: %d\n", err);
534 		return err;
535 	}
536 
537 	/* Clear #CHANGE line */
538 	err = regmap_read(ts->regmap, QT1050_DET_STATUS, &status);
539 	if (err) {
540 		dev_err(dev, "Failed to clear #CHANGE line level: %d\n", err);
541 		return err;
542 	}
543 
544 	/* Register the input device */
545 	err = input_register_device(ts->input);
546 	if (err) {
547 		dev_err(&client->dev, "Failed to register input device: %d\n",
548 			err);
549 		return err;
550 	}
551 
552 	return 0;
553 }
554 
555 static int qt1050_suspend(struct device *dev)
556 {
557 	struct i2c_client *client = to_i2c_client(dev);
558 	struct qt1050_priv *ts = i2c_get_clientdata(client);
559 
560 	disable_irq(client->irq);
561 
562 	/*
563 	 * Set measurement interval to 1s (125 x 8ms) if wakeup is allowed
564 	 * else turn off. The 1s interval seems to be a good compromise between
565 	 * low power and response time.
566 	 */
567 	return regmap_write(ts->regmap, QT1050_LPMODE,
568 			    device_may_wakeup(dev) ? 125 : 0);
569 }
570 
571 static int qt1050_resume(struct device *dev)
572 {
573 	struct i2c_client *client = to_i2c_client(dev);
574 	struct qt1050_priv *ts = i2c_get_clientdata(client);
575 
576 	enable_irq(client->irq);
577 
578 	/* Set measurement interval back to 16ms (2 x 8ms) */
579 	return regmap_write(ts->regmap, QT1050_LPMODE, 2);
580 }
581 
582 static DEFINE_SIMPLE_DEV_PM_OPS(qt1050_pm_ops, qt1050_suspend, qt1050_resume);
583 
584 static const struct of_device_id __maybe_unused qt1050_of_match[] = {
585 	{ .compatible = "microchip,qt1050", },
586 	{ },
587 };
588 MODULE_DEVICE_TABLE(of, qt1050_of_match);
589 
590 static struct i2c_driver qt1050_driver = {
591 	.driver	= {
592 		.name = "qt1050",
593 		.of_match_table = of_match_ptr(qt1050_of_match),
594 		.pm = pm_sleep_ptr(&qt1050_pm_ops),
595 	},
596 	.probe = qt1050_probe,
597 };
598 
599 module_i2c_driver(qt1050_driver);
600 
601 MODULE_AUTHOR("Marco Felsch <kernel@pengutronix.de");
602 MODULE_DESCRIPTION("Driver for AT42QT1050 QTouch sensor");
603 MODULE_LICENSE("GPL v2");
604