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/regulator/consumer.h>
14 #include <linux/slab.h>
18 #define BT541_SWRESET_CMD 0x0000
19 #define BT541_WAKEUP_CMD 0x0001
21 #define BT541_IDLE_CMD 0x0004
22 #define BT541_SLEEP_CMD 0x0005
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
30 #define BT541_THRESHOLD 0x0020
32 #define BT541_LARGE_PALM_REJECT_AREA_TH 0x003F
34 #define BT541_DEBUG_REG 0x0115 /* 0~7 */
36 #define BT541_TOUCH_MODE 0x0010
37 #define BT541_CHIP_REVISION 0x0011
38 #define BT541_FIRMWARE_VERSION 0x0012
40 #define ZINITIX_USB_DETECT 0x116
42 #define BT541_MINOR_FW_VERSION 0x0121
44 #define BT541_VENDOR_ID 0x001C
45 #define BT541_HW_ID 0x0014
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
52 #define BT541_TOTAL_NUMBER_OF_X 0x0060
53 #define BT541_TOTAL_NUMBER_OF_Y 0x0061
55 #define BT541_DELAY_RAW_FOR_HOST 0x007f
57 #define BT541_BUTTON_SUPPORTED_NUM 0x00B0
58 #define BT541_BUTTON_SENSITIVITY 0x00B2
59 #define BT541_DUMMY_BUTTON_SENSITIVITY 0X00C8
61 #define BT541_X_RESOLUTION 0x00C0
62 #define BT541_Y_RESOLUTION 0x00C1
64 #define BT541_POINT_STATUS_REG 0x0080
65 #define BT541_ICON_STATUS_REG 0x00AA
67 #define BT541_POINT_COORD_REG (BT541_POINT_STATUS_REG + 2)
69 #define BT541_AFE_FREQUENCY 0x0100
70 #define BT541_DND_N_COUNT 0x0122
71 #define BT541_DND_U_COUNT 0x0135
73 #define BT541_RAWDATA_REG 0x0200
75 #define BT541_EEPROM_INFO_REG 0x0018
77 #define BT541_INT_ENABLE_FLAG 0x00f0
78 #define BT541_PERIODICAL_INTERRUPT_INTERVAL 0x00f1
80 #define BT541_BTN_WIDTH 0x016d
82 #define BT541_CHECKSUM_RESULT 0x012c
84 #define BT541_INIT_FLASH 0x01d0
85 #define BT541_WRITE_FLASH 0x01d1
86 #define BT541_READ_FLASH 0x01d2
88 #define ZINITIX_INTERNAL_FLAG_02 0x011e
89 #define ZINITIX_INTERNAL_FLAG_03 0x011f
91 #define ZINITIX_I2C_CHECKSUM_WCNT 0x016a
92 #define ZINITIX_I2C_CHECKSUM_RESULT 0x016c
94 /* Interrupt & status register flags */
96 #define BIT_PT_CNT_CHANGE BIT(0)
97 #define BIT_DOWN BIT(1)
98 #define BIT_MOVE BIT(2)
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)
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)
120 #define DEFAULT_TOUCH_POINT_MODE 2
121 #define MAX_SUPPORTED_FINGER_NUM 5
123 #define CHIP_ON_DELAY 15 // ms
124 #define FIRMWARE_ON_DELAY 40 // ms
131 // currently unused, but needed as padding:
140 struct point_coord point_coord
[MAX_SUPPORTED_FINGER_NUM
];
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];
151 static int zinitix_read_data(struct i2c_client
*client
,
152 u16 reg
, void *values
, size_t length
)
154 __le16 reg_le
= cpu_to_le16(reg
);
157 /* A single i2c_transfer() transaction does not work here. */
158 ret
= i2c_master_send(client
, (u8
*)®_le
, sizeof(reg_le
));
159 if (ret
!= sizeof(reg_le
))
160 return ret
< 0 ? ret
: -EIO
;
162 ret
= i2c_master_recv(client
, (u8
*)values
, length
);
164 return ret
< 0 ? ret
: -EIO
; ;
169 static int zinitix_write_u16(struct i2c_client
*client
, u16 reg
, u16 value
)
171 __le16 packet
[2] = {cpu_to_le16(reg
), cpu_to_le16(value
)};
174 ret
= i2c_master_send(client
, (u8
*)packet
, sizeof(packet
));
175 if (ret
!= sizeof(packet
))
176 return ret
< 0 ? ret
: -EIO
;
181 static int zinitix_write_cmd(struct i2c_client
*client
, u16 reg
)
183 __le16 reg_le
= cpu_to_le16(reg
);
186 ret
= i2c_master_send(client
, (u8
*)®_le
, sizeof(reg_le
));
187 if (ret
!= sizeof(reg_le
))
188 return ret
< 0 ? ret
: -EIO
;
193 static bool zinitix_init_touch(struct bt541_ts_data
*bt541
)
195 struct i2c_client
*client
= bt541
->client
;
199 error
= zinitix_write_cmd(client
, BT541_SWRESET_CMD
);
201 dev_err(&client
->dev
, "Failed to write reset command\n");
205 error
= zinitix_write_u16(client
, BT541_INT_ENABLE_FLAG
, 0x0);
207 dev_err(&client
->dev
,
208 "Failed to reset interrupt enable flag\n");
213 error
= zinitix_write_u16(client
, BT541_X_RESOLUTION
,
218 error
= zinitix_write_u16(client
, BT541_Y_RESOLUTION
,
223 error
= zinitix_write_u16(client
, BT541_SUPPORTED_FINGER_NUM
,
224 MAX_SUPPORTED_FINGER_NUM
);
228 error
= zinitix_write_u16(client
, BT541_INITIAL_TOUCH_MODE
,
229 bt541
->zinitix_mode
);
233 error
= zinitix_write_u16(client
, BT541_TOUCH_MODE
,
234 bt541
->zinitix_mode
);
238 error
= zinitix_write_u16(client
, BT541_INT_ENABLE_FLAG
,
239 BIT_PT_CNT_CHANGE
| BIT_DOWN
| BIT_MOVE
|
245 for (i
= 0; i
< 10; i
++) {
246 zinitix_write_cmd(client
, BT541_CLEAR_INT_STATUS_CMD
);
253 static int zinitix_init_regulators(struct bt541_ts_data
*bt541
)
255 struct i2c_client
*client
= bt541
->client
;
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
),
264 dev_err(&client
->dev
, "Failed to get regulators: %d\n", error
);
271 static int zinitix_send_power_on_sequence(struct bt541_ts_data
*bt541
)
274 struct i2c_client
*client
= bt541
->client
;
276 error
= zinitix_write_u16(client
, 0xc000, 0x0001);
278 dev_err(&client
->dev
,
279 "Failed to send power sequence(vendor cmd enable)\n");
284 error
= zinitix_write_cmd(client
, 0xc004);
286 dev_err(&client
->dev
,
287 "Failed to send power sequence (intn clear)\n");
292 error
= zinitix_write_u16(client
, 0xc002, 0x0001);
294 dev_err(&client
->dev
,
295 "Failed to send power sequence (nvm init)\n");
300 error
= zinitix_write_u16(client
, 0xc001, 0x0001);
302 dev_err(&client
->dev
,
303 "Failed to send power sequence (program start)\n");
306 msleep(FIRMWARE_ON_DELAY
);
311 static void zinitix_report_finger(struct bt541_ts_data
*bt541
, int slot
,
312 const struct point_coord
*p
)
314 input_mt_slot(bt541
->input_dev
, slot
);
315 input_mt_report_slot_state(bt541
->input_dev
, MT_TOOL_FINGER
, true);
316 touchscreen_report_pos(bt541
->input_dev
, &bt541
->prop
,
317 le16_to_cpu(p
->x
), le16_to_cpu(p
->y
), true);
318 input_report_abs(bt541
->input_dev
, ABS_MT_TOUCH_MAJOR
, p
->width
);
321 static irqreturn_t
zinitix_ts_irq_handler(int irq
, void *bt541_handler
)
323 struct bt541_ts_data
*bt541
= bt541_handler
;
324 struct i2c_client
*client
= bt541
->client
;
325 struct touch_event touch_event
;
329 memset(&touch_event
, 0, sizeof(struct touch_event
));
331 error
= zinitix_read_data(bt541
->client
, BT541_POINT_STATUS_REG
,
332 &touch_event
, sizeof(struct touch_event
));
334 dev_err(&client
->dev
, "Failed to read in touchpoint struct\n");
338 for (i
= 0; i
< MAX_SUPPORTED_FINGER_NUM
; i
++)
339 if (touch_event
.point_coord
[i
].sub_status
& SUB_BIT_EXIST
)
340 zinitix_report_finger(bt541
, i
,
341 &touch_event
.point_coord
[i
]);
343 input_mt_sync_frame(bt541
->input_dev
);
344 input_sync(bt541
->input_dev
);
347 zinitix_write_cmd(bt541
->client
, BT541_CLEAR_INT_STATUS_CMD
);
351 static int zinitix_start(struct bt541_ts_data
*bt541
)
355 error
= regulator_bulk_enable(ARRAY_SIZE(bt541
->supplies
),
358 dev_err(&bt541
->client
->dev
,
359 "Failed to enable regulators: %d\n", error
);
363 msleep(CHIP_ON_DELAY
);
365 error
= zinitix_send_power_on_sequence(bt541
);
367 dev_err(&bt541
->client
->dev
,
368 "Error while sending power-on sequence: %d\n", error
);
372 error
= zinitix_init_touch(bt541
);
374 dev_err(&bt541
->client
->dev
,
375 "Error while configuring touch IC\n");
379 enable_irq(bt541
->client
->irq
);
384 static int zinitix_stop(struct bt541_ts_data
*bt541
)
388 disable_irq(bt541
->client
->irq
);
390 error
= regulator_bulk_disable(ARRAY_SIZE(bt541
->supplies
),
393 dev_err(&bt541
->client
->dev
,
394 "Failed to disable regulators: %d\n", error
);
401 static int zinitix_input_open(struct input_dev
*dev
)
403 struct bt541_ts_data
*bt541
= input_get_drvdata(dev
);
405 return zinitix_start(bt541
);
408 static void zinitix_input_close(struct input_dev
*dev
)
410 struct bt541_ts_data
*bt541
= input_get_drvdata(dev
);
415 static int zinitix_init_input_dev(struct bt541_ts_data
*bt541
)
417 struct input_dev
*input_dev
;
420 input_dev
= devm_input_allocate_device(&bt541
->client
->dev
);
422 dev_err(&bt541
->client
->dev
,
423 "Failed to allocate input device.");
427 input_set_drvdata(input_dev
, bt541
);
428 bt541
->input_dev
= input_dev
;
430 input_dev
->name
= "Zinitix Capacitive TouchScreen";
431 input_dev
->phys
= "input/ts";
432 input_dev
->id
.bustype
= BUS_I2C
;
433 input_dev
->open
= zinitix_input_open
;
434 input_dev
->close
= zinitix_input_close
;
436 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
437 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
438 input_set_abs_params(input_dev
, ABS_MT_WIDTH_MAJOR
, 0, 255, 0, 0);
439 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0, 255, 0, 0);
441 touchscreen_parse_properties(input_dev
, true, &bt541
->prop
);
442 if (!bt541
->prop
.max_x
|| !bt541
->prop
.max_y
) {
443 dev_err(&bt541
->client
->dev
,
444 "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
448 error
= input_mt_init_slots(input_dev
, MAX_SUPPORTED_FINGER_NUM
,
449 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
451 dev_err(&bt541
->client
->dev
,
452 "Failed to initialize MT slots: %d", error
);
456 error
= input_register_device(input_dev
);
458 dev_err(&bt541
->client
->dev
,
459 "Failed to register input device: %d", error
);
466 static int zinitix_ts_probe(struct i2c_client
*client
)
468 struct bt541_ts_data
*bt541
;
471 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
472 dev_err(&client
->dev
,
473 "Failed to assert adapter's support for plain I2C.\n");
477 bt541
= devm_kzalloc(&client
->dev
, sizeof(*bt541
), GFP_KERNEL
);
481 bt541
->client
= client
;
482 i2c_set_clientdata(client
, bt541
);
484 error
= zinitix_init_regulators(bt541
);
486 dev_err(&client
->dev
,
487 "Failed to initialize regulators: %d\n", error
);
491 error
= zinitix_init_input_dev(bt541
);
493 dev_err(&client
->dev
,
494 "Failed to initialize input device: %d\n", error
);
498 error
= device_property_read_u32(&client
->dev
, "zinitix,mode",
499 &bt541
->zinitix_mode
);
501 /* fall back to mode 2 */
502 bt541
->zinitix_mode
= DEFAULT_TOUCH_POINT_MODE
;
505 if (bt541
->zinitix_mode
!= 2) {
507 * If there are devices that don't support mode 2, support
508 * for other modes (0, 1) will be needed.
510 dev_err(&client
->dev
,
511 "Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
512 bt541
->zinitix_mode
);
516 irq_set_status_flags(client
->irq
, IRQ_NOAUTOEN
);
517 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
518 NULL
, zinitix_ts_irq_handler
,
519 IRQF_ONESHOT
, client
->name
, bt541
);
521 dev_err(&client
->dev
, "Failed to request IRQ: %d\n", error
);
528 static int __maybe_unused
zinitix_suspend(struct device
*dev
)
530 struct i2c_client
*client
= to_i2c_client(dev
);
531 struct bt541_ts_data
*bt541
= i2c_get_clientdata(client
);
533 mutex_lock(&bt541
->input_dev
->mutex
);
535 if (input_device_enabled(bt541
->input_dev
))
538 mutex_unlock(&bt541
->input_dev
->mutex
);
543 static int __maybe_unused
zinitix_resume(struct device
*dev
)
545 struct i2c_client
*client
= to_i2c_client(dev
);
546 struct bt541_ts_data
*bt541
= i2c_get_clientdata(client
);
549 mutex_lock(&bt541
->input_dev
->mutex
);
551 if (input_device_enabled(bt541
->input_dev
))
552 ret
= zinitix_start(bt541
);
554 mutex_unlock(&bt541
->input_dev
->mutex
);
559 static SIMPLE_DEV_PM_OPS(zinitix_pm_ops
, zinitix_suspend
, zinitix_resume
);
562 static const struct of_device_id zinitix_of_match
[] = {
563 { .compatible
= "zinitix,bt541" },
566 MODULE_DEVICE_TABLE(of
, zinitix_of_match
);
569 static struct i2c_driver zinitix_ts_driver
= {
570 .probe_new
= zinitix_ts_probe
,
572 .name
= "Zinitix-TS",
573 .pm
= &zinitix_pm_ops
,
574 .of_match_table
= of_match_ptr(zinitix_of_match
),
577 module_i2c_driver(zinitix_ts_driver
);
579 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
580 MODULE_DESCRIPTION("Zinitix touchscreen driver");
581 MODULE_LICENSE("GPL v2");