xref: /linux/drivers/input/touchscreen/hycon-hy46xx.c (revision 53597deca0e38c30e6cd4ba2114fa42d2bcd85bb)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021
4  * Author(s): Giulio Benetti <giulio.benetti@benettiengineering.com>
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/interrupt.h>
11 #include <linux/input.h>
12 #include <linux/input/mt.h>
13 #include <linux/input/touchscreen.h>
14 #include <linux/irq.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/regmap.h>
17 
18 #include <linux/unaligned.h>
19 
20 #define HY46XX_CHKSUM_CODE		0x1
21 #define HY46XX_FINGER_NUM		0x2
22 #define HY46XX_CHKSUM_LEN		0x7
23 #define HY46XX_THRESHOLD		0x80
24 #define HY46XX_GLOVE_EN			0x84
25 #define HY46XX_REPORT_SPEED		0x88
26 #define HY46XX_PWR_NOISE_EN		0x89
27 #define HY46XX_FILTER_DATA		0x8A
28 #define HY46XX_GAIN			0x92
29 #define HY46XX_EDGE_OFFSET		0x93
30 #define HY46XX_RX_NR_USED		0x94
31 #define HY46XX_TX_NR_USED		0x95
32 #define HY46XX_PWR_MODE			0xA5
33 #define HY46XX_FW_VERSION		0xA6
34 #define HY46XX_LIB_VERSION		0xA7
35 #define HY46XX_TP_INFO			0xA8
36 #define HY46XX_TP_CHIP_ID		0xA9
37 #define HY46XX_BOOT_VER			0xB0
38 
39 #define HY46XX_TPLEN			0x6
40 #define HY46XX_REPORT_PKT_LEN		0x44
41 
42 #define HY46XX_MAX_SUPPORTED_POINTS	11
43 
44 #define TOUCH_EVENT_DOWN		0x00
45 #define TOUCH_EVENT_UP			0x01
46 #define TOUCH_EVENT_CONTACT		0x02
47 #define TOUCH_EVENT_RESERVED		0x03
48 
49 struct hycon_hy46xx_data {
50 	struct i2c_client *client;
51 	struct input_dev *input;
52 	struct touchscreen_properties prop;
53 	struct regulator *vcc;
54 
55 	struct gpio_desc *reset_gpio;
56 
57 	struct mutex mutex;
58 	struct regmap *regmap;
59 
60 	int threshold;
61 	bool glove_enable;
62 	int report_speed;
63 	bool noise_filter_enable;
64 	int filter_data;
65 	int gain;
66 	int edge_offset;
67 	int rx_number_used;
68 	int tx_number_used;
69 	int power_mode;
70 	int fw_version;
71 	int lib_version;
72 	int tp_information;
73 	int tp_chip_id;
74 	int bootloader_version;
75 };
76 
77 static const struct regmap_config hycon_hy46xx_i2c_regmap_config = {
78 	.reg_bits = 8,
79 	.val_bits = 8,
80 };
81 
82 static bool hycon_hy46xx_check_checksum(struct hycon_hy46xx_data *tsdata, u8 *buf)
83 {
84 	u8 chksum = 0;
85 	int i;
86 
87 	for (i = 2; i < buf[HY46XX_CHKSUM_LEN]; i++)
88 		chksum += buf[i];
89 
90 	if (chksum == buf[HY46XX_CHKSUM_CODE])
91 		return true;
92 
93 	dev_err_ratelimited(&tsdata->client->dev,
94 			    "checksum error: 0x%02x expected, got 0x%02x\n",
95 			    chksum, buf[HY46XX_CHKSUM_CODE]);
96 
97 	return false;
98 }
99 
100 static irqreturn_t hycon_hy46xx_isr(int irq, void *dev_id)
101 {
102 	struct hycon_hy46xx_data *tsdata = dev_id;
103 	struct device *dev = &tsdata->client->dev;
104 	u8 rdbuf[HY46XX_REPORT_PKT_LEN];
105 	int i, x, y, id;
106 	int error;
107 
108 	memset(rdbuf, 0, sizeof(rdbuf));
109 
110 	error = regmap_bulk_read(tsdata->regmap, 0, rdbuf, sizeof(rdbuf));
111 	if (error) {
112 		dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
113 				    error);
114 		goto out;
115 	}
116 
117 	if (!hycon_hy46xx_check_checksum(tsdata, rdbuf))
118 		goto out;
119 
120 	for (i = 0; i < HY46XX_MAX_SUPPORTED_POINTS; i++) {
121 		u8 *buf = &rdbuf[3 + (HY46XX_TPLEN * i)];
122 		int type = buf[0] >> 6;
123 
124 		if (type == TOUCH_EVENT_RESERVED)
125 			continue;
126 
127 		x = get_unaligned_be16(buf) & 0x0fff;
128 		y = get_unaligned_be16(buf + 2) & 0x0fff;
129 
130 		id = buf[2] >> 4;
131 
132 		input_mt_slot(tsdata->input, id);
133 		if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
134 					       type != TOUCH_EVENT_UP))
135 			touchscreen_report_pos(tsdata->input, &tsdata->prop,
136 					       x, y, true);
137 	}
138 
139 	input_mt_report_pointer_emulation(tsdata->input, false);
140 	input_sync(tsdata->input);
141 
142 out:
143 	return IRQ_HANDLED;
144 }
145 
146 struct hycon_hy46xx_attribute {
147 	struct device_attribute dattr;
148 	size_t field_offset;
149 	u8 address;
150 	u8 limit_low;
151 	u8 limit_high;
152 };
153 
154 #define HYCON_ATTR_U8(_field, _mode, _address, _limit_low, _limit_high)	\
155 	struct hycon_hy46xx_attribute hycon_hy46xx_attr_##_field = {		\
156 		.dattr = __ATTR(_field, _mode,				\
157 				hycon_hy46xx_setting_show,			\
158 				hycon_hy46xx_setting_store),			\
159 		.field_offset = offsetof(struct hycon_hy46xx_data, _field),	\
160 		.address = _address,					\
161 		.limit_low = _limit_low,				\
162 		.limit_high = _limit_high,				\
163 	}
164 
165 #define HYCON_ATTR_BOOL(_field, _mode, _address)			\
166 	struct hycon_hy46xx_attribute hycon_hy46xx_attr_##_field = {		\
167 		.dattr = __ATTR(_field, _mode,				\
168 				hycon_hy46xx_setting_show,			\
169 				hycon_hy46xx_setting_store),			\
170 		.field_offset = offsetof(struct hycon_hy46xx_data, _field),	\
171 		.address = _address,					\
172 		.limit_low = false,					\
173 		.limit_high = true,					\
174 	}
175 
176 static ssize_t hycon_hy46xx_setting_show(struct device *dev,
177 				   struct device_attribute *dattr, char *buf)
178 {
179 	struct i2c_client *client = to_i2c_client(dev);
180 	struct hycon_hy46xx_data *tsdata = i2c_get_clientdata(client);
181 	struct hycon_hy46xx_attribute *attr =
182 			container_of(dattr, struct hycon_hy46xx_attribute, dattr);
183 	u8 *field = (u8 *)tsdata + attr->field_offset;
184 	int error = 0;
185 	int val;
186 
187 	guard(mutex)(&tsdata->mutex);
188 
189 	error = regmap_read(tsdata->regmap, attr->address, &val);
190 	if (error) {
191 		dev_err(&tsdata->client->dev,
192 			"Failed to fetch attribute %s, error %d\n",
193 			dattr->attr.name, error);
194 		return error;
195 	}
196 
197 	if (val != *field) {
198 		dev_warn(&tsdata->client->dev,
199 			 "%s: read (%d) and stored value (%d) differ\n",
200 			 dattr->attr.name, val, *field);
201 		*field = val;
202 	}
203 
204 	return sysfs_emit(buf, "%d\n", val);
205 }
206 
207 static ssize_t hycon_hy46xx_setting_store(struct device *dev,
208 					struct device_attribute *dattr,
209 					const char *buf, size_t count)
210 {
211 	struct i2c_client *client = to_i2c_client(dev);
212 	struct hycon_hy46xx_data *tsdata = i2c_get_clientdata(client);
213 	struct hycon_hy46xx_attribute *attr =
214 			container_of(dattr, struct hycon_hy46xx_attribute, dattr);
215 	u8 *field = (u8 *)tsdata + attr->field_offset;
216 	unsigned int val;
217 	int error;
218 
219 	guard(mutex)(&tsdata->mutex);
220 
221 	error = kstrtouint(buf, 0, &val);
222 	if (error)
223 		return error;
224 
225 	if (val < attr->limit_low || val > attr->limit_high)
226 		return -ERANGE;
227 
228 	error = regmap_write(tsdata->regmap, attr->address, val);
229 	if (error) {
230 		dev_err(&tsdata->client->dev,
231 			"Failed to update attribute %s, error: %d\n",
232 			dattr->attr.name, error);
233 		return error;
234 	}
235 	*field = val;
236 
237 	return count;
238 }
239 
240 static HYCON_ATTR_U8(threshold, 0644, HY46XX_THRESHOLD, 0, 255);
241 static HYCON_ATTR_BOOL(glove_enable, 0644, HY46XX_GLOVE_EN);
242 static HYCON_ATTR_U8(report_speed, 0644, HY46XX_REPORT_SPEED, 0, 255);
243 static HYCON_ATTR_BOOL(noise_filter_enable, 0644, HY46XX_PWR_NOISE_EN);
244 static HYCON_ATTR_U8(filter_data, 0644, HY46XX_FILTER_DATA, 0, 5);
245 static HYCON_ATTR_U8(gain, 0644, HY46XX_GAIN, 0, 5);
246 static HYCON_ATTR_U8(edge_offset, 0644, HY46XX_EDGE_OFFSET, 0, 5);
247 static HYCON_ATTR_U8(fw_version, 0444, HY46XX_FW_VERSION, 0, 255);
248 static HYCON_ATTR_U8(lib_version, 0444, HY46XX_LIB_VERSION, 0, 255);
249 static HYCON_ATTR_U8(tp_information, 0444, HY46XX_TP_INFO, 0, 255);
250 static HYCON_ATTR_U8(tp_chip_id, 0444, HY46XX_TP_CHIP_ID, 0, 255);
251 static HYCON_ATTR_U8(bootloader_version, 0444, HY46XX_BOOT_VER, 0, 255);
252 
253 static struct attribute *hycon_hy46xx_attrs[] = {
254 	&hycon_hy46xx_attr_threshold.dattr.attr,
255 	&hycon_hy46xx_attr_glove_enable.dattr.attr,
256 	&hycon_hy46xx_attr_report_speed.dattr.attr,
257 	&hycon_hy46xx_attr_noise_filter_enable.dattr.attr,
258 	&hycon_hy46xx_attr_filter_data.dattr.attr,
259 	&hycon_hy46xx_attr_gain.dattr.attr,
260 	&hycon_hy46xx_attr_edge_offset.dattr.attr,
261 	&hycon_hy46xx_attr_fw_version.dattr.attr,
262 	&hycon_hy46xx_attr_lib_version.dattr.attr,
263 	&hycon_hy46xx_attr_tp_information.dattr.attr,
264 	&hycon_hy46xx_attr_tp_chip_id.dattr.attr,
265 	&hycon_hy46xx_attr_bootloader_version.dattr.attr,
266 	NULL
267 };
268 ATTRIBUTE_GROUPS(hycon_hy46xx);
269 
270 static void hycon_hy46xx_get_defaults(struct device *dev, struct hycon_hy46xx_data *tsdata)
271 {
272 	bool val_bool;
273 	int error;
274 	u32 val;
275 
276 	error = device_property_read_u32(dev, "hycon,threshold", &val);
277 	if (!error) {
278 		error = regmap_write(tsdata->regmap, HY46XX_THRESHOLD, val);
279 		if (error < 0)
280 			goto out;
281 
282 		tsdata->threshold = val;
283 	}
284 
285 	val_bool = device_property_read_bool(dev, "hycon,glove-enable");
286 	error = regmap_write(tsdata->regmap, HY46XX_GLOVE_EN, val_bool);
287 	if (error < 0)
288 		goto out;
289 	tsdata->glove_enable = val_bool;
290 
291 	error = device_property_read_u32(dev, "hycon,report-speed-hz", &val);
292 	if (!error) {
293 		error = regmap_write(tsdata->regmap, HY46XX_REPORT_SPEED, val);
294 		if (error < 0)
295 			goto out;
296 
297 		tsdata->report_speed = val;
298 	}
299 
300 	val_bool = device_property_read_bool(dev, "hycon,noise-filter-enable");
301 	error = regmap_write(tsdata->regmap, HY46XX_PWR_NOISE_EN, val_bool);
302 	if (error < 0)
303 		goto out;
304 	tsdata->noise_filter_enable = val_bool;
305 
306 	error = device_property_read_u32(dev, "hycon,filter-data", &val);
307 	if (!error) {
308 		error = regmap_write(tsdata->regmap, HY46XX_FILTER_DATA, val);
309 		if (error < 0)
310 			goto out;
311 
312 		tsdata->filter_data = val;
313 	}
314 
315 	error = device_property_read_u32(dev, "hycon,gain", &val);
316 	if (!error) {
317 		error = regmap_write(tsdata->regmap, HY46XX_GAIN, val);
318 		if (error < 0)
319 			goto out;
320 
321 		tsdata->gain = val;
322 	}
323 
324 	error = device_property_read_u32(dev, "hycon,edge-offset", &val);
325 	if (!error) {
326 		error = regmap_write(tsdata->regmap, HY46XX_EDGE_OFFSET, val);
327 		if (error < 0)
328 			goto out;
329 
330 		tsdata->edge_offset = val;
331 	}
332 
333 	return;
334 out:
335 	dev_err(&tsdata->client->dev, "Failed to set default settings");
336 }
337 
338 static void hycon_hy46xx_get_parameters(struct hycon_hy46xx_data *tsdata)
339 {
340 	int error;
341 	u32 val;
342 
343 	error = regmap_read(tsdata->regmap, HY46XX_THRESHOLD, &val);
344 	if (error < 0)
345 		goto out;
346 	tsdata->threshold = val;
347 
348 	error = regmap_read(tsdata->regmap, HY46XX_GLOVE_EN, &val);
349 	if (error < 0)
350 		goto out;
351 	tsdata->glove_enable = val;
352 
353 	error = regmap_read(tsdata->regmap, HY46XX_REPORT_SPEED, &val);
354 	if (error < 0)
355 		goto out;
356 	tsdata->report_speed = val;
357 
358 	error = regmap_read(tsdata->regmap, HY46XX_PWR_NOISE_EN, &val);
359 	if (error < 0)
360 		goto out;
361 	tsdata->noise_filter_enable = val;
362 
363 	error = regmap_read(tsdata->regmap, HY46XX_FILTER_DATA, &val);
364 	if (error < 0)
365 		goto out;
366 	tsdata->filter_data = val;
367 
368 	error = regmap_read(tsdata->regmap, HY46XX_GAIN, &val);
369 	if (error < 0)
370 		goto out;
371 	tsdata->gain = val;
372 
373 	error = regmap_read(tsdata->regmap, HY46XX_EDGE_OFFSET, &val);
374 	if (error < 0)
375 		goto out;
376 	tsdata->edge_offset = val;
377 
378 	error = regmap_read(tsdata->regmap, HY46XX_RX_NR_USED, &val);
379 	if (error < 0)
380 		goto out;
381 	tsdata->rx_number_used = val;
382 
383 	error = regmap_read(tsdata->regmap, HY46XX_TX_NR_USED, &val);
384 	if (error < 0)
385 		goto out;
386 	tsdata->tx_number_used = val;
387 
388 	error = regmap_read(tsdata->regmap, HY46XX_PWR_MODE, &val);
389 	if (error < 0)
390 		goto out;
391 	tsdata->power_mode = val;
392 
393 	error = regmap_read(tsdata->regmap, HY46XX_FW_VERSION, &val);
394 	if (error < 0)
395 		goto out;
396 	tsdata->fw_version = val;
397 
398 	error = regmap_read(tsdata->regmap, HY46XX_LIB_VERSION, &val);
399 	if (error < 0)
400 		goto out;
401 	tsdata->lib_version = val;
402 
403 	error = regmap_read(tsdata->regmap, HY46XX_TP_INFO, &val);
404 	if (error < 0)
405 		goto out;
406 	tsdata->tp_information = val;
407 
408 	error = regmap_read(tsdata->regmap, HY46XX_TP_CHIP_ID, &val);
409 	if (error < 0)
410 		goto out;
411 	tsdata->tp_chip_id = val;
412 
413 	error = regmap_read(tsdata->regmap, HY46XX_BOOT_VER, &val);
414 	if (error < 0)
415 		goto out;
416 	tsdata->bootloader_version = val;
417 
418 	return;
419 out:
420 	dev_err(&tsdata->client->dev, "Failed to read default settings");
421 }
422 
423 static void hycon_hy46xx_disable_regulator(void *arg)
424 {
425 	struct hycon_hy46xx_data *data = arg;
426 
427 	regulator_disable(data->vcc);
428 }
429 
430 static int hycon_hy46xx_probe(struct i2c_client *client)
431 {
432 	struct hycon_hy46xx_data *tsdata;
433 	struct input_dev *input;
434 	int error;
435 
436 	dev_dbg(&client->dev, "probing for HYCON HY46XX I2C\n");
437 
438 	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
439 	if (!tsdata)
440 		return -ENOMEM;
441 
442 	tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
443 	if (IS_ERR(tsdata->vcc)) {
444 		error = PTR_ERR(tsdata->vcc);
445 		if (error != -EPROBE_DEFER)
446 			dev_err(&client->dev,
447 				"failed to request regulator: %d\n", error);
448 		return error;
449 	}
450 
451 	error = regulator_enable(tsdata->vcc);
452 	if (error < 0) {
453 		dev_err(&client->dev, "failed to enable vcc: %d\n", error);
454 		return error;
455 	}
456 
457 	error = devm_add_action_or_reset(&client->dev,
458 					 hycon_hy46xx_disable_regulator,
459 					 tsdata);
460 	if (error)
461 		return error;
462 
463 	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
464 						     "reset", GPIOD_OUT_LOW);
465 	if (IS_ERR(tsdata->reset_gpio)) {
466 		error = PTR_ERR(tsdata->reset_gpio);
467 		dev_err(&client->dev,
468 			"Failed to request GPIO reset pin, error %d\n", error);
469 		return error;
470 	}
471 
472 	if (tsdata->reset_gpio) {
473 		usleep_range(5000, 6000);
474 		gpiod_set_value_cansleep(tsdata->reset_gpio, 1);
475 		usleep_range(5000, 6000);
476 		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
477 		msleep(1000);
478 	}
479 
480 	input = devm_input_allocate_device(&client->dev);
481 	if (!input) {
482 		dev_err(&client->dev, "failed to allocate input device.\n");
483 		return -ENOMEM;
484 	}
485 
486 	mutex_init(&tsdata->mutex);
487 	tsdata->client = client;
488 	tsdata->input = input;
489 
490 	tsdata->regmap = devm_regmap_init_i2c(client,
491 					      &hycon_hy46xx_i2c_regmap_config);
492 	if (IS_ERR(tsdata->regmap)) {
493 		dev_err(&client->dev, "regmap allocation failed\n");
494 		return PTR_ERR(tsdata->regmap);
495 	}
496 
497 	hycon_hy46xx_get_defaults(&client->dev, tsdata);
498 	hycon_hy46xx_get_parameters(tsdata);
499 
500 	input->name = "Hycon Capacitive Touch";
501 	input->id.bustype = BUS_I2C;
502 	input->dev.parent = &client->dev;
503 
504 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, -1, 0, 0);
505 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, -1, 0, 0);
506 
507 	touchscreen_parse_properties(input, true, &tsdata->prop);
508 
509 	error = input_mt_init_slots(input, HY46XX_MAX_SUPPORTED_POINTS,
510 				    INPUT_MT_DIRECT);
511 	if (error) {
512 		dev_err(&client->dev, "Unable to init MT slots.\n");
513 		return error;
514 	}
515 
516 	i2c_set_clientdata(client, tsdata);
517 
518 	error = devm_request_threaded_irq(&client->dev, client->irq,
519 					  NULL, hycon_hy46xx_isr, IRQF_ONESHOT,
520 					  client->name, tsdata);
521 	if (error) {
522 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
523 		return error;
524 	}
525 
526 	error = input_register_device(input);
527 	if (error)
528 		return error;
529 
530 	dev_dbg(&client->dev,
531 		"HYCON HY46XX initialized: IRQ %d, Reset pin %d.\n",
532 		client->irq,
533 		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
534 
535 	return 0;
536 }
537 
538 static const struct i2c_device_id hycon_hy46xx_id[] = {
539 	{ .name = "hy4613" },
540 	{ .name = "hy4614" },
541 	{ .name = "hy4621" },
542 	{ .name = "hy4623" },
543 	{ .name = "hy4633" },
544 	{ .name = "hy4635" },
545 	{ /* sentinel */ }
546 };
547 MODULE_DEVICE_TABLE(i2c, hycon_hy46xx_id);
548 
549 static const struct of_device_id hycon_hy46xx_of_match[] = {
550 	{ .compatible = "hycon,hy4613" },
551 	{ .compatible = "hycon,hy4614" },
552 	{ .compatible = "hycon,hy4621" },
553 	{ .compatible = "hycon,hy4623" },
554 	{ .compatible = "hycon,hy4633" },
555 	{ .compatible = "hycon,hy4635" },
556 	{ /* sentinel */ }
557 };
558 MODULE_DEVICE_TABLE(of, hycon_hy46xx_of_match);
559 
560 static struct i2c_driver hycon_hy46xx_driver = {
561 	.driver = {
562 		.name = "hycon_hy46xx",
563 		.dev_groups = hycon_hy46xx_groups,
564 		.of_match_table = hycon_hy46xx_of_match,
565 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
566 	},
567 	.id_table = hycon_hy46xx_id,
568 	.probe = hycon_hy46xx_probe,
569 };
570 
571 module_i2c_driver(hycon_hy46xx_driver);
572 
573 MODULE_AUTHOR("Giulio Benetti <giulio.benetti@benettiengineering.com>");
574 MODULE_DESCRIPTION("HYCON HY46XX I2C Touchscreen Driver");
575 MODULE_LICENSE("GPL v2");
576