Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Goodix Touchscreens |
| 4 | * |
| 5 | * Copyright (c) 2014 Red Hat Inc. |
Karsten Merker | ad48cf5 | 2015-12-17 17:02:53 -0800 | [diff] [blame] | 6 | * Copyright (c) 2015 K. Merker <merker@debian.org> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 7 | * |
| 8 | * This code is based on gt9xx.c authored by andrew@goodix.com: |
| 9 | * |
| 10 | * 2010 - 2012 Goodix Technology. |
| 11 | */ |
| 12 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 13 | |
| 14 | #include <linux/kernel.h> |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 15 | #include <linux/dmi.h> |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 16 | #include <linux/firmware.h> |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 17 | #include <linux/gpio/consumer.h> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/input/mt.h> |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 21 | #include <linux/input/touchscreen.h> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/irq.h> |
| 25 | #include <linux/interrupt.h> |
Jagan Teki | ae97fb5 | 2019-04-03 15:20:26 -0700 | [diff] [blame] | 26 | #include <linux/regulator/consumer.h> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 27 | #include <linux/slab.h> |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 28 | #include <linux/acpi.h> |
| 29 | #include <linux/of.h> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
| 31 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 32 | struct goodix_ts_data; |
| 33 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 34 | enum goodix_irq_pin_access_method { |
| 35 | IRQ_PIN_ACCESS_NONE, |
| 36 | IRQ_PIN_ACCESS_GPIO, |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 37 | IRQ_PIN_ACCESS_ACPI_GPIO, |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 38 | IRQ_PIN_ACCESS_ACPI_METHOD, |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 41 | struct goodix_chip_data { |
| 42 | u16 config_addr; |
| 43 | int config_len; |
| 44 | int (*check_config)(struct goodix_ts_data *, const struct firmware *); |
| 45 | }; |
| 46 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 47 | struct goodix_ts_data { |
| 48 | struct i2c_client *client; |
| 49 | struct input_dev *input_dev; |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 50 | const struct goodix_chip_data *chip; |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 51 | struct touchscreen_properties prop; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 52 | unsigned int max_touch_num; |
| 53 | unsigned int int_trigger_type; |
Jagan Teki | ae97fb5 | 2019-04-03 15:20:26 -0700 | [diff] [blame] | 54 | struct regulator *avdd28; |
| 55 | struct regulator *vddio; |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 56 | struct gpio_desc *gpiod_int; |
| 57 | struct gpio_desc *gpiod_rst; |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 58 | int gpio_count; |
| 59 | int gpio_int_idx; |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 60 | u16 id; |
| 61 | u16 version; |
| 62 | const char *cfg_name; |
Hans de Goede | 1921dac | 2020-03-24 11:23:04 -0700 | [diff] [blame] | 63 | bool reset_controller_at_probe; |
Hans de Goede | e070a97b | 2020-03-24 11:12:35 -0700 | [diff] [blame] | 64 | bool load_cfg_from_disk; |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 65 | struct completion firmware_loading_complete; |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 66 | unsigned long irq_flags; |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 67 | enum goodix_irq_pin_access_method irq_pin_access_method; |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 68 | unsigned int contact_size; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 71 | #define GOODIX_GPIO_INT_NAME "irq" |
| 72 | #define GOODIX_GPIO_RST_NAME "reset" |
| 73 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 74 | #define GOODIX_MAX_HEIGHT 4096 |
| 75 | #define GOODIX_MAX_WIDTH 4096 |
| 76 | #define GOODIX_INT_TRIGGER 1 |
| 77 | #define GOODIX_CONTACT_SIZE 8 |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 78 | #define GOODIX_MAX_CONTACT_SIZE 9 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 79 | #define GOODIX_MAX_CONTACTS 10 |
| 80 | |
| 81 | #define GOODIX_CONFIG_MAX_LENGTH 240 |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 82 | #define GOODIX_CONFIG_911_LENGTH 186 |
| 83 | #define GOODIX_CONFIG_967_LENGTH 228 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 84 | |
| 85 | /* Register defines */ |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 86 | #define GOODIX_REG_COMMAND 0x8040 |
| 87 | #define GOODIX_CMD_SCREEN_OFF 0x05 |
| 88 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 89 | #define GOODIX_READ_COOR_ADDR 0x814E |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 90 | #define GOODIX_GT1X_REG_CONFIG_DATA 0x8050 |
| 91 | #define GOODIX_GT9X_REG_CONFIG_DATA 0x8047 |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 92 | #define GOODIX_REG_ID 0x8140 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 93 | |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 94 | #define GOODIX_BUFFER_STATUS_READY BIT(7) |
| 95 | #define GOODIX_BUFFER_STATUS_TIMEOUT 20 |
| 96 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 97 | #define RESOLUTION_LOC 1 |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 98 | #define MAX_CONTACTS_LOC 5 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 99 | #define TRIGGER_LOC 6 |
| 100 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 101 | static int goodix_check_cfg_8(struct goodix_ts_data *ts, |
| 102 | const struct firmware *cfg); |
| 103 | static int goodix_check_cfg_16(struct goodix_ts_data *ts, |
| 104 | const struct firmware *cfg); |
| 105 | |
| 106 | static const struct goodix_chip_data gt1x_chip_data = { |
| 107 | .config_addr = GOODIX_GT1X_REG_CONFIG_DATA, |
| 108 | .config_len = GOODIX_CONFIG_MAX_LENGTH, |
| 109 | .check_config = goodix_check_cfg_16, |
| 110 | }; |
| 111 | |
| 112 | static const struct goodix_chip_data gt911_chip_data = { |
| 113 | .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, |
| 114 | .config_len = GOODIX_CONFIG_911_LENGTH, |
| 115 | .check_config = goodix_check_cfg_8, |
| 116 | }; |
| 117 | |
| 118 | static const struct goodix_chip_data gt967_chip_data = { |
| 119 | .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, |
| 120 | .config_len = GOODIX_CONFIG_967_LENGTH, |
| 121 | .check_config = goodix_check_cfg_8, |
| 122 | }; |
| 123 | |
| 124 | static const struct goodix_chip_data gt9x_chip_data = { |
| 125 | .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, |
| 126 | .config_len = GOODIX_CONFIG_MAX_LENGTH, |
| 127 | .check_config = goodix_check_cfg_8, |
| 128 | }; |
| 129 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 130 | static const unsigned long goodix_irq_flags[] = { |
| 131 | IRQ_TYPE_EDGE_RISING, |
| 132 | IRQ_TYPE_EDGE_FALLING, |
| 133 | IRQ_TYPE_LEVEL_LOW, |
| 134 | IRQ_TYPE_LEVEL_HIGH, |
| 135 | }; |
| 136 | |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 137 | /* |
| 138 | * Those tablets have their coordinates origin at the bottom right |
| 139 | * of the tablet, as if rotated 180 degrees |
| 140 | */ |
| 141 | static const struct dmi_system_id rotated_screen[] = { |
| 142 | #if defined(CONFIG_DMI) && defined(CONFIG_X86) |
| 143 | { |
Hans de Goede | df5b5e5 | 2019-12-02 09:36:15 -0800 | [diff] [blame] | 144 | .ident = "Teclast X89", |
| 145 | .matches = { |
| 146 | /* tPAD is too generic, also match on bios date */ |
| 147 | DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), |
| 148 | DMI_MATCH(DMI_BOARD_NAME, "tPAD"), |
| 149 | DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"), |
| 150 | }, |
| 151 | }, |
| 152 | { |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 153 | .ident = "WinBook TW100", |
| 154 | .matches = { |
| 155 | DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), |
| 156 | DMI_MATCH(DMI_PRODUCT_NAME, "TW100") |
| 157 | } |
| 158 | }, |
| 159 | { |
| 160 | .ident = "WinBook TW700", |
| 161 | .matches = { |
| 162 | DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), |
| 163 | DMI_MATCH(DMI_PRODUCT_NAME, "TW700") |
| 164 | }, |
| 165 | }, |
| 166 | #endif |
| 167 | {} |
| 168 | }; |
| 169 | |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 170 | static const struct dmi_system_id nine_bytes_report[] = { |
| 171 | #if defined(CONFIG_DMI) && defined(CONFIG_X86) |
| 172 | { |
| 173 | .ident = "Lenovo YogaBook", |
| 174 | /* YB1-X91L/F and YB1-X90L/F */ |
| 175 | .matches = { |
| 176 | DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9") |
| 177 | } |
| 178 | }, |
| 179 | #endif |
| 180 | {} |
| 181 | }; |
| 182 | |
Sergei A. Trusov | 1dd5ddc | 2020-03-05 19:53:06 -0800 | [diff] [blame] | 183 | /* |
| 184 | * Those tablets have their x coordinate inverted |
| 185 | */ |
| 186 | static const struct dmi_system_id inverted_x_screen[] = { |
| 187 | #if defined(CONFIG_DMI) && defined(CONFIG_X86) |
| 188 | { |
| 189 | .ident = "Cube I15-TC", |
| 190 | .matches = { |
| 191 | DMI_MATCH(DMI_SYS_VENDOR, "Cube"), |
| 192 | DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC") |
| 193 | }, |
| 194 | }, |
| 195 | #endif |
| 196 | {} |
| 197 | }; |
| 198 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 199 | /** |
| 200 | * goodix_i2c_read - read data from a register of the i2c slave device. |
| 201 | * |
| 202 | * @client: i2c device. |
| 203 | * @reg: the register to read from. |
| 204 | * @buf: raw write data buffer. |
| 205 | * @len: length of the buffer to write |
| 206 | */ |
| 207 | static int goodix_i2c_read(struct i2c_client *client, |
Irina Tirdea | 0dfb35b | 2015-06-09 11:01:38 -0700 | [diff] [blame] | 208 | u16 reg, u8 *buf, int len) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 209 | { |
| 210 | struct i2c_msg msgs[2]; |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 211 | __be16 wbuf = cpu_to_be16(reg); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 212 | int ret; |
| 213 | |
| 214 | msgs[0].flags = 0; |
| 215 | msgs[0].addr = client->addr; |
| 216 | msgs[0].len = 2; |
Irina Tirdea | 0dfb35b | 2015-06-09 11:01:38 -0700 | [diff] [blame] | 217 | msgs[0].buf = (u8 *)&wbuf; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 218 | |
| 219 | msgs[1].flags = I2C_M_RD; |
| 220 | msgs[1].addr = client->addr; |
| 221 | msgs[1].len = len; |
| 222 | msgs[1].buf = buf; |
| 223 | |
| 224 | ret = i2c_transfer(client->adapter, msgs, 2); |
| 225 | return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0); |
| 226 | } |
| 227 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 228 | /** |
| 229 | * goodix_i2c_write - write data to a register of the i2c slave device. |
| 230 | * |
| 231 | * @client: i2c device. |
| 232 | * @reg: the register to write to. |
| 233 | * @buf: raw data buffer to write. |
| 234 | * @len: length of the buffer to write |
| 235 | */ |
| 236 | static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, |
| 237 | unsigned len) |
| 238 | { |
| 239 | u8 *addr_buf; |
| 240 | struct i2c_msg msg; |
| 241 | int ret; |
| 242 | |
| 243 | addr_buf = kmalloc(len + 2, GFP_KERNEL); |
| 244 | if (!addr_buf) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | addr_buf[0] = reg >> 8; |
| 248 | addr_buf[1] = reg & 0xFF; |
| 249 | memcpy(&addr_buf[2], buf, len); |
| 250 | |
| 251 | msg.flags = 0; |
| 252 | msg.addr = client->addr; |
| 253 | msg.buf = addr_buf; |
| 254 | msg.len = len + 2; |
| 255 | |
| 256 | ret = i2c_transfer(client->adapter, &msg, 1); |
| 257 | kfree(addr_buf); |
| 258 | return ret < 0 ? ret : (ret != 1 ? -EIO : 0); |
| 259 | } |
| 260 | |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 261 | static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value) |
| 262 | { |
| 263 | return goodix_i2c_write(client, reg, &value, sizeof(value)); |
| 264 | } |
| 265 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 266 | static const struct goodix_chip_data *goodix_get_chip_data(u16 id) |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 267 | { |
| 268 | switch (id) { |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 269 | case 1151: |
Jagan Teki | a5f50c5 | 2019-04-03 16:05:34 -0700 | [diff] [blame] | 270 | case 5663: |
Guido Günther | 84ef1b3 | 2019-02-16 23:03:13 -0800 | [diff] [blame] | 271 | case 5688: |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 272 | return >1x_chip_data; |
| 273 | |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 274 | case 911: |
| 275 | case 9271: |
| 276 | case 9110: |
| 277 | case 927: |
| 278 | case 928: |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 279 | return >911_chip_data; |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 280 | |
| 281 | case 912: |
| 282 | case 967: |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 283 | return >967_chip_data; |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 284 | |
| 285 | default: |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 286 | return >9x_chip_data; |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 290 | static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data) |
| 291 | { |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 292 | unsigned long max_timeout; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 293 | int touch_num; |
| 294 | int error; |
| 295 | |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 296 | /* |
| 297 | * The 'buffer status' bit, which indicates that the data is valid, is |
| 298 | * not set as soon as the interrupt is raised, but slightly after. |
| 299 | * This takes around 10 ms to happen, so we poll for 20 ms. |
| 300 | */ |
| 301 | max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT); |
| 302 | do { |
| 303 | error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 304 | data, ts->contact_size + 1); |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 305 | if (error) { |
| 306 | dev_err(&ts->client->dev, "I2C transfer error: %d\n", |
| 307 | error); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 308 | return error; |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 309 | } |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 310 | |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 311 | if (data[0] & GOODIX_BUFFER_STATUS_READY) { |
| 312 | touch_num = data[0] & 0x0f; |
| 313 | if (touch_num > ts->max_touch_num) |
| 314 | return -EPROTO; |
| 315 | |
| 316 | if (touch_num > 1) { |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 317 | data += 1 + ts->contact_size; |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 318 | error = goodix_i2c_read(ts->client, |
| 319 | GOODIX_READ_COOR_ADDR + |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 320 | 1 + ts->contact_size, |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 321 | data, |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 322 | ts->contact_size * |
Paul Cercueil | 9b5db7aa | 2017-10-13 11:04:48 -0700 | [diff] [blame] | 323 | (touch_num - 1)); |
| 324 | if (error) |
| 325 | return error; |
| 326 | } |
| 327 | |
| 328 | return touch_num; |
| 329 | } |
| 330 | |
| 331 | usleep_range(1000, 2000); /* Poll every 1 - 2 ms */ |
| 332 | } while (time_before(jiffies, max_timeout)); |
| 333 | |
| 334 | /* |
| 335 | * The Goodix panel will send spurious interrupts after a |
| 336 | * 'finger up' event, which will always cause a timeout. |
| 337 | */ |
| 338 | return 0; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 341 | static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 342 | { |
| 343 | int id = coor_data[0] & 0x0F; |
| 344 | int input_x = get_unaligned_le16(&coor_data[1]); |
| 345 | int input_y = get_unaligned_le16(&coor_data[3]); |
| 346 | int input_w = get_unaligned_le16(&coor_data[5]); |
| 347 | |
| 348 | input_mt_slot(ts->input_dev, id); |
| 349 | input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 350 | touchscreen_report_pos(ts->input_dev, &ts->prop, |
| 351 | input_x, input_y, true); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 352 | input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); |
| 353 | input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); |
| 354 | } |
| 355 | |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 356 | static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data) |
| 357 | { |
| 358 | int id = coor_data[1] & 0x0F; |
| 359 | int input_x = get_unaligned_le16(&coor_data[3]); |
| 360 | int input_y = get_unaligned_le16(&coor_data[5]); |
| 361 | int input_w = get_unaligned_le16(&coor_data[7]); |
| 362 | |
| 363 | input_mt_slot(ts->input_dev, id); |
| 364 | input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); |
| 365 | touchscreen_report_pos(ts->input_dev, &ts->prop, |
| 366 | input_x, input_y, true); |
| 367 | input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); |
| 368 | input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); |
| 369 | } |
| 370 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 371 | /** |
| 372 | * goodix_process_events - Process incoming events |
| 373 | * |
| 374 | * @ts: our goodix_ts_data pointer |
| 375 | * |
| 376 | * Called when the IRQ is triggered. Read the current device state, and push |
| 377 | * the input events to the user space. |
| 378 | */ |
| 379 | static void goodix_process_events(struct goodix_ts_data *ts) |
| 380 | { |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 381 | u8 point_data[1 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 382 | int touch_num; |
| 383 | int i; |
| 384 | |
| 385 | touch_num = goodix_ts_read_input_report(ts, point_data); |
| 386 | if (touch_num < 0) |
| 387 | return; |
| 388 | |
Sergei A. Trusov | 4a54fee | 2017-09-06 17:29:24 -0700 | [diff] [blame] | 389 | /* |
| 390 | * Bit 4 of the first byte reports the status of the capacitive |
| 391 | * Windows/Home button. |
| 392 | */ |
| 393 | input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4)); |
| 394 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 395 | for (i = 0; i < touch_num; i++) |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 396 | if (ts->contact_size == 9) |
| 397 | goodix_ts_report_touch_9b(ts, |
| 398 | &point_data[1 + ts->contact_size * i]); |
| 399 | else |
| 400 | goodix_ts_report_touch_8b(ts, |
| 401 | &point_data[1 + ts->contact_size * i]); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 402 | |
| 403 | input_mt_sync_frame(ts->input_dev); |
| 404 | input_sync(ts->input_dev); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * goodix_ts_irq_handler - The IRQ handler |
| 409 | * |
| 410 | * @irq: interrupt number. |
| 411 | * @dev_id: private data pointer. |
| 412 | */ |
| 413 | static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) |
| 414 | { |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 415 | struct goodix_ts_data *ts = dev_id; |
| 416 | |
| 417 | goodix_process_events(ts); |
| 418 | |
Irina Tirdea | 5d655b3 | 2015-12-17 16:47:42 -0800 | [diff] [blame] | 419 | if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 420 | dev_err(&ts->client->dev, "I2C write end_cmd error\n"); |
| 421 | |
| 422 | return IRQ_HANDLED; |
| 423 | } |
| 424 | |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 425 | static void goodix_free_irq(struct goodix_ts_data *ts) |
| 426 | { |
| 427 | devm_free_irq(&ts->client->dev, ts->client->irq, ts); |
| 428 | } |
| 429 | |
| 430 | static int goodix_request_irq(struct goodix_ts_data *ts) |
| 431 | { |
| 432 | return devm_request_threaded_irq(&ts->client->dev, ts->client->irq, |
| 433 | NULL, goodix_ts_irq_handler, |
| 434 | ts->irq_flags, ts->client->name, ts); |
| 435 | } |
| 436 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 437 | static int goodix_check_cfg_8(struct goodix_ts_data *ts, |
| 438 | const struct firmware *cfg) |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 439 | { |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 440 | int i, raw_cfg_len = cfg->size - 2; |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 441 | u8 check_sum = 0; |
| 442 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 443 | for (i = 0; i < raw_cfg_len; i++) |
| 444 | check_sum += cfg->data[i]; |
| 445 | check_sum = (~check_sum) + 1; |
| 446 | if (check_sum != cfg->data[raw_cfg_len]) { |
| 447 | dev_err(&ts->client->dev, |
| 448 | "The checksum of the config fw is not correct"); |
| 449 | return -EINVAL; |
| 450 | } |
| 451 | |
| 452 | if (cfg->data[raw_cfg_len + 1] != 1) { |
| 453 | dev_err(&ts->client->dev, |
| 454 | "Config fw must have Config_Fresh register set"); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 461 | static int goodix_check_cfg_16(struct goodix_ts_data *ts, |
| 462 | const struct firmware *cfg) |
| 463 | { |
| 464 | int i, raw_cfg_len = cfg->size - 3; |
| 465 | u16 check_sum = 0; |
| 466 | |
| 467 | for (i = 0; i < raw_cfg_len; i += 2) |
| 468 | check_sum += get_unaligned_be16(&cfg->data[i]); |
| 469 | check_sum = (~check_sum) + 1; |
| 470 | if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) { |
| 471 | dev_err(&ts->client->dev, |
| 472 | "The checksum of the config fw is not correct"); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | if (cfg->data[raw_cfg_len + 2] != 1) { |
| 477 | dev_err(&ts->client->dev, |
| 478 | "Config fw must have Config_Fresh register set"); |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * goodix_check_cfg - Checks if config fw is valid |
| 487 | * |
| 488 | * @ts: goodix_ts_data pointer |
| 489 | * @cfg: firmware config data |
| 490 | */ |
| 491 | static int goodix_check_cfg(struct goodix_ts_data *ts, |
| 492 | const struct firmware *cfg) |
| 493 | { |
| 494 | if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) { |
| 495 | dev_err(&ts->client->dev, |
| 496 | "The length of the config fw is not correct"); |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
| 500 | return ts->chip->check_config(ts, cfg); |
| 501 | } |
| 502 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 503 | /** |
| 504 | * goodix_send_cfg - Write fw config to device |
| 505 | * |
| 506 | * @ts: goodix_ts_data pointer |
| 507 | * @cfg: config firmware to write to device |
| 508 | */ |
| 509 | static int goodix_send_cfg(struct goodix_ts_data *ts, |
| 510 | const struct firmware *cfg) |
| 511 | { |
| 512 | int error; |
| 513 | |
| 514 | error = goodix_check_cfg(ts, cfg); |
| 515 | if (error) |
| 516 | return error; |
| 517 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 518 | error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data, |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 519 | cfg->size); |
| 520 | if (error) { |
| 521 | dev_err(&ts->client->dev, "Failed to write config data: %d", |
| 522 | error); |
| 523 | return error; |
| 524 | } |
| 525 | dev_dbg(&ts->client->dev, "Config sent successfully."); |
| 526 | |
| 527 | /* Let the firmware reconfigure itself, so sleep for 10ms */ |
| 528 | usleep_range(10000, 11000); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 533 | static int goodix_irq_direction_output(struct goodix_ts_data *ts, |
| 534 | int value) |
| 535 | { |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 536 | struct device *dev = &ts->client->dev; |
| 537 | acpi_status status; |
| 538 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 539 | switch (ts->irq_pin_access_method) { |
| 540 | case IRQ_PIN_ACCESS_NONE: |
| 541 | dev_err(&ts->client->dev, |
| 542 | "%s called without an irq_pin_access_method set\n", |
| 543 | __func__); |
| 544 | return -EINVAL; |
| 545 | case IRQ_PIN_ACCESS_GPIO: |
| 546 | return gpiod_direction_output(ts->gpiod_int, value); |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 547 | case IRQ_PIN_ACCESS_ACPI_GPIO: |
| 548 | /* |
| 549 | * The IRQ pin triggers on a falling edge, so its gets marked |
| 550 | * as active-low, use output_raw to avoid the value inversion. |
| 551 | */ |
| 552 | return gpiod_direction_output_raw(ts->gpiod_int, value); |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 553 | case IRQ_PIN_ACCESS_ACPI_METHOD: |
| 554 | status = acpi_execute_simple_method(ACPI_HANDLE(dev), |
| 555 | "INTO", value); |
| 556 | return ACPI_SUCCESS(status) ? 0 : -EIO; |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | return -EINVAL; /* Never reached */ |
| 560 | } |
| 561 | |
| 562 | static int goodix_irq_direction_input(struct goodix_ts_data *ts) |
| 563 | { |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 564 | struct device *dev = &ts->client->dev; |
| 565 | acpi_status status; |
| 566 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 567 | switch (ts->irq_pin_access_method) { |
| 568 | case IRQ_PIN_ACCESS_NONE: |
| 569 | dev_err(&ts->client->dev, |
| 570 | "%s called without an irq_pin_access_method set\n", |
| 571 | __func__); |
| 572 | return -EINVAL; |
| 573 | case IRQ_PIN_ACCESS_GPIO: |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 574 | case IRQ_PIN_ACCESS_ACPI_GPIO: |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 575 | return gpiod_direction_input(ts->gpiod_int); |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 576 | case IRQ_PIN_ACCESS_ACPI_METHOD: |
| 577 | status = acpi_evaluate_object(ACPI_HANDLE(dev), "INTI", |
| 578 | NULL, NULL); |
| 579 | return ACPI_SUCCESS(status) ? 0 : -EIO; |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | return -EINVAL; /* Never reached */ |
| 583 | } |
| 584 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 585 | static int goodix_int_sync(struct goodix_ts_data *ts) |
| 586 | { |
| 587 | int error; |
| 588 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 589 | error = goodix_irq_direction_output(ts, 0); |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 590 | if (error) |
| 591 | return error; |
| 592 | |
| 593 | msleep(50); /* T5: 50ms */ |
| 594 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 595 | error = goodix_irq_direction_input(ts); |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 596 | if (error) |
| 597 | return error; |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * goodix_reset - Reset device during power on |
| 604 | * |
| 605 | * @ts: goodix_ts_data pointer |
| 606 | */ |
| 607 | static int goodix_reset(struct goodix_ts_data *ts) |
| 608 | { |
| 609 | int error; |
| 610 | |
| 611 | /* begin select I2C slave addr */ |
| 612 | error = gpiod_direction_output(ts->gpiod_rst, 0); |
| 613 | if (error) |
| 614 | return error; |
| 615 | |
| 616 | msleep(20); /* T2: > 10ms */ |
| 617 | |
| 618 | /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */ |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 619 | error = goodix_irq_direction_output(ts, ts->client->addr == 0x14); |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 620 | if (error) |
| 621 | return error; |
| 622 | |
| 623 | usleep_range(100, 2000); /* T3: > 100us */ |
| 624 | |
| 625 | error = gpiod_direction_output(ts->gpiod_rst, 1); |
| 626 | if (error) |
| 627 | return error; |
| 628 | |
| 629 | usleep_range(6000, 10000); /* T4: > 5ms */ |
| 630 | |
| 631 | /* end select I2C slave addr */ |
| 632 | error = gpiod_direction_input(ts->gpiod_rst); |
| 633 | if (error) |
| 634 | return error; |
| 635 | |
| 636 | error = goodix_int_sync(ts); |
| 637 | if (error) |
| 638 | return error; |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 643 | #if defined CONFIG_X86 && defined CONFIG_ACPI |
Hans de Goede | 67abd9e | 2020-03-24 11:23:35 -0700 | [diff] [blame] | 644 | #include <asm/cpu_device_id.h> |
| 645 | #include <asm/intel-family.h> |
| 646 | |
| 647 | static const struct x86_cpu_id baytrail_cpu_ids[] = { |
| 648 | { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, X86_FEATURE_ANY, }, |
| 649 | {} |
| 650 | }; |
| 651 | |
| 652 | static inline bool is_byt(void) |
| 653 | { |
| 654 | const struct x86_cpu_id *id = x86_match_cpu(baytrail_cpu_ids); |
| 655 | |
| 656 | return !!id; |
| 657 | } |
| 658 | |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 659 | static const struct acpi_gpio_params first_gpio = { 0, 0, false }; |
| 660 | static const struct acpi_gpio_params second_gpio = { 1, 0, false }; |
| 661 | |
| 662 | static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = { |
| 663 | { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 }, |
| 664 | { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 }, |
| 665 | { }, |
| 666 | }; |
| 667 | |
| 668 | static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = { |
| 669 | { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 }, |
| 670 | { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 }, |
| 671 | { }, |
| 672 | }; |
| 673 | |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 674 | static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = { |
| 675 | { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 }, |
| 676 | { }, |
| 677 | }; |
| 678 | |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 679 | static int goodix_resource(struct acpi_resource *ares, void *data) |
| 680 | { |
| 681 | struct goodix_ts_data *ts = data; |
| 682 | struct device *dev = &ts->client->dev; |
| 683 | struct acpi_resource_gpio *gpio; |
| 684 | |
| 685 | switch (ares->type) { |
| 686 | case ACPI_RESOURCE_TYPE_GPIO: |
| 687 | gpio = &ares->data.gpio; |
| 688 | if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) { |
| 689 | if (ts->gpio_int_idx == -1) { |
| 690 | ts->gpio_int_idx = ts->gpio_count; |
| 691 | } else { |
| 692 | dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n"); |
| 693 | ts->gpio_int_idx = -2; |
| 694 | } |
| 695 | } |
| 696 | ts->gpio_count++; |
| 697 | break; |
| 698 | default: |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * This function gets called in case we fail to get the irq GPIO directly |
| 707 | * because the ACPI tables lack GPIO-name to APCI _CRS index mappings |
| 708 | * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data). |
| 709 | * In that case we add our own mapping and then goodix_get_gpio_config() |
| 710 | * retries to get the GPIOs based on the added mapping. |
| 711 | */ |
| 712 | static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts) |
| 713 | { |
| 714 | const struct acpi_gpio_mapping *gpio_mapping = NULL; |
| 715 | struct device *dev = &ts->client->dev; |
| 716 | LIST_HEAD(resources); |
| 717 | int ret; |
| 718 | |
| 719 | ts->gpio_count = 0; |
| 720 | ts->gpio_int_idx = -1; |
| 721 | ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources, |
| 722 | goodix_resource, ts); |
| 723 | if (ret < 0) { |
| 724 | dev_err(dev, "Error getting ACPI resources: %d\n", ret); |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | acpi_dev_free_resource_list(&resources); |
| 729 | |
| 730 | if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) { |
| 731 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO; |
| 732 | gpio_mapping = acpi_goodix_int_first_gpios; |
| 733 | } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) { |
| 734 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO; |
| 735 | gpio_mapping = acpi_goodix_int_last_gpios; |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 736 | } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 && |
| 737 | acpi_has_method(ACPI_HANDLE(dev), "INTI") && |
| 738 | acpi_has_method(ACPI_HANDLE(dev), "INTO")) { |
| 739 | dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n"); |
| 740 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD; |
| 741 | gpio_mapping = acpi_goodix_reset_only_gpios; |
Hans de Goede | 67abd9e | 2020-03-24 11:23:35 -0700 | [diff] [blame] | 742 | } else if (is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) { |
| 743 | dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n"); |
| 744 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO; |
| 745 | gpio_mapping = acpi_goodix_int_last_gpios; |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 746 | } else { |
| 747 | dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n", |
| 748 | ts->gpio_count, ts->gpio_int_idx); |
| 749 | return -EINVAL; |
| 750 | } |
| 751 | |
| 752 | return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping); |
| 753 | } |
| 754 | #else |
| 755 | static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts) |
| 756 | { |
| 757 | return -EINVAL; |
| 758 | } |
| 759 | #endif /* CONFIG_X86 && CONFIG_ACPI */ |
| 760 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 761 | /** |
| 762 | * goodix_get_gpio_config - Get GPIO config from ACPI/DT |
| 763 | * |
| 764 | * @ts: goodix_ts_data pointer |
| 765 | */ |
| 766 | static int goodix_get_gpio_config(struct goodix_ts_data *ts) |
| 767 | { |
| 768 | int error; |
| 769 | struct device *dev; |
| 770 | struct gpio_desc *gpiod; |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 771 | bool added_acpi_mappings = false; |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 772 | |
| 773 | if (!ts->client) |
| 774 | return -EINVAL; |
| 775 | dev = &ts->client->dev; |
| 776 | |
Jagan Teki | ae97fb5 | 2019-04-03 15:20:26 -0700 | [diff] [blame] | 777 | ts->avdd28 = devm_regulator_get(dev, "AVDD28"); |
| 778 | if (IS_ERR(ts->avdd28)) { |
| 779 | error = PTR_ERR(ts->avdd28); |
| 780 | if (error != -EPROBE_DEFER) |
| 781 | dev_err(dev, |
| 782 | "Failed to get AVDD28 regulator: %d\n", error); |
| 783 | return error; |
| 784 | } |
| 785 | |
| 786 | ts->vddio = devm_regulator_get(dev, "VDDIO"); |
| 787 | if (IS_ERR(ts->vddio)) { |
| 788 | error = PTR_ERR(ts->vddio); |
| 789 | if (error != -EPROBE_DEFER) |
| 790 | dev_err(dev, |
| 791 | "Failed to get VDDIO regulator: %d\n", error); |
| 792 | return error; |
| 793 | } |
| 794 | |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 795 | retry_get_irq_gpio: |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 796 | /* Get the interrupt GPIO pin number */ |
| 797 | gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN); |
| 798 | if (IS_ERR(gpiod)) { |
| 799 | error = PTR_ERR(gpiod); |
| 800 | if (error != -EPROBE_DEFER) |
| 801 | dev_dbg(dev, "Failed to get %s GPIO: %d\n", |
| 802 | GOODIX_GPIO_INT_NAME, error); |
| 803 | return error; |
| 804 | } |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 805 | if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) { |
| 806 | added_acpi_mappings = true; |
| 807 | if (goodix_add_acpi_gpio_mappings(ts) == 0) |
| 808 | goto retry_get_irq_gpio; |
| 809 | } |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 810 | |
| 811 | ts->gpiod_int = gpiod; |
| 812 | |
| 813 | /* Get the reset line GPIO pin number */ |
| 814 | gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN); |
| 815 | if (IS_ERR(gpiod)) { |
| 816 | error = PTR_ERR(gpiod); |
| 817 | if (error != -EPROBE_DEFER) |
| 818 | dev_dbg(dev, "Failed to get %s GPIO: %d\n", |
| 819 | GOODIX_GPIO_RST_NAME, error); |
| 820 | return error; |
| 821 | } |
| 822 | |
| 823 | ts->gpiod_rst = gpiod; |
| 824 | |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 825 | switch (ts->irq_pin_access_method) { |
| 826 | case IRQ_PIN_ACCESS_ACPI_GPIO: |
| 827 | /* |
| 828 | * We end up here if goodix_add_acpi_gpio_mappings() has |
| 829 | * called devm_acpi_dev_add_driver_gpios() because the ACPI |
| 830 | * tables did not contain name to index mappings. |
| 831 | * Check that we successfully got both GPIOs after we've |
| 832 | * added our own acpi_gpio_mapping and if we did not get both |
| 833 | * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE. |
| 834 | */ |
| 835 | if (!ts->gpiod_int || !ts->gpiod_rst) |
| 836 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE; |
| 837 | break; |
Hans de Goede | c5fca48 | 2020-03-24 11:24:31 -0700 | [diff] [blame^] | 838 | case IRQ_PIN_ACCESS_ACPI_METHOD: |
| 839 | if (!ts->gpiod_rst) |
| 840 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE; |
| 841 | break; |
Hans de Goede | a7d4b17 | 2020-03-24 11:23:21 -0700 | [diff] [blame] | 842 | default: |
| 843 | if (ts->gpiod_int && ts->gpiod_rst) { |
| 844 | ts->reset_controller_at_probe = true; |
| 845 | ts->load_cfg_from_disk = true; |
| 846 | ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO; |
| 847 | } |
Hans de Goede | e070a97b | 2020-03-24 11:12:35 -0700 | [diff] [blame] | 848 | } |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 849 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 853 | /** |
| 854 | * goodix_read_config - Read the embedded configuration of the panel |
| 855 | * |
| 856 | * @ts: our goodix_ts_data pointer |
| 857 | * |
| 858 | * Must be called during probe |
| 859 | */ |
| 860 | static void goodix_read_config(struct goodix_ts_data *ts) |
| 861 | { |
| 862 | u8 config[GOODIX_CONFIG_MAX_LENGTH]; |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 863 | int x_max, y_max; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 864 | int error; |
| 865 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 866 | error = goodix_i2c_read(ts->client, ts->chip->config_addr, |
| 867 | config, ts->chip->config_len); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 868 | if (error) { |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 869 | dev_warn(&ts->client->dev, "Error reading config: %d\n", |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 870 | error); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 871 | ts->int_trigger_type = GOODIX_INT_TRIGGER; |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 872 | ts->max_touch_num = GOODIX_MAX_CONTACTS; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 876 | ts->int_trigger_type = config[TRIGGER_LOC] & 0x03; |
| 877 | ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f; |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 878 | |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 879 | x_max = get_unaligned_le16(&config[RESOLUTION_LOC]); |
| 880 | y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]); |
| 881 | if (x_max && y_max) { |
| 882 | input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1); |
| 883 | input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1); |
Karsten Merker | 57c80e8 | 2015-12-17 17:08:31 -0800 | [diff] [blame] | 884 | } |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 887 | /** |
| 888 | * goodix_read_version - Read goodix touchscreen version |
| 889 | * |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 890 | * @ts: our goodix_ts_data pointer |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 891 | */ |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 892 | static int goodix_read_version(struct goodix_ts_data *ts) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 893 | { |
| 894 | int error; |
| 895 | u8 buf[6]; |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 896 | char id_str[5]; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 897 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 898 | error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf)); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 899 | if (error) { |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 900 | dev_err(&ts->client->dev, "read version failed: %d\n", error); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 901 | return error; |
| 902 | } |
| 903 | |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 904 | memcpy(id_str, buf, 4); |
| 905 | id_str[4] = 0; |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 906 | if (kstrtou16(id_str, 10, &ts->id)) |
| 907 | ts->id = 0x1001; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 908 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 909 | ts->version = get_unaligned_le16(&buf[4]); |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 910 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 911 | dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id, |
| 912 | ts->version); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * goodix_i2c_test - I2C test function to check if the device answers. |
| 919 | * |
| 920 | * @client: the i2c client |
| 921 | */ |
| 922 | static int goodix_i2c_test(struct i2c_client *client) |
| 923 | { |
| 924 | int retry = 0; |
| 925 | int error; |
| 926 | u8 test; |
| 927 | |
| 928 | while (retry++ < 2) { |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 929 | error = goodix_i2c_read(client, GOODIX_REG_ID, |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 930 | &test, 1); |
| 931 | if (!error) |
| 932 | return 0; |
| 933 | |
| 934 | dev_err(&client->dev, "i2c test failed attempt %d: %d\n", |
| 935 | retry, error); |
| 936 | msleep(20); |
| 937 | } |
| 938 | |
| 939 | return error; |
| 940 | } |
| 941 | |
| 942 | /** |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 943 | * goodix_configure_dev - Finish device initialization |
| 944 | * |
| 945 | * @ts: our goodix_ts_data pointer |
| 946 | * |
| 947 | * Must be called from probe to finish initialization of the device. |
| 948 | * Contains the common initialization code for both devices that |
| 949 | * declare gpio pins and devices that do not. It is either called |
| 950 | * directly from probe or from request_firmware_wait callback. |
| 951 | */ |
| 952 | static int goodix_configure_dev(struct goodix_ts_data *ts) |
| 953 | { |
| 954 | int error; |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 955 | |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 956 | ts->int_trigger_type = GOODIX_INT_TRIGGER; |
| 957 | ts->max_touch_num = GOODIX_MAX_CONTACTS; |
Karsten Merker | ad48cf5 | 2015-12-17 17:02:53 -0800 | [diff] [blame] | 958 | |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 959 | ts->input_dev = devm_input_allocate_device(&ts->client->dev); |
| 960 | if (!ts->input_dev) { |
| 961 | dev_err(&ts->client->dev, "Failed to allocate input device."); |
| 962 | return -ENOMEM; |
| 963 | } |
| 964 | |
| 965 | ts->input_dev->name = "Goodix Capacitive TouchScreen"; |
| 966 | ts->input_dev->phys = "input/ts"; |
| 967 | ts->input_dev->id.bustype = BUS_I2C; |
| 968 | ts->input_dev->id.vendor = 0x0416; |
| 969 | ts->input_dev->id.product = ts->id; |
| 970 | ts->input_dev->id.version = ts->version; |
| 971 | |
| 972 | /* Capacitive Windows/Home button on some devices */ |
| 973 | input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA); |
| 974 | |
| 975 | input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X); |
| 976 | input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y); |
| 977 | input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); |
| 978 | input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); |
| 979 | |
| 980 | /* Read configuration and apply touchscreen parameters */ |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 981 | goodix_read_config(ts); |
| 982 | |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 983 | /* Try overriding touchscreen parameters via device properties */ |
| 984 | touchscreen_parse_properties(ts->input_dev, true, &ts->prop); |
| 985 | |
| 986 | if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) { |
Guido Günther | c1c00aa | 2019-02-16 23:04:43 -0800 | [diff] [blame] | 987 | dev_err(&ts->client->dev, |
| 988 | "Invalid config (%d, %d, %d), using defaults\n", |
| 989 | ts->prop.max_x, ts->prop.max_y, ts->max_touch_num); |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 990 | ts->prop.max_x = GOODIX_MAX_WIDTH - 1; |
| 991 | ts->prop.max_y = GOODIX_MAX_HEIGHT - 1; |
| 992 | ts->max_touch_num = GOODIX_MAX_CONTACTS; |
| 993 | input_abs_set_max(ts->input_dev, |
| 994 | ABS_MT_POSITION_X, ts->prop.max_x); |
| 995 | input_abs_set_max(ts->input_dev, |
| 996 | ABS_MT_POSITION_Y, ts->prop.max_y); |
| 997 | } |
| 998 | |
| 999 | if (dmi_check_system(rotated_screen)) { |
| 1000 | ts->prop.invert_x = true; |
| 1001 | ts->prop.invert_y = true; |
| 1002 | dev_dbg(&ts->client->dev, |
| 1003 | "Applying '180 degrees rotated screen' quirk\n"); |
| 1004 | } |
| 1005 | |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 1006 | if (dmi_check_system(nine_bytes_report)) { |
| 1007 | ts->contact_size = 9; |
| 1008 | |
| 1009 | dev_dbg(&ts->client->dev, |
| 1010 | "Non-standard 9-bytes report format quirk\n"); |
| 1011 | } |
| 1012 | |
Sergei A. Trusov | 1dd5ddc | 2020-03-05 19:53:06 -0800 | [diff] [blame] | 1013 | if (dmi_check_system(inverted_x_screen)) { |
| 1014 | ts->prop.invert_x = true; |
| 1015 | dev_dbg(&ts->client->dev, |
| 1016 | "Applying 'inverted x screen' quirk\n"); |
| 1017 | } |
| 1018 | |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 1019 | error = input_mt_init_slots(ts->input_dev, ts->max_touch_num, |
| 1020 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); |
| 1021 | if (error) { |
| 1022 | dev_err(&ts->client->dev, |
| 1023 | "Failed to initialize MT slots: %d", error); |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1024 | return error; |
Marcin Niestroj | fafef98 | 2018-01-26 11:08:59 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | error = input_register_device(ts->input_dev); |
| 1028 | if (error) { |
| 1029 | dev_err(&ts->client->dev, |
| 1030 | "Failed to register input device: %d", error); |
| 1031 | return error; |
| 1032 | } |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1033 | |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1034 | ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT; |
| 1035 | error = goodix_request_irq(ts); |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1036 | if (error) { |
| 1037 | dev_err(&ts->client->dev, "request IRQ failed: %d\n", error); |
| 1038 | return error; |
| 1039 | } |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * goodix_config_cb - Callback to finish device init |
| 1046 | * |
| 1047 | * @ts: our goodix_ts_data pointer |
| 1048 | * |
| 1049 | * request_firmware_wait callback that finishes |
| 1050 | * initialization of the device. |
| 1051 | */ |
| 1052 | static void goodix_config_cb(const struct firmware *cfg, void *ctx) |
| 1053 | { |
| 1054 | struct goodix_ts_data *ts = ctx; |
| 1055 | int error; |
| 1056 | |
| 1057 | if (cfg) { |
| 1058 | /* send device configuration to the firmware */ |
| 1059 | error = goodix_send_cfg(ts, cfg); |
| 1060 | if (error) |
| 1061 | goto err_release_cfg; |
| 1062 | } |
| 1063 | |
| 1064 | goodix_configure_dev(ts); |
| 1065 | |
| 1066 | err_release_cfg: |
| 1067 | release_firmware(cfg); |
| 1068 | complete_all(&ts->firmware_loading_complete); |
| 1069 | } |
| 1070 | |
Jagan Teki | ae97fb5 | 2019-04-03 15:20:26 -0700 | [diff] [blame] | 1071 | static void goodix_disable_regulators(void *arg) |
| 1072 | { |
| 1073 | struct goodix_ts_data *ts = arg; |
| 1074 | |
| 1075 | regulator_disable(ts->vddio); |
| 1076 | regulator_disable(ts->avdd28); |
| 1077 | } |
| 1078 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1079 | static int goodix_ts_probe(struct i2c_client *client, |
| 1080 | const struct i2c_device_id *id) |
| 1081 | { |
| 1082 | struct goodix_ts_data *ts; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1083 | int error; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1084 | |
| 1085 | dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr); |
| 1086 | |
| 1087 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 1088 | dev_err(&client->dev, "I2C check functionality failed.\n"); |
| 1089 | return -ENXIO; |
| 1090 | } |
| 1091 | |
| 1092 | ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); |
| 1093 | if (!ts) |
| 1094 | return -ENOMEM; |
| 1095 | |
| 1096 | ts->client = client; |
| 1097 | i2c_set_clientdata(client, ts); |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1098 | init_completion(&ts->firmware_loading_complete); |
Yauhen Kharuzhy | bd3b848 | 2019-09-16 16:32:24 -0700 | [diff] [blame] | 1099 | ts->contact_size = GOODIX_CONTACT_SIZE; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1100 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 1101 | error = goodix_get_gpio_config(ts); |
| 1102 | if (error) |
| 1103 | return error; |
| 1104 | |
Jagan Teki | ae97fb5 | 2019-04-03 15:20:26 -0700 | [diff] [blame] | 1105 | /* power up the controller */ |
| 1106 | error = regulator_enable(ts->avdd28); |
| 1107 | if (error) { |
| 1108 | dev_err(&client->dev, |
| 1109 | "Failed to enable AVDD28 regulator: %d\n", |
| 1110 | error); |
| 1111 | return error; |
| 1112 | } |
| 1113 | |
| 1114 | error = regulator_enable(ts->vddio); |
| 1115 | if (error) { |
| 1116 | dev_err(&client->dev, |
| 1117 | "Failed to enable VDDIO regulator: %d\n", |
| 1118 | error); |
| 1119 | regulator_disable(ts->avdd28); |
| 1120 | return error; |
| 1121 | } |
| 1122 | |
| 1123 | error = devm_add_action_or_reset(&client->dev, |
| 1124 | goodix_disable_regulators, ts); |
| 1125 | if (error) |
| 1126 | return error; |
| 1127 | |
Hans de Goede | 1921dac | 2020-03-24 11:23:04 -0700 | [diff] [blame] | 1128 | if (ts->reset_controller_at_probe) { |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame] | 1129 | /* reset the controller */ |
| 1130 | error = goodix_reset(ts); |
| 1131 | if (error) { |
| 1132 | dev_err(&client->dev, "Controller reset failed.\n"); |
| 1133 | return error; |
| 1134 | } |
| 1135 | } |
| 1136 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1137 | error = goodix_i2c_test(client); |
| 1138 | if (error) { |
| 1139 | dev_err(&client->dev, "I2C communication failure: %d\n", error); |
| 1140 | return error; |
| 1141 | } |
| 1142 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1143 | error = goodix_read_version(ts); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1144 | if (error) { |
| 1145 | dev_err(&client->dev, "Read version failed.\n"); |
| 1146 | return error; |
| 1147 | } |
| 1148 | |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 1149 | ts->chip = goodix_get_chip_data(ts->id); |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 1150 | |
Hans de Goede | e070a97b | 2020-03-24 11:12:35 -0700 | [diff] [blame] | 1151 | if (ts->load_cfg_from_disk) { |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1152 | /* update device config */ |
| 1153 | ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL, |
| 1154 | "goodix_%d_cfg.bin", ts->id); |
| 1155 | if (!ts->cfg_name) |
| 1156 | return -ENOMEM; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1157 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1158 | error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name, |
| 1159 | &client->dev, GFP_KERNEL, ts, |
| 1160 | goodix_config_cb); |
| 1161 | if (error) { |
| 1162 | dev_err(&client->dev, |
| 1163 | "Failed to invoke firmware loader: %d\n", |
| 1164 | error); |
| 1165 | return error; |
| 1166 | } |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1167 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1168 | return 0; |
| 1169 | } else { |
| 1170 | error = goodix_configure_dev(ts); |
| 1171 | if (error) |
| 1172 | return error; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1178 | static int goodix_ts_remove(struct i2c_client *client) |
| 1179 | { |
| 1180 | struct goodix_ts_data *ts = i2c_get_clientdata(client); |
| 1181 | |
Hans de Goede | e070a97b | 2020-03-24 11:12:35 -0700 | [diff] [blame] | 1182 | if (ts->load_cfg_from_disk) |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1183 | wait_for_completion(&ts->firmware_loading_complete); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1188 | static int __maybe_unused goodix_suspend(struct device *dev) |
| 1189 | { |
| 1190 | struct i2c_client *client = to_i2c_client(dev); |
| 1191 | struct goodix_ts_data *ts = i2c_get_clientdata(client); |
| 1192 | int error; |
| 1193 | |
Hans de Goede | e070a97b | 2020-03-24 11:12:35 -0700 | [diff] [blame] | 1194 | if (ts->load_cfg_from_disk) |
| 1195 | wait_for_completion(&ts->firmware_loading_complete); |
| 1196 | |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1197 | /* We need gpio pins to suspend/resume */ |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 1198 | if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) { |
Hans de Goede | faec44b | 2018-01-12 00:36:48 -0800 | [diff] [blame] | 1199 | disable_irq(client->irq); |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1200 | return 0; |
Hans de Goede | faec44b | 2018-01-12 00:36:48 -0800 | [diff] [blame] | 1201 | } |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1202 | |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1203 | /* Free IRQ as IRQ pin is used as output in the suspend sequence */ |
| 1204 | goodix_free_irq(ts); |
| 1205 | |
| 1206 | /* Output LOW on the INT pin for 5 ms */ |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 1207 | error = goodix_irq_direction_output(ts, 0); |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1208 | if (error) { |
| 1209 | goodix_request_irq(ts); |
| 1210 | return error; |
| 1211 | } |
| 1212 | |
| 1213 | usleep_range(5000, 6000); |
| 1214 | |
| 1215 | error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND, |
| 1216 | GOODIX_CMD_SCREEN_OFF); |
| 1217 | if (error) { |
| 1218 | dev_err(&ts->client->dev, "Screen off command failed\n"); |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 1219 | goodix_irq_direction_input(ts); |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1220 | goodix_request_irq(ts); |
| 1221 | return -EAGAIN; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * The datasheet specifies that the interval between sending screen-off |
| 1226 | * command and wake-up should be longer than 58 ms. To avoid waking up |
| 1227 | * sooner, delay 58ms here. |
| 1228 | */ |
| 1229 | msleep(58); |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int __maybe_unused goodix_resume(struct device *dev) |
| 1234 | { |
| 1235 | struct i2c_client *client = to_i2c_client(dev); |
| 1236 | struct goodix_ts_data *ts = i2c_get_clientdata(client); |
| 1237 | int error; |
| 1238 | |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 1239 | if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) { |
Hans de Goede | faec44b | 2018-01-12 00:36:48 -0800 | [diff] [blame] | 1240 | enable_irq(client->irq); |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1241 | return 0; |
Hans de Goede | faec44b | 2018-01-12 00:36:48 -0800 | [diff] [blame] | 1242 | } |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 1245 | * Exit sleep mode by outputting HIGH level to INT pin |
| 1246 | * for 2ms~5ms. |
| 1247 | */ |
Hans de Goede | 49db399 | 2020-03-24 11:12:10 -0700 | [diff] [blame] | 1248 | error = goodix_irq_direction_output(ts, 1); |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1249 | if (error) |
| 1250 | return error; |
| 1251 | |
| 1252 | usleep_range(2000, 5000); |
| 1253 | |
| 1254 | error = goodix_int_sync(ts); |
| 1255 | if (error) |
| 1256 | return error; |
| 1257 | |
| 1258 | error = goodix_request_irq(ts); |
| 1259 | if (error) |
| 1260 | return error; |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume); |
| 1266 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1267 | static const struct i2c_device_id goodix_ts_id[] = { |
| 1268 | { "GDIX1001:00", 0 }, |
| 1269 | { } |
| 1270 | }; |
Javier Martinez Canillas | 2e9e910 | 2015-07-30 10:38:52 -0700 | [diff] [blame] | 1271 | MODULE_DEVICE_TABLE(i2c, goodix_ts_id); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1272 | |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 1273 | #ifdef CONFIG_ACPI |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1274 | static const struct acpi_device_id goodix_acpi_match[] = { |
| 1275 | { "GDIX1001", 0 }, |
Ethan Lee | 5ca4d1a | 2018-05-31 16:13:17 -0700 | [diff] [blame] | 1276 | { "GDIX1002", 0 }, |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1277 | { } |
| 1278 | }; |
| 1279 | MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 1280 | #endif |
| 1281 | |
| 1282 | #ifdef CONFIG_OF |
| 1283 | static const struct of_device_id goodix_of_match[] = { |
Marcin Niestroj | 2530900 | 2017-10-24 11:06:41 -0700 | [diff] [blame] | 1284 | { .compatible = "goodix,gt1151" }, |
Jagan Teki | a5f50c5 | 2019-04-03 16:05:34 -0700 | [diff] [blame] | 1285 | { .compatible = "goodix,gt5663" }, |
Guido Günther | 84ef1b3 | 2019-02-16 23:03:13 -0800 | [diff] [blame] | 1286 | { .compatible = "goodix,gt5688" }, |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 1287 | { .compatible = "goodix,gt911" }, |
| 1288 | { .compatible = "goodix,gt9110" }, |
| 1289 | { .compatible = "goodix,gt912" }, |
| 1290 | { .compatible = "goodix,gt927" }, |
| 1291 | { .compatible = "goodix,gt9271" }, |
| 1292 | { .compatible = "goodix,gt928" }, |
| 1293 | { .compatible = "goodix,gt967" }, |
| 1294 | { } |
| 1295 | }; |
| 1296 | MODULE_DEVICE_TABLE(of, goodix_of_match); |
| 1297 | #endif |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1298 | |
| 1299 | static struct i2c_driver goodix_ts_driver = { |
| 1300 | .probe = goodix_ts_probe, |
Irina Tirdea | 68caf858 | 2015-12-17 16:05:42 -0800 | [diff] [blame] | 1301 | .remove = goodix_ts_remove, |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1302 | .id_table = goodix_ts_id, |
| 1303 | .driver = { |
| 1304 | .name = "Goodix-TS", |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 1305 | .acpi_match_table = ACPI_PTR(goodix_acpi_match), |
| 1306 | .of_match_table = of_match_ptr(goodix_of_match), |
Irina Tirdea | 5ab09d6 | 2015-12-17 16:43:39 -0800 | [diff] [blame] | 1307 | .pm = &goodix_pm_ops, |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 1308 | }, |
| 1309 | }; |
| 1310 | module_i2c_driver(goodix_ts_driver); |
| 1311 | |
| 1312 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
| 1313 | MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); |
| 1314 | MODULE_DESCRIPTION("Goodix touchscreen driver"); |
| 1315 | MODULE_LICENSE("GPL v2"); |