1 /* -------------------------------------------------------------------------
2 * Copyright (C) 2014-2015, Intel Corporation
6 * Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * -------------------------------------------------------------------------
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/acpi.h>
23 #include <linux/interrupt.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/delay.h>
26 #include <linux/firmware.h>
27 #include <linux/input.h>
28 #include <linux/input/mt.h>
29 #include <linux/input/touchscreen.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/irq.h>
33 #include <linux/regulator/consumer.h>
35 #include <asm/unaligned.h>
37 #define SILEAD_TS_NAME "silead_ts"
39 #define SILEAD_REG_RESET 0xE0
40 #define SILEAD_REG_DATA 0x80
41 #define SILEAD_REG_TOUCH_NR 0x80
42 #define SILEAD_REG_POWER 0xBC
43 #define SILEAD_REG_CLOCK 0xE4
44 #define SILEAD_REG_STATUS 0xB0
45 #define SILEAD_REG_ID 0xFC
46 #define SILEAD_REG_MEM_CHECK 0xB0
48 #define SILEAD_STATUS_OK 0x5A5A5A5A
49 #define SILEAD_TS_DATA_LEN 44
50 #define SILEAD_CLOCK 0x04
52 #define SILEAD_CMD_RESET 0x88
53 #define SILEAD_CMD_START 0x00
55 #define SILEAD_POINT_DATA_LEN 0x04
56 #define SILEAD_POINT_Y_OFF 0x00
57 #define SILEAD_POINT_Y_MSB_OFF 0x01
58 #define SILEAD_POINT_X_OFF 0x02
59 #define SILEAD_POINT_X_MSB_OFF 0x03
60 #define SILEAD_EXTRA_DATA_MASK 0xF0
62 #define SILEAD_CMD_SLEEP_MIN 10000
63 #define SILEAD_CMD_SLEEP_MAX 20000
64 #define SILEAD_POWER_SLEEP 20
65 #define SILEAD_STARTUP_SLEEP 30
67 #define SILEAD_MAX_FINGERS 10
69 enum silead_ts_power {
74 struct silead_ts_data {
75 struct i2c_client *client;
76 struct gpio_desc *gpio_power;
77 struct input_dev *input;
78 struct regulator_bulk_data regulators[2];
80 struct touchscreen_properties prop;
83 struct input_mt_pos pos[SILEAD_MAX_FINGERS];
84 int slots[SILEAD_MAX_FINGERS];
85 int id[SILEAD_MAX_FINGERS];
88 struct silead_fw_data {
93 static int silead_ts_request_input_dev(struct silead_ts_data *data)
95 struct device *dev = &data->client->dev;
98 data->input = devm_input_allocate_device(dev);
101 "Failed to allocate input device\n");
105 input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
106 input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
107 touchscreen_parse_properties(data->input, true, &data->prop);
109 input_mt_init_slots(data->input, data->max_fingers,
110 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
113 if (device_property_read_bool(dev, "silead,home-button"))
114 input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
116 data->input->name = SILEAD_TS_NAME;
117 data->input->phys = "input/ts";
118 data->input->id.bustype = BUS_I2C;
120 error = input_register_device(data->input);
122 dev_err(dev, "Failed to register input device: %d\n", error);
129 static void silead_ts_set_power(struct i2c_client *client,
130 enum silead_ts_power state)
132 struct silead_ts_data *data = i2c_get_clientdata(client);
134 if (data->gpio_power) {
135 gpiod_set_value_cansleep(data->gpio_power, state);
136 msleep(SILEAD_POWER_SLEEP);
140 static void silead_ts_read_data(struct i2c_client *client)
142 struct silead_ts_data *data = i2c_get_clientdata(client);
143 struct input_dev *input = data->input;
144 struct device *dev = &client->dev;
145 u8 *bufp, buf[SILEAD_TS_DATA_LEN];
146 int touch_nr, softbutton, error, i;
147 bool softbutton_pressed = false;
149 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
150 SILEAD_TS_DATA_LEN, buf);
152 dev_err(dev, "Data read error %d\n", error);
156 if (buf[0] > data->max_fingers) {
157 dev_warn(dev, "More touches reported then supported %d > %d\n",
158 buf[0], data->max_fingers);
159 buf[0] = data->max_fingers;
163 bufp = buf + SILEAD_POINT_DATA_LEN;
164 for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
165 softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
166 SILEAD_EXTRA_DATA_MASK) >> 4;
170 * For now only respond to softbutton == 0x01, some
171 * tablets *without* a capacative button send 0x04
172 * when crossing the edges of the screen.
174 if (softbutton == 0x01)
175 softbutton_pressed = true;
181 * Bits 4-7 are the touch id, note not all models have
182 * hardware touch ids so atm we don't use these.
184 data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
185 SILEAD_EXTRA_DATA_MASK) >> 4;
186 touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
187 get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
188 get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
192 input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
194 for (i = 0; i < touch_nr; i++) {
195 input_mt_slot(input, data->slots[i]);
196 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
197 input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
198 input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
200 dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
201 data->pos[i].y, data->id[i], data->slots[i]);
204 input_mt_sync_frame(input);
205 input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
209 static int silead_ts_init(struct i2c_client *client)
211 struct silead_ts_data *data = i2c_get_clientdata(client);
214 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
218 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
220 error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
224 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
226 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
230 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
232 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
236 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
241 dev_err(&client->dev, "Registers clear error %d\n", error);
245 static int silead_ts_reset(struct i2c_client *client)
249 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
253 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
255 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
259 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
261 error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
265 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
270 dev_err(&client->dev, "Chip reset error %d\n", error);
274 static int silead_ts_startup(struct i2c_client *client)
278 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
280 dev_err(&client->dev, "Startup error %d\n", error);
284 msleep(SILEAD_STARTUP_SLEEP);
289 static int silead_ts_load_fw(struct i2c_client *client)
291 struct device *dev = &client->dev;
292 struct silead_ts_data *data = i2c_get_clientdata(client);
293 unsigned int fw_size, i;
294 const struct firmware *fw;
295 struct silead_fw_data *fw_data;
298 dev_dbg(dev, "Firmware file name: %s", data->fw_name);
300 error = reject_firmware(&fw, data->fw_name, dev);
302 dev_err(dev, "Firmware request error %d\n", error);
306 fw_size = fw->size / sizeof(*fw_data);
307 fw_data = (struct silead_fw_data *)fw->data;
309 for (i = 0; i < fw_size; i++) {
310 error = i2c_smbus_write_i2c_block_data(client,
313 (u8 *)&fw_data[i].val);
315 dev_err(dev, "Firmware load error %d\n", error);
320 release_firmware(fw);
324 static u32 silead_ts_get_status(struct i2c_client *client)
329 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
330 sizeof(status), (u8 *)&status);
332 dev_err(&client->dev, "Status read error %d\n", error);
336 return le32_to_cpu(status);
339 static int silead_ts_get_id(struct i2c_client *client)
341 struct silead_ts_data *data = i2c_get_clientdata(client);
345 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
346 sizeof(chip_id), (u8 *)&chip_id);
350 data->chip_id = le32_to_cpu(chip_id);
351 dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
356 static int silead_ts_setup(struct i2c_client *client)
362 * Some buggy BIOS-es bring up the chip in a stuck state where it
363 * blocks the I2C bus. The following steps are necessary to
364 * unstuck the chip / bus:
365 * 1. Turn off the Silead chip.
366 * 2. Try to do an I2C transfer with the chip, this will fail in
367 * response to which the I2C-bus-driver will call:
368 * i2c_recover_bus() which will unstuck the I2C-bus. Note the
369 * unstuck-ing of the I2C bus only works if we first drop the
370 * chip off the bus by turning it off.
371 * 3. Turn the chip back on.
373 * On the x86/ACPI systems were this problem is seen, step 1. and
374 * 3. require making ACPI calls and dealing with ACPI Power
375 * Resources. The workaround below runtime-suspends the chip to
376 * turn it off, leaving it up to the ACPI subsystem to deal with
380 if (device_property_read_bool(&client->dev,
381 "silead,stuck-controller-bug")) {
382 pm_runtime_set_active(&client->dev);
383 pm_runtime_enable(&client->dev);
384 pm_runtime_allow(&client->dev);
386 pm_runtime_suspend(&client->dev);
388 dev_warn(&client->dev, FW_BUG "Stuck I2C bus: please ignore the next 'controller timed out' error\n");
389 silead_ts_get_id(client);
391 /* The forbid will also resume the device */
392 pm_runtime_forbid(&client->dev);
393 pm_runtime_disable(&client->dev);
396 silead_ts_set_power(client, SILEAD_POWER_OFF);
397 silead_ts_set_power(client, SILEAD_POWER_ON);
399 error = silead_ts_get_id(client);
401 dev_err(&client->dev, "Chip ID read error %d\n", error);
405 error = silead_ts_init(client);
409 error = silead_ts_reset(client);
413 error = silead_ts_load_fw(client);
417 error = silead_ts_startup(client);
421 status = silead_ts_get_status(client);
422 if (status != SILEAD_STATUS_OK) {
423 dev_err(&client->dev,
424 "Initialization error, status: 0x%X\n", status);
431 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
433 struct silead_ts_data *data = id;
434 struct i2c_client *client = data->client;
436 silead_ts_read_data(client);
441 static void silead_ts_read_props(struct i2c_client *client)
443 struct silead_ts_data *data = i2c_get_clientdata(client);
444 struct device *dev = &client->dev;
448 error = device_property_read_u32(dev, "silead,max-fingers",
451 dev_dbg(dev, "Max fingers read error %d\n", error);
452 data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
455 error = device_property_read_string(dev, "firmware-name", &str);
459 dev_dbg(dev, "Firmware file name read error. Using default.");
463 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
464 const struct i2c_device_id *id)
466 const struct acpi_device_id *acpi_id;
467 struct device *dev = &data->client->dev;
470 if (ACPI_HANDLE(dev)) {
471 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
475 snprintf(data->fw_name, sizeof(data->fw_name),
476 "/*(DEBLOBBED)*/", acpi_id->id);
478 for (i = 0; i < strlen(data->fw_name); i++)
479 data->fw_name[i] = tolower(data->fw_name[i]);
481 snprintf(data->fw_name, sizeof(data->fw_name),
482 "/*(DEBLOBBED)*/", id->name);
488 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
489 const struct i2c_device_id *id)
491 snprintf(data->fw_name, sizeof(data->fw_name),
492 "/*(DEBLOBBED)*/", id->name);
497 static void silead_disable_regulator(void *arg)
499 struct silead_ts_data *data = arg;
501 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
504 static int silead_ts_probe(struct i2c_client *client,
505 const struct i2c_device_id *id)
507 struct silead_ts_data *data;
508 struct device *dev = &client->dev;
511 if (!i2c_check_functionality(client->adapter,
513 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
514 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
515 dev_err(dev, "I2C functionality check failed\n");
519 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
523 i2c_set_clientdata(client, data);
524 data->client = client;
526 error = silead_ts_set_default_fw_name(data, id);
530 silead_ts_read_props(client);
532 /* We must have the IRQ provided by DT or ACPI subsytem */
533 if (client->irq <= 0)
536 data->regulators[0].supply = "vddio";
537 data->regulators[1].supply = "avdd";
538 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
544 * Enable regulators at probe and disable them at remove, we need
545 * to keep the chip powered otherwise it forgets its firmware.
547 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
552 error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
557 data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
558 if (IS_ERR(data->gpio_power)) {
559 if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
560 dev_err(dev, "Shutdown GPIO request failed\n");
561 return PTR_ERR(data->gpio_power);
564 error = silead_ts_setup(client);
568 error = silead_ts_request_input_dev(data);
572 error = devm_request_threaded_irq(dev, client->irq,
573 NULL, silead_ts_threaded_irq_handler,
574 IRQF_ONESHOT, client->name, data);
576 if (error != -EPROBE_DEFER)
577 dev_err(dev, "IRQ request failed %d\n", error);
584 static int __maybe_unused silead_ts_suspend(struct device *dev)
586 struct i2c_client *client = to_i2c_client(dev);
588 disable_irq(client->irq);
589 silead_ts_set_power(client, SILEAD_POWER_OFF);
593 static int __maybe_unused silead_ts_resume(struct device *dev)
595 struct i2c_client *client = to_i2c_client(dev);
596 bool second_try = false;
599 silead_ts_set_power(client, SILEAD_POWER_ON);
602 error = silead_ts_reset(client);
607 error = silead_ts_load_fw(client);
612 error = silead_ts_startup(client);
616 status = silead_ts_get_status(client);
617 if (status != SILEAD_STATUS_OK) {
620 dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
623 dev_err(dev, "Resume error, status: 0x%02x\n", status);
627 enable_irq(client->irq);
632 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
634 static const struct i2c_device_id silead_ts_id[] = {
643 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
646 static const struct acpi_device_id silead_ts_acpi_match[] = {
658 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
662 static const struct of_device_id silead_ts_of_match[] = {
663 { .compatible = "silead,gsl1680" },
664 { .compatible = "silead,gsl1688" },
665 { .compatible = "silead,gsl3670" },
666 { .compatible = "silead,gsl3675" },
667 { .compatible = "silead,gsl3692" },
670 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
673 static struct i2c_driver silead_ts_driver = {
674 .probe = silead_ts_probe,
675 .id_table = silead_ts_id,
677 .name = SILEAD_TS_NAME,
678 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
679 .of_match_table = of_match_ptr(silead_ts_of_match),
683 module_i2c_driver(silead_ts_driver);
685 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
686 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
687 MODULE_LICENSE("GPL");