xref: /openbmc/linux/drivers/input/keyboard/qt1050.c (revision 979ac5ef)
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 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_RBTREE,
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 	regmap_read(ts->regmap, QT1050_CHIP_ID, &val);
230 	if (val != QT1050_CHIP_ID_VER) {
231 		dev_err(&ts->client->dev, "ID %d not supported\n", val);
232 		return false;
233 	}
234 
235 	/* Read firmware version */
236 	err = regmap_read(ts->regmap, QT1050_FW_VERSION, &val);
237 	if (err) {
238 		dev_err(&ts->client->dev, "could not read the firmware version\n");
239 		return false;
240 	}
241 
242 	dev_info(&ts->client->dev, "AT42QT1050 firmware version %1d.%1d\n",
243 		 val >> 4, val & 0xf);
244 
245 	return true;
246 }
247 
248 static irqreturn_t qt1050_irq_threaded(int irq, void *dev_id)
249 {
250 	struct qt1050_priv *ts = dev_id;
251 	struct input_dev *input = ts->input;
252 	unsigned long new_keys, changed;
253 	unsigned int val;
254 	int i, err;
255 
256 	/* Read the detected status register, thus clearing interrupt */
257 	err = regmap_read(ts->regmap, QT1050_DET_STATUS, &val);
258 	if (err) {
259 		dev_err(&ts->client->dev, "Fail to read detection status: %d\n",
260 			err);
261 		return IRQ_NONE;
262 	}
263 
264 	/* Read which key changed, keys are not continuous */
265 	err = regmap_read(ts->regmap, QT1050_KEY_STATUS, &val);
266 	if (err) {
267 		dev_err(&ts->client->dev,
268 			"Fail to determine the key status: %d\n", err);
269 		return IRQ_NONE;
270 	}
271 	new_keys = (val & 0x70) >> 2 | (val & 0x6) >> 1;
272 	changed = ts->last_keys ^ new_keys;
273 	/* Report registered keys only */
274 	changed &= ts->reg_keys;
275 
276 	for_each_set_bit(i, &changed, QT1050_MAX_KEYS)
277 		input_report_key(input, ts->keys[i].keycode,
278 				 test_bit(i, &new_keys));
279 
280 	ts->last_keys = new_keys;
281 	input_sync(input);
282 
283 	return IRQ_HANDLED;
284 }
285 
286 static const struct qt1050_key_regs *qt1050_get_key_regs(int key_num)
287 {
288 	return &qt1050_key_regs_data[key_num];
289 }
290 
291 static int qt1050_set_key(struct regmap *map, int number, int on)
292 {
293 	const struct qt1050_key_regs *key_regs;
294 
295 	key_regs = qt1050_get_key_regs(number);
296 
297 	return regmap_update_bits(map, key_regs->di_aks, 0xfc,
298 				  on ? BIT(4) : 0x00);
299 }
300 
301 static int qt1050_apply_fw_data(struct qt1050_priv *ts)
302 {
303 	struct regmap *map = ts->regmap;
304 	struct qt1050_key *button = &ts->keys[0];
305 	const struct qt1050_key_regs *key_regs;
306 	int i, err;
307 
308 	/* Disable all keys and enable only the specified ones */
309 	for (i = 0; i < QT1050_MAX_KEYS; i++) {
310 		err = qt1050_set_key(map, i, 0);
311 		if (err)
312 			return err;
313 	}
314 
315 	for (i = 0; i < QT1050_MAX_KEYS; i++, button++) {
316 		/* Keep KEY_RESERVED keys off */
317 		if (button->keycode == KEY_RESERVED)
318 			continue;
319 
320 		err = qt1050_set_key(map, button->num, 1);
321 		if (err)
322 			return err;
323 
324 		key_regs = qt1050_get_key_regs(button->num);
325 
326 		err = regmap_write(map, key_regs->pulse_scale,
327 				   (button->samples << 4) | (button->scale));
328 		if (err)
329 			return err;
330 		err = regmap_write(map, key_regs->csd, button->charge_delay);
331 		if (err)
332 			return err;
333 		err = regmap_write(map, key_regs->nthr, button->thr_cnt);
334 		if (err)
335 			return err;
336 	}
337 
338 	return 0;
339 }
340 
341 static int qt1050_parse_fw(struct qt1050_priv *ts)
342 {
343 	struct device *dev = &ts->client->dev;
344 	struct fwnode_handle *child;
345 	int nbuttons;
346 
347 	nbuttons = device_get_child_node_count(dev);
348 	if (nbuttons == 0 || nbuttons > QT1050_MAX_KEYS)
349 		return -ENODEV;
350 
351 	device_for_each_child_node(dev, child) {
352 		struct qt1050_key button;
353 
354 		/* Required properties */
355 		if (fwnode_property_read_u32(child, "linux,code",
356 					     &button.keycode)) {
357 			dev_err(dev, "Button without keycode\n");
358 			goto err;
359 		}
360 		if (button.keycode >= KEY_MAX) {
361 			dev_err(dev, "Invalid keycode 0x%x\n",
362 				button.keycode);
363 			goto err;
364 		}
365 
366 		if (fwnode_property_read_u32(child, "reg",
367 					     &button.num)) {
368 			dev_err(dev, "Button without pad number\n");
369 			goto err;
370 		}
371 		if (button.num < 0 || button.num > QT1050_MAX_KEYS - 1)
372 			goto err;
373 
374 		ts->reg_keys |= BIT(button.num);
375 
376 		/* Optional properties */
377 		if (fwnode_property_read_u32(child,
378 					     "microchip,pre-charge-time-ns",
379 					     &button.charge_delay)) {
380 			button.charge_delay = 0;
381 		} else {
382 			if (button.charge_delay % 2500 == 0)
383 				button.charge_delay =
384 					button.charge_delay / 2500;
385 			else
386 				button.charge_delay = 0;
387 		}
388 
389 		if (fwnode_property_read_u32(child, "microchip,average-samples",
390 					 &button.samples)) {
391 			button.samples = 0;
392 		} else {
393 			if (is_power_of_2(button.samples))
394 				button.samples = ilog2(button.samples);
395 			else
396 				button.samples = 0;
397 		}
398 
399 		if (fwnode_property_read_u32(child, "microchip,average-scaling",
400 					     &button.scale)) {
401 			button.scale = 0;
402 		} else {
403 			if (is_power_of_2(button.scale))
404 				button.scale = ilog2(button.scale);
405 			else
406 				button.scale = 0;
407 
408 		}
409 
410 		if (fwnode_property_read_u32(child, "microchip,threshold",
411 					 &button.thr_cnt)) {
412 			button.thr_cnt = 20;
413 		} else {
414 			if (button.thr_cnt > 255)
415 				button.thr_cnt = 20;
416 		}
417 
418 		ts->keys[button.num] = button;
419 	}
420 
421 	return 0;
422 
423 err:
424 	fwnode_handle_put(child);
425 	return -EINVAL;
426 }
427 
428 static int qt1050_probe(struct i2c_client *client)
429 {
430 	struct qt1050_priv *ts;
431 	struct input_dev *input;
432 	struct device *dev = &client->dev;
433 	struct regmap *map;
434 	unsigned int status, i;
435 	int err;
436 
437 	/* Check basic functionality */
438 	err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
439 	if (!err) {
440 		dev_err(&client->dev, "%s adapter not supported\n",
441 			dev_driver_string(&client->adapter->dev));
442 		return -ENODEV;
443 	}
444 
445 	if (!client->irq) {
446 		dev_err(dev, "assign a irq line to this device\n");
447 		return -EINVAL;
448 	}
449 
450 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
451 	if (!ts)
452 		return -ENOMEM;
453 
454 	input = devm_input_allocate_device(dev);
455 	if (!input)
456 		return -ENOMEM;
457 
458 	map = devm_regmap_init_i2c(client, &qt1050_regmap_config);
459 	if (IS_ERR(map))
460 		return PTR_ERR(map);
461 
462 	ts->client = client;
463 	ts->input = input;
464 	ts->regmap = map;
465 
466 	i2c_set_clientdata(client, ts);
467 
468 	/* Identify the qt1050 chip */
469 	if (!qt1050_identify(ts))
470 		return -ENODEV;
471 
472 	/* Get pdata */
473 	err = qt1050_parse_fw(ts);
474 	if (err) {
475 		dev_err(dev, "Failed to parse firmware: %d\n", err);
476 		return err;
477 	}
478 
479 	input->name = "AT42QT1050 QTouch Sensor";
480 	input->dev.parent = &client->dev;
481 	input->id.bustype = BUS_I2C;
482 
483 	/* Add the keycode */
484 	input->keycode = ts->keycodes;
485 	input->keycodesize = sizeof(ts->keycodes[0]);
486 	input->keycodemax = QT1050_MAX_KEYS;
487 
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);
492 	}
493 
494 	/* Trigger re-calibration */
495 	err = regmap_update_bits(ts->regmap, QT1050_RES_CAL, 0x7f,
496 				 QT1050_RES_CAL_CALIBRATE);
497 	if (err) {
498 		dev_err(dev, "Trigger calibration failed: %d\n", err);
499 		return err;
500 	}
501 	err = regmap_read_poll_timeout(ts->regmap, QT1050_DET_STATUS, status,
502 				 status >> 7 == 1, 10000, 200000);
503 	if (err) {
504 		dev_err(dev, "Calibration failed: %d\n", err);
505 		return err;
506 	}
507 
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);
511 	if (err) {
512 		dev_err(dev, "Trigger soft reset failed: %d\n", err);
513 		return err;
514 	}
515 	msleep(QT1050_RESET_TIME);
516 
517 	/* Set pdata */
518 	err = qt1050_apply_fw_data(ts);
519 	if (err) {
520 		dev_err(dev, "Failed to set firmware data: %d\n", err);
521 		return err;
522 	}
523 
524 	err = devm_request_threaded_irq(dev, client->irq, NULL,
525 					qt1050_irq_threaded, IRQF_ONESHOT,
526 					"qt1050", ts);
527 	if (err) {
528 		dev_err(&client->dev, "Failed to request irq: %d\n", err);
529 		return err;
530 	}
531 
532 	/* Clear #CHANGE line */
533 	err = regmap_read(ts->regmap, QT1050_DET_STATUS, &status);
534 	if (err) {
535 		dev_err(dev, "Failed to clear #CHANGE line level: %d\n", err);
536 		return err;
537 	}
538 
539 	/* Register the input device */
540 	err = input_register_device(ts->input);
541 	if (err) {
542 		dev_err(&client->dev, "Failed to register input device: %d\n",
543 			err);
544 		return err;
545 	}
546 
547 	return 0;
548 }
549 
550 static int qt1050_suspend(struct device *dev)
551 {
552 	struct i2c_client *client = to_i2c_client(dev);
553 	struct qt1050_priv *ts = i2c_get_clientdata(client);
554 
555 	disable_irq(client->irq);
556 
557 	/*
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.
561 	 */
562 	return regmap_write(ts->regmap, QT1050_LPMODE,
563 			    device_may_wakeup(dev) ? 125 : 0);
564 }
565 
566 static int qt1050_resume(struct device *dev)
567 {
568 	struct i2c_client *client = to_i2c_client(dev);
569 	struct qt1050_priv *ts = i2c_get_clientdata(client);
570 
571 	enable_irq(client->irq);
572 
573 	/* Set measurement interval back to 16ms (2 x 8ms) */
574 	return regmap_write(ts->regmap, QT1050_LPMODE, 2);
575 }
576 
577 static DEFINE_SIMPLE_DEV_PM_OPS(qt1050_pm_ops, qt1050_suspend, qt1050_resume);
578 
579 static const struct of_device_id __maybe_unused qt1050_of_match[] = {
580 	{ .compatible = "microchip,qt1050", },
581 	{ },
582 };
583 MODULE_DEVICE_TABLE(of, qt1050_of_match);
584 
585 static struct i2c_driver qt1050_driver = {
586 	.driver	= {
587 		.name = "qt1050",
588 		.of_match_table = of_match_ptr(qt1050_of_match),
589 		.pm = pm_sleep_ptr(&qt1050_pm_ops),
590 	},
591 	.probe_new = qt1050_probe,
592 };
593 
594 module_i2c_driver(qt1050_driver);
595 
596 MODULE_AUTHOR("Marco Felsch <kernel@pengutronix.de");
597 MODULE_DESCRIPTION("Driver for AT42QT1050 QTouch sensor");
598 MODULE_LICENSE("GPL v2");
599