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/pm_runtime.h>
24 #include <linux/irq.h>
25 #include <linux/regulator/consumer.h>
27 #include <asm/unaligned.h>
29 #define SILEAD_TS_NAME "silead_ts"
31 #define SILEAD_REG_RESET 0xE0
32 #define SILEAD_REG_DATA 0x80
33 #define SILEAD_REG_TOUCH_NR 0x80
34 #define SILEAD_REG_POWER 0xBC
35 #define SILEAD_REG_CLOCK 0xE4
36 #define SILEAD_REG_STATUS 0xB0
37 #define SILEAD_REG_ID 0xFC
38 #define SILEAD_REG_MEM_CHECK 0xB0
40 #define SILEAD_STATUS_OK 0x5A5A5A5A
41 #define SILEAD_TS_DATA_LEN 44
42 #define SILEAD_CLOCK 0x04
44 #define SILEAD_CMD_RESET 0x88
45 #define SILEAD_CMD_START 0x00
47 #define SILEAD_POINT_DATA_LEN 0x04
48 #define SILEAD_POINT_Y_OFF 0x00
49 #define SILEAD_POINT_Y_MSB_OFF 0x01
50 #define SILEAD_POINT_X_OFF 0x02
51 #define SILEAD_POINT_X_MSB_OFF 0x03
52 #define SILEAD_EXTRA_DATA_MASK 0xF0
54 #define SILEAD_CMD_SLEEP_MIN 10000
55 #define SILEAD_CMD_SLEEP_MAX 20000
56 #define SILEAD_POWER_SLEEP 20
57 #define SILEAD_STARTUP_SLEEP 30
59 #define SILEAD_MAX_FINGERS 10
61 enum silead_ts_power {
66 struct silead_ts_data {
67 struct i2c_client *client;
68 struct gpio_desc *gpio_power;
69 struct input_dev *input;
70 struct regulator_bulk_data regulators[2];
72 struct touchscreen_properties prop;
75 struct input_mt_pos pos[SILEAD_MAX_FINGERS];
76 int slots[SILEAD_MAX_FINGERS];
77 int id[SILEAD_MAX_FINGERS];
80 struct silead_fw_data {
85 static int silead_ts_request_input_dev(struct silead_ts_data *data)
87 struct device *dev = &data->client->dev;
90 data->input = devm_input_allocate_device(dev);
93 "Failed to allocate input device\n");
97 input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
98 input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
99 touchscreen_parse_properties(data->input, true, &data->prop);
101 input_mt_init_slots(data->input, data->max_fingers,
102 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
105 if (device_property_read_bool(dev, "silead,home-button"))
106 input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
108 data->input->name = SILEAD_TS_NAME;
109 data->input->phys = "input/ts";
110 data->input->id.bustype = BUS_I2C;
112 error = input_register_device(data->input);
114 dev_err(dev, "Failed to register input device: %d\n", error);
121 static void silead_ts_set_power(struct i2c_client *client,
122 enum silead_ts_power state)
124 struct silead_ts_data *data = i2c_get_clientdata(client);
126 if (data->gpio_power) {
127 gpiod_set_value_cansleep(data->gpio_power, state);
128 msleep(SILEAD_POWER_SLEEP);
132 static void silead_ts_read_data(struct i2c_client *client)
134 struct silead_ts_data *data = i2c_get_clientdata(client);
135 struct input_dev *input = data->input;
136 struct device *dev = &client->dev;
137 u8 *bufp, buf[SILEAD_TS_DATA_LEN];
138 int touch_nr, softbutton, error, i;
139 bool softbutton_pressed = false;
141 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
142 SILEAD_TS_DATA_LEN, buf);
144 dev_err(dev, "Data read error %d\n", error);
148 if (buf[0] > data->max_fingers) {
149 dev_warn(dev, "More touches reported then supported %d > %d\n",
150 buf[0], data->max_fingers);
151 buf[0] = data->max_fingers;
155 bufp = buf + SILEAD_POINT_DATA_LEN;
156 for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
157 softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
158 SILEAD_EXTRA_DATA_MASK) >> 4;
162 * For now only respond to softbutton == 0x01, some
163 * tablets *without* a capacative button send 0x04
164 * when crossing the edges of the screen.
166 if (softbutton == 0x01)
167 softbutton_pressed = true;
173 * Bits 4-7 are the touch id, note not all models have
174 * hardware touch ids so atm we don't use these.
176 data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
177 SILEAD_EXTRA_DATA_MASK) >> 4;
178 touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
179 get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
180 get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
184 input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
186 for (i = 0; i < touch_nr; i++) {
187 input_mt_slot(input, data->slots[i]);
188 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
189 input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
190 input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
192 dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
193 data->pos[i].y, data->id[i], data->slots[i]);
196 input_mt_sync_frame(input);
197 input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
201 static int silead_ts_init(struct i2c_client *client)
203 struct silead_ts_data *data = i2c_get_clientdata(client);
206 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
210 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
212 error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
216 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
218 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
222 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
224 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
228 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
233 dev_err(&client->dev, "Registers clear error %d\n", error);
237 static int silead_ts_reset(struct i2c_client *client)
241 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
245 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
247 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
251 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
253 error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
257 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
262 dev_err(&client->dev, "Chip reset error %d\n", error);
266 static int silead_ts_startup(struct i2c_client *client)
270 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
272 dev_err(&client->dev, "Startup error %d\n", error);
276 msleep(SILEAD_STARTUP_SLEEP);
281 static int silead_ts_load_fw(struct i2c_client *client)
283 struct device *dev = &client->dev;
284 struct silead_ts_data *data = i2c_get_clientdata(client);
285 unsigned int fw_size, i;
286 const struct firmware *fw;
287 struct silead_fw_data *fw_data;
290 dev_dbg(dev, "Firmware file name: %s", data->fw_name);
292 error = reject_firmware(&fw, data->fw_name, dev);
294 dev_err(dev, "Firmware request error %d\n", error);
298 fw_size = fw->size / sizeof(*fw_data);
299 fw_data = (struct silead_fw_data *)fw->data;
301 for (i = 0; i < fw_size; i++) {
302 error = i2c_smbus_write_i2c_block_data(client,
305 (u8 *)&fw_data[i].val);
307 dev_err(dev, "Firmware load error %d\n", error);
312 release_firmware(fw);
316 static u32 silead_ts_get_status(struct i2c_client *client)
321 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
322 sizeof(status), (u8 *)&status);
324 dev_err(&client->dev, "Status read error %d\n", error);
328 return le32_to_cpu(status);
331 static int silead_ts_get_id(struct i2c_client *client)
333 struct silead_ts_data *data = i2c_get_clientdata(client);
337 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
338 sizeof(chip_id), (u8 *)&chip_id);
342 data->chip_id = le32_to_cpu(chip_id);
343 dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
348 static int silead_ts_setup(struct i2c_client *client)
354 * Some buggy BIOS-es bring up the chip in a stuck state where it
355 * blocks the I2C bus. The following steps are necessary to
356 * unstuck the chip / bus:
357 * 1. Turn off the Silead chip.
358 * 2. Try to do an I2C transfer with the chip, this will fail in
359 * response to which the I2C-bus-driver will call:
360 * i2c_recover_bus() which will unstuck the I2C-bus. Note the
361 * unstuck-ing of the I2C bus only works if we first drop the
362 * chip off the bus by turning it off.
363 * 3. Turn the chip back on.
365 * On the x86/ACPI systems were this problem is seen, step 1. and
366 * 3. require making ACPI calls and dealing with ACPI Power
367 * Resources. The workaround below runtime-suspends the chip to
368 * turn it off, leaving it up to the ACPI subsystem to deal with
372 if (device_property_read_bool(&client->dev,
373 "silead,stuck-controller-bug")) {
374 pm_runtime_set_active(&client->dev);
375 pm_runtime_enable(&client->dev);
376 pm_runtime_allow(&client->dev);
378 pm_runtime_suspend(&client->dev);
380 dev_warn(&client->dev, FW_BUG "Stuck I2C bus: please ignore the next 'controller timed out' error\n");
381 silead_ts_get_id(client);
383 /* The forbid will also resume the device */
384 pm_runtime_forbid(&client->dev);
385 pm_runtime_disable(&client->dev);
388 silead_ts_set_power(client, SILEAD_POWER_OFF);
389 silead_ts_set_power(client, SILEAD_POWER_ON);
391 error = silead_ts_get_id(client);
393 dev_err(&client->dev, "Chip ID read error %d\n", error);
397 error = silead_ts_init(client);
401 error = silead_ts_reset(client);
405 error = silead_ts_load_fw(client);
409 error = silead_ts_startup(client);
413 status = silead_ts_get_status(client);
414 if (status != SILEAD_STATUS_OK) {
415 dev_err(&client->dev,
416 "Initialization error, status: 0x%X\n", status);
423 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
425 struct silead_ts_data *data = id;
426 struct i2c_client *client = data->client;
428 silead_ts_read_data(client);
433 static void silead_ts_read_props(struct i2c_client *client)
435 struct silead_ts_data *data = i2c_get_clientdata(client);
436 struct device *dev = &client->dev;
440 error = device_property_read_u32(dev, "silead,max-fingers",
443 dev_dbg(dev, "Max fingers read error %d\n", error);
444 data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
447 error = device_property_read_string(dev, "firmware-name", &str);
451 dev_dbg(dev, "Firmware file name read error. Using default.");
455 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
456 const struct i2c_device_id *id)
458 const struct acpi_device_id *acpi_id;
459 struct device *dev = &data->client->dev;
462 if (ACPI_HANDLE(dev)) {
463 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
467 snprintf(data->fw_name, sizeof(data->fw_name),
468 "/*(DEBLOBBED)*/", acpi_id->id);
470 for (i = 0; i < strlen(data->fw_name); i++)
471 data->fw_name[i] = tolower(data->fw_name[i]);
473 snprintf(data->fw_name, sizeof(data->fw_name),
474 "/*(DEBLOBBED)*/", id->name);
480 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
481 const struct i2c_device_id *id)
483 snprintf(data->fw_name, sizeof(data->fw_name),
484 "/*(DEBLOBBED)*/", id->name);
489 static void silead_disable_regulator(void *arg)
491 struct silead_ts_data *data = arg;
493 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
496 static int silead_ts_probe(struct i2c_client *client,
497 const struct i2c_device_id *id)
499 struct silead_ts_data *data;
500 struct device *dev = &client->dev;
503 if (!i2c_check_functionality(client->adapter,
505 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
506 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
507 dev_err(dev, "I2C functionality check failed\n");
511 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
515 i2c_set_clientdata(client, data);
516 data->client = client;
518 error = silead_ts_set_default_fw_name(data, id);
522 silead_ts_read_props(client);
524 /* We must have the IRQ provided by DT or ACPI subsytem */
525 if (client->irq <= 0)
528 data->regulators[0].supply = "vddio";
529 data->regulators[1].supply = "avdd";
530 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
536 * Enable regulators at probe and disable them at remove, we need
537 * to keep the chip powered otherwise it forgets its firmware.
539 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
544 error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
549 data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
550 if (IS_ERR(data->gpio_power)) {
551 if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
552 dev_err(dev, "Shutdown GPIO request failed\n");
553 return PTR_ERR(data->gpio_power);
556 error = silead_ts_setup(client);
560 error = silead_ts_request_input_dev(data);
564 error = devm_request_threaded_irq(dev, client->irq,
565 NULL, silead_ts_threaded_irq_handler,
566 IRQF_ONESHOT, client->name, data);
568 if (error != -EPROBE_DEFER)
569 dev_err(dev, "IRQ request failed %d\n", error);
576 static int __maybe_unused silead_ts_suspend(struct device *dev)
578 struct i2c_client *client = to_i2c_client(dev);
580 disable_irq(client->irq);
581 silead_ts_set_power(client, SILEAD_POWER_OFF);
585 static int __maybe_unused silead_ts_resume(struct device *dev)
587 struct i2c_client *client = to_i2c_client(dev);
588 bool second_try = false;
591 silead_ts_set_power(client, SILEAD_POWER_ON);
594 error = silead_ts_reset(client);
599 error = silead_ts_load_fw(client);
604 error = silead_ts_startup(client);
608 status = silead_ts_get_status(client);
609 if (status != SILEAD_STATUS_OK) {
612 dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
615 dev_err(dev, "Resume error, status: 0x%02x\n", status);
619 enable_irq(client->irq);
624 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
626 static const struct i2c_device_id silead_ts_id[] = {
635 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
638 static const struct acpi_device_id silead_ts_acpi_match[] = {
650 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
654 static const struct of_device_id silead_ts_of_match[] = {
655 { .compatible = "silead,gsl1680" },
656 { .compatible = "silead,gsl1688" },
657 { .compatible = "silead,gsl3670" },
658 { .compatible = "silead,gsl3675" },
659 { .compatible = "silead,gsl3692" },
662 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
665 static struct i2c_driver silead_ts_driver = {
666 .probe = silead_ts_probe,
667 .id_table = silead_ts_id,
669 .name = SILEAD_TS_NAME,
670 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
671 .of_match_table = of_match_ptr(silead_ts_of_match),
675 module_i2c_driver(silead_ts_driver);
677 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
678 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
679 MODULE_LICENSE("GPL");