1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/delay.h>
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/input/touchscreen.h>
8 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/property.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
19 #define ZINITIX_SWRESET_CMD 0x0000
20 #define ZINITIX_WAKEUP_CMD 0x0001
22 #define ZINITIX_IDLE_CMD 0x0004
23 #define ZINITIX_SLEEP_CMD 0x0005
25 #define ZINITIX_CLEAR_INT_STATUS_CMD 0x0003
26 #define ZINITIX_CALIBRATE_CMD 0x0006
27 #define ZINITIX_SAVE_STATUS_CMD 0x0007
28 #define ZINITIX_SAVE_CALIBRATION_CMD 0x0008
29 #define ZINITIX_RECALL_FACTORY_CMD 0x000f
31 #define ZINITIX_THRESHOLD 0x0020
33 #define ZINITIX_LARGE_PALM_REJECT_AREA_TH 0x003F
35 #define ZINITIX_DEBUG_REG 0x0115 /* 0~7 */
37 #define ZINITIX_TOUCH_MODE 0x0010
39 #define ZINITIX_CHIP_REVISION 0x0011
40 #define ZINITIX_CHIP_BTX0X_MASK 0xF0F0
41 #define ZINITIX_CHIP_BT4X2 0x4020
42 #define ZINITIX_CHIP_BT4X3 0x4030
43 #define ZINITIX_CHIP_BT4X4 0x4040
45 #define ZINITIX_FIRMWARE_VERSION 0x0012
47 #define ZINITIX_USB_DETECT 0x116
49 #define ZINITIX_MINOR_FW_VERSION 0x0121
51 #define ZINITIX_VENDOR_ID 0x001C
52 #define ZINITIX_HW_ID 0x0014
54 #define ZINITIX_DATA_VERSION_REG 0x0013
55 #define ZINITIX_SUPPORTED_FINGER_NUM 0x0015
56 #define ZINITIX_EEPROM_INFO 0x0018
57 #define ZINITIX_INITIAL_TOUCH_MODE 0x0019
59 #define ZINITIX_TOTAL_NUMBER_OF_X 0x0060
60 #define ZINITIX_TOTAL_NUMBER_OF_Y 0x0061
62 #define ZINITIX_DELAY_RAW_FOR_HOST 0x007f
64 #define ZINITIX_BUTTON_SUPPORTED_NUM 0x00B0
65 #define ZINITIX_BUTTON_SENSITIVITY 0x00B2
66 #define ZINITIX_DUMMY_BUTTON_SENSITIVITY 0X00C8
68 #define ZINITIX_X_RESOLUTION 0x00C0
69 #define ZINITIX_Y_RESOLUTION 0x00C1
71 #define ZINITIX_POINT_STATUS_REG 0x0080
73 #define ZINITIX_BT4X2_ICON_STATUS_REG 0x009A
74 #define ZINITIX_BT4X3_ICON_STATUS_REG 0x00A0
75 #define ZINITIX_BT4X4_ICON_STATUS_REG 0x00A0
76 #define ZINITIX_BT5XX_ICON_STATUS_REG 0x00AA
78 #define ZINITIX_POINT_COORD_REG (ZINITIX_POINT_STATUS_REG + 2)
80 #define ZINITIX_AFE_FREQUENCY 0x0100
81 #define ZINITIX_DND_N_COUNT 0x0122
82 #define ZINITIX_DND_U_COUNT 0x0135
84 #define ZINITIX_RAWDATA_REG 0x0200
86 #define ZINITIX_EEPROM_INFO_REG 0x0018
88 #define ZINITIX_INT_ENABLE_FLAG 0x00f0
89 #define ZINITIX_PERIODICAL_INTERRUPT_INTERVAL 0x00f1
91 #define ZINITIX_BTN_WIDTH 0x016d
93 #define ZINITIX_CHECKSUM_RESULT 0x012c
95 #define ZINITIX_INIT_FLASH 0x01d0
96 #define ZINITIX_WRITE_FLASH 0x01d1
97 #define ZINITIX_READ_FLASH 0x01d2
99 #define ZINITIX_INTERNAL_FLAG_02 0x011e
100 #define ZINITIX_INTERNAL_FLAG_03 0x011f
102 #define ZINITIX_I2C_CHECKSUM_WCNT 0x016a
103 #define ZINITIX_I2C_CHECKSUM_RESULT 0x016c
105 /* Interrupt & status register flags */
107 #define BIT_PT_CNT_CHANGE BIT(0)
108 #define BIT_DOWN BIT(1)
109 #define BIT_MOVE BIT(2)
110 #define BIT_UP BIT(3)
111 #define BIT_PALM BIT(4)
112 #define BIT_PALM_REJECT BIT(5)
113 #define BIT_RESERVED_0 BIT(6)
114 #define BIT_RESERVED_1 BIT(7)
115 #define BIT_WEIGHT_CHANGE BIT(8)
116 #define BIT_PT_NO_CHANGE BIT(9)
117 #define BIT_REJECT BIT(10)
118 #define BIT_PT_EXIST BIT(11)
119 #define BIT_RESERVED_2 BIT(12)
120 #define BIT_ERROR BIT(13)
121 #define BIT_DEBUG BIT(14)
122 #define BIT_ICON_EVENT BIT(15)
124 #define SUB_BIT_EXIST BIT(0)
125 #define SUB_BIT_DOWN BIT(1)
126 #define SUB_BIT_MOVE BIT(2)
127 #define SUB_BIT_UP BIT(3)
128 #define SUB_BIT_UPDATE BIT(4)
129 #define SUB_BIT_WAIT BIT(5)
131 #define DEFAULT_TOUCH_POINT_MODE 2
132 #define MAX_SUPPORTED_FINGER_NUM 5
133 #define MAX_SUPPORTED_BUTTON_NUM 8
135 #define CHIP_ON_DELAY 15 // ms
136 #define FIRMWARE_ON_DELAY 40 // ms
143 // currently unused, but needed as padding:
152 struct point_coord point_coord
[MAX_SUPPORTED_FINGER_NUM
];
155 struct bt541_ts_data
{
156 struct i2c_client
*client
;
157 struct input_dev
*input_dev
;
158 struct touchscreen_properties prop
;
159 struct regulator_bulk_data supplies
[2];
161 u32 keycodes
[MAX_SUPPORTED_BUTTON_NUM
];
163 bool have_versioninfo
;
165 u16 firmware_version
;
170 static int zinitix_read_data(struct i2c_client
*client
,
171 u16 reg
, void *values
, size_t length
)
173 __le16 reg_le
= cpu_to_le16(reg
);
176 /* A single i2c_transfer() transaction does not work here. */
177 ret
= i2c_master_send(client
, (u8
*)®_le
, sizeof(reg_le
));
178 if (ret
!= sizeof(reg_le
))
179 return ret
< 0 ? ret
: -EIO
;
181 ret
= i2c_master_recv(client
, (u8
*)values
, length
);
183 return ret
< 0 ? ret
: -EIO
;
188 static int zinitix_write_u16(struct i2c_client
*client
, u16 reg
, u16 value
)
190 __le16 packet
[2] = {cpu_to_le16(reg
), cpu_to_le16(value
)};
193 ret
= i2c_master_send(client
, (u8
*)packet
, sizeof(packet
));
194 if (ret
!= sizeof(packet
))
195 return ret
< 0 ? ret
: -EIO
;
200 static int zinitix_write_cmd(struct i2c_client
*client
, u16 reg
)
202 __le16 reg_le
= cpu_to_le16(reg
);
205 ret
= i2c_master_send(client
, (u8
*)®_le
, sizeof(reg_le
));
206 if (ret
!= sizeof(reg_le
))
207 return ret
< 0 ? ret
: -EIO
;
212 static u16
zinitix_get_u16_reg(struct bt541_ts_data
*bt541
, u16 vreg
)
214 struct i2c_client
*client
= bt541
->client
;
218 error
= zinitix_read_data(client
, vreg
, (void *)&val
, 2);
222 return le16_to_cpu(val
);
225 static int zinitix_init_touch(struct bt541_ts_data
*bt541
)
227 struct i2c_client
*client
= bt541
->client
;
232 error
= zinitix_write_cmd(client
, ZINITIX_SWRESET_CMD
);
234 dev_err(&client
->dev
, "Failed to write reset command\n");
239 * Read and cache the chip revision and firmware version the first time
242 if (!bt541
->have_versioninfo
) {
243 bt541
->chip_revision
= zinitix_get_u16_reg(bt541
,
244 ZINITIX_CHIP_REVISION
);
245 bt541
->firmware_version
= zinitix_get_u16_reg(bt541
,
246 ZINITIX_FIRMWARE_VERSION
);
247 bt541
->regdata_version
= zinitix_get_u16_reg(bt541
,
248 ZINITIX_DATA_VERSION_REG
);
249 bt541
->have_versioninfo
= true;
251 dev_dbg(&client
->dev
,
252 "chip revision %04x firmware version %04x regdata version %04x\n",
253 bt541
->chip_revision
, bt541
->firmware_version
,
254 bt541
->regdata_version
);
257 * Determine the "icon" status register which varies by the
260 switch (bt541
->chip_revision
& ZINITIX_CHIP_BTX0X_MASK
) {
261 case ZINITIX_CHIP_BT4X2
:
262 bt541
->icon_status_reg
= ZINITIX_BT4X2_ICON_STATUS_REG
;
265 case ZINITIX_CHIP_BT4X3
:
266 bt541
->icon_status_reg
= ZINITIX_BT4X3_ICON_STATUS_REG
;
269 case ZINITIX_CHIP_BT4X4
:
270 bt541
->icon_status_reg
= ZINITIX_BT4X4_ICON_STATUS_REG
;
274 bt541
->icon_status_reg
= ZINITIX_BT5XX_ICON_STATUS_REG
;
279 error
= zinitix_write_u16(client
, ZINITIX_INT_ENABLE_FLAG
, 0x0);
281 dev_err(&client
->dev
,
282 "Failed to reset interrupt enable flag\n");
287 error
= zinitix_write_u16(client
, ZINITIX_X_RESOLUTION
,
292 error
= zinitix_write_u16(client
, ZINITIX_Y_RESOLUTION
,
297 error
= zinitix_write_u16(client
, ZINITIX_SUPPORTED_FINGER_NUM
,
298 MAX_SUPPORTED_FINGER_NUM
);
302 error
= zinitix_write_u16(client
, ZINITIX_BUTTON_SUPPORTED_NUM
,
303 bt541
->num_keycodes
);
307 error
= zinitix_write_u16(client
, ZINITIX_INITIAL_TOUCH_MODE
,
308 bt541
->zinitix_mode
);
312 error
= zinitix_write_u16(client
, ZINITIX_TOUCH_MODE
,
313 bt541
->zinitix_mode
);
317 int_flags
= BIT_PT_CNT_CHANGE
| BIT_DOWN
| BIT_MOVE
| BIT_UP
;
318 if (bt541
->num_keycodes
)
319 int_flags
|= BIT_ICON_EVENT
;
321 error
= zinitix_write_u16(client
, ZINITIX_INT_ENABLE_FLAG
, int_flags
);
326 for (i
= 0; i
< 10; i
++) {
327 zinitix_write_cmd(client
, ZINITIX_CLEAR_INT_STATUS_CMD
);
334 static int zinitix_init_regulators(struct bt541_ts_data
*bt541
)
336 struct device
*dev
= &bt541
->client
->dev
;
340 * Some older device trees have erroneous names for the regulators,
341 * so check if "vddo" is present and in that case use these names.
342 * Else use the proper supply names on the component.
344 if (of_property_present(dev
->of_node
, "vddo-supply")) {
345 bt541
->supplies
[0].supply
= "vdd";
346 bt541
->supplies
[1].supply
= "vddo";
348 /* Else use the proper supply names */
349 bt541
->supplies
[0].supply
= "vcca";
350 bt541
->supplies
[1].supply
= "vdd";
352 error
= devm_regulator_bulk_get(dev
,
353 ARRAY_SIZE(bt541
->supplies
),
356 dev_err(dev
, "Failed to get regulators: %d\n", error
);
363 static int zinitix_send_power_on_sequence(struct bt541_ts_data
*bt541
)
366 struct i2c_client
*client
= bt541
->client
;
368 error
= zinitix_write_u16(client
, 0xc000, 0x0001);
370 dev_err(&client
->dev
,
371 "Failed to send power sequence(vendor cmd enable)\n");
376 error
= zinitix_write_cmd(client
, 0xc004);
378 dev_err(&client
->dev
,
379 "Failed to send power sequence (intn clear)\n");
384 error
= zinitix_write_u16(client
, 0xc002, 0x0001);
386 dev_err(&client
->dev
,
387 "Failed to send power sequence (nvm init)\n");
392 error
= zinitix_write_u16(client
, 0xc001, 0x0001);
394 dev_err(&client
->dev
,
395 "Failed to send power sequence (program start)\n");
398 msleep(FIRMWARE_ON_DELAY
);
403 static void zinitix_report_finger(struct bt541_ts_data
*bt541
, int slot
,
404 const struct point_coord
*p
)
408 if (unlikely(!(p
->sub_status
&
409 (SUB_BIT_UP
| SUB_BIT_DOWN
| SUB_BIT_MOVE
)))) {
410 dev_dbg(&bt541
->client
->dev
, "unknown finger event %#02x\n",
415 x
= le16_to_cpu(p
->x
);
416 y
= le16_to_cpu(p
->y
);
418 input_mt_slot(bt541
->input_dev
, slot
);
419 if (input_mt_report_slot_state(bt541
->input_dev
, MT_TOOL_FINGER
,
420 !(p
->sub_status
& SUB_BIT_UP
))) {
421 touchscreen_report_pos(bt541
->input_dev
,
422 &bt541
->prop
, x
, y
, true);
423 input_report_abs(bt541
->input_dev
,
424 ABS_MT_TOUCH_MAJOR
, p
->width
);
425 dev_dbg(&bt541
->client
->dev
, "finger %d %s (%u, %u)\n",
426 slot
, p
->sub_status
& SUB_BIT_DOWN
? "down" : "move",
429 dev_dbg(&bt541
->client
->dev
, "finger %d up (%u, %u)\n",
434 static void zinitix_report_keys(struct bt541_ts_data
*bt541
, u16 icon_events
)
438 for (i
= 0; i
< bt541
->num_keycodes
; i
++)
439 input_report_key(bt541
->input_dev
,
440 bt541
->keycodes
[i
], icon_events
& BIT(i
));
443 static irqreturn_t
zinitix_ts_irq_handler(int irq
, void *bt541_handler
)
445 struct bt541_ts_data
*bt541
= bt541_handler
;
446 struct i2c_client
*client
= bt541
->client
;
447 struct touch_event touch_event
;
448 unsigned long finger_mask
;
453 memset(&touch_event
, 0, sizeof(struct touch_event
));
455 error
= zinitix_read_data(bt541
->client
, ZINITIX_POINT_STATUS_REG
,
456 &touch_event
, sizeof(struct touch_event
));
458 dev_err(&client
->dev
, "Failed to read in touchpoint struct\n");
462 if (le16_to_cpu(touch_event
.status
) & BIT_ICON_EVENT
) {
463 error
= zinitix_read_data(bt541
->client
, bt541
->icon_status_reg
,
464 &icon_events
, sizeof(icon_events
));
466 dev_err(&client
->dev
, "Failed to read icon events\n");
470 zinitix_report_keys(bt541
, le16_to_cpu(icon_events
));
473 finger_mask
= touch_event
.finger_mask
;
474 for_each_set_bit(i
, &finger_mask
, MAX_SUPPORTED_FINGER_NUM
) {
475 const struct point_coord
*p
= &touch_event
.point_coord
[i
];
477 /* Only process contacts that are actually reported */
478 if (p
->sub_status
& SUB_BIT_EXIST
)
479 zinitix_report_finger(bt541
, i
, p
);
482 input_mt_sync_frame(bt541
->input_dev
);
483 input_sync(bt541
->input_dev
);
486 zinitix_write_cmd(bt541
->client
, ZINITIX_CLEAR_INT_STATUS_CMD
);
490 static int zinitix_start(struct bt541_ts_data
*bt541
)
494 error
= regulator_bulk_enable(ARRAY_SIZE(bt541
->supplies
),
497 dev_err(&bt541
->client
->dev
,
498 "Failed to enable regulators: %d\n", error
);
502 msleep(CHIP_ON_DELAY
);
504 error
= zinitix_send_power_on_sequence(bt541
);
506 dev_err(&bt541
->client
->dev
,
507 "Error while sending power-on sequence: %d\n", error
);
511 error
= zinitix_init_touch(bt541
);
513 dev_err(&bt541
->client
->dev
,
514 "Error while configuring touch IC\n");
518 enable_irq(bt541
->client
->irq
);
523 static int zinitix_stop(struct bt541_ts_data
*bt541
)
527 disable_irq(bt541
->client
->irq
);
529 error
= regulator_bulk_disable(ARRAY_SIZE(bt541
->supplies
),
532 dev_err(&bt541
->client
->dev
,
533 "Failed to disable regulators: %d\n", error
);
540 static int zinitix_input_open(struct input_dev
*dev
)
542 struct bt541_ts_data
*bt541
= input_get_drvdata(dev
);
544 return zinitix_start(bt541
);
547 static void zinitix_input_close(struct input_dev
*dev
)
549 struct bt541_ts_data
*bt541
= input_get_drvdata(dev
);
554 static int zinitix_init_input_dev(struct bt541_ts_data
*bt541
)
556 struct input_dev
*input_dev
;
560 input_dev
= devm_input_allocate_device(&bt541
->client
->dev
);
562 dev_err(&bt541
->client
->dev
,
563 "Failed to allocate input device.");
567 input_set_drvdata(input_dev
, bt541
);
568 bt541
->input_dev
= input_dev
;
570 input_dev
->name
= "Zinitix Capacitive TouchScreen";
571 input_dev
->phys
= "input/ts";
572 input_dev
->id
.bustype
= BUS_I2C
;
573 input_dev
->open
= zinitix_input_open
;
574 input_dev
->close
= zinitix_input_close
;
576 if (bt541
->num_keycodes
) {
577 input_dev
->keycode
= bt541
->keycodes
;
578 input_dev
->keycodemax
= bt541
->num_keycodes
;
579 input_dev
->keycodesize
= sizeof(bt541
->keycodes
[0]);
580 for (i
= 0; i
< bt541
->num_keycodes
; i
++)
581 input_set_capability(input_dev
, EV_KEY
, bt541
->keycodes
[i
]);
584 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
585 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
586 input_set_abs_params(input_dev
, ABS_MT_WIDTH_MAJOR
, 0, 255, 0, 0);
587 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0, 255, 0, 0);
589 touchscreen_parse_properties(input_dev
, true, &bt541
->prop
);
590 if (!bt541
->prop
.max_x
|| !bt541
->prop
.max_y
) {
591 dev_err(&bt541
->client
->dev
,
592 "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
596 error
= input_mt_init_slots(input_dev
, MAX_SUPPORTED_FINGER_NUM
,
597 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
599 dev_err(&bt541
->client
->dev
,
600 "Failed to initialize MT slots: %d", error
);
604 error
= input_register_device(input_dev
);
606 dev_err(&bt541
->client
->dev
,
607 "Failed to register input device: %d", error
);
614 static int zinitix_ts_probe(struct i2c_client
*client
)
616 struct bt541_ts_data
*bt541
;
619 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
620 dev_err(&client
->dev
,
621 "Failed to assert adapter's support for plain I2C.\n");
625 bt541
= devm_kzalloc(&client
->dev
, sizeof(*bt541
), GFP_KERNEL
);
629 bt541
->client
= client
;
630 i2c_set_clientdata(client
, bt541
);
632 error
= zinitix_init_regulators(bt541
);
634 dev_err(&client
->dev
,
635 "Failed to initialize regulators: %d\n", error
);
639 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
640 NULL
, zinitix_ts_irq_handler
,
641 IRQF_ONESHOT
| IRQF_NO_AUTOEN
,
642 client
->name
, bt541
);
644 dev_err(&client
->dev
, "Failed to request IRQ: %d\n", error
);
648 if (device_property_present(&client
->dev
, "linux,keycodes")) {
649 bt541
->num_keycodes
= device_property_count_u32(&client
->dev
,
651 if (bt541
->num_keycodes
< 0) {
652 dev_err(&client
->dev
, "Failed to count keys (%d)\n",
653 bt541
->num_keycodes
);
654 return bt541
->num_keycodes
;
655 } else if (bt541
->num_keycodes
> ARRAY_SIZE(bt541
->keycodes
)) {
656 dev_err(&client
->dev
, "Too many keys defined (%d)\n",
657 bt541
->num_keycodes
);
661 error
= device_property_read_u32_array(&client
->dev
,
664 bt541
->num_keycodes
);
666 dev_err(&client
->dev
,
667 "Unable to parse \"linux,keycodes\" property: %d\n",
673 error
= zinitix_init_input_dev(bt541
);
675 dev_err(&client
->dev
,
676 "Failed to initialize input device: %d\n", error
);
680 error
= device_property_read_u32(&client
->dev
, "zinitix,mode",
681 &bt541
->zinitix_mode
);
683 /* fall back to mode 2 */
684 bt541
->zinitix_mode
= DEFAULT_TOUCH_POINT_MODE
;
687 if (bt541
->zinitix_mode
!= 2) {
689 * If there are devices that don't support mode 2, support
690 * for other modes (0, 1) will be needed.
692 dev_err(&client
->dev
,
693 "Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
694 bt541
->zinitix_mode
);
701 static int zinitix_suspend(struct device
*dev
)
703 struct i2c_client
*client
= to_i2c_client(dev
);
704 struct bt541_ts_data
*bt541
= i2c_get_clientdata(client
);
706 mutex_lock(&bt541
->input_dev
->mutex
);
708 if (input_device_enabled(bt541
->input_dev
))
711 mutex_unlock(&bt541
->input_dev
->mutex
);
716 static int zinitix_resume(struct device
*dev
)
718 struct i2c_client
*client
= to_i2c_client(dev
);
719 struct bt541_ts_data
*bt541
= i2c_get_clientdata(client
);
722 mutex_lock(&bt541
->input_dev
->mutex
);
724 if (input_device_enabled(bt541
->input_dev
))
725 ret
= zinitix_start(bt541
);
727 mutex_unlock(&bt541
->input_dev
->mutex
);
732 static DEFINE_SIMPLE_DEV_PM_OPS(zinitix_pm_ops
, zinitix_suspend
, zinitix_resume
);
735 static const struct of_device_id zinitix_of_match
[] = {
736 { .compatible
= "zinitix,bt402" },
737 { .compatible
= "zinitix,bt403" },
738 { .compatible
= "zinitix,bt404" },
739 { .compatible
= "zinitix,bt412" },
740 { .compatible
= "zinitix,bt413" },
741 { .compatible
= "zinitix,bt431" },
742 { .compatible
= "zinitix,bt432" },
743 { .compatible
= "zinitix,bt531" },
744 { .compatible
= "zinitix,bt532" },
745 { .compatible
= "zinitix,bt538" },
746 { .compatible
= "zinitix,bt541" },
747 { .compatible
= "zinitix,bt548" },
748 { .compatible
= "zinitix,bt554" },
749 { .compatible
= "zinitix,at100" },
752 MODULE_DEVICE_TABLE(of
, zinitix_of_match
);
755 static struct i2c_driver zinitix_ts_driver
= {
756 .probe
= zinitix_ts_probe
,
758 .name
= "Zinitix-TS",
759 .pm
= pm_sleep_ptr(&zinitix_pm_ops
),
760 .of_match_table
= of_match_ptr(zinitix_of_match
),
763 module_i2c_driver(zinitix_ts_driver
);
765 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
766 MODULE_DESCRIPTION("Zinitix touchscreen driver");
767 MODULE_LICENSE("GPL v2");