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 * ------------------------------------------------------------------------- */
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/acpi.h>
22 #include <linux/interrupt.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/delay.h>
25 #include <linux/firmware.h>
26 #include <linux/input.h>
27 #include <linux/input/mt.h>
29 #include <linux/irq.h>
31 #define SILEAD_TS_NAME "silead_ts"
33 #define SILEAD_REG_RESET 0xE0
34 #define SILEAD_REG_DATA 0x80
35 #define SILEAD_REG_TOUCH_NR 0x80
36 #define SILEAD_REG_POWER 0xBC
37 #define SILEAD_REG_CLOCK 0xE4
38 #define SILEAD_REG_STATUS 0xB0
39 #define SILEAD_REG_ID 0xFC
40 #define SILEAD_REG_MEM_CHECK 0xB0
42 #define SILEAD_STATUS_OK 0x5A5A5A5A
43 #define SILEAD_TS_DATA_LEN 44
44 #define SILEAD_CLOCK 0x04
46 #define SILEAD_CMD_RESET 0x88
47 #define SILEAD_CMD_START 0x00
49 #define SILEAD_POINT_DATA_LEN 0x04
50 #define SILEAD_POINT_X_OFF 0x02
51 #define SILEAD_POINT_ID_OFF 0x03
52 #define SILEAD_X_HSB_MASK 0xF0
53 #define SILEAD_TOUCH_ID_MASK 0x0F
55 #define SILEAD_DP_X_MAX "resolution-x"
56 #define SILEAD_DP_Y_MAX "resolution-y"
57 #define SILEAD_DP_MAX_FINGERS "max-fingers"
58 #define SILEAD_DP_FW_NAME "fw-name"
59 #define SILEAD_PWR_GPIO_NAME "power"
61 #define SILEAD_CMD_SLEEP_MIN 10000
62 #define SILEAD_CMD_SLEEP_MAX 20000
63 #define SILEAD_POWER_SLEEP 20
64 #define SILEAD_STARTUP_SLEEP 30
66 enum silead_ts_power {
71 struct silead_ts_data {
72 struct i2c_client *client;
73 struct gpio_desc *gpio_power;
74 struct input_dev *input;
75 const char *custom_fw_name;
76 char fw_name[I2C_NAME_SIZE];
83 struct silead_fw_data {
88 static int silead_ts_request_input_dev(struct silead_ts_data *data)
90 struct device *dev = &data->client->dev;
93 data->input = devm_input_allocate_device(dev);
96 "Failed to allocate input device\n");
100 input_set_capability(data->input, EV_ABS, ABS_X);
101 input_set_capability(data->input, EV_ABS, ABS_Y);
103 input_set_abs_params(data->input, ABS_MT_POSITION_X, 0,
105 input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0,
108 input_mt_init_slots(data->input, data->max_fingers,
109 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
111 data->input->name = SILEAD_TS_NAME;
112 data->input->phys = "input/ts";
113 data->input->id.bustype = BUS_I2C;
115 ret = input_register_device(data->input);
117 dev_err(dev, "Failed to register input device: %d\n", ret);
124 static void silead_ts_report_touch(struct silead_ts_data *data, u16 x, u16 y,
127 input_mt_slot(data->input, id);
128 input_mt_report_slot_state(data->input, MT_TOOL_FINGER, true);
129 input_report_abs(data->input, ABS_MT_POSITION_X, x);
130 input_report_abs(data->input, ABS_MT_POSITION_Y, y);
133 static void silead_ts_set_power(struct i2c_client *client,
134 enum silead_ts_power state)
136 struct silead_ts_data *data = i2c_get_clientdata(client);
138 gpiod_set_value_cansleep(data->gpio_power, state);
139 msleep(SILEAD_POWER_SLEEP);
142 static void silead_ts_read_data(struct i2c_client *client)
144 struct silead_ts_data *data = i2c_get_clientdata(client);
145 struct device *dev = &client->dev;
146 u8 buf[SILEAD_TS_DATA_LEN];
147 int x, y, id, touch_nr, ret, i, offset;
149 ret = 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", ret);
161 dev_dbg(dev, "Touch number: %d\n", touch_nr);
163 for (i = 1; i <= touch_nr; i++) {
164 offset = i * SILEAD_POINT_DATA_LEN;
166 /* The last 4 bits are the touch id */
167 id = buf[offset + SILEAD_POINT_ID_OFF] & SILEAD_TOUCH_ID_MASK;
169 /* The 1st 4 bits are part of X */
170 buf[offset + SILEAD_POINT_ID_OFF] =
171 (buf[offset + SILEAD_POINT_ID_OFF] & SILEAD_X_HSB_MASK)
174 y = le16_to_cpup((__le16 *)(buf + offset));
175 x = le16_to_cpup((__le16 *)(buf + offset + SILEAD_POINT_X_OFF));
177 dev_dbg(dev, "x=%d y=%d id=%d\n", x, y, id);
178 silead_ts_report_touch(data, x, y, id);
181 input_mt_sync_frame(data->input);
182 input_sync(data->input);
185 static int silead_ts_init(struct i2c_client *client)
187 struct silead_ts_data *data = i2c_get_clientdata(client);
190 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
194 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
196 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
200 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
202 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK, SILEAD_CLOCK);
205 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
207 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
211 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
216 dev_err(&client->dev, "Registers clear error %d\n", ret);
220 static int silead_ts_reset(struct i2c_client *client)
224 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
228 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
230 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK, SILEAD_CLOCK);
233 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
235 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
239 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
244 dev_err(&client->dev, "Chip reset error %d\n", ret);
248 static int silead_ts_startup(struct i2c_client *client)
252 ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
254 dev_err(&client->dev, "Startup error %d\n", ret);
257 msleep(SILEAD_STARTUP_SLEEP);
262 static int silead_ts_load_fw(struct i2c_client *client)
264 struct device *dev = &client->dev;
265 struct silead_ts_data *data = i2c_get_clientdata(client);
266 unsigned int fw_size, i;
267 const struct firmware *fw;
268 struct silead_fw_data *fw_data;
271 dev_dbg(dev, "Firmware file name: %s", data->fw_name);
273 if (data->custom_fw_name)
274 ret = request_firmware(&fw, data->custom_fw_name, dev);
276 ret = request_firmware(&fw, data->fw_name, dev);
279 dev_err(dev, "Firmware request error %d\n", ret);
283 fw_size = fw->size / sizeof(*fw_data);
284 fw_data = (struct silead_fw_data *)fw->data;
286 for (i = 0; i < fw_size; i++) {
287 ret = i2c_smbus_write_i2c_block_data(client, fw_data[i].offset,
288 4, (u8 *)&fw_data[i].val);
290 dev_err(dev, "Firmware load error %d\n", ret);
295 release_firmware(fw);
299 release_firmware(fw);
303 static u32 silead_ts_get_status(struct i2c_client *client)
308 ret = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS, 4,
311 dev_err(&client->dev, "Status read error %d\n", ret);
318 static int silead_ts_get_id(struct i2c_client *client)
320 struct silead_ts_data *data = i2c_get_clientdata(client);
323 ret = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID, 4,
324 (u8 *)&data->chip_id);
326 dev_err(&client->dev, "Chip ID read error %d\n", ret);
333 static int silead_ts_setup(struct i2c_client *client)
335 struct silead_ts_data *data = i2c_get_clientdata(client);
336 struct device *dev = &client->dev;
340 silead_ts_set_power(client, SILEAD_POWER_OFF);
341 silead_ts_set_power(client, SILEAD_POWER_ON);
343 ret = silead_ts_get_id(client);
346 dev_dbg(dev, "Chip ID: 0x%8X", data->chip_id);
348 ret = silead_ts_init(client);
352 ret = silead_ts_reset(client);
356 ret = silead_ts_load_fw(client);
360 ret = silead_ts_startup(client);
364 status = silead_ts_get_status(client);
365 if (status != SILEAD_STATUS_OK) {
366 dev_err(dev, "Initialization error, status: 0x%X\n", status);
373 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
375 struct silead_ts_data *data = (struct silead_ts_data *)id;
376 struct i2c_client *client = data->client;
378 silead_ts_read_data(client);
383 static int silead_ts_read_props(struct i2c_client *client)
385 struct silead_ts_data *data = i2c_get_clientdata(client);
386 struct device *dev = &client->dev;
389 ret = device_property_read_u16(dev, SILEAD_DP_X_MAX, &data->x_max);
391 dev_err(dev, "Resolution X read error %d\n", ret);
395 ret = device_property_read_u16(dev, SILEAD_DP_Y_MAX, &data->y_max);
397 dev_err(dev, "Resolution Y read error %d\n", ret);
401 ret = device_property_read_u8(dev, SILEAD_DP_MAX_FINGERS,
404 dev_err(dev, "Max fingers read error %d\n", ret);
408 ret = device_property_read_string(dev, SILEAD_DP_FW_NAME,
409 &data->custom_fw_name);
411 dev_info(dev, "Firmware file name read error. Using default.");
413 dev_dbg(dev, "X max = %d, Y max = %d, max fingers = %d",
414 data->x_max, data->y_max, data->max_fingers);
423 static const struct acpi_device_id silead_ts_acpi_match[];
425 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
426 const struct i2c_device_id *id)
428 const struct acpi_device_id *acpi_id;
429 struct device *dev = &data->client->dev;
432 if (ACPI_HANDLE(dev)) {
433 acpi_id = acpi_match_device(silead_ts_acpi_match, dev);
437 sprintf(data->fw_name, "%s.fw", acpi_id->id);
439 for (i = 0; i < strlen(data->fw_name); i++)
440 data->fw_name[i] = tolower(data->fw_name[i]);
442 sprintf(data->fw_name, "%s.fw", id->name);
448 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
449 const struct i2c_device_id *id)
451 sprintf(data->fw_name, "%s.fw", id->name);
456 static int silead_ts_probe(struct i2c_client *client,
457 const struct i2c_device_id *id)
459 struct silead_ts_data *data;
460 struct device *dev = &client->dev;
463 if (!i2c_check_functionality(client->adapter,
465 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
466 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
467 dev_err(dev, "I2C functionality check failed\n");
471 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
475 i2c_set_clientdata(client, data);
476 data->client = client;
478 ret = silead_ts_set_default_fw_name(data, id);
482 /* If the IRQ is not filled by DT or ACPI subsytem
483 * we can't continue without it */
484 if (client->irq <= 0)
488 data->gpio_power = devm_gpiod_get(dev, SILEAD_PWR_GPIO_NAME,
490 if (IS_ERR(data->gpio_power)) {
491 dev_err(dev, "Shutdown GPIO request failed\n");
492 return PTR_ERR(data->gpio_power);
495 ret = silead_ts_read_props(client);
499 ret = silead_ts_setup(client);
503 ret = silead_ts_request_input_dev(data);
507 ret = devm_request_threaded_irq(dev, client->irq, NULL,
508 silead_ts_threaded_irq_handler,
509 IRQF_ONESHOT | IRQ_TYPE_EDGE_RISING,
512 dev_err(dev, "IRQ request failed %d\n", ret);
516 dev_dbg(dev, "Probing succeded\n");
520 #ifdef CONFIG_PM_SLEEP
521 static int silead_ts_suspend(struct device *dev)
523 struct i2c_client *client = to_i2c_client(dev);
525 silead_ts_set_power(client, SILEAD_POWER_OFF);
529 static int silead_ts_resume(struct device *dev)
531 struct i2c_client *client = to_i2c_client(dev);
534 silead_ts_set_power(client, SILEAD_POWER_ON);
536 ret = silead_ts_reset(client);
540 ret = silead_ts_startup(client);
544 status = silead_ts_get_status(client);
545 if (status != SILEAD_STATUS_OK) {
546 dev_err(dev, "Resume error, status: 0x%X\n", status);
553 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
556 static const struct i2c_device_id silead_ts_id[] = {
564 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
567 static const struct acpi_device_id silead_ts_acpi_match[] = {
575 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
578 static struct i2c_driver silead_ts_driver = {
579 .probe = silead_ts_probe,
580 .id_table = silead_ts_id,
582 .name = SILEAD_TS_NAME,
583 .owner = THIS_MODULE,
585 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
587 #ifdef CONFIG_PM_SLEEP
592 module_i2c_driver(silead_ts_driver);
594 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
595 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
596 MODULE_LICENSE("GPL");