| 1 | |
| 2 | |
| 3 | * Goodix "Berlin" Touchscreen IC driver
|
| 4 | * Copyright (C) 2020 - 2021 Goodix, Inc.
|
| 5 | * Copyright (C) 2023 Linaro Ltd.
|
| 6 | *
|
| 7 | * Based on goodix_ts_berlin driver.
|
| 8 | *
|
| 9 | * This driver is distinct from goodix.c since hardware interface
|
| 10 | * is different enough to require a new driver.
|
| 11 | * None of the register address or data structure are close enough
|
| 12 | * to the previous generations.
|
| 13 | *
|
| 14 | * Currently the driver only handles Multitouch events with already
|
| 15 | * programmed firmware and "config" for "Revision A/D" Berlin IC.
|
| 16 | *
|
| 17 | * Support is missing for:
|
| 18 | * - ESD Management
|
| 19 | * - Firmware update/flashing
|
| 20 | * - "Config" update/flashing
|
| 21 | * - Stylus Events
|
| 22 | * - Gesture Events
|
| 23 | * - Support for revision B
|
| 24 |
|
| 25 |
|
| 26 | #include <linux/bitfield.h>
|
| 27 | #include <linux/export.h>
|
| 28 | #include <linux/gpio/consumer.h>
|
| 29 | #include <linux/input.h>
|
| 30 | #include <linux/input/mt.h>
|
| 31 | #include <linux/input/touchscreen.h>
|
| 32 | #include <linux/property.h>
|
| 33 | #include <linux/regmap.h>
|
| 34 | #include <linux/regulator/consumer.h>
|
| 35 | #include <linux/sizes.h>
|
| 36 | #include <linux/unaligned.h>
|
| 37 |
|
| 38 | #include "goodix_berlin.h"
|
| 39 |
|
| 40 | #define GOODIX_BERLIN_MAX_TOUCH 10
|
| 41 |
|
| 42 | #define GOODIX_BERLIN_NORMAL_RESET_DELAY_MS 100
|
| 43 |
|
| 44 | #define GOODIX_BERLIN_TOUCH_EVENT BIT(7)
|
| 45 | #define GOODIX_BERLIN_REQUEST_EVENT BIT(6)
|
| 46 | #define GOODIX_BERLIN_TOUCH_COUNT_MASK GENMASK(3, 0)
|
| 47 |
|
| 48 | #define GOODIX_BERLIN_REQUEST_CODE_RESET 3
|
| 49 |
|
| 50 | #define GOODIX_BERLIN_POINT_TYPE_MASK GENMASK(3, 0)
|
| 51 | #define GOODIX_BERLIN_POINT_TYPE_STYLUS_HOVER 1
|
| 52 | #define GOODIX_BERLIN_POINT_TYPE_STYLUS 3
|
| 53 |
|
| 54 | #define GOODIX_BERLIN_TOUCH_ID_MASK GENMASK(7, 4)
|
| 55 |
|
| 56 | #define GOODIX_BERLIN_DEV_CONFIRM_VAL 0xAA
|
| 57 | #define GOODIX_BERLIN_BOOTOPTION_ADDR 0x10000
|
| 58 |
|
| 59 | #define GOODIX_BERLIN_IC_INFO_MAX_LEN SZ_1K
|
| 60 |
|
| 61 | #define GOODIX_BERLIN_CHECKSUM_SIZE sizeof(u16)
|
| 62 |
|
| 63 | struct goodix_berlin_fw_version {
|
| 64 | u8 rom_pid[6];
|
| 65 | u8 rom_vid[3];
|
| 66 | u8 rom_vid_reserved;
|
| 67 | u8 patch_pid[8];
|
| 68 | u8 patch_vid[4];
|
| 69 | u8 patch_vid_reserved;
|
| 70 | u8 sensor_id;
|
| 71 | u8 reserved[2];
|
| 72 | __le16 checksum;
|
| 73 | };
|
| 74 |
|
| 75 | struct goodix_berlin_ic_info_version {
|
| 76 | u8 info_customer_id;
|
| 77 | u8 info_version_id;
|
| 78 | u8 ic_die_id;
|
| 79 | u8 ic_version_id;
|
| 80 | __le32 config_id;
|
| 81 | u8 config_version;
|
| 82 | u8 frame_data_customer_id;
|
| 83 | u8 frame_data_version_id;
|
| 84 | u8 touch_data_customer_id;
|
| 85 | u8 touch_data_version_id;
|
| 86 | u8 reserved[3];
|
| 87 | } __packed;
|
| 88 |
|
| 89 | struct goodix_berlin_ic_info_feature {
|
| 90 | __le16 freqhop_feature;
|
| 91 | __le16 calibration_feature;
|
| 92 | __le16 gesture_feature;
|
| 93 | __le16 side_touch_feature;
|
| 94 | __le16 stylus_feature;
|
| 95 | } __packed;
|
| 96 |
|
| 97 | struct goodix_berlin_ic_info_misc {
|
| 98 | __le32 cmd_addr;
|
| 99 | __le16 cmd_max_len;
|
| 100 | __le32 cmd_reply_addr;
|
| 101 | __le16 cmd_reply_len;
|
| 102 | __le32 fw_state_addr;
|
| 103 | __le16 fw_state_len;
|
| 104 | __le32 fw_buffer_addr;
|
| 105 | __le16 fw_buffer_max_len;
|
| 106 | __le32 frame_data_addr;
|
| 107 | __le16 frame_data_head_len;
|
| 108 | __le16 fw_attr_len;
|
| 109 | __le16 fw_log_len;
|
| 110 | u8 pack_max_num;
|
| 111 | u8 pack_compress_version;
|
| 112 | __le16 stylus_struct_len;
|
| 113 | __le16 mutual_struct_len;
|
| 114 | __le16 self_struct_len;
|
| 115 | __le16 noise_struct_len;
|
| 116 | __le32 touch_data_addr;
|
| 117 | __le16 touch_data_head_len;
|
| 118 | __le16 point_struct_len;
|
| 119 | __le16 reserved1;
|
| 120 | __le16 reserved2;
|
| 121 | __le32 mutual_rawdata_addr;
|
| 122 | __le32 mutual_diffdata_addr;
|
| 123 | __le32 mutual_refdata_addr;
|
| 124 | __le32 self_rawdata_addr;
|
| 125 | __le32 self_diffdata_addr;
|
| 126 | __le32 self_refdata_addr;
|
| 127 | __le32 iq_rawdata_addr;
|
| 128 | __le32 iq_refdata_addr;
|
| 129 | __le32 im_rawdata_addr;
|
| 130 | __le16 im_readata_len;
|
| 131 | __le32 noise_rawdata_addr;
|
| 132 | __le16 noise_rawdata_len;
|
| 133 | __le32 stylus_rawdata_addr;
|
| 134 | __le16 stylus_rawdata_len;
|
| 135 | __le32 noise_data_addr;
|
| 136 | __le32 esd_addr;
|
| 137 | } __packed;
|
| 138 |
|
| 139 | struct goodix_berlin_touch {
|
| 140 | u8 status;
|
| 141 | u8 reserved;
|
| 142 | __le16 x;
|
| 143 | __le16 y;
|
| 144 | __le16 w;
|
| 145 | };
|
| 146 | #define GOODIX_BERLIN_TOUCH_SIZE sizeof(struct goodix_berlin_touch)
|
| 147 |
|
| 148 | struct goodix_berlin_header {
|
| 149 | u8 status;
|
| 150 | u8 reserved1;
|
| 151 | u8 request_type;
|
| 152 | u8 reserved2[3];
|
| 153 | __le16 checksum;
|
| 154 | };
|
| 155 | #define GOODIX_BERLIN_HEADER_SIZE sizeof(struct goodix_berlin_header)
|
| 156 |
|
| 157 | struct goodix_berlin_event {
|
| 158 | struct goodix_berlin_header hdr;
|
| 159 |
|
| 160 | u8 data[GOODIX_BERLIN_TOUCH_SIZE * GOODIX_BERLIN_MAX_TOUCH +
|
| 161 | GOODIX_BERLIN_CHECKSUM_SIZE];
|
| 162 | };
|
| 163 |
|
| 164 | struct goodix_berlin_core {
|
| 165 | struct device *dev;
|
| 166 | struct regmap *regmap;
|
| 167 | struct regulator *avdd;
|
| 168 | struct regulator *vddio;
|
| 169 | struct gpio_desc *reset_gpio;
|
| 170 | struct touchscreen_properties props;
|
| 171 | struct goodix_berlin_fw_version fw_version;
|
| 172 | struct input_dev *input_dev;
|
| 173 | int irq;
|
| 174 |
|
| 175 |
|
| 176 | u32 touch_data_addr;
|
| 177 |
|
| 178 | const struct goodix_berlin_ic_data *ic_data;
|
| 179 |
|
| 180 | struct goodix_berlin_event event;
|
| 181 | };
|
| 182 |
|
| 183 | static bool goodix_berlin_checksum_valid(const u8 *data, int size)
|
| 184 | {
|
| 185 | u32 cal_checksum = 0;
|
| 186 | u16 r_checksum;
|
| 187 | int i;
|
| 188 |
|
| 189 | if (size < GOODIX_BERLIN_CHECKSUM_SIZE)
|
| 190 | return false;
|
| 191 |
|
| 192 | for (i = 0; i < size - GOODIX_BERLIN_CHECKSUM_SIZE; i++)
|
| 193 | cal_checksum += data[i];
|
| 194 |
|
| 195 | r_checksum = get_unaligned_le16(&data[i]);
|
| 196 |
|
| 197 | return (u16)cal_checksum == r_checksum;
|
| 198 | }
|
| 199 |
|
| 200 | static bool goodix_berlin_is_dummy_data(struct goodix_berlin_core *cd,
|
| 201 | const u8 *data, int size)
|
| 202 | {
|
| 203 | int i;
|
| 204 |
|
| 205 | |
| 206 | * If the device is missing or doesn't respond the buffer
|
| 207 | * could be filled with bus default line state, 0x00 or 0xff,
|
| 208 | * so declare success the first time we encounter neither.
|
| 209 |
|
| 210 | for (i = 0; i < size; i++)
|
| 211 | if (data[i] > 0 && data[i] < 0xff)
|
| 212 | return false;
|
| 213 |
|
| 214 | return true;
|
| 215 | }
|
| 216 |
|
| 217 | static int goodix_berlin_dev_confirm(struct goodix_berlin_core *cd)
|
| 218 | {
|
| 219 | u8 tx_buf[8], rx_buf[8];
|
| 220 | int retry = 3;
|
| 221 | int error;
|
| 222 |
|
| 223 | memset(tx_buf, GOODIX_BERLIN_DEV_CONFIRM_VAL, sizeof(tx_buf));
|
| 224 | while (retry--) {
|
| 225 | error = regmap_raw_write(cd->regmap,
|
| 226 | GOODIX_BERLIN_BOOTOPTION_ADDR,
|
| 227 | tx_buf, sizeof(tx_buf));
|
| 228 | if (error)
|
| 229 | return error;
|
| 230 |
|
| 231 | error = regmap_raw_read(cd->regmap,
|
| 232 | GOODIX_BERLIN_BOOTOPTION_ADDR,
|
| 233 | rx_buf, sizeof(rx_buf));
|
| 234 | if (error)
|
| 235 | return error;
|
| 236 |
|
| 237 | if (!memcmp(tx_buf, rx_buf, sizeof(tx_buf)))
|
| 238 | return 0;
|
| 239 |
|
| 240 | usleep_range(5000, 5100);
|
| 241 | }
|
| 242 |
|
| 243 | dev_err(cd->dev, "device confirm failed, rx_buf: %*ph\n",
|
| 244 | (int)sizeof(rx_buf), rx_buf);
|
| 245 |
|
| 246 | return -EINVAL;
|
| 247 | }
|
| 248 |
|
| 249 | static int goodix_berlin_power_on(struct goodix_berlin_core *cd)
|
| 250 | {
|
| 251 | int error;
|
| 252 |
|
| 253 | error = regulator_enable(cd->vddio);
|
| 254 | if (error) {
|
| 255 | dev_err(cd->dev, "Failed to enable vddio: %d\n", error);
|
| 256 | return error;
|
| 257 | }
|
| 258 |
|
| 259 |
|
| 260 | usleep_range(3000, 3100);
|
| 261 |
|
| 262 | error = regulator_enable(cd->avdd);
|
| 263 | if (error) {
|
| 264 | dev_err(cd->dev, "Failed to enable avdd: %d\n", error);
|
| 265 | goto err_vddio_disable;
|
| 266 | }
|
| 267 |
|
| 268 |
|
| 269 | usleep_range(15000, 15100);
|
| 270 |
|
| 271 | gpiod_set_value_cansleep(cd->reset_gpio, 0);
|
| 272 |
|
| 273 |
|
| 274 | usleep_range(4000, 4100);
|
| 275 |
|
| 276 | error = goodix_berlin_dev_confirm(cd);
|
| 277 | if (error)
|
| 278 | goto err_dev_reset;
|
| 279 |
|
| 280 |
|
| 281 | msleep(GOODIX_BERLIN_NORMAL_RESET_DELAY_MS);
|
| 282 |
|
| 283 | return 0;
|
| 284 |
|
| 285 | err_dev_reset:
|
| 286 | gpiod_set_value_cansleep(cd->reset_gpio, 1);
|
| 287 | regulator_disable(cd->avdd);
|
| 288 | err_vddio_disable:
|
| 289 | regulator_disable(cd->vddio);
|
| 290 | return error;
|
| 291 | }
|
| 292 |
|
| 293 | static void goodix_berlin_power_off(struct goodix_berlin_core *cd)
|
| 294 | {
|
| 295 | gpiod_set_value_cansleep(cd->reset_gpio, 1);
|
| 296 | regulator_disable(cd->avdd);
|
| 297 | regulator_disable(cd->vddio);
|
| 298 | }
|
| 299 |
|
| 300 | static int goodix_berlin_read_version(struct goodix_berlin_core *cd)
|
| 301 | {
|
| 302 | int error;
|
| 303 |
|
| 304 | error = regmap_raw_read(cd->regmap, cd->ic_data->fw_version_info_addr,
|
| 305 | &cd->fw_version, sizeof(cd->fw_version));
|
| 306 | if (error) {
|
| 307 | dev_err(cd->dev, "error reading fw version, %d\n", error);
|
| 308 | return error;
|
| 309 | }
|
| 310 |
|
| 311 | if (!goodix_berlin_checksum_valid((u8 *)&cd->fw_version,
|
| 312 | sizeof(cd->fw_version))) {
|
| 313 | dev_err(cd->dev, "invalid fw version: checksum error\n");
|
| 314 | return -EINVAL;
|
| 315 | }
|
| 316 |
|
| 317 | return 0;
|
| 318 | }
|
| 319 |
|
| 320 |
|
| 321 | static int goodix_berlin_parse_ic_info(struct goodix_berlin_core *cd,
|
| 322 | const u8 *data, u16 length)
|
| 323 | {
|
| 324 | struct goodix_berlin_ic_info_misc *misc;
|
| 325 | unsigned int offset = 0;
|
| 326 |
|
| 327 | offset += sizeof(__le16);
|
| 328 | offset += sizeof(struct goodix_berlin_ic_info_version);
|
| 329 | offset += sizeof(struct goodix_berlin_ic_info_feature);
|
| 330 |
|
| 331 |
|
| 332 | offset += 4 * sizeof(u8);
|
| 333 | if (offset >= length)
|
| 334 | goto invalid_offset;
|
| 335 |
|
| 336 | #define ADVANCE_LE16_PARAMS() \
|
| 337 | do { \
|
| 338 | u8 param_num = data[offset++]; \
|
| 339 | offset += param_num * sizeof(__le16); \
|
| 340 | if (offset >= length) \
|
| 341 | goto invalid_offset; \
|
| 342 | } while (0)
|
| 343 | ADVANCE_LE16_PARAMS();
|
| 344 | ADVANCE_LE16_PARAMS();
|
| 345 | ADVANCE_LE16_PARAMS();
|
| 346 | ADVANCE_LE16_PARAMS();
|
| 347 | ADVANCE_LE16_PARAMS();
|
| 348 | #undef ADVANCE_LE16_PARAMS
|
| 349 |
|
| 350 | misc = (struct goodix_berlin_ic_info_misc *)&data[offset];
|
| 351 | cd->touch_data_addr = le32_to_cpu(misc->touch_data_addr);
|
| 352 |
|
| 353 | return 0;
|
| 354 |
|
| 355 | invalid_offset:
|
| 356 | dev_err(cd->dev, "ic_info length is invalid (offset %d length %d)\n",
|
| 357 | offset, length);
|
| 358 | return -EINVAL;
|
| 359 | }
|
| 360 |
|
| 361 | static int goodix_berlin_get_ic_info(struct goodix_berlin_core *cd)
|
| 362 | {
|
| 363 | u8 *afe_data __free(kfree) = NULL;
|
| 364 | __le16 length_raw;
|
| 365 | u16 length;
|
| 366 | int error;
|
| 367 |
|
| 368 | afe_data = kzalloc(GOODIX_BERLIN_IC_INFO_MAX_LEN, GFP_KERNEL);
|
| 369 | if (!afe_data)
|
| 370 | return -ENOMEM;
|
| 371 |
|
| 372 | error = regmap_raw_read(cd->regmap, cd->ic_data->ic_info_addr,
|
| 373 | &length_raw, sizeof(length_raw));
|
| 374 | if (error) {
|
| 375 | dev_err(cd->dev, "failed get ic info length, %d\n", error);
|
| 376 | return error;
|
| 377 | }
|
| 378 |
|
| 379 | length = le16_to_cpu(length_raw);
|
| 380 | if (length >= GOODIX_BERLIN_IC_INFO_MAX_LEN) {
|
| 381 | dev_err(cd->dev, "invalid ic info length %d\n", length);
|
| 382 | return -EINVAL;
|
| 383 | }
|
| 384 |
|
| 385 | error = regmap_raw_read(cd->regmap, cd->ic_data->ic_info_addr, afe_data,
|
| 386 | length);
|
| 387 | if (error) {
|
| 388 | dev_err(cd->dev, "failed get ic info data, %d\n", error);
|
| 389 | return error;
|
| 390 | }
|
| 391 |
|
| 392 |
|
| 393 | if (goodix_berlin_is_dummy_data(cd, afe_data, length)) {
|
| 394 | dev_err(cd->dev, "fw info data invalid\n");
|
| 395 | return -EINVAL;
|
| 396 | }
|
| 397 |
|
| 398 | if (!goodix_berlin_checksum_valid(afe_data, length)) {
|
| 399 | dev_err(cd->dev, "fw info checksum error\n");
|
| 400 | return -EINVAL;
|
| 401 | }
|
| 402 |
|
| 403 | error = goodix_berlin_parse_ic_info(cd, afe_data, length);
|
| 404 | if (error)
|
| 405 | return error;
|
| 406 |
|
| 407 |
|
| 408 | if (!cd->touch_data_addr) {
|
| 409 | dev_err(cd->dev, "touch_data_addr is null\n");
|
| 410 | return -EINVAL;
|
| 411 | }
|
| 412 |
|
| 413 | return 0;
|
| 414 | }
|
| 415 |
|
| 416 | static int goodix_berlin_get_remaining_contacts(struct goodix_berlin_core *cd,
|
| 417 | int n)
|
| 418 | {
|
| 419 | size_t offset = 2 * GOODIX_BERLIN_TOUCH_SIZE +
|
| 420 | GOODIX_BERLIN_CHECKSUM_SIZE;
|
| 421 | u32 addr = cd->touch_data_addr + GOODIX_BERLIN_HEADER_SIZE + offset;
|
| 422 | int error;
|
| 423 |
|
| 424 | error = regmap_raw_read(cd->regmap, addr,
|
| 425 | &cd->event.data[offset],
|
| 426 | (n - 2) * GOODIX_BERLIN_TOUCH_SIZE);
|
| 427 | if (error) {
|
| 428 | dev_err_ratelimited(cd->dev, "failed to get touch data, %d\n",
|
| 429 | error);
|
| 430 | return error;
|
| 431 | }
|
| 432 |
|
| 433 | return 0;
|
| 434 | }
|
| 435 |
|
| 436 | static void goodix_berlin_report_state(struct goodix_berlin_core *cd, int n)
|
| 437 | {
|
| 438 | struct goodix_berlin_touch *touch_data =
|
| 439 | (struct goodix_berlin_touch *)cd->event.data;
|
| 440 | struct goodix_berlin_touch *t;
|
| 441 | int i;
|
| 442 | u8 type, id;
|
| 443 |
|
| 444 | for (i = 0; i < n; i++) {
|
| 445 | t = &touch_data[i];
|
| 446 |
|
| 447 | type = FIELD_GET(GOODIX_BERLIN_POINT_TYPE_MASK, t->status);
|
| 448 | if (type == GOODIX_BERLIN_POINT_TYPE_STYLUS ||
|
| 449 | type == GOODIX_BERLIN_POINT_TYPE_STYLUS_HOVER) {
|
| 450 | dev_warn_once(cd->dev, "Stylus event type not handled\n");
|
| 451 | continue;
|
| 452 | }
|
| 453 |
|
| 454 | id = FIELD_GET(GOODIX_BERLIN_TOUCH_ID_MASK, t->status);
|
| 455 | if (id >= GOODIX_BERLIN_MAX_TOUCH) {
|
| 456 | dev_warn_ratelimited(cd->dev, "invalid finger id %d\n", id);
|
| 457 | continue;
|
| 458 | }
|
| 459 |
|
| 460 | input_mt_slot(cd->input_dev, id);
|
| 461 | input_mt_report_slot_state(cd->input_dev, MT_TOOL_FINGER, true);
|
| 462 |
|
| 463 | touchscreen_report_pos(cd->input_dev, &cd->props,
|
| 464 | __le16_to_cpu(t->x), __le16_to_cpu(t->y),
|
| 465 | true);
|
| 466 | input_report_abs(cd->input_dev, ABS_MT_TOUCH_MAJOR,
|
| 467 | __le16_to_cpu(t->w));
|
| 468 | }
|
| 469 |
|
| 470 | input_mt_sync_frame(cd->input_dev);
|
| 471 | input_sync(cd->input_dev);
|
| 472 | }
|
| 473 |
|
| 474 | static void goodix_berlin_touch_handler(struct goodix_berlin_core *cd)
|
| 475 | {
|
| 476 | u8 touch_num;
|
| 477 | int error;
|
| 478 |
|
| 479 | touch_num = FIELD_GET(GOODIX_BERLIN_TOUCH_COUNT_MASK,
|
| 480 | cd->event.hdr.request_type);
|
| 481 | if (touch_num > GOODIX_BERLIN_MAX_TOUCH) {
|
| 482 | dev_warn(cd->dev, "invalid touch num %d\n", touch_num);
|
| 483 | return;
|
| 484 | }
|
| 485 |
|
| 486 | if (touch_num > 2) {
|
| 487 |
|
| 488 | error = goodix_berlin_get_remaining_contacts(cd, touch_num);
|
| 489 | if (error)
|
| 490 | return;
|
| 491 | }
|
| 492 |
|
| 493 | if (touch_num) {
|
| 494 | int len = touch_num * GOODIX_BERLIN_TOUCH_SIZE +
|
| 495 | GOODIX_BERLIN_CHECKSUM_SIZE;
|
| 496 | if (!goodix_berlin_checksum_valid(cd->event.data, len)) {
|
| 497 | dev_err(cd->dev, "touch data checksum error: %*ph\n",
|
| 498 | len, cd->event.data);
|
| 499 | return;
|
| 500 | }
|
| 501 | }
|
| 502 |
|
| 503 | goodix_berlin_report_state(cd, touch_num);
|
| 504 | }
|
| 505 |
|
| 506 | static int goodix_berlin_request_handle_reset(struct goodix_berlin_core *cd)
|
| 507 | {
|
| 508 | gpiod_set_value_cansleep(cd->reset_gpio, 1);
|
| 509 | usleep_range(2000, 2100);
|
| 510 | gpiod_set_value_cansleep(cd->reset_gpio, 0);
|
| 511 |
|
| 512 | msleep(GOODIX_BERLIN_NORMAL_RESET_DELAY_MS);
|
| 513 |
|
| 514 | return 0;
|
| 515 | }
|
| 516 |
|
| 517 | static irqreturn_t goodix_berlin_irq(int irq, void *data)
|
| 518 | {
|
| 519 | struct goodix_berlin_core *cd = data;
|
| 520 | int error;
|
| 521 |
|
| 522 | |
| 523 | * First, read buffer with space for 2 touch events:
|
| 524 | * - GOODIX_BERLIN_HEADER_SIZE = 8 bytes
|
| 525 | * - GOODIX_BERLIN_TOUCH_SIZE * 2 = 16 bytes
|
| 526 | * - GOODIX_BERLIN_CHECKLSUM_SIZE = 2 bytes
|
| 527 | * For a total of 26 bytes.
|
| 528 | *
|
| 529 | * If only a single finger is reported, we will read 8 bytes more than
|
| 530 | * needed:
|
| 531 | * - bytes 0-7: Header (GOODIX_BERLIN_HEADER_SIZE)
|
| 532 | * - bytes 8-15: Finger 0 Data
|
| 533 | * - bytes 24-25: Checksum
|
| 534 | * - bytes 18-25: Unused 8 bytes
|
| 535 | *
|
| 536 | * If 2 fingers are reported, we would have read the exact needed
|
| 537 | * amount of data and checksum would be at the end of the buffer:
|
| 538 | * - bytes 0-7: Header (GOODIX_BERLIN_HEADER_SIZE)
|
| 539 | * - bytes 8-15: Finger 0 Bytes 0-7
|
| 540 | * - bytes 16-23: Finger 1 Bytes 0-7
|
| 541 | * - bytes 24-25: Checksum
|
| 542 | *
|
| 543 | * If more than 2 fingers were reported, the "Checksum" bytes would
|
| 544 | * in fact contain part of the next finger data, and then
|
| 545 | * goodix_berlin_get_remaining_contacts() would complete the buffer
|
| 546 | * with the missing bytes, including the trailing checksum.
|
| 547 | * For example, if 3 fingers are reported, then we would do:
|
| 548 | * Read 1:
|
| 549 | * - bytes 0-7: Header (GOODIX_BERLIN_HEADER_SIZE)
|
| 550 | * - bytes 8-15: Finger 0 Bytes 0-7
|
| 551 | * - bytes 16-23: Finger 1 Bytes 0-7
|
| 552 | * - bytes 24-25: Finger 2 Bytes 0-1
|
| 553 | * Read 2 (with length of (3 - 2) * 8 = 8 bytes):
|
| 554 | * - bytes 26-31: Finger 2 Bytes 2-7
|
| 555 | * - bytes 32-33: Checksum
|
| 556 |
|
| 557 | error = regmap_raw_read(cd->regmap, cd->touch_data_addr,
|
| 558 | &cd->event,
|
| 559 | GOODIX_BERLIN_HEADER_SIZE +
|
| 560 | 2 * GOODIX_BERLIN_TOUCH_SIZE +
|
| 561 | GOODIX_BERLIN_CHECKSUM_SIZE);
|
| 562 | if (error) {
|
| 563 | dev_warn_ratelimited(cd->dev,
|
| 564 | "failed get event head data: %d\n", error);
|
| 565 | goto out;
|
| 566 | }
|
| 567 |
|
| 568 | if (cd->event.hdr.status == 0)
|
| 569 | goto out;
|
| 570 |
|
| 571 | if (!goodix_berlin_checksum_valid((u8 *)&cd->event.hdr,
|
| 572 | GOODIX_BERLIN_HEADER_SIZE)) {
|
| 573 | dev_warn_ratelimited(cd->dev,
|
| 574 | "touch head checksum error: %*ph\n",
|
| 575 | (int)GOODIX_BERLIN_HEADER_SIZE,
|
| 576 | &cd->event.hdr);
|
| 577 | goto out_clear;
|
| 578 | }
|
| 579 |
|
| 580 | if (cd->event.hdr.status & GOODIX_BERLIN_TOUCH_EVENT)
|
| 581 | goodix_berlin_touch_handler(cd);
|
| 582 |
|
| 583 | if (cd->event.hdr.status & GOODIX_BERLIN_REQUEST_EVENT) {
|
| 584 | switch (cd->event.hdr.request_type) {
|
| 585 | case GOODIX_BERLIN_REQUEST_CODE_RESET:
|
| 586 | if (cd->reset_gpio)
|
| 587 | goodix_berlin_request_handle_reset(cd);
|
| 588 | break;
|
| 589 |
|
| 590 | default:
|
| 591 | dev_warn(cd->dev, "unsupported request code 0x%x\n",
|
| 592 | cd->event.hdr.request_type);
|
| 593 | }
|
| 594 | }
|
| 595 |
|
| 596 |
|
| 597 | out_clear:
|
| 598 |
|
| 599 | regmap_write(cd->regmap, cd->touch_data_addr, 0);
|
| 600 |
|
| 601 | out:
|
| 602 | return IRQ_HANDLED;
|
| 603 | }
|
| 604 |
|
| 605 | static int goodix_berlin_input_dev_config(struct goodix_berlin_core *cd,
|
| 606 | const struct input_id *id)
|
| 607 | {
|
| 608 | struct input_dev *input_dev;
|
| 609 | int error;
|
| 610 |
|
| 611 | input_dev = devm_input_allocate_device(cd->dev);
|
| 612 | if (!input_dev)
|
| 613 | return -ENOMEM;
|
| 614 |
|
| 615 | cd->input_dev = input_dev;
|
| 616 | input_set_drvdata(input_dev, cd);
|
| 617 |
|
| 618 | input_dev->name = "Goodix Berlin Capacitive TouchScreen";
|
| 619 | input_dev->phys = "input/ts";
|
| 620 |
|
| 621 | input_dev->id = *id;
|
| 622 |
|
| 623 | input_set_abs_params(cd->input_dev, ABS_MT_POSITION_X,
|
| 624 | 0, SZ_64K - 1, 0, 0);
|
| 625 | input_set_abs_params(cd->input_dev, ABS_MT_POSITION_Y,
|
| 626 | 0, SZ_64K - 1, 0, 0);
|
| 627 | input_set_abs_params(cd->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
|
| 628 |
|
| 629 | touchscreen_parse_properties(cd->input_dev, true, &cd->props);
|
| 630 |
|
| 631 | |
| 632 | * The resolution of these touchscreens is about 10 units/mm, the actual
|
| 633 | * resolution does not matter much since we set INPUT_PROP_DIRECT.
|
| 634 | * Set it to 10 to ensure userspace isn't off by an order of magnitude.
|
| 635 |
|
| 636 | input_abs_set_res(cd->input_dev, ABS_MT_POSITION_X, 10);
|
| 637 | input_abs_set_res(cd->input_dev, ABS_MT_POSITION_Y, 10);
|
| 638 |
|
| 639 | error = input_mt_init_slots(cd->input_dev, GOODIX_BERLIN_MAX_TOUCH,
|
| 640 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
|
| 641 | if (error)
|
| 642 | return error;
|
| 643 |
|
| 644 | error = input_register_device(cd->input_dev);
|
| 645 | if (error)
|
| 646 | return error;
|
| 647 |
|
| 648 | return 0;
|
| 649 | }
|
| 650 |
|
| 651 | static int goodix_berlin_suspend(struct device *dev)
|
| 652 | {
|
| 653 | struct goodix_berlin_core *cd = dev_get_drvdata(dev);
|
| 654 |
|
| 655 | disable_irq(cd->irq);
|
| 656 | goodix_berlin_power_off(cd);
|
| 657 |
|
| 658 | return 0;
|
| 659 | }
|
| 660 |
|
| 661 | static int goodix_berlin_resume(struct device *dev)
|
| 662 | {
|
| 663 | struct goodix_berlin_core *cd = dev_get_drvdata(dev);
|
| 664 | int error;
|
| 665 |
|
| 666 | error = goodix_berlin_power_on(cd);
|
| 667 | if (error)
|
| 668 | return error;
|
| 669 |
|
| 670 | enable_irq(cd->irq);
|
| 671 |
|
| 672 | return 0;
|
| 673 | }
|
| 674 |
|
| 675 | EXPORT_GPL_SIMPLE_DEV_PM_OPS(goodix_berlin_pm_ops,
|
| 676 | goodix_berlin_suspend, goodix_berlin_resume);
|
| 677 |
|
| 678 | static void goodix_berlin_power_off_act(void *data)
|
| 679 | {
|
| 680 | struct goodix_berlin_core *cd = data;
|
| 681 |
|
| 682 | goodix_berlin_power_off(cd);
|
| 683 | }
|
| 684 |
|
| 685 | static ssize_t registers_read(struct file *filp, struct kobject *kobj,
|
| 686 | const struct bin_attribute *bin_attr,
|
| 687 | char *buf, loff_t off, size_t count)
|
| 688 | {
|
| 689 | struct device *dev = kobj_to_dev(kobj);
|
| 690 | struct goodix_berlin_core *cd = dev_get_drvdata(dev);
|
| 691 | int error;
|
| 692 |
|
| 693 | error = regmap_raw_read(cd->regmap, off, buf, count);
|
| 694 |
|
| 695 | return error ? error : count;
|
| 696 | }
|
| 697 |
|
| 698 | static ssize_t registers_write(struct file *filp, struct kobject *kobj,
|
| 699 | const struct bin_attribute *bin_attr,
|
| 700 | char *buf, loff_t off, size_t count)
|
| 701 | {
|
| 702 | struct device *dev = kobj_to_dev(kobj);
|
| 703 | struct goodix_berlin_core *cd = dev_get_drvdata(dev);
|
| 704 | int error;
|
| 705 |
|
| 706 | error = regmap_raw_write(cd->regmap, off, buf, count);
|
| 707 |
|
| 708 | return error ? error : count;
|
| 709 | }
|
| 710 |
|
| 711 | static const BIN_ATTR_ADMIN_RW(registers, 0);
|
| 712 |
|
| 713 | static const struct bin_attribute *const goodix_berlin_bin_attrs[] = {
|
| 714 | &bin_attr_registers,
|
| 715 | NULL,
|
| 716 | };
|
| 717 |
|
| 718 | static const struct attribute_group goodix_berlin_attr_group = {
|
| 719 | .bin_attrs = goodix_berlin_bin_attrs,
|
| 720 | };
|
| 721 |
|
| 722 | const struct attribute_group *goodix_berlin_groups[] = {
|
| 723 | &goodix_berlin_attr_group,
|
| 724 | NULL,
|
| 725 | };
|
| 726 | EXPORT_SYMBOL_GPL(goodix_berlin_groups);
|
| 727 |
|
| 728 | int goodix_berlin_probe(struct device *dev, int irq, const struct input_id *id,
|
| 729 | struct regmap *regmap,
|
| 730 | const struct goodix_berlin_ic_data *ic_data)
|
| 731 | {
|
| 732 | struct goodix_berlin_core *cd;
|
| 733 | int error;
|
| 734 |
|
| 735 | if (irq <= 0) {
|
| 736 | dev_err(dev, "Missing interrupt number\n");
|
| 737 | return -EINVAL;
|
| 738 | }
|
| 739 |
|
| 740 | cd = devm_kzalloc(dev, sizeof(*cd), GFP_KERNEL);
|
| 741 | if (!cd)
|
| 742 | return -ENOMEM;
|
| 743 |
|
| 744 | cd->dev = dev;
|
| 745 | cd->regmap = regmap;
|
| 746 | cd->irq = irq;
|
| 747 | cd->ic_data = ic_data;
|
| 748 |
|
| 749 | cd->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
|
| 750 | if (IS_ERR(cd->reset_gpio))
|
| 751 | return dev_err_probe(dev, PTR_ERR(cd->reset_gpio),
|
| 752 | "Failed to request reset gpio\n");
|
| 753 |
|
| 754 | cd->avdd = devm_regulator_get(dev, "avdd");
|
| 755 | if (IS_ERR(cd->avdd))
|
| 756 | return dev_err_probe(dev, PTR_ERR(cd->avdd),
|
| 757 | "Failed to request avdd regulator\n");
|
| 758 |
|
| 759 | cd->vddio = devm_regulator_get(dev, "vddio");
|
| 760 | if (IS_ERR(cd->vddio))
|
| 761 | return dev_err_probe(dev, PTR_ERR(cd->vddio),
|
| 762 | "Failed to request vddio regulator\n");
|
| 763 |
|
| 764 | error = goodix_berlin_power_on(cd);
|
| 765 | if (error) {
|
| 766 | dev_err(dev, "failed power on");
|
| 767 | return error;
|
| 768 | }
|
| 769 |
|
| 770 | error = devm_add_action_or_reset(dev, goodix_berlin_power_off_act, cd);
|
| 771 | if (error)
|
| 772 | return error;
|
| 773 |
|
| 774 | error = goodix_berlin_read_version(cd);
|
| 775 | if (error) {
|
| 776 | dev_err(dev, "failed to get version info");
|
| 777 | return error;
|
| 778 | }
|
| 779 |
|
| 780 | error = goodix_berlin_get_ic_info(cd);
|
| 781 | if (error) {
|
| 782 | dev_err(dev, "invalid ic info, abort");
|
| 783 | return error;
|
| 784 | }
|
| 785 |
|
| 786 | error = goodix_berlin_input_dev_config(cd, id);
|
| 787 | if (error) {
|
| 788 | dev_err(dev, "failed set input device");
|
| 789 | return error;
|
| 790 | }
|
| 791 |
|
| 792 | error = devm_request_threaded_irq(dev, cd->irq, NULL, goodix_berlin_irq,
|
| 793 | IRQF_ONESHOT, "goodix-berlin", cd);
|
| 794 | if (error) {
|
| 795 | dev_err(dev, "request threaded irq failed: %d\n", error);
|
| 796 | return error;
|
| 797 | }
|
| 798 |
|
| 799 | dev_set_drvdata(dev, cd);
|
| 800 |
|
| 801 | dev_dbg(dev, "Goodix Berlin %s Touchscreen Controller",
|
| 802 | cd->fw_version.patch_pid);
|
| 803 |
|
| 804 | return 0;
|
| 805 | }
|
| 806 | EXPORT_SYMBOL_GPL(goodix_berlin_probe);
|
| 807 |
|
| 808 | MODULE_LICENSE("GPL");
|
| 809 | MODULE_DESCRIPTION("Goodix Berlin Core Touchscreen driver");
|
| 810 | MODULE_AUTHOR("Neil Armstrong <[email protected]>");
|