xref: /linux/drivers/input/keyboard/qt1050.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 
qt1050_volatile_reg(struct device * dev,unsigned int reg)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 
qt1050_identify(struct qt1050_priv * ts)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 
qt1050_irq_threaded(int irq,void * dev_id)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 
qt1050_get_key_regs(int key_num)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 
qt1050_set_key(struct regmap * map,int number,int on)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 
qt1050_apply_fw_data(struct qt1050_priv * ts)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 
qt1050_parse_fw(struct qt1050_priv * ts)346 static int qt1050_parse_fw(struct qt1050_priv *ts)
347 {
348 	struct device *dev = &ts->client->dev;
349 	int nbuttons;
350 
351 	nbuttons = device_get_child_node_count(dev);
352 	if (nbuttons == 0 || nbuttons > QT1050_MAX_KEYS)
353 		return -ENODEV;
354 
355 	device_for_each_child_node_scoped(dev, child) {
356 		struct qt1050_key button;
357 
358 		/* Required properties */
359 		if (fwnode_property_read_u32(child, "linux,code",
360 					     &button.keycode)) {
361 			dev_err(dev, "Button without keycode\n");
362 			return -EINVAL;
363 		}
364 		if (button.keycode >= KEY_MAX) {
365 			dev_err(dev, "Invalid keycode 0x%x\n",
366 				button.keycode);
367 			return -EINVAL;
368 		}
369 
370 		if (fwnode_property_read_u32(child, "reg",
371 					     &button.num)) {
372 			dev_err(dev, "Button without pad number\n");
373 			return -EINVAL;
374 		}
375 		if (button.num < 0 || button.num > QT1050_MAX_KEYS - 1)
376 			return -EINVAL;
377 
378 		ts->reg_keys |= BIT(button.num);
379 
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;
385 		} else {
386 			if (button.charge_delay % 2500 == 0)
387 				button.charge_delay =
388 					button.charge_delay / 2500;
389 			else
390 				button.charge_delay = 0;
391 		}
392 
393 		if (fwnode_property_read_u32(child, "microchip,average-samples",
394 					 &button.samples)) {
395 			button.samples = 0;
396 		} else {
397 			if (is_power_of_2(button.samples))
398 				button.samples = ilog2(button.samples);
399 			else
400 				button.samples = 0;
401 		}
402 
403 		if (fwnode_property_read_u32(child, "microchip,average-scaling",
404 					     &button.scale)) {
405 			button.scale = 0;
406 		} else {
407 			if (is_power_of_2(button.scale))
408 				button.scale = ilog2(button.scale);
409 			else
410 				button.scale = 0;
411 
412 		}
413 
414 		if (fwnode_property_read_u32(child, "microchip,threshold",
415 					 &button.thr_cnt)) {
416 			button.thr_cnt = 20;
417 		} else {
418 			if (button.thr_cnt > 255)
419 				button.thr_cnt = 20;
420 		}
421 
422 		ts->keys[button.num] = button;
423 	}
424 
425 	return 0;
426 }
427 
qt1050_probe(struct i2c_client * client)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 
qt1050_suspend(struct device * dev)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 
qt1050_resume(struct device * dev)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 = 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