// SPDX-License-Identifier: GPL-2.0-only/* * Driver for Goodix Touchscreens * * Copyright (c) 2014 Red Hat Inc. * Copyright (c) 2015 K. Merker <merker@debian.org> * * This code is based on gt9xx.c authored by andrew@goodix.com: * * 2010 - 2012 Goodix Technology. */ #include <linux/kernel.h>#include <linux/dmi.h>#include <linux/firmware.h>#include <linux/gpio/consumer.h>#include <linux/i2c.h>#include <linux/input.h>#include <linux/input/mt.h>#include <linux/input/touchscreen.h>#include <linux/module.h>#include <linux/delay.h>#include <linux/irq.h>#include <linux/interrupt.h>#include <linux/regulator/consumer.h>#include <linux/slab.h>#include <linux/acpi.h>#include <linux/of.h>#include <asm/unaligned.h> #define GOODIX_GPIO_INT_NAME "irq"#define GOODIX_GPIO_RST_NAME "reset" #define GOODIX_MAX_HEIGHT 4096#define GOODIX_MAX_WIDTH 4096#define GOODIX_INT_TRIGGER 1#define GOODIX_CONTACT_SIZE 8#define GOODIX_MAX_CONTACT_SIZE 9#define GOODIX_MAX_CONTACTS 10#define GOODIX_MAX_KEYS 7 #define GOODIX_CONFIG_MIN_LENGTH 186#define GOODIX_CONFIG_911_LENGTH 186#define GOODIX_CONFIG_967_LENGTH 228#define GOODIX_CONFIG_GT9X_LENGTH 240#define GOODIX_CONFIG_MAX_LENGTH 240 /* Register defines */#define GOODIX_REG_COMMAND 0x8040#define GOODIX_CMD_SCREEN_OFF 0x05 #define GOODIX_READ_COOR_ADDR 0x814E#define GOODIX_GT1X_REG_CONFIG_DATA 0x8050#define GOODIX_GT9X_REG_CONFIG_DATA 0x8047#define GOODIX_REG_ID 0x8140 #define GOODIX_BUFFER_STATUS_READY BIT(7)#define GOODIX_HAVE_KEY BIT(4)#define GOODIX_BUFFER_STATUS_TIMEOUT 20 #define RESOLUTION_LOC 1#define MAX_CONTACTS_LOC 5#define TRIGGER_LOC 6 /* Our special handling for GPIO accesses through ACPI is x86 specific */#if defined CONFIG_X86 && defined CONFIG_ACPI#define ACPI_GPIO_SUPPORT#endif struct goodix_ts_data; enum goodix_irq_pin_access_method { IRQ_PIN_ACCESS_NONE, IRQ_PIN_ACCESS_GPIO, IRQ_PIN_ACCESS_ACPI_GPIO, IRQ_PIN_ACCESS_ACPI_METHOD,}; struct goodix_chip_data { u16 config_addr; int config_len; int (*check_config)(struct goodix_ts_data *ts, const u8 *cfg, int len); void (*calc_config_checksum)(struct goodix_ts_data *ts);}; struct goodix_chip_id { const char *id; const struct goodix_chip_data *data;}; #define GOODIX_ID_MAX_LEN 4 struct goodix_ts_data { struct i2c_client *client; struct input_dev *input_dev; const struct goodix_chip_data *chip; struct touchscreen_properties prop; unsigned int max_touch_num; unsigned int int_trigger_type; struct regulator *avdd28; struct regulator *vddio; struct gpio_desc *gpiod_int; struct gpio_desc *gpiod_rst; int gpio_count; int gpio_int_idx; char id[GOODIX_ID_MAX_LEN + 1]; u16 version; const char *cfg_name; bool reset_controller_at_probe; bool load_cfg_from_disk; struct completion firmware_loading_complete; unsigned long irq_flags; enum goodix_irq_pin_access_method irq_pin_access_method; unsigned int contact_size; u8 config[GOODIX_CONFIG_MAX_LENGTH]; unsigned short keymap[GOODIX_MAX_KEYS];}; static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len);static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg, int len);static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts); static const struct goodix_chip_data gt1x_chip_data = { .config_addr = GOODIX_GT1X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_GT9X_LENGTH, .check_config = goodix_check_cfg_16, .calc_config_checksum = goodix_calc_cfg_checksum_16,}; static const struct goodix_chip_data gt911_chip_data = { .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_911_LENGTH, .check_config = goodix_check_cfg_8, .calc_config_checksum = goodix_calc_cfg_checksum_8,}; static const struct goodix_chip_data gt967_chip_data = { .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_967_LENGTH, .check_config = goodix_check_cfg_8, .calc_config_checksum = goodix_calc_cfg_checksum_8,}; static const struct goodix_chip_data gt9x_chip_data = { .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_GT9X_LENGTH, .check_config = goodix_check_cfg_8, .calc_config_checksum = goodix_calc_cfg_checksum_8,}; static const struct goodix_chip_id goodix_chip_ids[] = { { .id = "1151", .data = >1x_chip_data }, { .id = "5663", .data = >1x_chip_data }, { .id = "5688", .data = >1x_chip_data }, { .id = "917S", .data = >1x_chip_data }, { .id = "9286", .data = >1x_chip_data }, { .id = "911", .data = >911_chip_data }, { .id = "9271", .data = >911_chip_data }, { .id = "9110", .data = >911_chip_data }, { .id = "927", .data = >911_chip_data }, { .id = "928", .data = >911_chip_data }, { .id = "912", .data = >967_chip_data }, { .id = "9147", .data = >967_chip_data }, { .id = "967", .data = >967_chip_data }, { }}; static const unsigned long goodix_irq_flags[] = { IRQ_TYPE_EDGE_RISING, IRQ_TYPE_EDGE_FALLING, IRQ_TYPE_LEVEL_LOW, IRQ_TYPE_LEVEL_HIGH,}; /* * Those tablets have their coordinates origin at the bottom right * of the tablet, as if rotated 180 degrees */static const struct dmi_system_id rotated_screen[] = {#if defined(CONFIG_DMI) && defined(CONFIG_X86) { .ident = "Teclast X89", .matches = { /* tPAD is too generic, also match on bios date */ DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), DMI_MATCH(DMI_BOARD_NAME, "tPAD"), DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"), }, }, { .ident = "Teclast X98 Pro", .matches = { /* * Only match BIOS date, because the manufacturers * BIOS does not report the board name at all * (sometimes)... */ DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), DMI_MATCH(DMI_BIOS_DATE, "10/28/2015"), }, }, { .ident = "WinBook TW100", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), DMI_MATCH(DMI_PRODUCT_NAME, "TW100") } }, { .ident = "WinBook TW700", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), DMI_MATCH(DMI_PRODUCT_NAME, "TW700") }, },#endif {}}; static const struct dmi_system_id nine_bytes_report[] = {#if defined(CONFIG_DMI) && defined(CONFIG_X86) { .ident = "Lenovo YogaBook", /* YB1-X91L/F and YB1-X90L/F */ .matches = { DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9") } },#endif {}}; /* * Those tablets have their x coordinate inverted */static const struct dmi_system_id inverted_x_screen[] = {#if defined(CONFIG_DMI) && defined(CONFIG_X86) { .ident = "Cube I15-TC", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Cube"), DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC") }, },#endif {}}; /** * goodix_i2c_read - read data from a register of the i2c slave device. * * @client: i2c device. * @reg: the register to read from. * @buf: raw write data buffer. * @len: length of the buffer to write */static int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len){ struct i2c_msg msgs[2]; __be16 wbuf = cpu_to_be16(reg); int ret; msgs[0].flags = 0; msgs[0].addr = client->addr; msgs[0].len = 2; msgs[0].buf = (u8 *)&wbuf; msgs[1].flags = I2C_M_RD; msgs[1].addr = client->addr; msgs[1].len = len; msgs[1].buf = buf; ret = i2c_transfer(client->adapter, msgs, 2); return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);} /** * goodix_i2c_write - write data to a register of the i2c slave device. * * @client: i2c device. * @reg: the register to write to. * @buf: raw data buffer to write. * @len: length of the buffer to write */static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, unsigned len){ u8 *addr_buf; struct i2c_msg msg; int ret; addr_buf = kmalloc(len + 2, GFP_KERNEL); if (!addr_buf) return -ENOMEM; addr_buf[0] = reg >> 8; addr_buf[1] = reg & 0xFF; memcpy(&addr_buf[2], buf, len); msg.flags = 0; msg.addr = client->addr; msg.buf = addr_buf; msg.len = len + 2; ret = i2c_transfer(client->adapter, &msg, 1); kfree(addr_buf); return ret < 0 ? ret : (ret != 1 ? -EIO : 0);} static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value){ return goodix_i2c_write(client, reg, &value, sizeof(value));} static const struct goodix_chip_data *goodix_get_chip_data(const char *id){ unsigned int i; for (i = 0; goodix_chip_ids[i].id; i++) { if (!strcmp(goodix_chip_ids[i].id, id)) return goodix_chip_ids[i].data; } return >9x_chip_data;} static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data){ unsigned long max_timeout; int touch_num; int error; u16 addr = GOODIX_READ_COOR_ADDR; /* * We are going to read 1-byte header, * ts->contact_size * max(1, touch_num) bytes of coordinates * and 1-byte footer which contains the touch-key code. */ const int header_contact_keycode_size = 1 + ts->contact_size + 1; /* * The 'buffer status' bit, which indicates that the data is valid, is * not set as soon as the interrupt is raised, but slightly after. * This takes around 10 ms to happen, so we poll for 20 ms. */ max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT); do { error = goodix_i2c_read(ts->client, addr, data, header_contact_keycode_size); if (error) { dev_err(&ts->client->dev, "I2C transfer error: %d\n", error); return error; } if (data[0] & GOODIX_BUFFER_STATUS_READY) { touch_num = data[0] & 0x0f; if (touch_num > ts->max_touch_num) return -EPROTO; if (touch_num > 1) { addr += header_contact_keycode_size; data += header_contact_keycode_size; error = goodix_i2c_read(ts->client, addr, data, ts->contact_size * (touch_num - 1)); if (error) return error; } return touch_num; } usleep_range(1000, 2000); /* Poll every 1 - 2 ms */ } while (time_before(jiffies, max_timeout)); /* * The Goodix panel will send spurious interrupts after a * 'finger up' event, which will always cause a timeout. */ return -ENOMSG;} static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data){ int id = coor_data[0] & 0x0F; int input_x = get_unaligned_le16(&coor_data[1]); int input_y = get_unaligned_le16(&coor_data[3]); int input_w = get_unaligned_le16(&coor_data[5]); input_mt_slot(ts->input_dev, id); input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); touchscreen_report_pos(ts->input_dev, &ts->prop, input_x, input_y, true); input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);} static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data){ int id = coor_data[1] & 0x0F; int input_x = get_unaligned_le16(&coor_data[3]); int input_y = get_unaligned_le16(&coor_data[5]); int input_w = get_unaligned_le16(&coor_data[7]); input_mt_slot(ts->input_dev, id); input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); touchscreen_report_pos(ts->input_dev, &ts->prop, input_x, input_y, true); input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);} static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data){ int touch_num; u8 key_value; int i; if (data[0] & GOODIX_HAVE_KEY) { touch_num = data[0] & 0x0f; key_value = data[1 + ts->contact_size * touch_num]; for (i = 0; i < GOODIX_MAX_KEYS; i++) if (key_value & BIT(i)) input_report_key(ts->input_dev, ts->keymap[i], 1); } else { for (i = 0; i < GOODIX_MAX_KEYS; i++) input_report_key(ts->input_dev, ts->keymap[i], 0); }} /** * goodix_process_events - Process incoming events * * @ts: our goodix_ts_data pointer * * Called when the IRQ is triggered. Read the current device state, and push * the input events to the user space. */static void goodix_process_events(struct goodix_ts_data *ts){ u8 point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; int touch_num; int i; touch_num = goodix_ts_read_input_report(ts, point_data); if (touch_num < 0) return; goodix_ts_report_key(ts, point_data); for (i = 0; i < touch_num; i++) if (ts->contact_size == 9) goodix_ts_report_touch_9b(ts, &point_data[1 + ts->contact_size * i]); else goodix_ts_report_touch_8b(ts, &point_data[1 + ts->contact_size * i]); input_mt_sync_frame(ts->input_dev); input_sync(ts->input_dev);} /** * goodix_ts_irq_handler - The IRQ handler * * @irq: interrupt number. * @dev_id: private data pointer. */static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id){ struct goodix_ts_data *ts = dev_id; goodix_process_events(ts); if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0) dev_err(&ts->client->dev, "I2C write end_cmd error\n"); return IRQ_HANDLED;} static void goodix_free_irq(struct goodix_ts_data *ts){ devm_free_irq(&ts->client->dev, ts->client->irq, ts);} static int goodix_request_irq(struct goodix_ts_data *ts){ return devm_request_threaded_irq(&ts->client->dev, ts->client->irq, NULL, goodix_ts_irq_handler, ts->irq_flags, ts->client->name, ts);}Lines 1–500 of 1475. Load more
// SPDX-License-Identifier: GPL-2.0-only /* * Driver for Goodix Touchscreens * * Copyright (c) 2014 Red Hat Inc. * Copyright (c) 2015 K. Merker <merker@debian.org> * * This code is based on gt9xx.c authored by andrew@goodix.com: * * 2010 - 2012 Goodix Technology. */ #include <linux/kernel.h> #include <linux/dmi.h> #include <linux/firmware.h> #include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/input.h> #include <linux/input/mt.h> #include <linux/input/touchscreen.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/irq.h> #include <linux/interrupt.h> #include <linux/regulator/consumer.h> #include <linux/slab.h> #include <linux/acpi.h> #include <linux/of.h> #include <asm/unaligned.h> #define GOODIX_GPIO_INT_NAME "irq" #define GOODIX_GPIO_RST_NAME "reset" #define GOODIX_MAX_HEIGHT 4096 #define GOODIX_MAX_WIDTH 4096 #define GOODIX_INT_TRIGGER 1 #define GOODIX_CONTACT_SIZE 8 #define GOODIX_MAX_CONTACT_SIZE 9 #define GOODIX_MAX_CONTACTS 10 #define GOODIX_MAX_KEYS 7 #define GOODIX_CONFIG_MIN_LENGTH 186 #define GOODIX_CONFIG_911_LENGTH 186 #define GOODIX_CONFIG_967_LENGTH 228 #define GOODIX_CONFIG_GT9X_LENGTH 240 #define GOODIX_CONFIG_MAX_LENGTH 240 /* Register defines */ #define GOODIX_REG_COMMAND 0x8040 #define GOODIX_CMD_SCREEN_OFF 0x05 #define GOODIX_READ_COOR_ADDR 0x814E #define GOODIX_GT1X_REG_CONFIG_DATA 0x8050 #define GOODIX_GT9X_REG_CONFIG_DATA 0x8047 #define GOODIX_REG_ID 0x8140 #define GOODIX_BUFFER_STATUS_READY BIT(7) #define GOODIX_HAVE_KEY BIT(4) #define GOODIX_BUFFER_STATUS_TIMEOUT 20 #define RESOLUTION_LOC 1 #define MAX_CONTACTS_LOC 5 #define TRIGGER_LOC 6 /* Our special handling for GPIO accesses through ACPI is x86 specific */ #if defined CONFIG_X86 && defined CONFIG_ACPI #define ACPI_GPIO_SUPPORT #endif struct goodix_ts_data; enum goodix_irq_pin_access_method { IRQ_PIN_ACCESS_NONE, IRQ_PIN_ACCESS_GPIO, IRQ_PIN_ACCESS_ACPI_GPIO, IRQ_PIN_ACCESS_ACPI_METHOD, }; struct goodix_chip_data { u16 config_addr; int config_len; int (*check_config)(struct goodix_ts_data *ts, const u8 *cfg, int len); void (*calc_config_checksum)(struct goodix_ts_data *ts); }; struct goodix_chip_id { const char *id; const struct goodix_chip_data *data; }; #define GOODIX_ID_MAX_LEN 4 struct goodix_ts_data { struct i2c_client *client; struct input_dev *input_dev; const struct goodix_chip_data *chip; struct touchscreen_properties prop; unsigned int max_touch_num; unsigned int int_trigger_type; struct regulator *avdd28; struct regulator *vddio; struct gpio_desc *gpiod_int; struct gpio_desc *gpiod_rst; int gpio_count; int gpio_int_idx; char id[GOODIX_ID_MAX_LEN + 1]; u16 version; const char *cfg_name; bool reset_controller_at_probe; bool load_cfg_from_disk; struct completion firmware_loading_complete; unsigned long irq_flags; enum goodix_irq_pin_access_method irq_pin_access_method; unsigned int contact_size; u8 config[GOODIX_CONFIG_MAX_LENGTH]; unsigned short keymap[GOODIX_MAX_KEYS]; }; static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len); static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg, int len); static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts); static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts); static const struct goodix_chip_data gt1x_chip_data = { .config_addr = GOODIX_GT1X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_GT9X_LENGTH, .check_config = goodix_check_cfg_16, .calc_config_checksum = goodix_calc_cfg_checksum_16, }; static const struct goodix_chip_data gt911_chip_data = { .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_911_LENGTH, .check_config = goodix_check_cfg_8, .calc_config_checksum = goodix_calc_cfg_checksum_8, }; static const struct goodix_chip_data gt967_chip_data = { .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_967_LENGTH, .check_config = goodix_check_cfg_8, .calc_config_checksum = goodix_calc_cfg_checksum_8, }; static const struct goodix_chip_data gt9x_chip_data = { .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, .config_len = GOODIX_CONFIG_GT9X_LENGTH, .check_config = goodix_check_cfg_8, .calc_config_checksum = goodix_calc_cfg_checksum_8, }; static const struct goodix_chip_id goodix_chip_ids[] = { { .id = "1151", .data = >1x_chip_data }, { .id = "5663", .data = >1x_chip_data }, { .id = "5688", .data = >1x_chip_data }, { .id = "917S", .data = >1x_chip_data }, { .id = "9286", .data = >1x_chip_data }, { .id = "911", .data = >911_chip_data }, { .id = "9271", .data = >911_chip_data }, { .id = "9110", .data = >911_chip_data }, { .id = "927", .data = >911_chip_data }, { .id = "928", .data = >911_chip_data }, { .id = "912", .data = >967_chip_data }, { .id = "9147", .data = >967_chip_data }, { .id = "967", .data = >967_chip_data }, { } }; static const unsigned long goodix_irq_flags[] = { IRQ_TYPE_EDGE_RISING, IRQ_TYPE_EDGE_FALLING, IRQ_TYPE_LEVEL_LOW, IRQ_TYPE_LEVEL_HIGH, }; /* * Those tablets have their coordinates origin at the bottom right * of the tablet, as if rotated 180 degrees */ static const struct dmi_system_id rotated_screen[] = { #if defined(CONFIG_DMI) && defined(CONFIG_X86) { .ident = "Teclast X89", .matches = { /* tPAD is too generic, also match on bios date */ DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), DMI_MATCH(DMI_BOARD_NAME, "tPAD"), DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"), }, }, { .ident = "Teclast X98 Pro", .matches = { /* * Only match BIOS date, because the manufacturers * BIOS does not report the board name at all * (sometimes)... */ DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), DMI_MATCH(DMI_BIOS_DATE, "10/28/2015"), }, }, { .ident = "WinBook TW100", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), DMI_MATCH(DMI_PRODUCT_NAME, "TW100") } }, { .ident = "WinBook TW700", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), DMI_MATCH(DMI_PRODUCT_NAME, "TW700") }, }, #endif {} }; static const struct dmi_system_id nine_bytes_report[] = { #if defined(CONFIG_DMI) && defined(CONFIG_X86) { .ident = "Lenovo YogaBook", /* YB1-X91L/F and YB1-X90L/F */ .matches = { DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9") } }, #endif {} }; /* * Those tablets have their x coordinate inverted */ static const struct dmi_system_id inverted_x_screen[] = { #if defined(CONFIG_DMI) && defined(CONFIG_X86) { .ident = "Cube I15-TC", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Cube"), DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC") }, }, #endif {} }; /** * goodix_i2c_read - read data from a register of the i2c slave device. * * @client: i2c device. * @reg: the register to read from. * @buf: raw write data buffer. * @len: length of the buffer to write */ static int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len) { struct i2c_msg msgs[2]; __be16 wbuf = cpu_to_be16(reg); int ret; msgs[0].flags = 0; msgs[0].addr = client->addr; msgs[0].len = 2; msgs[0].buf = (u8 *)&wbuf; msgs[1].flags = I2C_M_RD; msgs[1].addr = client->addr; msgs[1].len = len; msgs[1].buf = buf; ret = i2c_transfer(client->adapter, msgs, 2); return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0); } /** * goodix_i2c_write - write data to a register of the i2c slave device. * * @client: i2c device. * @reg: the register to write to. * @buf: raw data buffer to write. * @len: length of the buffer to write */ static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, unsigned len) { u8 *addr_buf; struct i2c_msg msg; int ret; addr_buf = kmalloc(len + 2, GFP_KERNEL); if (!addr_buf) return -ENOMEM; addr_buf[0] = reg >> 8; addr_buf[1] = reg & 0xFF; memcpy(&addr_buf[2], buf, len); msg.flags = 0; msg.addr = client->addr; msg.buf = addr_buf; msg.len = len + 2; ret = i2c_transfer(client->adapter, &msg, 1); kfree(addr_buf); return ret < 0 ? ret : (ret != 1 ? -EIO : 0); } static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value) { return goodix_i2c_write(client, reg, &value, sizeof(value)); } static const struct goodix_chip_data *goodix_get_chip_data(const char *id) { unsigned int i; for (i = 0; goodix_chip_ids[i].id; i++) { if (!strcmp(goodix_chip_ids[i].id, id)) return goodix_chip_ids[i].data; } return >9x_chip_data; } static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data) { unsigned long max_timeout; int touch_num; int error; u16 addr = GOODIX_READ_COOR_ADDR; /* * We are going to read 1-byte header, * ts->contact_size * max(1, touch_num) bytes of coordinates * and 1-byte footer which contains the touch-key code. */ const int header_contact_keycode_size = 1 + ts->contact_size + 1; /* * The 'buffer status' bit, which indicates that the data is valid, is * not set as soon as the interrupt is raised, but slightly after. * This takes around 10 ms to happen, so we poll for 20 ms. */ max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT); do { error = goodix_i2c_read(ts->client, addr, data, header_contact_keycode_size); if (error) { dev_err(&ts->client->dev, "I2C transfer error: %d\n", error); return error; } if (data[0] & GOODIX_BUFFER_STATUS_READY) { touch_num = data[0] & 0x0f; if (touch_num > ts->max_touch_num) return -EPROTO; if (touch_num > 1) { addr += header_contact_keycode_size; data += header_contact_keycode_size; error = goodix_i2c_read(ts->client, addr, data, ts->contact_size * (touch_num - 1)); if (error) return error; } return touch_num; } usleep_range(1000, 2000); /* Poll every 1 - 2 ms */ } while (time_before(jiffies, max_timeout)); /* * The Goodix panel will send spurious interrupts after a * 'finger up' event, which will always cause a timeout. */ return -ENOMSG; } static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data) { int id = coor_data[0] & 0x0F; int input_x = get_unaligned_le16(&coor_data[1]); int input_y = get_unaligned_le16(&coor_data[3]); int input_w = get_unaligned_le16(&coor_data[5]); input_mt_slot(ts->input_dev, id); input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); touchscreen_report_pos(ts->input_dev, &ts->prop, input_x, input_y, true); input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); } static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data) { int id = coor_data[1] & 0x0F; int input_x = get_unaligned_le16(&coor_data[3]); int input_y = get_unaligned_le16(&coor_data[5]); int input_w = get_unaligned_le16(&coor_data[7]); input_mt_slot(ts->input_dev, id); input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); touchscreen_report_pos(ts->input_dev, &ts->prop, input_x, input_y, true); input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); } static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data) { int touch_num; u8 key_value; int i; if (data[0] & GOODIX_HAVE_KEY) { touch_num = data[0] & 0x0f; key_value = data[1 + ts->contact_size * touch_num]; for (i = 0; i < GOODIX_MAX_KEYS; i++) if (key_value & BIT(i)) input_report_key(ts->input_dev, ts->keymap[i], 1); } else { for (i = 0; i < GOODIX_MAX_KEYS; i++) input_report_key(ts->input_dev, ts->keymap[i], 0); } } /** * goodix_process_events - Process incoming events * * @ts: our goodix_ts_data pointer * * Called when the IRQ is triggered. Read the current device state, and push * the input events to the user space. */ static void goodix_process_events(struct goodix_ts_data *ts) { u8 point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; int touch_num; int i; touch_num = goodix_ts_read_input_report(ts, point_data); if (touch_num < 0) return; goodix_ts_report_key(ts, point_data); for (i = 0; i < touch_num; i++) if (ts->contact_size == 9) goodix_ts_report_touch_9b(ts, &point_data[1 + ts->contact_size * i]); else goodix_ts_report_touch_8b(ts, &point_data[1 + ts->contact_size * i]); input_mt_sync_frame(ts->input_dev); input_sync(ts->input_dev); } /** * goodix_ts_irq_handler - The IRQ handler * * @irq: interrupt number. * @dev_id: private data pointer. */ static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) { struct goodix_ts_data *ts = dev_id; goodix_process_events(ts); if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0) dev_err(&ts->client->dev, "I2C write end_cmd error\n"); return IRQ_HANDLED; } static void goodix_free_irq(struct goodix_ts_data *ts) { devm_free_irq(&ts->client->dev, ts->client->irq, ts); } static int goodix_request_irq(struct goodix_ts_data *ts) { return devm_request_threaded_irq(&ts->client->dev, ts->client->irq, NULL, goodix_ts_irq_handler, ts->irq_flags, ts->client->name, ts); }