]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - drivers/input/touchscreen/elants_i2c.c
Input: elants_i2c - detect enum overflow
[mirror_ubuntu-jammy-kernel.git] / drivers / input / touchscreen / elants_i2c.c
index b0bd5bb079bec69cfb012caa6b0f86e1088cca6d..4c2b579f6c8bf35a07a22ca0d95db620defff79a 100644 (file)
@@ -56,6 +56,7 @@
 #define QUEUE_HEADER_SINGLE    0x62
 #define QUEUE_HEADER_NORMAL    0X63
 #define QUEUE_HEADER_WAIT      0x64
+#define QUEUE_HEADER_NORMAL2   0x66
 
 /* Command header definition */
 #define CMD_HEADER_WRITE       0x54
@@ -69,6 +70,7 @@
 #define CMD_HEADER_REK         0x66
 
 /* FW position data */
+#define PACKET_SIZE_OLD                40
 #define PACKET_SIZE            55
 #define MAX_CONTACT_NUM                10
 #define FW_POS_HEADER          0
@@ -82,7 +84,7 @@
 
 #define HEADER_REPORT_10_FINGER        0x62
 
-/* Header (4 bytes) plus 3 fill 10-finger packets */
+/* Header (4 bytes) plus 3 full 10-finger packets */
 #define MAX_PACKET_SIZE                169
 
 #define BOOT_TIME_DELAY_MS     50
 /* FW read command, 0x53 0x?? 0x0, 0x01 */
 #define E_ELAN_INFO_FW_VER     0x00
 #define E_ELAN_INFO_BC_VER     0x10
-#define E_ELAN_INFO_REK                0xE0
+#define E_ELAN_INFO_X_RES      0x60
+#define E_ELAN_INFO_Y_RES      0x63
+#define E_ELAN_INFO_REK                0xD0
 #define E_ELAN_INFO_TEST_VER   0xE0
 #define E_ELAN_INFO_FW_ID      0xF0
 #define E_INFO_OSR             0xD6
 #define E_INFO_PHY_SCAN                0xD7
 #define E_INFO_PHY_DRIVER      0xD8
 
+/* FW write command, 0x54 0x?? 0x0, 0x01 */
+#define E_POWER_STATE_SLEEP    0x50
+#define E_POWER_STATE_RESUME   0x58
+
 #define MAX_RETRIES            3
 #define MAX_FW_UPDATE_RETRIES  30
 
 #define ELAN_POWERON_DELAY_USEC        500
 #define ELAN_RESET_DELAY_MSEC  20
 
+enum elants_chip_id {
+       EKTH3500,
+       EKTF3624,
+};
+
 enum elants_state {
        ELAN_STATE_NORMAL,
        ELAN_WAIT_QUEUE_HEADER,
@@ -134,13 +147,17 @@ struct elants_data {
        u8 bc_version;
        u8 iap_version;
        u16 hw_version;
+       u8 major_res;
        unsigned int x_res;     /* resolution in units/mm */
        unsigned int y_res;
        unsigned int x_max;
        unsigned int y_max;
+       unsigned int phy_x;
+       unsigned int phy_y;
        struct touchscreen_properties prop;
 
        enum elants_state state;
+       enum elants_chip_id chip_id;
        enum elants_iap_mode iap_mode;
 
        /* Guards against concurrent access to the device via sysfs */
@@ -268,8 +285,8 @@ static int elants_i2c_calibrate(struct elants_data *ts)
 {
        struct i2c_client *client = ts->client;
        int ret, error;
-       static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A };
-       static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 };
+       static const u8 w_flashkey[] = { CMD_HEADER_WRITE, 0xC0, 0xE1, 0x5A };
+       static const u8 rek[] = { CMD_HEADER_WRITE, 0x29, 0x00, 0x01 };
        static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 };
 
        disable_irq(client->irq);
@@ -428,7 +445,51 @@ static int elants_i2c_query_bc_version(struct elants_data *ts)
        return 0;
 }
 
