1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3 * Copyright (C) 2014-2015, Intel Corporation
7 * Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
9 * -------------------------------------------------------------------------
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
15 #include <linux/interrupt.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/delay.h>
18 #include <linux/firmware.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
23 #include <linux/irq.h>
24 #include <linux/regulator/consumer.h>
26 #include <asm/unaligned.h>
28 #define SILEAD_TS_NAME "silead_ts"
30 #define SILEAD_REG_RESET 0xE0
31 #define SILEAD_REG_DATA 0x80
32 #define SILEAD_REG_TOUCH_NR 0x80
33 #define SILEAD_REG_POWER 0xBC
34 #define SILEAD_REG_CLOCK 0xE4
35 #define SILEAD_REG_STATUS 0xB0
36 #define SILEAD_REG_ID 0xFC
37 #define SILEAD_REG_MEM_CHECK 0xB0
39 #define SILEAD_STATUS_OK 0x5A5A5A5A
40 #define SILEAD_TS_DATA_LEN 44
41 #define SILEAD_CLOCK 0x04
43 #define SILEAD_CMD_RESET 0x88
44 #define SILEAD_CMD_START 0x00
46 #define SILEAD_POINT_DATA_LEN 0x04
47 #define SILEAD_POINT_Y_OFF 0x00
48 #define SILEAD_POINT_Y_MSB_OFF 0x01
49 #define SILEAD_POINT_X_OFF 0x02
50 #define SILEAD_POINT_X_MSB_OFF 0x03
51 #define SILEAD_EXTRA_DATA_MASK 0xF0
53 #define SILEAD_CMD_SLEEP_MIN 10000
54 #define SILEAD_CMD_SLEEP_MAX 20000
55 #define SILEAD_POWER_SLEEP 20
56 #define SILEAD_STARTUP_SLEEP 30
58 #define SILEAD_MAX_FINGERS 10
60 enum silead_ts_power
{
65 struct silead_ts_data
{
66 struct i2c_client
*client
;
67 struct gpio_desc
*gpio_power
;
68 struct input_dev
*input
;
69 struct regulator_bulk_data regulators
[2];
71 struct touchscreen_properties prop
;
74 struct input_mt_pos pos
[SILEAD_MAX_FINGERS
];
75 int slots
[SILEAD_MAX_FINGERS
];
76 int id
[SILEAD_MAX_FINGERS
];
79 struct silead_fw_data
{
84 static int silead_ts_request_input_dev(struct silead_ts_data
*data
)
86 struct device
*dev
= &data
->client
->dev
;
89 data
->input
= devm_input_allocate_device(dev
);
92 "Failed to allocate input device\n");
96 input_set_abs_params(data
->input
, ABS_MT_POSITION_X
, 0, 4095, 0, 0);
97 input_set_abs_params(data
->input
, ABS_MT_POSITION_Y
, 0, 4095, 0, 0);
98 touchscreen_parse_properties(data
->input
, true, &data
->prop
);
100 input_mt_init_slots(data
->input
, data
->max_fingers
,
101 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
|
104 if (device_property_read_bool(dev
, "silead,home-button"))
105 input_set_capability(data
->input
, EV_KEY
, KEY_LEFTMETA
);
107 data
->input
->name
= SILEAD_TS_NAME
;
108 data
->input
->phys
= "input/ts";
109 data
->input
->id
.bustype
= BUS_I2C
;
111 error
= input_register_device(data
->input
);
113 dev_err(dev
, "Failed to register input device: %d\n", error
);
120 static void silead_ts_set_power(struct i2c_client
*client
,
121 enum silead_ts_power state
)
123 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
125 if (data
->gpio_power
) {
126 gpiod_set_value_cansleep(data
->gpio_power
, state
);
127 msleep(SILEAD_POWER_SLEEP
);
131 static void silead_ts_read_data(struct i2c_client
*client
)
133 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
134 struct input_dev
*input
= data
->input
;
135 struct device
*dev
= &client
->dev
;
136 u8
*bufp
, buf
[SILEAD_TS_DATA_LEN
];
137 int touch_nr
, softbutton
, error
, i
;
138 bool softbutton_pressed
= false;
140 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_DATA
,
141 SILEAD_TS_DATA_LEN
, buf
);
143 dev_err(dev
, "Data read error %d\n", error
);
147 if (buf
[0] > data
->max_fingers
) {
148 dev_warn(dev
, "More touches reported then supported %d > %d\n",
149 buf
[0], data
->max_fingers
);
150 buf
[0] = data
->max_fingers
;
154 bufp
= buf
+ SILEAD_POINT_DATA_LEN
;
155 for (i
= 0; i
< buf
[0]; i
++, bufp
+= SILEAD_POINT_DATA_LEN
) {
156 softbutton
= (bufp
[SILEAD_POINT_Y_MSB_OFF
] &
157 SILEAD_EXTRA_DATA_MASK
) >> 4;
161 * For now only respond to softbutton == 0x01, some
162 * tablets *without* a capacative button send 0x04
163 * when crossing the edges of the screen.
165 if (softbutton
== 0x01)
166 softbutton_pressed
= true;
172 * Bits 4-7 are the touch id, note not all models have
173 * hardware touch ids so atm we don't use these.
175 data
->id
[touch_nr
] = (bufp
[SILEAD_POINT_X_MSB_OFF
] &
176 SILEAD_EXTRA_DATA_MASK
) >> 4;
177 touchscreen_set_mt_pos(&data
->pos
[touch_nr
], &data
->prop
,
178 get_unaligned_le16(&bufp
[SILEAD_POINT_X_OFF
]) & 0xfff,
179 get_unaligned_le16(&bufp
[SILEAD_POINT_Y_OFF
]) & 0xfff);
183 input_mt_assign_slots(input
, data
->slots
, data
->pos
, touch_nr
, 0);
185 for (i
= 0; i
< touch_nr
; i
++) {
186 input_mt_slot(input
, data
->slots
[i
]);
187 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
188 input_report_abs(input
, ABS_MT_POSITION_X
, data
->pos
[i
].x
);
189 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->pos
[i
].y
);
191 dev_dbg(dev
, "x=%d y=%d hw_id=%d sw_id=%d\n", data
->pos
[i
].x
,
192 data
->pos
[i
].y
, data
->id
[i
], data
->slots
[i
]);
195 input_mt_sync_frame(input
);
196 input_report_key(input
, KEY_LEFTMETA
, softbutton_pressed
);
200 static int silead_ts_init(struct i2c_client
*client
)
202 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
205 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
209 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
211 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_TOUCH_NR
,
215 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
217 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_CLOCK
,
221 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
223 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
227 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
232 dev_err(&client
->dev
, "Registers clear error %d\n", error
);
236 static int silead_ts_reset(struct i2c_client
*client
)
240 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
244 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
246 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_CLOCK
,
250 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
252 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_POWER
,
256 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
261 dev_err(&client
->dev
, "Chip reset error %d\n", error
);
265 static int silead_ts_startup(struct i2c_client
*client
)
269 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
, 0x00);
271 dev_err(&client
->dev
, "Startup error %d\n", error
);
275 msleep(SILEAD_STARTUP_SLEEP
);
280 static int silead_ts_load_fw(struct i2c_client
*client
)
282 struct device
*dev
= &client
->dev
;
283 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
284 unsigned int fw_size
, i
;
285 const struct firmware
*fw
;
286 struct silead_fw_data
*fw_data
;
289 dev_dbg(dev
, "Firmware file name: %s", data
->fw_name
);
291 error
= request_firmware(&fw
, data
->fw_name
, dev
);
293 dev_err(dev
, "Firmware request error %d\n", error
);
297 fw_size
= fw
->size
/ sizeof(*fw_data
);
298 fw_data
= (struct silead_fw_data
*)fw
->data
;
300 for (i
= 0; i
< fw_size
; i
++) {
301 error
= i2c_smbus_write_i2c_block_data(client
,
304 (u8
*)&fw_data
[i
].val
);
306 dev_err(dev
, "Firmware load error %d\n", error
);
311 release_firmware(fw
);
315 static u32
silead_ts_get_status(struct i2c_client
*client
)
320 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_STATUS
,
321 sizeof(status
), (u8
*)&status
);
323 dev_err(&client
->dev
, "Status read error %d\n", error
);
327 return le32_to_cpu(status
);
330 static int silead_ts_get_id(struct i2c_client
*client
)
332 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
336 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_ID
,
337 sizeof(chip_id
), (u8
*)&chip_id
);
339 dev_err(&client
->dev
, "Chip ID read error %d\n", error
);
343 data
->chip_id
= le32_to_cpu(chip_id
);
344 dev_info(&client
->dev
, "Silead chip ID: 0x%8X", data
->chip_id
);
349 static int silead_ts_setup(struct i2c_client
*client
)
354 silead_ts_set_power(client
, SILEAD_POWER_OFF
);
355 silead_ts_set_power(client
, SILEAD_POWER_ON
);
357 error
= silead_ts_get_id(client
);
361 error
= silead_ts_init(client
);
365 error
= silead_ts_reset(client
);
369 error
= silead_ts_load_fw(client
);
373 error
= silead_ts_startup(client
);
377 status
= silead_ts_get_status(client
);
378 if (status
!= SILEAD_STATUS_OK
) {
379 dev_err(&client
->dev
,
380 "Initialization error, status: 0x%X\n", status
);
387 static irqreturn_t
silead_ts_threaded_irq_handler(int irq
, void *id
)
389 struct silead_ts_data
*data
= id
;
390 struct i2c_client
*client
= data
->client
;
392 silead_ts_read_data(client
);
397 static void silead_ts_read_props(struct i2c_client
*client
)
399 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
400 struct device
*dev
= &client
->dev
;
404 error
= device_property_read_u32(dev
, "silead,max-fingers",
407 dev_dbg(dev
, "Max fingers read error %d\n", error
);
408 data
->max_fingers
= 5; /* Most devices handle up-to 5 fingers */
411 error
= device_property_read_string(dev
, "firmware-name", &str
);
413 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
416 dev_dbg(dev
, "Firmware file name read error. Using default.");
420 static int silead_ts_set_default_fw_name(struct silead_ts_data
*data
,
421 const struct i2c_device_id
*id
)
423 const struct acpi_device_id
*acpi_id
;
424 struct device
*dev
= &data
->client
->dev
;
427 if (ACPI_HANDLE(dev
)) {
428 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
432 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
433 "silead/%s.fw", acpi_id
->id
);
435 for (i
= 0; i
< strlen(data
->fw_name
); i
++)
436 data
->fw_name
[i
] = tolower(data
->fw_name
[i
]);
438 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
439 "silead/%s.fw", id
->name
);
445 static int silead_ts_set_default_fw_name(struct silead_ts_data
*data
,
446 const struct i2c_device_id
*id
)
448 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
449 "silead/%s.fw", id
->name
);
454 static void silead_disable_regulator(void *arg
)
456 struct silead_ts_data
*data
= arg
;
458 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
461 static int silead_ts_probe(struct i2c_client
*client
,
462 const struct i2c_device_id
*id
)
464 struct silead_ts_data
*data
;
465 struct device
*dev
= &client
->dev
;
468 if (!i2c_check_functionality(client
->adapter
,
470 I2C_FUNC_SMBUS_READ_I2C_BLOCK
|
471 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
)) {
472 dev_err(dev
, "I2C functionality check failed\n");
476 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
480 i2c_set_clientdata(client
, data
);
481 data
->client
= client
;
483 error
= silead_ts_set_default_fw_name(data
, id
);
487 silead_ts_read_props(client
);
489 /* We must have the IRQ provided by DT or ACPI subsytem */
490 if (client
->irq
<= 0)
493 data
->regulators
[0].supply
= "vddio";
494 data
->regulators
[1].supply
= "avdd";
495 error
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(data
->regulators
),
501 * Enable regulators at probe and disable them at remove, we need
502 * to keep the chip powered otherwise it forgets its firmware.
504 error
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
),
509 error
= devm_add_action_or_reset(dev
, silead_disable_regulator
, data
);
514 data
->gpio_power
= devm_gpiod_get_optional(dev
, "power", GPIOD_OUT_LOW
);
515 if (IS_ERR(data
->gpio_power
)) {
516 if (PTR_ERR(data
->gpio_power
) != -EPROBE_DEFER
)
517 dev_err(dev
, "Shutdown GPIO request failed\n");
518 return PTR_ERR(data
->gpio_power
);
521 error
= silead_ts_setup(client
);
525 error
= silead_ts_request_input_dev(data
);
529 error
= devm_request_threaded_irq(dev
, client
->irq
,
530 NULL
, silead_ts_threaded_irq_handler
,
531 IRQF_ONESHOT
, client
->name
, data
);
533 if (error
!= -EPROBE_DEFER
)
534 dev_err(dev
, "IRQ request failed %d\n", error
);
541 static int __maybe_unused
silead_ts_suspend(struct device
*dev
)
543 struct i2c_client
*client
= to_i2c_client(dev
);
545 disable_irq(client
->irq
);
546 silead_ts_set_power(client
, SILEAD_POWER_OFF
);
550 static int __maybe_unused
silead_ts_resume(struct device
*dev
)
552 struct i2c_client
*client
= to_i2c_client(dev
);
553 bool second_try
= false;
556 silead_ts_set_power(client
, SILEAD_POWER_ON
);
559 error
= silead_ts_reset(client
);
564 error
= silead_ts_load_fw(client
);
569 error
= silead_ts_startup(client
);
573 status
= silead_ts_get_status(client
);
574 if (status
!= SILEAD_STATUS_OK
) {
577 dev_dbg(dev
, "Reloading firmware after unsuccessful resume\n");
580 dev_err(dev
, "Resume error, status: 0x%02x\n", status
);
584 enable_irq(client
->irq
);
589 static SIMPLE_DEV_PM_OPS(silead_ts_pm
, silead_ts_suspend
, silead_ts_resume
);
591 static const struct i2c_device_id silead_ts_id
[] = {
600 MODULE_DEVICE_TABLE(i2c
, silead_ts_id
);
603 static const struct acpi_device_id silead_ts_acpi_match
[] = {
615 MODULE_DEVICE_TABLE(acpi
, silead_ts_acpi_match
);
619 static const struct of_device_id silead_ts_of_match
[] = {
620 { .compatible
= "silead,gsl1680" },
621 { .compatible
= "silead,gsl1688" },
622 { .compatible
= "silead,gsl3670" },
623 { .compatible
= "silead,gsl3675" },
624 { .compatible
= "silead,gsl3692" },
627 MODULE_DEVICE_TABLE(of
, silead_ts_of_match
);
630 static struct i2c_driver silead_ts_driver
= {
631 .probe
= silead_ts_probe
,
632 .id_table
= silead_ts_id
,
634 .name
= SILEAD_TS_NAME
,
635 .acpi_match_table
= ACPI_PTR(silead_ts_acpi_match
),
636 .of_match_table
= of_match_ptr(silead_ts_of_match
),
640 module_i2c_driver(silead_ts_driver
);
642 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
643 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
644 MODULE_LICENSE("GPL");