• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BT541_SWRESET_CMD			0x0000
19 #define BT541_WAKEUP_CMD			0x0001
20 
21 #define BT541_IDLE_CMD				0x0004
22 #define BT541_SLEEP_CMD				0x0005
23 
24 #define BT541_CLEAR_INT_STATUS_CMD		0x0003
25 #define BT541_CALIBRATE_CMD			0x0006
26 #define BT541_SAVE_STATUS_CMD			0x0007
27 #define BT541_SAVE_CALIBRATION_CMD		0x0008
28 #define BT541_RECALL_FACTORY_CMD		0x000f
29 
30 #define BT541_THRESHOLD				0x0020
31 
32 #define BT541_LARGE_PALM_REJECT_AREA_TH		0x003F
33 
34 #define BT541_DEBUG_REG				0x0115 /* 0~7 */
35 
36 #define BT541_TOUCH_MODE			0x0010
37 #define BT541_CHIP_REVISION			0x0011
38 #define BT541_FIRMWARE_VERSION			0x0012
39 
40 #define ZINITIX_USB_DETECT			0x116
41 
42 #define BT541_MINOR_FW_VERSION			0x0121
43 
44 #define BT541_VENDOR_ID				0x001C
45 #define BT541_HW_ID				0x0014
46 
47 #define BT541_DATA_VERSION_REG			0x0013
48 #define BT541_SUPPORTED_FINGER_NUM		0x0015
49 #define BT541_EEPROM_INFO			0x0018
50 #define BT541_INITIAL_TOUCH_MODE		0x0019
51 
52 #define BT541_TOTAL_NUMBER_OF_X			0x0060
53 #define BT541_TOTAL_NUMBER_OF_Y			0x0061
54 
55 #define BT541_DELAY_RAW_FOR_HOST		0x007f
56 
57 #define BT541_BUTTON_SUPPORTED_NUM		0x00B0
58 #define BT541_BUTTON_SENSITIVITY		0x00B2
59 #define BT541_DUMMY_BUTTON_SENSITIVITY		0X00C8
60 
61 #define BT541_X_RESOLUTION			0x00C0
62 #define BT541_Y_RESOLUTION			0x00C1
63 
64 #define BT541_POINT_STATUS_REG			0x0080
65 #define BT541_ICON_STATUS_REG			0x00AA
66 
67 #define BT541_POINT_COORD_REG			(BT541_POINT_STATUS_REG + 2)
68 
69 #define BT541_AFE_FREQUENCY			0x0100
70 #define BT541_DND_N_COUNT			0x0122
71 #define BT541_DND_U_COUNT			0x0135
72 
73 #define BT541_RAWDATA_REG			0x0200
74 
75 #define BT541_EEPROM_INFO_REG			0x0018
76 
77 #define BT541_INT_ENABLE_FLAG			0x00f0
78 #define BT541_PERIODICAL_INTERRUPT_INTERVAL	0x00f1
79 
80 #define BT541_BTN_WIDTH				0x016d
81 
82 #define BT541_CHECKSUM_RESULT			0x012c
83 
84 #define BT541_INIT_FLASH			0x01d0
85 #define BT541_WRITE_FLASH			0x01d1
86 #define BT541_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 
zinitix_read_data(struct i2c_client * client,u16 reg,void * values,size_t length)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 
zinitix_write_u16(struct i2c_client * client,u16 reg,u16 value)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 
zinitix_write_cmd(struct i2c_client * client,u16 reg)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 
zinitix_init_touch(struct bt541_ts_data * bt541)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, BT541_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, BT541_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, BT541_X_RESOLUTION,
214 				  bt541->prop.max_x);
215 	if (error)
216 		return error;
217 
218 	error = zinitix_write_u16(client, BT541_Y_RESOLUTION,
219 				  bt541->prop.max_y);
220 	if (error)
221 		return error;
222 
223 	error = zinitix_write_u16(client, BT541_SUPPORTED_FINGER_NUM,
224 				  MAX_SUPPORTED_FINGER_NUM);
225 	if (error)
226 		return error;
227 
228 	error = zinitix_write_u16(client, BT541_INITIAL_TOUCH_MODE,
229 				  bt541->zinitix_mode);
230 	if (error)
231 		return error;
232 
233 	error = zinitix_write_u16(client, BT541_TOUCH_MODE,
234 				  bt541->zinitix_mode);
235 	if (error)
236 		return error;
237 
238 	error = zinitix_write_u16(client, BT541_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, BT541_CLEAR_INT_STATUS_CMD);
247 		udelay(10);
248 	}
249 
250 	return 0;
251 }
252 
zinitix_init_regulators(struct bt541_ts_data * bt541)253 static int zinitix_init_regulators(struct bt541_ts_data *bt541)
254 {
255 	struct i2c_client *client = bt541->client;
256 	int error;
257 
258 	bt541->supplies[0].supply = "vdd";
259 	bt541->supplies[1].supply = "vddo";
260 	error = devm_regulator_bulk_get(&client->dev,
261 					ARRAY_SIZE(bt541->supplies),
262 					bt541->supplies);
263 	if (error < 0) {
264 		dev_err(&client->dev, "Failed to get regulators: %d\n", error);
265 		return error;
266 	}
267 
268 	return 0;
269 }
270 
zinitix_send_power_on_sequence(struct bt541_ts_data * bt541)271 static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
272 {
273 	int error;
274 	struct i2c_client *client = bt541->client;
275 
276 	error = zinitix_write_u16(client, 0xc000, 0x0001);
277 	if (error) {
278 		dev_err(&client->dev,
279 			"Failed to send power sequence(vendor cmd enable)\n");
280 		return error;
281 	}
282 	udelay(10);
283 
284 	error = zinitix_write_cmd(client, 0xc004);
285 	if (error) {
286 		dev_err(&client->dev,
287 			"Failed to send power sequence (intn clear)\n");
288 		return error;
289 	}
290 	udelay(10);
291 
292 	error = zinitix_write_u16(client, 0xc002, 0x0001);
293 	if (error) {
294 		dev_err(&client->dev,
295 			"Failed to send power sequence (nvm init)\n");
296 		return error;
297 	}
298 	mdelay(2);
299 
300 	error = zinitix_write_u16(client, 0xc001, 0x0001);
301 	if (error) {
302 		dev_err(&client->dev,
303 			"Failed to send power sequence (program start)\n");
304 		return error;
305 	}
306 	msleep(FIRMWARE_ON_DELAY);
307 
308 	return 0;
309 }
310 
zinitix_report_finger(struct bt541_ts_data * bt541,int slot,const struct point_coord * p)311 static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
312 				  const struct point_coord *p)
313 {
314 	u16 x, y;
315 
316 	if (unlikely(!(p->sub_status &
317 		       (SUB_BIT_UP | SUB_BIT_DOWN | SUB_BIT_MOVE)))) {
318 		dev_dbg(&bt541->client->dev, "unknown finger event %#02x\n",
319 			p->sub_status);
320 		return;
321 	}
322 
323 	x = le16_to_cpu(p->x);
324 	y = le16_to_cpu(p->y);
325 
326 	input_mt_slot(bt541->input_dev, slot);
327 	if (input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER,
328 				       !(p->sub_status & SUB_BIT_UP))) {
329 		touchscreen_report_pos(bt541->input_dev,
330 				       &bt541->prop, x, y, true);
331 		input_report_abs(bt541->input_dev,
332 				 ABS_MT_TOUCH_MAJOR, p->width);
333 		dev_dbg(&bt541->client->dev, "finger %d %s (%u, %u)\n",
334 			slot, p->sub_status & SUB_BIT_DOWN ? "down" : "move",
335 			x, y);
336 	} else {
337 		dev_dbg(&bt541->client->dev, "finger %d up (%u, %u)\n",
338 			slot, x, y);
339 	}
340 }
341 
zinitix_ts_irq_handler(int irq,void * bt541_handler)342 static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
343 {
344 	struct bt541_ts_data *bt541 = bt541_handler;
345 	struct i2c_client *client = bt541->client;
346 	struct touch_event touch_event;
347 	unsigned long finger_mask;
348 	int error;
349 	int i;
350 
351 	memset(&touch_event, 0, sizeof(struct touch_event));
352 
353 	error = zinitix_read_data(bt541->client, BT541_POINT_STATUS_REG,
354 				  &touch_event, sizeof(struct touch_event));
355 	if (error) {
356 		dev_err(&client->dev, "Failed to read in touchpoint struct\n");
357 		goto out;
358 	}
359 
360 	finger_mask = touch_event.finger_mask;
361 	for_each_set_bit(i, &finger_mask, MAX_SUPPORTED_FINGER_NUM) {
362 		const struct point_coord *p = &touch_event.point_coord[i];
363 
364 		/* Only process contacts that are actually reported */
365 		if (p->sub_status & SUB_BIT_EXIST)
366 			zinitix_report_finger(bt541, i, p);
367 	}
368 
369 	input_mt_sync_frame(bt541->input_dev);
370 	input_sync(bt541->input_dev);
371 
372 out:
373 	zinitix_write_cmd(bt541->client, BT541_CLEAR_INT_STATUS_CMD);
374 	return IRQ_HANDLED;
375 }
376 
zinitix_start(struct bt541_ts_data * bt541)377 static int zinitix_start(struct bt541_ts_data *bt541)
378 {
379 	int error;
380 
381 	error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
382 				      bt541->supplies);
383 	if (error) {
384 		dev_err(&bt541->client->dev,
385 			"Failed to enable regulators: %d\n", error);
386 		return error;
387 	}
388 
389 	msleep(CHIP_ON_DELAY);
390 
391 	error = zinitix_send_power_on_sequence(bt541);
392 	if (error) {
393 		dev_err(&bt541->client->dev,
394 			"Error while sending power-on sequence: %d\n", error);
395 		return error;
396 	}
397 
398 	error = zinitix_init_touch(bt541);
399 	if (error) {
400 		dev_err(&bt541->client->dev,
401 			"Error while configuring touch IC\n");
402 		return error;
403 	}
404 
405 	enable_irq(bt541->client->irq);
406 
407 	return 0;
408 }
409 
zinitix_stop(struct bt541_ts_data * bt541)410 static int zinitix_stop(struct bt541_ts_data *bt541)
411 {
412 	int error;
413 
414 	disable_irq(bt541->client->irq);
415 
416 	error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
417 				       bt541->supplies);
418 	if (error) {
419 		dev_err(&bt541->client->dev,
420 			"Failed to disable regulators: %d\n", error);
421 		return error;
422 	}
423 
424 	return 0;
425 }
426 
zinitix_input_open(struct input_dev * dev)427 static int zinitix_input_open(struct input_dev *dev)
428 {
429 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
430 
431 	return zinitix_start(bt541);
432 }
433 
zinitix_input_close(struct input_dev * dev)434 static void zinitix_input_close(struct input_dev *dev)
435 {
436 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
437 
438 	zinitix_stop(bt541);
439 }
440 
zinitix_init_input_dev(struct bt541_ts_data * bt541)441 static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
442 {
443 	struct input_dev *input_dev;
444 	int error;
445 
446 	input_dev = devm_input_allocate_device(&bt541->client->dev);
447 	if (!input_dev) {
448 		dev_err(&bt541->client->dev,
449 			"Failed to allocate input device.");
450 		return -ENOMEM;
451 	}
452 
453 	input_set_drvdata(input_dev, bt541);
454 	bt541->input_dev = input_dev;
455 
456 	input_dev->name = "Zinitix Capacitive TouchScreen";
457 	input_dev->phys = "input/ts";
458 	input_dev->id.bustype = BUS_I2C;
459 	input_dev->open = zinitix_input_open;
460 	input_dev->close = zinitix_input_close;
461 
462 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
463 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
464 	input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
465 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
466 
467 	touchscreen_parse_properties(input_dev, true, &bt541->prop);
468 	if (!bt541->prop.max_x || !bt541->prop.max_y) {
469 		dev_err(&bt541->client->dev,
470 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
471 		return -EINVAL;
472 	}
473 
474 	error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
475 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
476 	if (error) {
477 		dev_err(&bt541->client->dev,
478 			"Failed to initialize MT slots: %d", error);
479 		return error;
480 	}
481 
482 	error = input_register_device(input_dev);
483 	if (error) {
484 		dev_err(&bt541->client->dev,
485 			"Failed to register input device: %d", error);
486 		return error;
487 	}
488 
489 	return 0;
490 }
491 
zinitix_ts_probe(struct i2c_client * client)492 static int zinitix_ts_probe(struct i2c_client *client)
493 {
494 	struct bt541_ts_data *bt541;
495 	int error;
496 
497 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
498 		dev_err(&client->dev,
499 			"Failed to assert adapter's support for plain I2C.\n");
500 		return -ENXIO;
501 	}
502 
503 	bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
504 	if (!bt541)
505 		return -ENOMEM;
506 
507 	bt541->client = client;
508 	i2c_set_clientdata(client, bt541);
509 
510 	error = zinitix_init_regulators(bt541);
511 	if (error) {
512 		dev_err(&client->dev,
513 			"Failed to initialize regulators: %d\n", error);
514 		return error;
515 	}
516 
517 	error = devm_request_threaded_irq(&client->dev, client->irq,
518 					  NULL, zinitix_ts_irq_handler,
519 					  IRQF_ONESHOT,
520 					  client->name, bt541);
521 	if (error) {
522 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
523 		return error;
524 	}
525 
526 	error = zinitix_init_input_dev(bt541);
527 	if (error) {
528 		dev_err(&client->dev,
529 			"Failed to initialize input device: %d\n", error);
530 		return error;
531 	}
532 
533 	error = device_property_read_u32(&client->dev, "zinitix,mode",
534 					 &bt541->zinitix_mode);
535 	if (error < 0) {
536 		/* fall back to mode 2 */
537 		bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
538 	}
539 
540 	if (bt541->zinitix_mode != 2) {
541 		/*
542 		 * If there are devices that don't support mode 2, support
543 		 * for other modes (0, 1) will be needed.
544 		 */
545 		dev_err(&client->dev,
546 			"Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
547 			bt541->zinitix_mode);
548 		return -EINVAL;
549 	}
550 
551 	irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
552 
553 	return 0;
554 }
555 
zinitix_suspend(struct device * dev)556 static int __maybe_unused zinitix_suspend(struct device *dev)
557 {
558 	struct i2c_client *client = to_i2c_client(dev);
559 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
560 
561 	mutex_lock(&bt541->input_dev->mutex);
562 
563 	if (bt541->input_dev->users)
564 		zinitix_stop(bt541);
565 
566 	mutex_unlock(&bt541->input_dev->mutex);
567 
568 	return 0;
569 }
570 
zinitix_resume(struct device * dev)571 static int __maybe_unused zinitix_resume(struct device *dev)
572 {
573 	struct i2c_client *client = to_i2c_client(dev);
574 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
575 	int ret = 0;
576 
577 	mutex_lock(&bt541->input_dev->mutex);
578 
579 	if (bt541->input_dev->users)
580 		ret = zinitix_start(bt541);
581 
582 	mutex_unlock(&bt541->input_dev->mutex);
583 
584 	return ret;
585 }
586 
587 static SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
588 
589 #ifdef CONFIG_OF
590 static const struct of_device_id zinitix_of_match[] = {
591 	{ .compatible = "zinitix,bt541" },
592 	{ }
593 };
594 MODULE_DEVICE_TABLE(of, zinitix_of_match);
595 #endif
596 
597 static struct i2c_driver zinitix_ts_driver = {
598 	.probe_new = zinitix_ts_probe,
599 	.driver = {
600 		.name = "Zinitix-TS",
601 		.pm = &zinitix_pm_ops,
602 		.of_match_table = of_match_ptr(zinitix_of_match),
603 	},
604 };
605 module_i2c_driver(zinitix_ts_driver);
606 
607 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
608 MODULE_DESCRIPTION("Zinitix touchscreen driver");
609 MODULE_LICENSE("GPL v2");
610