root/drivers/input/touchscreen/goodix.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. goodix_i2c_read
  2. goodix_i2c_write
  3. goodix_i2c_write_u8
  4. goodix_get_chip_data
  5. goodix_ts_read_input_report
  6. goodix_ts_report_touch_8b
  7. goodix_ts_report_touch_9b
  8. goodix_process_events
  9. goodix_ts_irq_handler
  10. goodix_free_irq
  11. goodix_request_irq
  12. goodix_check_cfg_8
  13. goodix_check_cfg_16
  14. goodix_check_cfg
  15. goodix_send_cfg
  16. goodix_int_sync
  17. goodix_reset
  18. goodix_get_gpio_config
  19. goodix_read_config
  20. goodix_read_version
  21. goodix_i2c_test
  22. goodix_configure_dev
  23. goodix_config_cb
  24. goodix_disable_regulators
  25. goodix_ts_probe
  26. goodix_ts_remove
  27. goodix_suspend
  28. goodix_resume

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  *  Driver for Goodix Touchscreens
   4  *
   5  *  Copyright (c) 2014 Red Hat Inc.
   6  *  Copyright (c) 2015 K. Merker <merker@debian.org>
   7  *
   8  *  This code is based on gt9xx.c authored by andrew@goodix.com:
   9  *
  10  *  2010 - 2012 Goodix Technology.
  11  */
  12 
  13 
  14 #include <linux/kernel.h>
  15 #include <linux/dmi.h>
  16 #include <linux/firmware.h>
  17 #include <linux/gpio/consumer.h>
  18 #include <linux/i2c.h>
  19 #include <linux/input.h>
  20 #include <linux/input/mt.h>
  21 #include <linux/input/touchscreen.h>
  22 #include <linux/module.h>
  23 #include <linux/delay.h>
  24 #include <linux/irq.h>
  25 #include <linux/interrupt.h>
  26 #include <linux/regulator/consumer.h>
  27 #include <linux/slab.h>
  28 #include <linux/acpi.h>
  29 #include <linux/of.h>
  30 #include <asm/unaligned.h>
  31 
  32 struct goodix_ts_data;
  33 
  34 struct goodix_chip_data {
  35         u16 config_addr;
  36         int config_len;
  37         int (*check_config)(struct goodix_ts_data *, const struct firmware *);
  38 };
  39 
  40 struct goodix_ts_data {
  41         struct i2c_client *client;
  42         struct input_dev *input_dev;
  43         const struct goodix_chip_data *chip;
  44         struct touchscreen_properties prop;
  45         unsigned int max_touch_num;
  46         unsigned int int_trigger_type;
  47         struct regulator *avdd28;
  48         struct regulator *vddio;
  49         struct gpio_desc *gpiod_int;
  50         struct gpio_desc *gpiod_rst;
  51         u16 id;
  52         u16 version;
  53         const char *cfg_name;
  54         struct completion firmware_loading_complete;
  55         unsigned long irq_flags;
  56         unsigned int contact_size;
  57 };
  58 
  59 #define GOODIX_GPIO_INT_NAME            "irq"
  60 #define GOODIX_GPIO_RST_NAME            "reset"
  61 
  62 #define GOODIX_MAX_HEIGHT               4096
  63 #define GOODIX_MAX_WIDTH                4096
  64 #define GOODIX_INT_TRIGGER              1
  65 #define GOODIX_CONTACT_SIZE             8
  66 #define GOODIX_MAX_CONTACT_SIZE         9
  67 #define GOODIX_MAX_CONTACTS             10
  68 
  69 #define GOODIX_CONFIG_MAX_LENGTH        240
  70 #define GOODIX_CONFIG_911_LENGTH        186
  71 #define GOODIX_CONFIG_967_LENGTH        228
  72 
  73 /* Register defines */
  74 #define GOODIX_REG_COMMAND              0x8040
  75 #define GOODIX_CMD_SCREEN_OFF           0x05
  76 
  77 #define GOODIX_READ_COOR_ADDR           0x814E
  78 #define GOODIX_GT1X_REG_CONFIG_DATA     0x8050
  79 #define GOODIX_GT9X_REG_CONFIG_DATA     0x8047
  80 #define GOODIX_REG_ID                   0x8140
  81 
  82 #define GOODIX_BUFFER_STATUS_READY      BIT(7)
  83 #define GOODIX_BUFFER_STATUS_TIMEOUT    20
  84 
  85 #define RESOLUTION_LOC          1
  86 #define MAX_CONTACTS_LOC        5
  87 #define TRIGGER_LOC             6
  88 
  89 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
  90                         const struct firmware *cfg);
  91 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
  92                         const struct firmware *cfg);
  93 
  94 static const struct goodix_chip_data gt1x_chip_data = {
  95         .config_addr            = GOODIX_GT1X_REG_CONFIG_DATA,
  96         .config_len             = GOODIX_CONFIG_MAX_LENGTH,
  97         .check_config           = goodix_check_cfg_16,
  98 };
  99 
 100 static const struct goodix_chip_data gt911_chip_data = {
 101         .config_addr            = GOODIX_GT9X_REG_CONFIG_DATA,
 102         .config_len             = GOODIX_CONFIG_911_LENGTH,
 103         .check_config           = goodix_check_cfg_8,
 104 };
 105 
 106 static const struct goodix_chip_data gt967_chip_data = {
 107         .config_addr            = GOODIX_GT9X_REG_CONFIG_DATA,
 108         .config_len             = GOODIX_CONFIG_967_LENGTH,
 109         .check_config           = goodix_check_cfg_8,
 110 };
 111 
 112 static const struct goodix_chip_data gt9x_chip_data = {
 113         .config_addr            = GOODIX_GT9X_REG_CONFIG_DATA,
 114         .config_len             = GOODIX_CONFIG_MAX_LENGTH,
 115         .check_config           = goodix_check_cfg_8,
 116 };
 117 
 118 static const unsigned long goodix_irq_flags[] = {
 119         IRQ_TYPE_EDGE_RISING,
 120         IRQ_TYPE_EDGE_FALLING,
 121         IRQ_TYPE_LEVEL_LOW,
 122         IRQ_TYPE_LEVEL_HIGH,
 123 };
 124 
 125 /*
 126  * Those tablets have their coordinates origin at the bottom right
 127  * of the tablet, as if rotated 180 degrees
 128  */
 129 static const struct dmi_system_id rotated_screen[] = {
 130 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
 131         {
 132                 .ident = "Teclast X89",
 133                 .matches = {
 134                         /* tPAD is too generic, also match on bios date */
 135                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
 136                         DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
 137                         DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"),
 138                 },
 139         },
 140         {
 141                 .ident = "WinBook TW100",
 142                 .matches = {
 143                         DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
 144                         DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
 145                 }
 146         },
 147         {
 148                 .ident = "WinBook TW700",
 149                 .matches = {
 150                         DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
 151                         DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
 152                 },
 153         },
 154 #endif
 155         {}
 156 };
 157 
 158 static const struct dmi_system_id nine_bytes_report[] = {
 159 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
 160         {
 161                 .ident = "Lenovo YogaBook",
 162                 /* YB1-X91L/F and YB1-X90L/F */
 163                 .matches = {
 164                         DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9")
 165                 }
 166         },
 167 #endif
 168         {}
 169 };
 170 
 171 /**
 172  * goodix_i2c_read - read data from a register of the i2c slave device.
 173  *
 174  * @client: i2c device.
 175  * @reg: the register to read from.
 176  * @buf: raw write data buffer.
 177  * @len: length of the buffer to write
 178  */
 179 static int goodix_i2c_read(struct i2c_client *client,
 180                            u16 reg, u8 *buf, int len)
 181 {
 182         struct i2c_msg msgs[2];
 183         __be16 wbuf = cpu_to_be16(reg);
 184         int ret;
 185 
 186         msgs[0].flags = 0;
 187         msgs[0].addr  = client->addr;
 188         msgs[0].len   = 2;
 189         msgs[0].buf   = (u8 *)&wbuf;
 190 
 191         msgs[1].flags = I2C_M_RD;
 192         msgs[1].addr  = client->addr;
 193         msgs[1].len   = len;
 194         msgs[1].buf   = buf;
 195 
 196         ret = i2c_transfer(client->adapter, msgs, 2);
 197         return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
 198 }
 199 
 200 /**
 201  * goodix_i2c_write - write data to a register of the i2c slave device.
 202  *
 203  * @client: i2c device.
 204  * @reg: the register to write to.
 205  * @buf: raw data buffer to write.
 206  * @len: length of the buffer to write
 207  */
 208 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
 209                             unsigned len)
 210 {
 211         u8 *addr_buf;
 212         struct i2c_msg msg;
 213         int ret;
 214 
 215         addr_buf = kmalloc(len + 2, GFP_KERNEL);
 216         if (!addr_buf)
 217                 return -ENOMEM;
 218 
 219         addr_buf[0] = reg >> 8;
 220         addr_buf[1] = reg & 0xFF;
 221         memcpy(&addr_buf[2], buf, len);
 222 
 223         msg.flags = 0;
 224         msg.addr = client->addr;
 225         msg.buf = addr_buf;
 226         msg.len = len + 2;
 227 
 228         ret = i2c_transfer(client->adapter, &msg, 1);
 229         kfree(addr_buf);
 230         return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
 231 }
 232 
 233 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
 234 {
 235         return goodix_i2c_write(client, reg, &value, sizeof(value));
 236 }
 237 
 238 static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
 239 {
 240         switch (id) {
 241         case 1151:
 242         case 5663:
 243         case 5688:
 244                 return &gt1x_chip_data;
 245 
 246         case 911:
 247         case 9271:
 248         case 9110:
 249         case 927:
 250         case 928:
 251                 return &gt911_chip_data;
 252 
 253         case 912:
 254         case 967:
 255                 return &gt967_chip_data;
 256 
 257         default:
 258                 return &gt9x_chip_data;
 259         }
 260 }
 261 
 262 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
 263 {
 264         unsigned long max_timeout;
 265         int touch_num;
 266         int error;
 267 
 268         /*
 269          * The 'buffer status' bit, which indicates that the data is valid, is
 270          * not set as soon as the interrupt is raised, but slightly after.
 271          * This takes around 10 ms to happen, so we poll for 20 ms.
 272          */
 273         max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
 274         do {
 275                 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
 276                                         data, ts->contact_size + 1);
 277                 if (error) {
 278                         dev_err(&ts->client->dev, "I2C transfer error: %d\n",
 279                                         error);
 280                         return error;
 281                 }
 282 
 283                 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
 284                         touch_num = data[0] & 0x0f;
 285                         if (touch_num > ts->max_touch_num)
 286                                 return -EPROTO;
 287 
 288                         if (touch_num > 1) {
 289                                 data += 1 + ts->contact_size;
 290                                 error = goodix_i2c_read(ts->client,
 291                                                 GOODIX_READ_COOR_ADDR +
 292                                                         1 + ts->contact_size,
 293                                                 data,
 294                                                 ts->contact_size *
 295                                                         (touch_num - 1));
 296                                 if (error)
 297                                         return error;
 298                         }
 299 
 300                         return touch_num;
 301                 }
 302 
 303                 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
 304         } while (time_before(jiffies, max_timeout));
 305 
 306         /*
 307          * The Goodix panel will send spurious interrupts after a
 308          * 'finger up' event, which will always cause a timeout.
 309          */
 310         return 0;
 311 }
 312 
 313 static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
 314 {
 315         int id = coor_data[0] & 0x0F;
 316         int input_x = get_unaligned_le16(&coor_data[1]);
 317         int input_y = get_unaligned_le16(&coor_data[3]);
 318         int input_w = get_unaligned_le16(&coor_data[5]);
 319 
 320         input_mt_slot(ts->input_dev, id);
 321         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
 322         touchscreen_report_pos(ts->input_dev, &ts->prop,
 323                                input_x, input_y, true);
 324         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
 325         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
 326 }
 327 
 328 static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
 329 {
 330         int id = coor_data[1] & 0x0F;
 331         int input_x = get_unaligned_le16(&coor_data[3]);
 332         int input_y = get_unaligned_le16(&coor_data[5]);
 333         int input_w = get_unaligned_le16(&coor_data[7]);
 334 
 335         input_mt_slot(ts->input_dev, id);
 336         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
 337         touchscreen_report_pos(ts->input_dev, &ts->prop,
 338                                input_x, input_y, true);
 339         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
 340         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
 341 }
 342 
 343 /**
 344  * goodix_process_events - Process incoming events
 345  *
 346  * @ts: our goodix_ts_data pointer
 347  *
 348  * Called when the IRQ is triggered. Read the current device state, and push
 349  * the input events to the user space.
 350  */
 351 static void goodix_process_events(struct goodix_ts_data *ts)
 352 {
 353         u8  point_data[1 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
 354         int touch_num;
 355         int i;
 356 
 357         touch_num = goodix_ts_read_input_report(ts, point_data);
 358         if (touch_num < 0)
 359                 return;
 360 
 361         /*
 362          * Bit 4 of the first byte reports the status of the capacitive
 363          * Windows/Home button.
 364          */
 365         input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
 366 
 367         for (i = 0; i < touch_num; i++)
 368                 if (ts->contact_size == 9)
 369                         goodix_ts_report_touch_9b(ts,
 370                                 &point_data[1 + ts->contact_size * i]);
 371                 else
 372                         goodix_ts_report_touch_8b(ts,
 373                                 &point_data[1 + ts->contact_size * i]);
 374 
 375         input_mt_sync_frame(ts->input_dev);
 376         input_sync(ts->input_dev);
 377 }
 378 
 379 /**
 380  * goodix_ts_irq_handler - The IRQ handler
 381  *
 382  * @irq: interrupt number.
 383  * @dev_id: private data pointer.
 384  */
 385 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
 386 {
 387         struct goodix_ts_data *ts = dev_id;
 388 
 389         goodix_process_events(ts);
 390 
 391         if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
 392                 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
 393 
 394         return IRQ_HANDLED;
 395 }
 396 
 397 static void goodix_free_irq(struct goodix_ts_data *ts)
 398 {
 399         devm_free_irq(&ts->client->dev, ts->client->irq, ts);
 400 }
 401 
 402 static int goodix_request_irq(struct goodix_ts_data *ts)
 403 {
 404         return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
 405                                          NULL, goodix_ts_irq_handler,
 406                                          ts->irq_flags, ts->client->name, ts);
 407 }
 408 
 409 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
 410                         const struct firmware *cfg)
 411 {
 412         int i, raw_cfg_len = cfg->size - 2;
 413         u8 check_sum = 0;
 414 
 415         for (i = 0; i < raw_cfg_len; i++)
 416                 check_sum += cfg->data[i];
 417         check_sum = (~check_sum) + 1;
 418         if (check_sum != cfg->data[raw_cfg_len]) {
 419                 dev_err(&ts->client->dev,
 420                         "The checksum of the config fw is not correct");
 421                 return -EINVAL;
 422         }
 423 
 424         if (cfg->data[raw_cfg_len + 1] != 1) {
 425                 dev_err(&ts->client->dev,
 426                         "Config fw must have Config_Fresh register set");
 427                 return -EINVAL;
 428         }
 429 
 430         return 0;
 431 }
 432 
 433 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
 434                         const struct firmware *cfg)
 435 {
 436         int i, raw_cfg_len = cfg->size - 3;
 437         u16 check_sum = 0;
 438 
 439         for (i = 0; i < raw_cfg_len; i += 2)
 440                 check_sum += get_unaligned_be16(&cfg->data[i]);
 441         check_sum = (~check_sum) + 1;
 442         if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) {
 443                 dev_err(&ts->client->dev,
 444                         "The checksum of the config fw is not correct");
 445                 return -EINVAL;
 446         }
 447 
 448         if (cfg->data[raw_cfg_len + 2] != 1) {
 449                 dev_err(&ts->client->dev,
 450                         "Config fw must have Config_Fresh register set");
 451                 return -EINVAL;
 452         }
 453 
 454         return 0;
 455 }
 456 
 457 /**
 458  * goodix_check_cfg - Checks if config fw is valid
 459  *
 460  * @ts: goodix_ts_data pointer
 461  * @cfg: firmware config data
 462  */
 463 static int goodix_check_cfg(struct goodix_ts_data *ts,
 464                             const struct firmware *cfg)
 465 {
 466         if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
 467                 dev_err(&ts->client->dev,
 468                         "The length of the config fw is not correct");
 469                 return -EINVAL;
 470         }
 471 
 472         return ts->chip->check_config(ts, cfg);
 473 }
 474 
 475 /**
 476  * goodix_send_cfg - Write fw config to device
 477  *
 478  * @ts: goodix_ts_data pointer
 479  * @cfg: config firmware to write to device
 480  */
 481 static int goodix_send_cfg(struct goodix_ts_data *ts,
 482                            const struct firmware *cfg)
 483 {
 484         int error;
 485 
 486         error = goodix_check_cfg(ts, cfg);
 487         if (error)
 488                 return error;
 489 
 490         error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data,
 491                                  cfg->size);
 492         if (error) {
 493                 dev_err(&ts->client->dev, "Failed to write config data: %d",
 494                         error);
 495                 return error;
 496         }
 497         dev_dbg(&ts->client->dev, "Config sent successfully.");
 498 
 499         /* Let the firmware reconfigure itself, so sleep for 10ms */
 500         usleep_range(10000, 11000);
 501 
 502         return 0;
 503 }
 504 
 505 static int goodix_int_sync(struct goodix_ts_data *ts)
 506 {
 507         int error;
 508 
 509         error = gpiod_direction_output(ts->gpiod_int, 0);
 510         if (error)
 511                 return error;
 512 
 513         msleep(50);                             /* T5: 50ms */
 514 
 515         error = gpiod_direction_input(ts->gpiod_int);
 516         if (error)
 517                 return error;
 518 
 519         return 0;
 520 }
 521 
 522 /**
 523  * goodix_reset - Reset device during power on
 524  *
 525  * @ts: goodix_ts_data pointer
 526  */
 527 static int goodix_reset(struct goodix_ts_data *ts)
 528 {
 529         int error;
 530 
 531         /* begin select I2C slave addr */
 532         error = gpiod_direction_output(ts->gpiod_rst, 0);
 533         if (error)
 534                 return error;
 535 
 536         msleep(20);                             /* T2: > 10ms */
 537 
 538         /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
 539         error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
 540         if (error)
 541                 return error;
 542 
 543         usleep_range(100, 2000);                /* T3: > 100us */
 544 
 545         error = gpiod_direction_output(ts->gpiod_rst, 1);
 546         if (error)
 547                 return error;
 548 
 549         usleep_range(6000, 10000);              /* T4: > 5ms */
 550 
 551         /* end select I2C slave addr */
 552         error = gpiod_direction_input(ts->gpiod_rst);
 553         if (error)
 554                 return error;
 555 
 556         error = goodix_int_sync(ts);
 557         if (error)
 558                 return error;
 559 
 560         return 0;
 561 }
 562 
 563 /**
 564  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
 565  *
 566  * @ts: goodix_ts_data pointer
 567  */
 568 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
 569 {
 570         int error;
 571         struct device *dev;
 572         struct gpio_desc *gpiod;
 573 
 574         if (!ts->client)
 575                 return -EINVAL;
 576         dev = &ts->client->dev;
 577 
 578         ts->avdd28 = devm_regulator_get(dev, "AVDD28");
 579         if (IS_ERR(ts->avdd28)) {
 580                 error = PTR_ERR(ts->avdd28);
 581                 if (error != -EPROBE_DEFER)
 582                         dev_err(dev,
 583                                 "Failed to get AVDD28 regulator: %d\n", error);
 584                 return error;
 585         }
 586 
 587         ts->vddio = devm_regulator_get(dev, "VDDIO");
 588         if (IS_ERR(ts->vddio)) {
 589                 error = PTR_ERR(ts->vddio);
 590                 if (error != -EPROBE_DEFER)
 591                         dev_err(dev,
 592                                 "Failed to get VDDIO regulator: %d\n", error);
 593                 return error;
 594         }
 595 
 596         /* Get the interrupt GPIO pin number */
 597         gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
 598         if (IS_ERR(gpiod)) {
 599                 error = PTR_ERR(gpiod);
 600                 if (error != -EPROBE_DEFER)
 601                         dev_dbg(dev, "Failed to get %s GPIO: %d\n",
 602                                 GOODIX_GPIO_INT_NAME, error);
 603                 return error;
 604         }
 605 
 606         ts->gpiod_int = gpiod;
 607 
 608         /* Get the reset line GPIO pin number */
 609         gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
 610         if (IS_ERR(gpiod)) {
 611                 error = PTR_ERR(gpiod);
 612                 if (error != -EPROBE_DEFER)
 613                         dev_dbg(dev, "Failed to get %s GPIO: %d\n",
 614                                 GOODIX_GPIO_RST_NAME, error);
 615                 return error;
 616         }
 617 
 618         ts->gpiod_rst = gpiod;
 619 
 620         return 0;
 621 }
 622 
 623 /**
 624  * goodix_read_config - Read the embedded configuration of the panel
 625  *
 626  * @ts: our goodix_ts_data pointer
 627  *
 628  * Must be called during probe
 629  */
 630 static void goodix_read_config(struct goodix_ts_data *ts)
 631 {
 632         u8 config[GOODIX_CONFIG_MAX_LENGTH];
 633         int x_max, y_max;
 634         int error;
 635 
 636         error = goodix_i2c_read(ts->client, ts->chip->config_addr,
 637                                 config, ts->chip->config_len);
 638         if (error) {
 639                 dev_warn(&ts->client->dev, "Error reading config: %d\n",
 640                          error);
 641                 ts->int_trigger_type = GOODIX_INT_TRIGGER;
 642                 ts->max_touch_num = GOODIX_MAX_CONTACTS;
 643                 return;
 644         }
 645 
 646         ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
 647         ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
 648 
 649         x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
 650         y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
 651         if (x_max && y_max) {
 652                 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
 653                 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
 654         }
 655 }
 656 
 657 /**
 658  * goodix_read_version - Read goodix touchscreen version
 659  *
 660  * @ts: our goodix_ts_data pointer
 661  */
 662 static int goodix_read_version(struct goodix_ts_data *ts)
 663 {
 664         int error;
 665         u8 buf[6];
 666         char id_str[5];
 667 
 668         error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
 669         if (error) {
 670                 dev_err(&ts->client->dev, "read version failed: %d\n", error);
 671                 return error;
 672         }
 673 
 674         memcpy(id_str, buf, 4);
 675         id_str[4] = 0;
 676         if (kstrtou16(id_str, 10, &ts->id))
 677                 ts->id = 0x1001;
 678 
 679         ts->version = get_unaligned_le16(&buf[4]);
 680 
 681         dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
 682                  ts->version);
 683 
 684         return 0;
 685 }
 686 
 687 /**
 688  * goodix_i2c_test - I2C test function to check if the device answers.
 689  *
 690  * @client: the i2c client
 691  */
 692 static int goodix_i2c_test(struct i2c_client *client)
 693 {
 694         int retry = 0;
 695         int error;
 696         u8 test;
 697 
 698         while (retry++ < 2) {
 699                 error = goodix_i2c_read(client, GOODIX_REG_ID,
 700                                         &test, 1);
 701                 if (!error)
 702                         return 0;
 703 
 704                 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
 705                         retry, error);
 706                 msleep(20);
 707         }
 708 
 709         return error;
 710 }
 711 
 712 /**
 713  * goodix_configure_dev - Finish device initialization
 714  *
 715  * @ts: our goodix_ts_data pointer
 716  *
 717  * Must be called from probe to finish initialization of the device.
 718  * Contains the common initialization code for both devices that
 719  * declare gpio pins and devices that do not. It is either called
 720  * directly from probe or from request_firmware_wait callback.
 721  */
 722 static int goodix_configure_dev(struct goodix_ts_data *ts)
 723 {
 724         int error;
 725 
 726         ts->int_trigger_type = GOODIX_INT_TRIGGER;
 727         ts->max_touch_num = GOODIX_MAX_CONTACTS;
 728 
 729         ts->input_dev = devm_input_allocate_device(&ts->client->dev);
 730         if (!ts->input_dev) {
 731                 dev_err(&ts->client->dev, "Failed to allocate input device.");
 732                 return -ENOMEM;
 733         }
 734 
 735         ts->input_dev->name = "Goodix Capacitive TouchScreen";
 736         ts->input_dev->phys = "input/ts";
 737         ts->input_dev->id.bustype = BUS_I2C;
 738         ts->input_dev->id.vendor = 0x0416;
 739         ts->input_dev->id.product = ts->id;
 740         ts->input_dev->id.version = ts->version;
 741 
 742         /* Capacitive Windows/Home button on some devices */
 743         input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
 744 
 745         input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
 746         input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
 747         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
 748         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
 749 
 750         /* Read configuration and apply touchscreen parameters */
 751         goodix_read_config(ts);
 752 
 753         /* Try overriding touchscreen parameters via device properties */
 754         touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
 755 
 756         if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
 757                 dev_err(&ts->client->dev,
 758                         "Invalid config (%d, %d, %d), using defaults\n",
 759                         ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
 760                 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
 761                 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
 762                 ts->max_touch_num = GOODIX_MAX_CONTACTS;
 763                 input_abs_set_max(ts->input_dev,
 764                                   ABS_MT_POSITION_X, ts->prop.max_x);
 765                 input_abs_set_max(ts->input_dev,
 766                                   ABS_MT_POSITION_Y, ts->prop.max_y);
 767         }
 768 
 769         if (dmi_check_system(rotated_screen)) {
 770                 ts->prop.invert_x = true;
 771                 ts->prop.invert_y = true;
 772                 dev_dbg(&ts->client->dev,
 773                         "Applying '180 degrees rotated screen' quirk\n");
 774         }
 775 
 776         if (dmi_check_system(nine_bytes_report)) {
 777                 ts->contact_size = 9;
 778 
 779                 dev_dbg(&ts->client->dev,
 780                         "Non-standard 9-bytes report format quirk\n");
 781         }
 782 
 783         error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
 784                                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
 785         if (error) {
 786                 dev_err(&ts->client->dev,
 787                         "Failed to initialize MT slots: %d", error);
 788                 return error;
 789         }
 790 
 791         error = input_register_device(ts->input_dev);
 792         if (error) {
 793                 dev_err(&ts->client->dev,
 794                         "Failed to register input device: %d", error);
 795                 return error;
 796         }
 797 
 798         ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
 799         error = goodix_request_irq(ts);
 800         if (error) {
 801                 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
 802                 return error;
 803         }
 804 
 805         return 0;
 806 }
 807 
 808 /**
 809  * goodix_config_cb - Callback to finish device init
 810  *
 811  * @ts: our goodix_ts_data pointer
 812  *
 813  * request_firmware_wait callback that finishes
 814  * initialization of the device.
 815  */
 816 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
 817 {
 818         struct goodix_ts_data *ts = ctx;
 819         int error;
 820 
 821         if (cfg) {
 822                 /* send device configuration to the firmware */
 823                 error = goodix_send_cfg(ts, cfg);
 824                 if (error)
 825                         goto err_release_cfg;
 826         }
 827 
 828         goodix_configure_dev(ts);
 829 
 830 err_release_cfg:
 831         release_firmware(cfg);
 832         complete_all(&ts->firmware_loading_complete);
 833 }
 834 
 835 static void goodix_disable_regulators(void *arg)
 836 {
 837         struct goodix_ts_data *ts = arg;
 838 
 839         regulator_disable(ts->vddio);
 840         regulator_disable(ts->avdd28);
 841 }
 842 
 843 static int goodix_ts_probe(struct i2c_client *client,
 844                            const struct i2c_device_id *id)
 845 {
 846         struct goodix_ts_data *ts;
 847         int error;
 848 
 849         dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
 850 
 851         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
 852                 dev_err(&client->dev, "I2C check functionality failed.\n");
 853                 return -ENXIO;
 854         }
 855 
 856         ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
 857         if (!ts)
 858                 return -ENOMEM;
 859 
 860         ts->client = client;
 861         i2c_set_clientdata(client, ts);
 862         init_completion(&ts->firmware_loading_complete);
 863         ts->contact_size = GOODIX_CONTACT_SIZE;
 864 
 865         error = goodix_get_gpio_config(ts);
 866         if (error)
 867                 return error;
 868 
 869         /* power up the controller */
 870         error = regulator_enable(ts->avdd28);
 871         if (error) {
 872                 dev_err(&client->dev,
 873                         "Failed to enable AVDD28 regulator: %d\n",
 874                         error);
 875                 return error;
 876         }
 877 
 878         error = regulator_enable(ts->vddio);
 879         if (error) {
 880                 dev_err(&client->dev,
 881                         "Failed to enable VDDIO regulator: %d\n",
 882                         error);
 883                 regulator_disable(ts->avdd28);
 884                 return error;
 885         }
 886 
 887         error = devm_add_action_or_reset(&client->dev,
 888                                          goodix_disable_regulators, ts);
 889         if (error)
 890                 return error;
 891 
 892         if (ts->gpiod_int && ts->gpiod_rst) {
 893                 /* reset the controller */
 894                 error = goodix_reset(ts);
 895                 if (error) {
 896                         dev_err(&client->dev, "Controller reset failed.\n");
 897                         return error;
 898                 }
 899         }
 900 
 901         error = goodix_i2c_test(client);
 902         if (error) {
 903                 dev_err(&client->dev, "I2C communication failure: %d\n", error);
 904                 return error;
 905         }
 906 
 907         error = goodix_read_version(ts);
 908         if (error) {
 909                 dev_err(&client->dev, "Read version failed.\n");
 910                 return error;
 911         }
 912 
 913         ts->chip = goodix_get_chip_data(ts->id);
 914 
 915         if (ts->gpiod_int && ts->gpiod_rst) {
 916                 /* update device config */
 917                 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
 918                                               "goodix_%d_cfg.bin", ts->id);
 919                 if (!ts->cfg_name)
 920                         return -ENOMEM;
 921 
 922                 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
 923                                                 &client->dev, GFP_KERNEL, ts,
 924                                                 goodix_config_cb);
 925                 if (error) {
 926                         dev_err(&client->dev,
 927                                 "Failed to invoke firmware loader: %d\n",
 928                                 error);
 929                         return error;
 930                 }
 931 
 932                 return 0;
 933         } else {
 934                 error = goodix_configure_dev(ts);
 935                 if (error)
 936                         return error;
 937         }
 938 
 939         return 0;
 940 }
 941 
 942 static int goodix_ts_remove(struct i2c_client *client)
 943 {
 944         struct goodix_ts_data *ts = i2c_get_clientdata(client);
 945 
 946         if (ts->gpiod_int && ts->gpiod_rst)
 947                 wait_for_completion(&ts->firmware_loading_complete);
 948 
 949         return 0;
 950 }
 951 
 952 static int __maybe_unused goodix_suspend(struct device *dev)
 953 {
 954         struct i2c_client *client = to_i2c_client(dev);
 955         struct goodix_ts_data *ts = i2c_get_clientdata(client);
 956         int error;
 957 
 958         /* We need gpio pins to suspend/resume */
 959         if (!ts->gpiod_int || !ts->gpiod_rst) {
 960                 disable_irq(client->irq);
 961                 return 0;
 962         }
 963 
 964         wait_for_completion(&ts->firmware_loading_complete);
 965 
 966         /* Free IRQ as IRQ pin is used as output in the suspend sequence */
 967         goodix_free_irq(ts);
 968 
 969         /* Output LOW on the INT pin for 5 ms */
 970         error = gpiod_direction_output(ts->gpiod_int, 0);
 971         if (error) {
 972                 goodix_request_irq(ts);
 973                 return error;
 974         }
 975 
 976         usleep_range(5000, 6000);
 977 
 978         error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
 979                                     GOODIX_CMD_SCREEN_OFF);
 980         if (error) {
 981                 dev_err(&ts->client->dev, "Screen off command failed\n");
 982                 gpiod_direction_input(ts->gpiod_int);
 983                 goodix_request_irq(ts);
 984                 return -EAGAIN;
 985         }
 986 
 987         /*
 988          * The datasheet specifies that the interval between sending screen-off
 989          * command and wake-up should be longer than 58 ms. To avoid waking up
 990          * sooner, delay 58ms here.
 991          */
 992         msleep(58);
 993         return 0;
 994 }
 995 
 996 static int __maybe_unused goodix_resume(struct device *dev)
 997 {
 998         struct i2c_client *client = to_i2c_client(dev);
 999         struct goodix_ts_data *ts = i2c_get_clientdata(client);
1000         int error;
1001 
1002         if (!ts->gpiod_int || !ts->gpiod_rst) {
1003                 enable_irq(client->irq);
1004                 return 0;
1005         }
1006 
1007         /*
1008          * Exit sleep mode by outputting HIGH level to INT pin
1009          * for 2ms~5ms.
1010          */
1011         error = gpiod_direction_output(ts->gpiod_int, 1);
1012         if (error)
1013                 return error;
1014 
1015         usleep_range(2000, 5000);
1016 
1017         error = goodix_int_sync(ts);
1018         if (error)
1019                 return error;
1020 
1021         error = goodix_request_irq(ts);
1022         if (error)
1023                 return error;
1024 
1025         return 0;
1026 }
1027 
1028 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1029 
1030 static const struct i2c_device_id goodix_ts_id[] = {
1031         { "GDIX1001:00", 0 },
1032         { }
1033 };
1034 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1035 
1036 #ifdef CONFIG_ACPI
1037 static const struct acpi_device_id goodix_acpi_match[] = {
1038         { "GDIX1001", 0 },
1039         { "GDIX1002", 0 },
1040         { }
1041 };
1042 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1043 #endif
1044 
1045 #ifdef CONFIG_OF
1046 static const struct of_device_id goodix_of_match[] = {
1047         { .compatible = "goodix,gt1151" },
1048         { .compatible = "goodix,gt5663" },
1049         { .compatible = "goodix,gt5688" },
1050         { .compatible = "goodix,gt911" },
1051         { .compatible = "goodix,gt9110" },
1052         { .compatible = "goodix,gt912" },
1053         { .compatible = "goodix,gt927" },
1054         { .compatible = "goodix,gt9271" },
1055         { .compatible = "goodix,gt928" },
1056         { .compatible = "goodix,gt967" },
1057         { }
1058 };
1059 MODULE_DEVICE_TABLE(of, goodix_of_match);
1060 #endif
1061 
1062 static struct i2c_driver goodix_ts_driver = {
1063         .probe = goodix_ts_probe,
1064         .remove = goodix_ts_remove,
1065         .id_table = goodix_ts_id,
1066         .driver = {
1067                 .name = "Goodix-TS",
1068                 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
1069                 .of_match_table = of_match_ptr(goodix_of_match),
1070                 .pm = &goodix_pm_ops,
1071         },
1072 };
1073 module_i2c_driver(goodix_ts_driver);
1074 
1075 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1076 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1077 MODULE_DESCRIPTION("Goodix touchscreen driver");
1078 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */