| 1 | |
| 2 | |
| 3 | * Driver for I2C connected EETI EXC3000 multiple touch controller
|
| 4 | *
|
| 5 | * Copyright (C) 2017 Ahmet Inan <[email protected]>
|
| 6 | *
|
| 7 | * minimal implementation based on egalax_ts.c and egalax_i2c.c
|
| 8 |
|
| 9 |
|
| 10 | #include <linux/acpi.h>
|
| 11 | #include <linux/bitops.h>
|
| 12 | #include <linux/delay.h>
|
| 13 | #include <linux/device.h>
|
| 14 | #include <linux/gpio/consumer.h>
|
| 15 | #include <linux/i2c.h>
|
| 16 | #include <linux/input.h>
|
| 17 | #include <linux/input/mt.h>
|
| 18 | #include <linux/input/touchscreen.h>
|
| 19 | #include <linux/interrupt.h>
|
| 20 | #include <linux/module.h>
|
| 21 | #include <linux/of.h>
|
| 22 | #include <linux/regulator/consumer.h>
|
| 23 | #include <linux/sizes.h>
|
| 24 | #include <linux/timer.h>
|
| 25 | #include <linux/unaligned.h>
|
| 26 |
|
| 27 | #define EXC3000_NUM_SLOTS 10
|
| 28 | #define EXC3000_SLOTS_PER_FRAME 5
|
| 29 | #define EXC3000_LEN_FRAME 66
|
| 30 | #define EXC3000_LEN_VENDOR_REQUEST 68
|
| 31 | #define EXC3000_LEN_POINT 10
|
| 32 |
|
| 33 | #define EXC3000_LEN_MODEL_NAME 16
|
| 34 | #define EXC3000_LEN_FW_VERSION 16
|
| 35 |
|
| 36 | #define EXC3000_VENDOR_EVENT 0x03
|
| 37 | #define EXC3000_MT1_EVENT 0x06
|
| 38 | #define EXC3000_MT2_EVENT 0x18
|
| 39 |
|
| 40 | #define EXC3000_TIMEOUT_MS 100
|
| 41 |
|
| 42 | #define EXC3000_RESET_MS 10
|
| 43 | #define EXC3000_READY_MS 100
|
| 44 |
|
| 45 | static const struct i2c_device_id exc3000_id[];
|
| 46 |
|
| 47 | struct eeti_dev_info {
|
| 48 | const char *name;
|
| 49 | int max_xy;
|
| 50 | };
|
| 51 |
|
| 52 | enum eeti_dev_id {
|
| 53 | EETI_EXC3000,
|
| 54 | EETI_EXC80H60,
|
| 55 | EETI_EXC80H84,
|
| 56 | EETI_EXC81W32,
|
| 57 | };
|
| 58 |
|
| 59 | static struct eeti_dev_info exc3000_info[] = {
|
| 60 | [EETI_EXC3000] = {
|
| 61 | .name = "EETI EXC3000 Touch Screen",
|
| 62 | .max_xy = SZ_4K - 1,
|
| 63 | },
|
| 64 | [EETI_EXC80H60] = {
|
| 65 | .name = "EETI EXC80H60 Touch Screen",
|
| 66 | .max_xy = SZ_16K - 1,
|
| 67 | },
|
| 68 | [EETI_EXC80H84] = {
|
| 69 | .name = "EETI EXC80H84 Touch Screen",
|
| 70 | .max_xy = SZ_16K - 1,
|
| 71 | },
|
| 72 | [EETI_EXC81W32] = {
|
| 73 | .name = "EETI EXC81W32 Touch Screen",
|
| 74 | .max_xy = SZ_16K - 1,
|
| 75 | },
|
| 76 | };
|
| 77 |
|
| 78 | struct exc3000_data {
|
| 79 | struct i2c_client *client;
|
| 80 | const struct eeti_dev_info *info;
|
| 81 | struct input_dev *input;
|
| 82 | struct touchscreen_properties prop;
|
| 83 | struct gpio_desc *reset;
|
| 84 | struct timer_list timer;
|
| 85 | u8 buf[2 * EXC3000_LEN_FRAME];
|
| 86 | struct completion wait_event;
|
| 87 | struct mutex query_lock;
|
| 88 | };
|
| 89 |
|
| 90 | static void exc3000_report_slots(struct input_dev *input,
|
| 91 | struct touchscreen_properties *prop,
|
| 92 | const u8 *buf, int num)
|
| 93 | {
|
| 94 | for (; num--; buf += EXC3000_LEN_POINT) {
|
| 95 | if (buf[0] & BIT(0)) {
|
| 96 | input_mt_slot(input, buf[1]);
|
| 97 | input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
|
| 98 | touchscreen_report_pos(input, prop,
|
| 99 | get_unaligned_le16(buf + 2),
|
| 100 | get_unaligned_le16(buf + 4),
|
| 101 | true);
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | static void exc3000_timer(struct timer_list *t)
|
| 107 | {
|
| 108 | struct exc3000_data *data = timer_container_of(data, t, timer);
|
| 109 |
|
| 110 | input_mt_sync_frame(data->input);
|
| 111 | input_sync(data->input);
|
| 112 | }
|
| 113 |
|
| 114 | static inline void exc3000_schedule_timer(struct exc3000_data *data)
|
| 115 | {
|
| 116 | mod_timer(&data->timer, jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS));
|
| 117 | }
|
| 118 |
|
| 119 | static void exc3000_shutdown_timer(void *timer)
|
| 120 | {
|
| 121 | timer_shutdown_sync(timer);
|
| 122 | }
|
| 123 |
|
| 124 | static int exc3000_read_frame(struct exc3000_data *data, u8 *buf)
|
| 125 | {
|
| 126 | struct i2c_client *client = data->client;
|
| 127 | int ret;
|
| 128 |
|
| 129 | ret = i2c_master_send(client, "'", 2);
|
| 130 | if (ret < 0)
|
| 131 | return ret;
|
| 132 |
|
| 133 | if (ret != 2)
|
| 134 | return -EIO;
|
| 135 |
|
| 136 | ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME);
|
| 137 | if (ret < 0)
|
| 138 | return ret;
|
| 139 |
|
| 140 | if (ret != EXC3000_LEN_FRAME)
|
| 141 | return -EIO;
|
| 142 |
|
| 143 | if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME)
|
| 144 | return -EINVAL;
|
| 145 |
|
| 146 | return 0;
|
| 147 | }
|
| 148 |
|
| 149 | static int exc3000_handle_mt_event(struct exc3000_data *data)
|
| 150 | {
|
| 151 | struct input_dev *input = data->input;
|
| 152 | int ret, total_slots;
|
| 153 | u8 *buf = data->buf;
|
| 154 |
|
| 155 | total_slots = buf[3];
|
| 156 | if (!total_slots || total_slots > EXC3000_NUM_SLOTS) {
|
| 157 | ret = -EINVAL;
|
| 158 | goto out_fail;
|
| 159 | }
|
| 160 |
|
| 161 | if (total_slots > EXC3000_SLOTS_PER_FRAME) {
|
| 162 |
|
| 163 | ret = exc3000_read_frame(data, buf + EXC3000_LEN_FRAME);
|
| 164 | if (ret)
|
| 165 | goto out_fail;
|
| 166 |
|
| 167 |
|
| 168 | if (buf[EXC3000_LEN_FRAME + 3] != 0) {
|
| 169 | ret = -EINVAL;
|
| 170 | goto out_fail;
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | |
| 175 | * We read full state successfully, no contacts will be "stuck".
|
| 176 |
|
| 177 | timer_delete_sync(&data->timer);
|
| 178 |
|
| 179 | while (total_slots > 0) {
|
| 180 | int slots = min(total_slots, EXC3000_SLOTS_PER_FRAME);
|
| 181 |
|
| 182 | exc3000_report_slots(input, &data->prop, buf + 4, slots);
|
| 183 | total_slots -= slots;
|
| 184 | buf += EXC3000_LEN_FRAME;
|
| 185 | }
|
| 186 |
|
| 187 | input_mt_sync_frame(input);
|
| 188 | input_sync(input);
|
| 189 |
|
| 190 | return 0;
|
| 191 |
|
| 192 | out_fail:
|
| 193 |
|
| 194 | exc3000_schedule_timer(data);
|
| 195 |
|
| 196 | return ret;
|
| 197 | }
|
| 198 |
|
| 199 | static irqreturn_t exc3000_interrupt(int irq, void *dev_id)
|
| 200 | {
|
| 201 | struct exc3000_data *data = dev_id;
|
| 202 | u8 *buf = data->buf;
|
| 203 | int ret;
|
| 204 |
|
| 205 | ret = exc3000_read_frame(data, buf);
|
| 206 | if (ret) {
|
| 207 |
|
| 208 | exc3000_schedule_timer(data);
|
| 209 | goto out;
|
| 210 | }
|
| 211 |
|
| 212 | switch (buf[2]) {
|
| 213 | case EXC3000_VENDOR_EVENT:
|
| 214 | complete(&data->wait_event);
|
| 215 | break;
|
| 216 |
|
| 217 | case EXC3000_MT1_EVENT:
|
| 218 | case EXC3000_MT2_EVENT:
|
| 219 | exc3000_handle_mt_event(data);
|
| 220 | break;
|
| 221 |
|
| 222 | default:
|
| 223 | break;
|
| 224 | }
|
| 225 |
|
| 226 | out:
|
| 227 | return IRQ_HANDLED;
|
| 228 | }
|
| 229 |
|
| 230 | static int exc3000_vendor_data_request(struct exc3000_data *data, u8 *request,
|
| 231 | u8 request_len, u8 *response, int timeout)
|
| 232 | {
|
| 233 | u8 buf[EXC3000_LEN_VENDOR_REQUEST] = { 0x67, 0x00, 0x42, 0x00, 0x03 };
|
| 234 | int ret;
|
| 235 | unsigned long time_left;
|
| 236 |
|
| 237 | guard(mutex)(&data->query_lock);
|
| 238 |
|
| 239 | reinit_completion(&data->wait_event);
|
| 240 |
|
| 241 | buf[5] = request_len;
|
| 242 | memcpy(&buf[6], request, request_len);
|
| 243 |
|
| 244 | ret = i2c_master_send(data->client, buf, EXC3000_LEN_VENDOR_REQUEST);
|
| 245 | if (ret < 0)
|
| 246 | return ret;
|
| 247 |
|
| 248 | time_left = wait_for_completion_timeout(&data->wait_event,
|
| 249 | timeout * HZ);
|
| 250 | if (time_left == 0)
|
| 251 | return -ETIMEDOUT;
|
| 252 |
|
| 253 | if (data->buf[3] >= EXC3000_LEN_FRAME)
|
| 254 | return -ENOSPC;
|
| 255 |
|
| 256 | memcpy(response, &data->buf[4], data->buf[3]);
|
| 257 | return data->buf[3];
|
| 258 | }
|
| 259 |
|
| 260 | static ssize_t fw_version_show(struct device *dev,
|
| 261 | struct device_attribute *attr, char *buf)
|
| 262 | {
|
| 263 | struct i2c_client *client = to_i2c_client(dev);
|
| 264 | struct exc3000_data *data = i2c_get_clientdata(client);
|
| 265 | u8 response[EXC3000_LEN_FRAME];
|
| 266 | int ret;
|
| 267 |
|
| 268 |
|
| 269 | ret = exc3000_vendor_data_request(data,
|
| 270 | (u8[]){0x39, 0x02}, 2, response, 1);
|
| 271 | if (ret < 0)
|
| 272 | return ret;
|
| 273 |
|
| 274 | |
| 275 | * If the bootloader version is non-zero then the device is in
|
| 276 | * bootloader mode and won't answer a query for the application FW
|
| 277 | * version, so we just use the bootloader version info.
|
| 278 |
|
| 279 | if (response[2] || response[3])
|
| 280 | return sprintf(buf, "%d.%d\n", response[2], response[3]);
|
| 281 |
|
| 282 | ret = exc3000_vendor_data_request(data, (u8[]){'D'}, 1, response, 1);
|
| 283 | if (ret < 0)
|
| 284 | return ret;
|
| 285 |
|
| 286 | return sprintf(buf, "%s\n", &response[1]);
|
| 287 | }
|
| 288 | static DEVICE_ATTR_RO(fw_version);
|
| 289 |
|
| 290 | static ssize_t model_show(struct device *dev,
|
| 291 | struct device_attribute *attr, char *buf)
|
| 292 | {
|
| 293 | struct i2c_client *client = to_i2c_client(dev);
|
| 294 | struct exc3000_data *data = i2c_get_clientdata(client);
|
| 295 | u8 response[EXC3000_LEN_FRAME];
|
| 296 | int ret;
|
| 297 |
|
| 298 | ret = exc3000_vendor_data_request(data, (u8[]){'E'}, 1, response, 1);
|
| 299 | if (ret < 0)
|
| 300 | return ret;
|
| 301 |
|
| 302 | return sprintf(buf, "%s\n", &response[1]);
|
| 303 | }
|
| 304 | static DEVICE_ATTR_RO(model);
|
| 305 |
|
| 306 | static ssize_t type_show(struct device *dev,
|
| 307 | struct device_attribute *attr, char *buf)
|
| 308 | {
|
| 309 | struct i2c_client *client = to_i2c_client(dev);
|
| 310 | struct exc3000_data *data = i2c_get_clientdata(client);
|
| 311 | u8 response[EXC3000_LEN_FRAME];
|
| 312 | int ret;
|
| 313 |
|
| 314 | ret = exc3000_vendor_data_request(data, (u8[]){'F'}, 1, response, 1);
|
| 315 | if (ret < 0)
|
| 316 | return ret;
|
| 317 |
|
| 318 | return sprintf(buf, "%s\n", &response[1]);
|
| 319 | }
|
| 320 | static DEVICE_ATTR_RO(type);
|
| 321 |
|
| 322 | static struct attribute *exc3000_attrs[] = {
|
| 323 | &dev_attr_fw_version.attr,
|
| 324 | &dev_attr_model.attr,
|
| 325 | &dev_attr_type.attr,
|
| 326 | NULL
|
| 327 | };
|
| 328 | ATTRIBUTE_GROUPS(exc3000);
|
| 329 |
|
| 330 | static int exc3000_probe(struct i2c_client *client)
|
| 331 | {
|
| 332 | struct exc3000_data *data;
|
| 333 | struct input_dev *input;
|
| 334 | int error, max_xy, retry;
|
| 335 |
|
| 336 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
|
| 337 | if (!data)
|
| 338 | return -ENOMEM;
|
| 339 |
|
| 340 | data->client = client;
|
| 341 | data->info = device_get_match_data(&client->dev);
|
| 342 | if (!data->info) {
|
| 343 | enum eeti_dev_id eeti_dev_id =
|
| 344 | i2c_match_id(exc3000_id, client)->driver_data;
|
| 345 | data->info = &exc3000_info[eeti_dev_id];
|
| 346 | }
|
| 347 | timer_setup(&data->timer, exc3000_timer, 0);
|
| 348 | init_completion(&data->wait_event);
|
| 349 | mutex_init(&data->query_lock);
|
| 350 |
|
| 351 | data->reset = devm_gpiod_get_optional(&client->dev, "reset",
|
| 352 | GPIOD_OUT_HIGH);
|
| 353 | if (IS_ERR(data->reset))
|
| 354 | return PTR_ERR(data->reset);
|
| 355 |
|
| 356 |
|
| 357 | error = devm_regulator_get_enable(&client->dev, "vdd");
|
| 358 | if (error && error != -ENODEV)
|
| 359 | return dev_err_probe(&client->dev, error,
|
| 360 | "failed to request vdd regulator\n");
|
| 361 |
|
| 362 | if (data->reset) {
|
| 363 | msleep(EXC3000_RESET_MS);
|
| 364 | gpiod_set_value_cansleep(data->reset, 0);
|
| 365 | msleep(EXC3000_READY_MS);
|
| 366 | }
|
| 367 |
|
| 368 | input = devm_input_allocate_device(&client->dev);
|
| 369 | if (!input)
|
| 370 | return -ENOMEM;
|
| 371 |
|
| 372 | data->input = input;
|
| 373 | input_set_drvdata(input, data);
|
| 374 |
|
| 375 | input->name = data->info->name;
|
| 376 | input->id.bustype = BUS_I2C;
|
| 377 |
|
| 378 | max_xy = data->info->max_xy;
|
| 379 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
|
| 380 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
|
| 381 |
|
| 382 | touchscreen_parse_properties(input, true, &data->prop);
|
| 383 |
|
| 384 | error = input_mt_init_slots(input, EXC3000_NUM_SLOTS,
|
| 385 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
|
| 386 | if (error)
|
| 387 | return error;
|
| 388 |
|
| 389 | error = input_register_device(input);
|
| 390 | if (error)
|
| 391 | return error;
|
| 392 |
|
| 393 | error = devm_add_action_or_reset(&client->dev, exc3000_shutdown_timer,
|
| 394 | &data->timer);
|
| 395 | if (error)
|
| 396 | return error;
|
| 397 |
|
| 398 | error = devm_request_threaded_irq(&client->dev, client->irq,
|
| 399 | NULL, exc3000_interrupt, IRQF_ONESHOT,
|
| 400 | client->name, data);
|
| 401 | if (error)
|
| 402 | return error;
|
| 403 |
|
| 404 | |
| 405 | * I²C does not have built-in recovery, so retry on failure. This
|
| 406 | * ensures, that the device probe will not fail for temporary issues
|
| 407 | * on the bus. This is not needed for the sysfs calls (userspace
|
| 408 | * will receive the error code and can start another query) and
|
| 409 | * cannot be done for touch events (but that only means loosing one
|
| 410 | * or two touch events anyways).
|
| 411 |
|
| 412 | for (retry = 0; retry < 3; retry++) {
|
| 413 | u8 response[EXC3000_LEN_FRAME];
|
| 414 |
|
| 415 | error = exc3000_vendor_data_request(data, (u8[]){'E'}, 1,
|
| 416 | response, 1);
|
| 417 | if (error > 0) {
|
| 418 | dev_dbg(&client->dev, "TS Model: %s", &response[1]);
|
| 419 | error = 0;
|
| 420 | break;
|
| 421 | }
|
| 422 | dev_warn(&client->dev, "Retry %d get EETI EXC3000 model: %d\n",
|
| 423 | retry + 1, error);
|
| 424 | }
|
| 425 |
|
| 426 | if (error)
|
| 427 | return error;
|
| 428 |
|
| 429 | i2c_set_clientdata(client, data);
|
| 430 |
|
| 431 | return 0;
|
| 432 | }
|
| 433 |
|
| 434 | static const struct i2c_device_id exc3000_id[] = {
|
| 435 | { .name = "exc3000", .driver_data = EETI_EXC3000 },
|
| 436 | { .name = "exc80h60", .driver_data = EETI_EXC80H60 },
|
| 437 | { .name = "exc80h84", .driver_data = EETI_EXC80H84 },
|
| 438 | { .name = "exc81w32", .driver_data = EETI_EXC81W32 },
|
| 439 | { }
|
| 440 | };
|
| 441 | MODULE_DEVICE_TABLE(i2c, exc3000_id);
|
| 442 |
|
| 443 | #ifdef CONFIG_OF
|
| 444 | static const struct of_device_id exc3000_of_match[] = {
|
| 445 | { .compatible = "eeti,exc3000", .data = &exc3000_info[EETI_EXC3000] },
|
| 446 | { .compatible = "eeti,exc80h60", .data = &exc3000_info[EETI_EXC80H60] },
|
| 447 | { .compatible = "eeti,exc80h84", .data = &exc3000_info[EETI_EXC80H84] },
|
| 448 | { .compatible = "eeti,exc81w32", .data = &exc3000_info[EETI_EXC81W32] },
|
| 449 | { }
|
| 450 | };
|
| 451 | MODULE_DEVICE_TABLE(of, exc3000_of_match);
|
| 452 | #endif
|
| 453 |
|
| 454 | #ifdef CONFIG_ACPI
|
| 455 | static const struct acpi_device_id exc3000_acpi_match[] = {
|
| 456 | { "EGA00001", .driver_data = (kernel_ulong_t)&exc3000_info[EETI_EXC80H60] },
|
| 457 | { }
|
| 458 | };
|
| 459 | MODULE_DEVICE_TABLE(acpi, exc3000_acpi_match);
|
| 460 | #endif
|
| 461 |
|
| 462 | static struct i2c_driver exc3000_driver = {
|
| 463 | .driver = {
|
| 464 | .name = "exc3000",
|
| 465 | .dev_groups = exc3000_groups,
|
| 466 | .of_match_table = of_match_ptr(exc3000_of_match),
|
| 467 | .acpi_match_table = ACPI_PTR(exc3000_acpi_match),
|
| 468 | },
|
| 469 | .id_table = exc3000_id,
|
| 470 | .probe = exc3000_probe,
|
| 471 | };
|
| 472 |
|
| 473 | module_i2c_driver(exc3000_driver);
|
| 474 |
|
| 475 | MODULE_AUTHOR("Ahmet Inan <[email protected]>");
|
| 476 | MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver");
|
| 477 | MODULE_LICENSE("GPL v2");
|