-static int elants_i2c_query_ts_info(struct elants_data *ts)
+static int elants_i2c_query_ts_info_ektf(struct elants_data *ts)
+{
+       struct i2c_client *client = ts->client;
+       int error;
+       u8 resp[4];
+       u16 phy_x, phy_y;
+       const u8 get_xres_cmd[] = {
+               CMD_HEADER_READ, E_ELAN_INFO_X_RES, 0x00, 0x00
+       };
+       const u8 get_yres_cmd[] = {
+               CMD_HEADER_READ, E_ELAN_INFO_Y_RES, 0x00, 0x00
+       };
+
+       /* Get X/Y size in mm */
+       error = elants_i2c_execute_command(client, get_xres_cmd,
+                                          sizeof(get_xres_cmd),
+                                          resp, sizeof(resp), 1,
+                                          "get X size");
+       if (error)
+               return error;
+
+       phy_x = resp[2] | ((resp[3] & 0xF0) << 4);
+
+       error = elants_i2c_execute_command(client, get_yres_cmd,
+                                          sizeof(get_yres_cmd),
+                                          resp, sizeof(resp), 1,
+                                          "get Y size");
+       if (error)
+               return error;
+
+       phy_y = resp[2] | ((resp[3] & 0xF0) << 4);
+
+       dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
+
+       ts->phy_x = phy_x;
+       ts->phy_y = phy_y;
+
+       /* eKTF doesn't report max size, set it to default values */
+       ts->x_max = 2240 - 1;
+       ts->y_max = 1408 - 1;
+
+       return 0;
+}
+
+static int elants_i2c_query_ts_info_ekth(struct elants_data *ts)
 {
        struct i2c_client *client = ts->client;
        int error;
@@ -459,6 +520,9 @@ static int elants_i2c_query_ts_info(struct elants_data *ts)
        rows = resp[2] + resp[6] + resp[10];
        cols = resp[3] + resp[7] + resp[11];
 
+       /* Get report resolution value of ABS_MT_TOUCH_MAJOR */
+       ts->major_res = resp[16];
+
        /* Process mm_to_pixel information */
        error = elants_i2c_execute_command(client,
                                           get_osr_cmd, sizeof(get_osr_cmd),
@@ -500,6 +564,8 @@ static int elants_i2c_query_ts_info(struct elants_data *ts)
                ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
                ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
                ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
+               ts->phy_x = phy_x;
+               ts->phy_y = phy_y;
        }
 
        return 0;
@@ -579,8 +645,19 @@ static int elants_i2c_initialize(struct elants_data *ts)
                error = elants_i2c_query_fw_version(ts);
        if (!error)
                error = elants_i2c_query_test_version(ts);
-       if (!error)
-               error = elants_i2c_query_ts_info(ts);
+
+       switch (ts->chip_id) {
+       case EKTH3500:
+               if (!error)
+                       error = elants_i2c_query_ts_info_ekth(ts);
+               break;
+       case EKTF3624:
+               if (!error)
+                       error = elants_i2c_query_ts_info_ektf(ts);
+               break;
+       default:
+               BUG();
+       }
 
        if (error)
                ts->iap_mode = ELAN_IAP_RECOVERY;
@@ -845,7 +922,8 @@ out:
  * Event reporting.
  */
 
-static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
+static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf,
+                               size_t packet_size)
 {
        struct input_dev *input = ts->input;
        unsigned int n_fingers;
@@ -872,8 +950,24 @@ static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
                        pos = &buf[FW_POS_XY + i * 3];
                        x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
                        y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
-                       p = buf[FW_POS_PRESSURE + i];
-                       w = buf[FW_POS_WIDTH + i];
+
+                       /*
+                        * eKTF3624 may have use "old" touch-report format,
+                        * depending on a device and TS firmware version.
+                        * For example, ASUS Transformer devices use the "old"
+                        * format, while ASUS Nexus 7 uses the "new" formant.
+                        */
+                       if (packet_size == PACKET_SIZE_OLD &&
+                           ts->chip_id == EKTF3624) {
+                               w = buf[FW_POS_WIDTH + i / 2];
+                               w >>= 4 * (~i & 1);
+                               w |= w << 4;
+                               w |= !w;
+                               p = w;
+                       } else {
+                               p = buf[FW_POS_PRESSURE + i];
+                               w = buf[FW_POS_WIDTH + i];
+                       }
 
                        dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
                                i, x, y, p, w);
@@ -905,7 +999,8 @@ static u8 elants_i2c_calculate_checksum(u8 *buf)
        return checksum;
 }
 
-static void elants_i2c_event(struct elants_data *ts, u8 *buf)
+static void elants_i2c_event(struct elants_data *ts, u8 *buf,
+                            size_t packet_size)
 {
        u8 checksum = elants_i2c_calculate_checksum(buf);
 
@@ -919,7 +1014,7 @@ static void elants_i2c_event(struct elants_data *ts, u8 *buf)
                         "%s: unknown packet type: %02x\n",
                         __func__, buf[FW_POS_HEADER]);
        else
-               elants_i2c_mt_event(ts, buf);
+               elants_i2c_mt_event(ts, buf, packet_size);
 }
 
 static irqreturn_t elants_i2c_irq(int irq, void *_dev)
