1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/delay.h>
4 #include <linux/i2c.h>
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/input/touchscreen.h>
8 #include <linux/interrupt.h>
9 #include <linux/irq.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/slab.h>
15 
16 /* Register Map */
17 
18 #define ZINITIX_SWRESET_CMD			0x0000
19 #define ZINITIX_WAKEUP_CMD			0x0001
20 
21 #define ZINITIX_IDLE_CMD			0x0004
22 #define ZINITIX_SLEEP_CMD			0x0005
23 
24 #define ZINITIX_CLEAR_INT_STATUS_CMD		0x0003
25 #define ZINITIX_CALIBRATE_CMD			0x0006
26 #define ZINITIX_SAVE_STATUS_CMD			0x0007
27 #define ZINITIX_SAVE_CALIBRATION_CMD		0x0008
28 #define ZINITIX_RECALL_FACTORY_CMD		0x000f
29 
30 #define ZINITIX_THRESHOLD			0x0020
31 
32 #define ZINITIX_LARGE_PALM_REJECT_AREA_TH	0x003F
33 
34 #define ZINITIX_DEBUG_REG			0x0115 /* 0~7 */
35 
36 #define ZINITIX_TOUCH_MODE			0x0010
37 #define ZINITIX_CHIP_REVISION			0x0011
38 #define ZINITIX_FIRMWARE_VERSION		0x0012
39 
40 #define ZINITIX_USB_DETECT			0x116
41 
42 #define ZINITIX_MINOR_FW_VERSION		0x0121
43 
44 #define ZINITIX_VENDOR_ID			0x001C
45 #define ZINITIX_HW_ID				0x0014
46 
47 #define ZINITIX_DATA_VERSION_REG		0x0013
48 #define ZINITIX_SUPPORTED_FINGER_NUM		0x0015
49 #define ZINITIX_EEPROM_INFO			0x0018
50 #define ZINITIX_INITIAL_TOUCH_MODE		0x0019
51 
52 #define ZINITIX_TOTAL_NUMBER_OF_X		0x0060
53 #define ZINITIX_TOTAL_NUMBER_OF_Y		0x0061
54 
55 #define ZINITIX_DELAY_RAW_FOR_HOST		0x007f
56 
57 #define ZINITIX_BUTTON_SUPPORTED_NUM		0x00B0
58 #define ZINITIX_BUTTON_SENSITIVITY		0x00B2
59 #define ZINITIX_DUMMY_BUTTON_SENSITIVITY	0X00C8
60 
61 #define ZINITIX_X_RESOLUTION			0x00C0
62 #define ZINITIX_Y_RESOLUTION			0x00C1
63 
64 #define ZINITIX_POINT_STATUS_REG		0x0080
65 #define ZINITIX_ICON_STATUS_REG			0x00AA
66 
67 #define ZINITIX_POINT_COORD_REG			(ZINITIX_POINT_STATUS_REG + 2)
68 
69 #define ZINITIX_AFE_FREQUENCY			0x0100
70 #define ZINITIX_DND_N_COUNT			0x0122
71 #define ZINITIX_DND_U_COUNT			0x0135
72 
73 #define ZINITIX_RAWDATA_REG			0x0200
74 
75 #define ZINITIX_EEPROM_INFO_REG			0x0018
76 
77 #define ZINITIX_INT_ENABLE_FLAG			0x00f0
78 #define ZINITIX_PERIODICAL_INTERRUPT_INTERVAL	0x00f1
79 
80 #define ZINITIX_BTN_WIDTH			0x016d
81 
82 #define ZINITIX_CHECKSUM_RESULT			0x012c
83 
84 #define ZINITIX_INIT_FLASH			0x01d0
85 #define ZINITIX_WRITE_FLASH			0x01d1
86 #define ZINITIX_READ_FLASH			0x01d2
87 
88 #define ZINITIX_INTERNAL_FLAG_02		0x011e
89 #define ZINITIX_INTERNAL_FLAG_03		0x011f
90 
91 #define ZINITIX_I2C_CHECKSUM_WCNT		0x016a
92 #define ZINITIX_I2C_CHECKSUM_RESULT		0x016c
93 
94 /* Interrupt & status register flags */
95 
96 #define BIT_PT_CNT_CHANGE			BIT(0)
97 #define BIT_DOWN				BIT(1)
98 #define BIT_MOVE				BIT(2)
99 #define BIT_UP					BIT(3)
100 #define BIT_PALM				BIT(4)
101 #define BIT_PALM_REJECT				BIT(5)
102 #define BIT_RESERVED_0				BIT(6)
103 #define BIT_RESERVED_1				BIT(7)
104 #define BIT_WEIGHT_CHANGE			BIT(8)
105 #define BIT_PT_NO_CHANGE			BIT(9)
106 #define BIT_REJECT				BIT(10)
107 #define BIT_PT_EXIST				BIT(11)
108 #define BIT_RESERVED_2				BIT(12)
109 #define BIT_ERROR				BIT(13)
110 #define BIT_DEBUG				BIT(14)
111 #define BIT_ICON_EVENT				BIT(15)
112 
113 #define SUB_BIT_EXIST				BIT(0)
114 #define SUB_BIT_DOWN				BIT(1)
115 #define SUB_BIT_MOVE				BIT(2)
116 #define SUB_BIT_UP				BIT(3)
117 #define SUB_BIT_UPDATE				BIT(4)
118 #define SUB_BIT_WAIT				BIT(5)
119 
120 #define DEFAULT_TOUCH_POINT_MODE		2
121 #define MAX_SUPPORTED_FINGER_NUM		5
122 
123 #define CHIP_ON_DELAY				15 // ms
124 #define FIRMWARE_ON_DELAY			40 // ms
125 
126 struct point_coord {
127 	__le16	x;
128 	__le16	y;
129 	u8	width;
130 	u8	sub_status;
131 	// currently unused, but needed as padding:
132 	u8	minor_width;
133 	u8	angle;
134 };
135 
136 struct touch_event {
137 	__le16	status;
138 	u8	finger_mask;
139 	u8	time_stamp;
140 	struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM];
141 };
142 
143 struct bt541_ts_data {
144 	struct i2c_client *client;
145 	struct input_dev *input_dev;
146 	struct touchscreen_properties prop;
147 	struct regulator_bulk_data supplies[2];
148 	u32 zinitix_mode;
149 };
150 
151 static int zinitix_read_data(struct i2c_client *client,
152 			     u16 reg, void *values, size_t length)
153 {
154 	__le16 reg_le = cpu_to_le16(reg);
155 	int ret;
156 
157 	/* A single i2c_transfer() transaction does not work here. */
158 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
159 	if (ret != sizeof(reg_le))
160 		return ret < 0 ? ret : -EIO;
161 
162 	ret = i2c_master_recv(client, (u8 *)values, length);
163 	if (ret != length)
164 		return ret < 0 ? ret : -EIO;
165 
166 	return 0;
167 }
168 
169 static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value)
170 {
171 	__le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)};
172 	int ret;
173 
174 	ret = i2c_master_send(client, (u8 *)packet, sizeof(packet));
175 	if (ret != sizeof(packet))
176 		return ret < 0 ? ret : -EIO;
177 
178 	return 0;
179 }
180 
181 static int zinitix_write_cmd(struct i2c_client *client, u16 reg)
182 {
183 	__le16 reg_le = cpu_to_le16(reg);
184 	int ret;
185 
186 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
187 	if (ret != sizeof(reg_le))
188 		return ret < 0 ? ret : -EIO;
189 
190 	return 0;
191 }
192 
193 static int zinitix_init_touch(struct bt541_ts_data *bt541)
194 {
195 	struct i2c_client *client = bt541->client;
196 	int i;
197 	int error;
198 
199 	error = zinitix_write_cmd(client, ZINITIX_SWRESET_CMD);
200 	if (error) {
201 		dev_err(&client->dev, "Failed to write reset command\n");
202 		return error;
203 	}
204 
205 	error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG, 0x0);
206 	if (error) {
207 		dev_err(&client->dev,
208 			"Failed to reset interrupt enable flag\n");
209 		return error;
210 	}
211 
212 	/* initialize */
213 	error = zinitix_write_u16(client, ZINITIX_X_RESOLUTION,
214 				  bt541->prop.max_x);
215 	if (error)
216 		return error;
217 
218 	error = zinitix_write_u16(client, ZINITIX_Y_RESOLUTION,
219 				  bt541->prop.max_y);
220 	if (error)
221 		return error;
222 
223 	error = zinitix_write_u16(client, ZINITIX_SUPPORTED_FINGER_NUM,
224 				  MAX_SUPPORTED_FINGER_NUM);
225 	if (error)
226 		return error;
227 
228 	error = zinitix_write_u16(client, ZINITIX_INITIAL_TOUCH_MODE,
229 				  bt541->zinitix_mode);
230 	if (error)
231 		return error;
232 
233 	error = zinitix_write_u16(client, ZINITIX_TOUCH_MODE,
234 				  bt541->zinitix_mode);
235 	if (error)
236 		return error;
237 
238 	error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG,
239 				  BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE |
240 					BIT_UP);
241 	if (error)
242 		return error;
243 
244 	/* clear queue */
245 	for (i = 0; i < 10; i++) {
246 		zinitix_write_cmd(client, ZINITIX_CLEAR_INT_STATUS_CMD);
247 		udelay(10);
248 	}
249 
250 	return 0;
251 }
252 
253 static int zinitix_init_regulators(struct bt541_ts_data *bt541)
254 {
255 	struct device *dev = &bt541->client->dev;
256 	int error;
257 
258 	/*
259 	 * Some older device trees have erroneous names for the regulators,
260 	 * so check if "vddo" is present and in that case use these names.
261 	 * Else use the proper supply names on the component.
262 	 */
263 	if (of_property_present(dev->of_node, "vddo-supply")) {
264 		bt541->supplies[0].supply = "vdd";
265 		bt541->supplies[1].supply = "vddo";
266 	} else {
267 		/* Else use the proper supply names */
268 		bt541->supplies[0].supply = "vcca";
269 		bt541->supplies[1].supply = "vdd";
270 	}
271 	error = devm_regulator_bulk_get(dev,
272 					ARRAY_SIZE(bt541->supplies),
273 					bt541->supplies);
274 	if (error < 0) {
275 		dev_err(dev, "Failed to get regulators: %d\n", error);
276 		return error;
277 	}
278 
279 	return 0;
280 }
281 
282 static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
283 {
284 	int error;
285 	struct i2c_client *client = bt541->client;
286 
287 	error = zinitix_write_u16(client, 0xc000, 0x0001);
288 	if (error) {
289 		dev_err(&client->dev,
290 			"Failed to send power sequence(vendor cmd enable)\n");
291 		return error;
292 	}
293 	udelay(10);
294 
295 	error = zinitix_write_cmd(client, 0xc004);
296 	if (error) {
297 		dev_err(&client->dev,
298 			"Failed to send power sequence (intn clear)\n");
299 		return error;
300 	}
301 	udelay(10);
302 
303 	error = zinitix_write_u16(client, 0xc002, 0x0001);
304 	if (error) {
305 		dev_err(&client->dev,
306 			"Failed to send power sequence (nvm init)\n");
307 		return error;
308 	}
309 	mdelay(2);
310 
311 	error = zinitix_write_u16(client, 0xc001, 0x0001);
312 	if (error) {
313 		dev_err(&client->dev,
314 			"Failed to send power sequence (program start)\n");
315 		return error;
316 	}
317 	msleep(FIRMWARE_ON_DELAY);
318 
319 	return 0;
320 }
321 
322 static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
323 				  const struct point_coord *p)
324 {
325 	u16 x, y;
326 
327 	if (unlikely(!(p->sub_status &
328 		       (SUB_BIT_UP | SUB_BIT_DOWN | SUB_BIT_MOVE)))) {
329 		dev_dbg(&bt541->client->dev, "unknown finger event %#02x\n",
330 			p->sub_status);
331 		return;
332 	}
333 
334 	x = le16_to_cpu(p->x);
335 	y = le16_to_cpu(p->y);
336 
337 	input_mt_slot(bt541->input_dev, slot);
338 	if (input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER,
339 				       !(p->sub_status & SUB_BIT_UP))) {
340 		touchscreen_report_pos(bt541->input_dev,
341 				       &bt541->prop, x, y, true);
342 		input_report_abs(bt541->input_dev,
343 				 ABS_MT_TOUCH_MAJOR, p->width);
344 		dev_dbg(&bt541->client->dev, "finger %d %s (%u, %u)\n",
345 			slot, p->sub_status & SUB_BIT_DOWN ? "down" : "move",
346 			x, y);
347 	} else {
348 		dev_dbg(&bt541->client->dev, "finger %d up (%u, %u)\n",
349 			slot, x, y);
350 	}
351 }
352 
353 static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
354 {
355 	struct bt541_ts_data *bt541 = bt541_handler;
356 	struct i2c_client *client = bt541->client;
357 	struct touch_event touch_event;
358 	unsigned long finger_mask;
359 	int error;
360 	int i;
361 
362 	memset(&touch_event, 0, sizeof(struct touch_event));
363 
364 	error = zinitix_read_data(bt541->client, ZINITIX_POINT_STATUS_REG,
365 				  &touch_event, sizeof(struct touch_event));
366 	if (error) {
367 		dev_err(&client->dev, "Failed to read in touchpoint struct\n");
368 		goto out;
369 	}
370 
371 	finger_mask = touch_event.finger_mask;
372 	for_each_set_bit(i, &finger_mask, MAX_SUPPORTED_FINGER_NUM) {
373 		const struct point_coord *p = &touch_event.point_coord[i];
374 
375 		/* Only process contacts that are actually reported */
376 		if (p->sub_status & SUB_BIT_EXIST)
377 			zinitix_report_finger(bt541, i, p);
378 	}
379 
380 	input_mt_sync_frame(bt541->input_dev);
381 	input_sync(bt541->input_dev);
382 
383 out:
384 	zinitix_write_cmd(bt541->client, ZINITIX_CLEAR_INT_STATUS_CMD);
385 	return IRQ_HANDLED;
386 }
387 
388 static int zinitix_start(struct bt541_ts_data *bt541)
389 {
390 	int error;
391 
392 	error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
393 				      bt541->supplies);
394 	if (error) {
395 		dev_err(&bt541->client->dev,
396 			"Failed to enable regulators: %d\n", error);
397 		return error;
398 	}
399 
400 	msleep(CHIP_ON_DELAY);
401 
402 	error = zinitix_send_power_on_sequence(bt541);
403 	if (error) {
404 		dev_err(&bt541->client->dev,
405 			"Error while sending power-on sequence: %d\n", error);
406 		return error;
407 	}
408 
409 	error = zinitix_init_touch(bt541);
410 	if (error) {
411 		dev_err(&bt541->client->dev,
412 			"Error while configuring touch IC\n");
413 		return error;
414 	}
415 
416 	enable_irq(bt541->client->irq);
417 
418 	return 0;
419 }
420 
421 static int zinitix_stop(struct bt541_ts_data *bt541)
422 {
423 	int error;
424 
425 	disable_irq(bt541->client->irq);
426 
427 	error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
428 				       bt541->supplies);
429 	if (error) {
430 		dev_err(&bt541->client->dev,
431 			"Failed to disable regulators: %d\n", error);
432 		return error;
433 	}
434 
435 	return 0;
436 }
437 
438 static int zinitix_input_open(struct input_dev *dev)
439 {
440 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
441 
442 	return zinitix_start(bt541);
443 }
444 
445 static void zinitix_input_close(struct input_dev *dev)
446 {
447 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
448 
449 	zinitix_stop(bt541);
450 }
451 
452 static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
453 {
454 	struct input_dev *input_dev;
455 	int error;
456 
457 	input_dev = devm_input_allocate_device(&bt541->client->dev);
458 	if (!input_dev) {
459 		dev_err(&bt541->client->dev,
460 			"Failed to allocate input device.");
461 		return -ENOMEM;
462 	}
463 
464 	input_set_drvdata(input_dev, bt541);
465 	bt541->input_dev = input_dev;
466 
467 	input_dev->name = "Zinitix Capacitive TouchScreen";
468 	input_dev->phys = "input/ts";
469 	input_dev->id.bustype = BUS_I2C;
470 	input_dev->open = zinitix_input_open;
471 	input_dev->close = zinitix_input_close;
472 
473 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
474 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
475 	input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
476 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
477 
478 	touchscreen_parse_properties(input_dev, true, &bt541->prop);
479 	if (!bt541->prop.max_x || !bt541->prop.max_y) {
480 		dev_err(&bt541->client->dev,
481 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
482 		return -EINVAL;
483 	}
484 
485 	error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
486 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
487 	if (error) {
488 		dev_err(&bt541->client->dev,
489 			"Failed to initialize MT slots: %d", error);
490 		return error;
491 	}
492 
493 	error = input_register_device(input_dev);
494 	if (error) {
495 		dev_err(&bt541->client->dev,
496 			"Failed to register input device: %d", error);
497 		return error;
498 	}
499 
500 	return 0;
501 }
502 
503 static int zinitix_ts_probe(struct i2c_client *client)
504 {
505 	struct bt541_ts_data *bt541;
506 	int error;
507 
508 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
509 		dev_err(&client->dev,
510 			"Failed to assert adapter's support for plain I2C.\n");
511 		return -ENXIO;
512 	}
513 
514 	bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
515 	if (!bt541)
516 		return -ENOMEM;
517 
518 	bt541->client = client;
519 	i2c_set_clientdata(client, bt541);
520 
521 	error = zinitix_init_regulators(bt541);
522 	if (error) {
523 		dev_err(&client->dev,
524 			"Failed to initialize regulators: %d\n", error);
525 		return error;
526 	}
527 
528 	error = devm_request_threaded_irq(&client->dev, client->irq,
529 					  NULL, zinitix_ts_irq_handler,
530 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
531 					  client->name, bt541);
532 	if (error) {
533 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
534 		return error;
535 	}
536 
537 	error = zinitix_init_input_dev(bt541);
538 	if (error) {
539 		dev_err(&client->dev,
540 			"Failed to initialize input device: %d\n", error);
541 		return error;
542 	}
543 
544 	error = device_property_read_u32(&client->dev, "zinitix,mode",
545 					 &bt541->zinitix_mode);
546 	if (error < 0) {
547 		/* fall back to mode 2 */
548 		bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
549 	}
550 
551 	if (bt541->zinitix_mode != 2) {
552 		/*
553 		 * If there are devices that don't support mode 2, support
554 		 * for other modes (0, 1) will be needed.
555 		 */
556 		dev_err(&client->dev,
557 			"Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
558 			bt541->zinitix_mode);
559 		return -EINVAL;
560 	}
561 
562 	return 0;
563 }
564 
565 static int zinitix_suspend(struct device *dev)
566 {
567 	struct i2c_client *client = to_i2c_client(dev);
568 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
569 
570 	mutex_lock(&bt541->input_dev->mutex);
571 
572 	if (input_device_enabled(bt541->input_dev))
573 		zinitix_stop(bt541);
574 
575 	mutex_unlock(&bt541->input_dev->mutex);
576 
577 	return 0;
578 }
579 
580 static int zinitix_resume(struct device *dev)
581 {
582 	struct i2c_client *client = to_i2c_client(dev);
583 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
584 	int ret = 0;
585 
586 	mutex_lock(&bt541->input_dev->mutex);
587 
588 	if (input_device_enabled(bt541->input_dev))
589 		ret = zinitix_start(bt541);
590 
591 	mutex_unlock(&bt541->input_dev->mutex);
592 
593 	return ret;
594 }
595 
596 static DEFINE_SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
597 
598 #ifdef CONFIG_OF
599 static const struct of_device_id zinitix_of_match[] = {
600 	{ .compatible = "zinitix,bt402" },
601 	{ .compatible = "zinitix,bt403" },
602 	{ .compatible = "zinitix,bt404" },
603 	{ .compatible = "zinitix,bt412" },
604 	{ .compatible = "zinitix,bt413" },
605 	{ .compatible = "zinitix,bt431" },
606 	{ .compatible = "zinitix,bt432" },
607 	{ .compatible = "zinitix,bt531" },
608 	{ .compatible = "zinitix,bt532" },
609 	{ .compatible = "zinitix,bt538" },
610 	{ .compatible = "zinitix,bt541" },
611 	{ .compatible = "zinitix,bt548" },
612 	{ .compatible = "zinitix,bt554" },
613 	{ .compatible = "zinitix,at100" },
614 	{ }
615 };
616 MODULE_DEVICE_TABLE(of, zinitix_of_match);
617 #endif
618 
619 static struct i2c_driver zinitix_ts_driver = {
620 	.probe_new = zinitix_ts_probe,
621 	.driver = {
622 		.name = "Zinitix-TS",
623 		.pm = pm_sleep_ptr(&zinitix_pm_ops),
624 		.of_match_table = of_match_ptr(zinitix_of_match),
625 	},
626 };
627 module_i2c_driver(zinitix_ts_driver);
628 
629 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
630 MODULE_DESCRIPTION("Zinitix touchscreen driver");
631 MODULE_LICENSE("GPL v2");
632