@@ -962,7 +1057,6 @@ static irqreturn_t elants_i2c_irq(int irq, void *_dev)
                switch (ts->buf[FW_HDR_TYPE]) {
                case CMD_HEADER_HELLO:
                case CMD_HEADER_RESP:
-               case CMD_HEADER_REK:
                        break;
 
                case QUEUE_HEADER_WAIT:
@@ -977,9 +1071,24 @@ static irqreturn_t elants_i2c_irq(int irq, void *_dev)
                        break;
 
                case QUEUE_HEADER_SINGLE:
-                       elants_i2c_event(ts, &ts->buf[HEADER_SIZE]);
+                       elants_i2c_event(ts, &ts->buf[HEADER_SIZE],
+                                        ts->buf[FW_HDR_LENGTH]);
                        break;
 
+               case QUEUE_HEADER_NORMAL2: /* CMD_HEADER_REK */
+                       /*
+                        * Depending on firmware version, eKTF3624 touchscreens
+                        * may utilize one of these opcodes for the touch events:
+                        * 0x63 (NORMAL) and 0x66 (NORMAL2).  The 0x63 is used by
+                        * older firmware version and differs from 0x66 such that
+                        * touch pressure value needs to be adjusted.  The 0x66
+                        * opcode of newer firmware is equal to 0x63 of eKTH3500.
+                        */
+                       if (ts->chip_id != EKTF3624)
+                               break;
+
+                       fallthrough;
+
                case QUEUE_HEADER_NORMAL:
                        report_count = ts->buf[FW_HDR_COUNT];
                        if (report_count == 0 || report_count > 3) {
@@ -990,7 +1099,12 @@ static irqreturn_t elants_i2c_irq(int irq, void *_dev)
                        }
 
                        report_len = ts->buf[FW_HDR_LENGTH] / report_count;
-                       if (report_len != PACKET_SIZE) {
+
+                       if (report_len == PACKET_SIZE_OLD &&
+                           ts->chip_id == EKTF3624) {
+                               dev_dbg_once(&client->dev,
+                                            "using old report format\n");
+                       } else if (report_len != PACKET_SIZE) {
                                dev_err(&client->dev,
                                        "mismatching report length: %*ph\n",
                                        HEADER_SIZE, ts->buf);
@@ -999,8 +1113,8 @@ static irqreturn_t elants_i2c_irq(int irq, void *_dev)
 
                        for (i = 0; i < report_count; i++) {
                                u8 *buf = ts->buf + HEADER_SIZE +
-                                                       i * PACKET_SIZE;
-                               elants_i2c_event(ts, buf);
+                                                       i * report_len;
+                               elants_i2c_event(ts, buf, report_len);
                        }
                        break;
 
@@ -1242,6 +1356,7 @@ static int elants_i2c_probe(struct i2c_client *client,
        init_completion(&ts->cmd_done);
 
        ts->client = client;
+       ts->chip_id = (enum elants_chip_id)id->driver_data;
        i2c_set_clientdata(client, ts);
 
        ts->vcc33 = devm_regulator_get(&client->dev, "vcc33");
@@ -1323,11 +1438,20 @@ static int elants_i2c_probe(struct i2c_client *client,
        input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
        input_set_abs_params(ts->input, ABS_MT_TOOL_TYPE,
                             0, MT_TOOL_PALM, 0, 0);
-       input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
-       input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
 
        touchscreen_parse_properties(ts->input, true, &ts->prop);
 
+       if (ts->chip_id == EKTF3624) {
+               /* calculate resolution from size */
+               ts->x_res = DIV_ROUND_CLOSEST(ts->prop.max_x, ts->phy_x);
+               ts->y_res = DIV_ROUND_CLOSEST(ts->prop.max_y, ts->phy_y);
+       }
+
+       input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
+       input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
+       if (ts->major_res > 0)
+               input_abs_set_res(ts->input, ABS_MT_TOUCH_MAJOR, ts->major_res);
+
        error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
                                    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
        if (error) {
@@ -1382,7 +1506,9 @@ static int __maybe_unused elants_i2c_suspend(struct device *dev)
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct elants_data *ts = i2c_get_clientdata(client);
-       const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 };
+       const u8 set_sleep_cmd[] = {
+               CMD_HEADER_WRITE, E_POWER_STATE_SLEEP, 0x00, 0x01
+       };
        int retry_cnt;
        int error;
 
@@ -1419,7 +1545,9 @@ static int __maybe_unused elants_i2c_resume(struct device *dev)
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct elants_data *ts = i2c_get_clientdata(client);
-       const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 };
+       const u8 set_active_cmd[] = {
+               CMD_HEADER_WRITE, E_POWER_STATE_RESUME, 0x00, 0x01
+       };
        int retry_cnt;
        int error;
 
@@ -1452,14 +1580,16 @@ static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
                         elants_i2c_suspend, elants_i2c_resume);
 
 static const struct i2c_device_id elants_i2c_id[] = {
-       { DEVICE_NAME, 0 },
+       { DEVICE_NAME, EKTH3500 },
+       { "ekth3500", EKTH3500 },
+       { "ektf3624", EKTF3624 },
        { }
 };
 MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
 
 #ifdef CONFIG_ACPI
 static const struct acpi_device_id elants_acpi_id[] = {
-       { "ELAN0001", 0 },
+       { "ELAN0001", EKTH3500 },
        { }
 };
 MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
@@ -1468,6 +1598,7 @@ MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
 #ifdef CONFIG_OF
 static const struct of_device_id elants_of_match[] = {
        { .compatible = "elan,ekth3500" },
+       { .compatible = "elan,ektf3624" },
        { /* sentinel */ }
 };
 MODULE_DEVICE_TABLE(of, elants_of_match);