]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/input/touchscreen/elants_i2c.c
Input: elants_i2c - report resolution of ABS_MT_TOUCH_MAJOR by FW information.
[mirror_ubuntu-jammy-kernel.git] / drivers / input / touchscreen / elants_i2c.c
CommitLineData
ac1dc6b2 1// SPDX-License-Identifier: GPL-2.0-only
66aee900
SL
2/*
3 * Elan Microelectronics touch panels with I2C interface
4 *
5 * Copyright (C) 2014 Elan Microelectronics Corporation.
6 * Scott Liu <scott.liu@emc.com.tw>
7 *
8 * This code is partly based on hid-multitouch.c:
9 *
10 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
11 * Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
12 * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
13 *
66aee900
SL
14 * This code is partly based on i2c-hid.c:
15 *
16 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
17 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
18 * Copyright (c) 2012 Red Hat, Inc
19 */
20
66aee900 21
f27ad893 22#include <linux/bits.h>
66aee900
SL
23#include <linux/module.h>
24#include <linux/input.h>
25#include <linux/interrupt.h>
4c83c071 26#include <linux/irq.h>
66aee900
SL
27#include <linux/platform_device.h>
28#include <linux/async.h>
29#include <linux/i2c.h>
30#include <linux/delay.h>
31#include <linux/uaccess.h>
32#include <linux/buffer_head.h>
66aee900
SL
33#include <linux/slab.h>
34#include <linux/firmware.h>
66aee900 35#include <linux/input/mt.h>
68334dba 36#include <linux/input/touchscreen.h>
66aee900
SL
37#include <linux/acpi.h>
38#include <linux/of.h>
afe10358
DT
39#include <linux/gpio/consumer.h>
40#include <linux/regulator/consumer.h>
66aee900
SL
41#include <asm/unaligned.h>
42
43/* Device, Driver information */
44#define DEVICE_NAME "elants_i2c"
66aee900
SL
45
46/* Convert from rows or columns into resolution */
47#define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m))
48
49/* FW header data */
50#define HEADER_SIZE 4
51#define FW_HDR_TYPE 0
52#define FW_HDR_COUNT 1
53#define FW_HDR_LENGTH 2
54
55/* Buffer mode Queue Header information */
56#define QUEUE_HEADER_SINGLE 0x62
57#define QUEUE_HEADER_NORMAL 0X63
58#define QUEUE_HEADER_WAIT 0x64
59
60/* Command header definition */
61#define CMD_HEADER_WRITE 0x54
62#define CMD_HEADER_READ 0x53
63#define CMD_HEADER_6B_READ 0x5B
f0b57e19 64#define CMD_HEADER_ROM_READ 0x96
66aee900
SL
65#define CMD_HEADER_RESP 0x52
66#define CMD_HEADER_6B_RESP 0x9B
f0b57e19 67#define CMD_HEADER_ROM_RESP 0x95
66aee900
SL
68#define CMD_HEADER_HELLO 0x55
69#define CMD_HEADER_REK 0x66
70
71/* FW position data */
72#define PACKET_SIZE 55
73#define MAX_CONTACT_NUM 10
74#define FW_POS_HEADER 0
75#define FW_POS_STATE 1
76#define FW_POS_TOTAL 2
77#define FW_POS_XY 3
f27ad893 78#define FW_POS_TOOL_TYPE 33
66aee900
SL
79#define FW_POS_CHECKSUM 34
80#define FW_POS_WIDTH 35
81#define FW_POS_PRESSURE 45
82
83#define HEADER_REPORT_10_FINGER 0x62
84
85/* Header (4 bytes) plus 3 fill 10-finger packets */
86#define MAX_PACKET_SIZE 169
87
88#define BOOT_TIME_DELAY_MS 50
89
90/* FW read command, 0x53 0x?? 0x0, 0x01 */
91#define E_ELAN_INFO_FW_VER 0x00
92#define E_ELAN_INFO_BC_VER 0x10
cf520c64 93#define E_ELAN_INFO_REK 0xE0
66aee900
SL
94#define E_ELAN_INFO_TEST_VER 0xE0
95#define E_ELAN_INFO_FW_ID 0xF0
96#define E_INFO_OSR 0xD6
97#define E_INFO_PHY_SCAN 0xD7
98#define E_INFO_PHY_DRIVER 0xD8
99
100#define MAX_RETRIES 3
101#define MAX_FW_UPDATE_RETRIES 30
102
103#define ELAN_FW_PAGESIZE 132
66aee900
SL
104
105/* calibration timeout definition */
22c15e5e 106#define ELAN_CALI_TIMEOUT_MSEC 12000
66aee900 107
afe10358
DT
108#define ELAN_POWERON_DELAY_USEC 500
109#define ELAN_RESET_DELAY_MSEC 20
110
66aee900
SL
111enum elants_state {
112 ELAN_STATE_NORMAL,
113 ELAN_WAIT_QUEUE_HEADER,
114 ELAN_WAIT_RECALIBRATION,
115};
116
117enum elants_iap_mode {
118 ELAN_IAP_OPERATIONAL,
119 ELAN_IAP_RECOVERY,
120};
121
122/* struct elants_data - represents state of Elan touchscreen device */
123struct elants_data {
124 struct i2c_client *client;
125 struct input_dev *input;
126
afe10358
DT
127 struct regulator *vcc33;
128 struct regulator *vccio;
129 struct gpio_desc *reset_gpio;
130
66aee900
SL
131 u16 fw_version;
132 u8 test_version;
133 u8 solution_version;
134 u8 bc_version;
135 u8 iap_version;
136 u16 hw_version;
4238e52c 137 u8 major_res;
66aee900
SL
138 unsigned int x_res; /* resolution in units/mm */
139 unsigned int y_res;
140 unsigned int x_max;
141 unsigned int y_max;
68334dba 142 struct touchscreen_properties prop;
66aee900
SL
143
144 enum elants_state state;
145 enum elants_iap_mode iap_mode;
146
147 /* Guards against concurrent access to the device via sysfs */
148 struct mutex sysfs_mutex;
149
150 u8 cmd_resp[HEADER_SIZE];
151 struct completion cmd_done;
152
66aee900 153 bool wake_irq_enabled;
afe10358 154 bool keep_power_in_suspend;
00f73f97
SB
155
156 /* Must be last to be used for DMA operations */
157 u8 buf[MAX_PACKET_SIZE] ____cacheline_aligned;
66aee900
SL
158};
159
160static int elants_i2c_send(struct i2c_client *client,
161 const void *data, size_t size)
162{
163 int ret;
164
165 ret = i2c_master_send(client, data, size);
166 if (ret == size)
167 return 0;
168
169 if (ret >= 0)
170 ret = -EIO;
171
172 dev_err(&client->dev, "%s failed (%*ph): %d\n",
173 __func__, (int)size, data, ret);
174
175 return ret;
176}
177
178static int elants_i2c_read(struct i2c_client *client, void *data, size_t size)
179{
180 int ret;
181
182 ret = i2c_master_recv(client, data, size);
183 if (ret == size)
184 return 0;
185
186 if (ret >= 0)
187 ret = -EIO;
188
189 dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
190
191 return ret;
192}
193
194static int elants_i2c_execute_command(struct i2c_client *client,
195 const u8 *cmd, size_t cmd_size,
918e2844
MM
196 u8 *resp, size_t resp_size,
197 int retries, const char *cmd_name)
66aee900
SL
198{
199 struct i2c_msg msgs[2];
200 int ret;
201 u8 expected_response;
202
203 switch (cmd[0]) {
204 case CMD_HEADER_READ:
205 expected_response = CMD_HEADER_RESP;
206 break;
207
208 case CMD_HEADER_6B_READ:
209 expected_response = CMD_HEADER_6B_RESP;
210 break;
211
f0b57e19
JC
212 case CMD_HEADER_ROM_READ:
213 expected_response = CMD_HEADER_ROM_RESP;
214 break;
215
66aee900 216 default:
918e2844
MM
217 dev_err(&client->dev, "(%s): invalid command: %*ph\n",
218 cmd_name, (int)cmd_size, cmd);
66aee900
SL
219 return -EINVAL;
220 }
221
918e2844
MM
222 for (;;) {
223 msgs[0].addr = client->addr;
224 msgs[0].flags = client->flags & I2C_M_TEN;
225 msgs[0].len = cmd_size;
226 msgs[0].buf = (u8 *)cmd;
227
228 msgs[1].addr = client->addr;
229 msgs[1].flags = (client->flags & I2C_M_TEN) | I2C_M_RD;
230 msgs[1].flags |= I2C_M_RD;
231 msgs[1].len = resp_size;
232 msgs[1].buf = resp;
233
234 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
235 if (ret < 0) {
236 if (--retries > 0) {
237 dev_dbg(&client->dev,
238 "(%s) I2C transfer failed: %pe (retrying)\n",
239 cmd_name, ERR_PTR(ret));
240 continue;
241 }
66aee900 242
918e2844
MM
243 dev_err(&client->dev,
244 "(%s) I2C transfer failed: %pe\n",
245 cmd_name, ERR_PTR(ret));
246 return ret;
247 }
66aee900 248
918e2844
MM
249 if (ret != ARRAY_SIZE(msgs) ||
250 resp[FW_HDR_TYPE] != expected_response) {
251 if (--retries > 0) {
252 dev_dbg(&client->dev,
253 "(%s) unexpected response: %*ph (retrying)\n",
254 cmd_name, ret, resp);
255 continue;
256 }
66aee900 257
918e2844
MM
258 dev_err(&client->dev,
259 "(%s) unexpected response: %*ph\n",
260 cmd_name, ret, resp);
261 return -EIO;
262 }
66aee900 263
918e2844
MM
264 return 0;
265 }
66aee900
SL
266}
267
268static int elants_i2c_calibrate(struct elants_data *ts)
269{
270 struct i2c_client *client = ts->client;
271 int ret, error;
272 static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A };
273 static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 };
274 static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 };
275
276 disable_irq(client->irq);
277
278 ts->state = ELAN_WAIT_RECALIBRATION;
279 reinit_completion(&ts->cmd_done);
280
281 elants_i2c_send(client, w_flashkey, sizeof(w_flashkey));
282 elants_i2c_send(client, rek, sizeof(rek));
283
284 enable_irq(client->irq);
285
286 ret = wait_for_completion_interruptible_timeout(&ts->cmd_done,
287 msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC));
288
289 ts->state = ELAN_STATE_NORMAL;
290
291 if (ret <= 0) {
292 error = ret < 0 ? ret : -ETIMEDOUT;
293 dev_err(&client->dev,
294 "error while waiting for calibration to complete: %d\n",
295 error);
296 return error;
297 }
298
299 if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) {
300 dev_err(&client->dev,
301 "unexpected calibration response: %*ph\n",
302 (int)sizeof(ts->cmd_resp), ts->cmd_resp);
303 return -EINVAL;
304 }
305
306 return 0;
307}
308
309static int elants_i2c_sw_reset(struct i2c_client *client)
310{
311 const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 };
312 int error;
313
314 error = elants_i2c_send(client, soft_rst_cmd,
315 sizeof(soft_rst_cmd));
316 if (error) {
317 dev_err(&client->dev, "software reset failed: %d\n", error);
318 return error;
319 }
320
321 /*
322 * We should wait at least 10 msec (but no more than 40) before
323 * sending fastboot or IAP command to the device.
324 */
325 msleep(30);
326
327 return 0;
328}
329
330static u16 elants_i2c_parse_version(u8 *buf)
331{
332 return get_unaligned_be32(buf) >> 4;
333}
334
bc1d57fe 335static int elants_i2c_query_hw_version(struct elants_data *ts)
66aee900
SL
336{
337 struct i2c_client *client = ts->client;
918e2844 338 int retry_cnt = MAX_RETRIES;
66aee900
SL
339 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 };
340 u8 resp[HEADER_SIZE];
918e2844 341 int error;
66aee900 342
918e2844 343 while (retry_cnt--) {
66aee900 344 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
918e2844
MM
345 resp, sizeof(resp), 1,
346 "read fw id");
347 if (error)
348 return error;
66aee900 349
918e2844
MM
350 ts->hw_version = elants_i2c_parse_version(resp);
351 if (ts->hw_version != 0xffff)
352 return 0;
bc1d57fe
JC
353 }
354
355 dev_err(&client->dev, "Invalid fw id: %#04x\n", ts->hw_version);
66aee900
SL
356
357 return -EINVAL;
358}
359
360static int elants_i2c_query_fw_version(struct elants_data *ts)
361{
362 struct i2c_client *client = ts->client;
918e2844 363 int retry_cnt = MAX_RETRIES;
66aee900
SL
364 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 };
365 u8 resp[HEADER_SIZE];
918e2844 366 int error;
66aee900 367
918e2844 368 while (retry_cnt--) {
66aee900 369 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
918e2844
MM
370 resp, sizeof(resp), 1,
371 "read fw version");
372 if (error)
373 return error;
374
375 ts->fw_version = elants_i2c_parse_version(resp);
376 if (ts->fw_version != 0x0000 && ts->fw_version != 0xffff)
377 return 0;
66aee900 378
918e2844
MM
379 dev_dbg(&client->dev, "(read fw version) resp %*phC\n",
380 (int)sizeof(resp), resp);
66aee900
SL
381 }
382
918e2844 383 dev_err(&client->dev, "Invalid fw ver: %#04x\n", ts->fw_version);
66aee900
SL
384
385 return -EINVAL;
386}
387
388static int elants_i2c_query_test_version(struct elants_data *ts)
389{
390 struct i2c_client *client = ts->client;
918e2844 391 int error;
66aee900
SL
392 u16 version;
393 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 };
394 u8 resp[HEADER_SIZE];
395
918e2844
MM
396 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
397 resp, sizeof(resp), MAX_RETRIES,
398 "read test version");
399 if (error) {
400 dev_err(&client->dev, "Failed to read test version\n");
401 return error;
66aee900
SL
402 }
403
918e2844
MM
404 version = elants_i2c_parse_version(resp);
405 ts->test_version = version >> 8;
406 ts->solution_version = version & 0xff;
66aee900 407
918e2844 408 return 0;
66aee900
SL
409}
410
411static int elants_i2c_query_bc_version(struct elants_data *ts)
412{
413 struct i2c_client *client = ts->client;
414 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 };
415 u8 resp[HEADER_SIZE];
416 u16 version;
417 int error;
418
419 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
918e2844
MM
420 resp, sizeof(resp), 1,
421 "read BC version");
422 if (error)
66aee900 423 return error;
66aee900
SL
424
425 version = elants_i2c_parse_version(resp);
426 ts->bc_version = version >> 8;
427 ts->iap_version = version & 0xff;
428
429 return 0;
430}
431
432static int elants_i2c_query_ts_info(struct elants_data *ts)
433{
434 struct i2c_client *client = ts->client;
435 int error;
436 u8 resp[17];
437 u16 phy_x, phy_y, rows, cols, osr;
438 const u8 get_resolution_cmd[] = {
439 CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00
440 };
441 const u8 get_osr_cmd[] = {
442 CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01
443 };
444 const u8 get_physical_scan_cmd[] = {
445 CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01
446 };
447 const u8 get_physical_drive_cmd[] = {
448 CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01
449 };
450
451 /* Get trace number */
452 error = elants_i2c_execute_command(client,
453 get_resolution_cmd,
454 sizeof(get_resolution_cmd),
918e2844
MM
455 resp, sizeof(resp), 1,
456 "get resolution");
457 if (error)
66aee900 458 return error;
66aee900
SL
459
460 rows = resp[2] + resp[6] + resp[10];
461 cols = resp[3] + resp[7] + resp[11];
462
4238e52c
JC
463 /* Get report resolution value of ABS_MT_TOUCH_MAJOR */
464 ts->major_res = resp[16];
465
66aee900
SL
466 /* Process mm_to_pixel information */
467 error = elants_i2c_execute_command(client,
468 get_osr_cmd, sizeof(get_osr_cmd),
918e2844
MM
469 resp, sizeof(resp), 1, "get osr");
470 if (error)
66aee900 471 return error;
66aee900
SL
472
473 osr = resp[3];
474
475 error = elants_i2c_execute_command(client,
476 get_physical_scan_cmd,
477 sizeof(get_physical_scan_cmd),
918e2844
MM
478 resp, sizeof(resp), 1,
479 "get physical scan");
480 if (error)
66aee900 481 return error;
66aee900
SL
482
483 phy_x = get_unaligned_be16(&resp[2]);
484
485 error = elants_i2c_execute_command(client,
486 get_physical_drive_cmd,
487 sizeof(get_physical_drive_cmd),
918e2844
MM
488 resp, sizeof(resp), 1,
489 "get physical drive");
490 if (error)
66aee900 491 return error;
66aee900
SL
492
493 phy_y = get_unaligned_be16(&resp[2]);
494
495 dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
496
497 if (rows == 0 || cols == 0 || osr == 0) {
498 dev_warn(&client->dev,
499 "invalid trace number data: %d, %d, %d\n",
500 rows, cols, osr);
501 } else {
502 /* translate trace number to TS resolution */
503 ts->x_max = ELAN_TS_RESOLUTION(rows, osr);
504 ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
505 ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
506 ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
507 }
508
509 return 0;
510}
511
512static int elants_i2c_fastboot(struct i2c_client *client)
513{
514 const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E };
515 int error;
516
517 error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd));
518 if (error) {
519 dev_err(&client->dev, "boot failed: %d\n", error);
520 return error;
521 }
522
523 dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr);
524 return 0;
525}
526
527static int elants_i2c_initialize(struct elants_data *ts)
528{
529 struct i2c_client *client = ts->client;
bc1d57fe 530 int error, error2, retry_cnt;
66aee900
SL
531 const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 };
532 const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 };
533 u8 buf[HEADER_SIZE];
534
535 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
536 error = elants_i2c_sw_reset(client);
537 if (error) {
538 /* Continue initializing if it's the last try */
539 if (retry_cnt < MAX_RETRIES - 1)
540 continue;
541 }
542
543 error = elants_i2c_fastboot(client);
544 if (error) {
545 /* Continue initializing if it's the last try */
546 if (retry_cnt < MAX_RETRIES - 1)
547 continue;
548 }
549
550 /* Wait for Hello packet */
551 msleep(BOOT_TIME_DELAY_MS);
552
553 error = elants_i2c_read(client, buf, sizeof(buf));
554 if (error) {
555 dev_err(&client->dev,
556 "failed to read 'hello' packet: %d\n", error);
557 } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) {
558 ts->iap_mode = ELAN_IAP_OPERATIONAL;
559 break;
560 } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) {
561 /*
562 * Setting error code will mark device
563 * in recovery mode below.
564 */
565 error = -EIO;
566 break;
567 } else {
568 error = -EINVAL;
569 dev_err(&client->dev,
570 "invalid 'hello' packet: %*ph\n",
571 (int)sizeof(buf), buf);
572 }
573 }
574
bc1d57fe
JC
575 /* hw version is available even if device in recovery state */
576 error2 = elants_i2c_query_hw_version(ts);
f0b57e19
JC
577 if (!error2)
578 error2 = elants_i2c_query_bc_version(ts);
66aee900 579 if (!error)
bc1d57fe
JC
580 error = error2;
581
66aee900
SL
582 if (!error)
583 error = elants_i2c_query_fw_version(ts);
bc1d57fe
JC
584 if (!error)
585 error = elants_i2c_query_test_version(ts);
bc1d57fe
JC
586 if (!error)
587 error = elants_i2c_query_ts_info(ts);
66aee900 588
bc1d57fe 589 if (error)
66aee900 590 ts->iap_mode = ELAN_IAP_RECOVERY;
66aee900
SL
591
592 return 0;
593}
594
595/*
596 * Firmware update interface.
597 */
598
599static int elants_i2c_fw_write_page(struct i2c_client *client,
600 const void *page)
601{
602 const u8 ack_ok[] = { 0xaa, 0xaa };
603 u8 buf[2];
604 int retry;
605 int error;
606
607 for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) {
608 error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE);
609 if (error) {
610 dev_err(&client->dev,
611 "IAP Write Page failed: %d\n", error);
612 continue;
613 }
614
615 error = elants_i2c_read(client, buf, 2);
616 if (error) {
617 dev_err(&client->dev,
618 "IAP Ack read failed: %d\n", error);
619 return error;
620 }
621
622 if (!memcmp(buf, ack_ok, sizeof(ack_ok)))
623 return 0;
624
625 error = -EIO;
626 dev_err(&client->dev,
627 "IAP Get Ack Error [%02x:%02x]\n",
628 buf[0], buf[1]);
629 }
630
631 return error;
632}
633
f0b57e19
JC
634static int elants_i2c_validate_remark_id(struct elants_data *ts,
635 const struct firmware *fw)
636{
637 struct i2c_client *client = ts->client;
638 int error;
639 const u8 cmd[] = { CMD_HEADER_ROM_READ, 0x80, 0x1F, 0x00, 0x00, 0x21 };
640 u8 resp[6] = { 0 };
641 u16 ts_remark_id = 0;
642 u16 fw_remark_id = 0;
643
644 /* Compare TS Remark ID and FW Remark ID */
645 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
918e2844
MM
646 resp, sizeof(resp),
647 1, "read Remark ID");
648 if (error)
f0b57e19 649 return error;
f0b57e19
JC
650
651 ts_remark_id = get_unaligned_be16(&resp[3]);
652
653 fw_remark_id = get_unaligned_le16(&fw->data[fw->size - 4]);
654
655 if (fw_remark_id != ts_remark_id) {
656 dev_err(&client->dev,
657 "Remark ID Mismatched: ts_remark_id=0x%04x, fw_remark_id=0x%04x.\n",
658 ts_remark_id, fw_remark_id);
659 return -EINVAL;
660 }
661
662 return 0;
663}
664
66aee900
SL
665static int elants_i2c_do_update_firmware(struct i2c_client *client,
666 const struct firmware *fw,
667 bool force)
668{
f0b57e19 669 struct elants_data *ts = i2c_get_clientdata(client);
66aee900
SL
670 const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 };
671 const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 };
672 const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc };
f0b57e19 673 const u8 close_idle[] = { 0x54, 0x2c, 0x01, 0x01 };
66aee900
SL
674 u8 buf[HEADER_SIZE];
675 u16 send_id;
676 int page, n_fw_pages;
677 int error;
f0b57e19 678 bool check_remark_id = ts->iap_version >= 0x60;
66aee900
SL
679
680 /* Recovery mode detection! */
681 if (force) {
682 dev_dbg(&client->dev, "Recovery mode procedure\n");
f0b57e19
JC
683
684 if (check_remark_id) {
685 error = elants_i2c_validate_remark_id(ts, fw);
686 if (error)
687 return error;
688 }
689
66aee900 690 error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2));
f0b57e19
JC
691 if (error) {
692 dev_err(&client->dev, "failed to enter IAP mode: %d\n",
693 error);
694 return error;
695 }
66aee900
SL
696 } else {
697 /* Start IAP Procedure */
698 dev_dbg(&client->dev, "Normal IAP procedure\n");
f0b57e19 699
6fd38502
JC
700 /* Close idle mode */
701 error = elants_i2c_send(client, close_idle, sizeof(close_idle));
702 if (error)
703 dev_err(&client->dev, "Failed close idle: %d\n", error);
704 msleep(60);
f0b57e19 705
66aee900 706 elants_i2c_sw_reset(client);
6fd38502 707 msleep(20);
66aee900 708
f0b57e19
JC
709 if (check_remark_id) {
710 error = elants_i2c_validate_remark_id(ts, fw);
711 if (error)
712 return error;
713 }
714
715 error = elants_i2c_send(client, enter_iap, sizeof(enter_iap));
716 if (error) {
717 dev_err(&client->dev, "failed to enter IAP mode: %d\n",
718 error);
719 return error;
720 }
66aee900
SL
721 }
722
723 msleep(20);
724
725 /* check IAP state */
726 error = elants_i2c_read(client, buf, 4);
727 if (error) {
728 dev_err(&client->dev,
729 "failed to read IAP acknowledgement: %d\n",
730 error);
731 return error;
732 }
733
734 if (memcmp(buf, iap_ack, sizeof(iap_ack))) {
735 dev_err(&client->dev,
736 "failed to enter IAP: %*ph (expected %*ph)\n",
737 (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack);
738 return -EIO;
739 }
740
741 dev_info(&client->dev, "successfully entered IAP mode");
742
743 send_id = client->addr;
744 error = elants_i2c_send(client, &send_id, 1);
745 if (error) {
746 dev_err(&client->dev, "sending dummy byte failed: %d\n",
747 error);
748 return error;
749 }
750
751 /* Clear the last page of Master */
752 error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE);
753 if (error) {
754 dev_err(&client->dev, "clearing of the last page failed: %d\n",
755 error);
756 return error;
757 }
758
759 error = elants_i2c_read(client, buf, 2);
760 if (error) {
761 dev_err(&client->dev,
762 "failed to read ACK for clearing the last page: %d\n",
763 error);
764 return error;
765 }
766
767 n_fw_pages = fw->size / ELAN_FW_PAGESIZE;
768 dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages);
769
770 for (page = 0; page < n_fw_pages; page++) {
771 error = elants_i2c_fw_write_page(client,
772 fw->data + page * ELAN_FW_PAGESIZE);
773 if (error) {
774 dev_err(&client->dev,
775 "failed to write FW page %d: %d\n",
776 page, error);
777 return error;
778 }
779 }
780
781 /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
782 msleep(300);
783
784 dev_info(&client->dev, "firmware update completed\n");
785 return 0;
786}
787
788static int elants_i2c_fw_update(struct elants_data *ts)
789{
790 struct i2c_client *client = ts->client;
791 const struct firmware *fw;
37dee1ac 792 char *fw_name;
66aee900
SL
793 int error;
794
8c0776a8 795 fw_name = kasprintf(GFP_KERNEL, "elants_i2c_%04x.bin", ts->hw_version);
37dee1ac
CM
796 if (!fw_name)
797 return -ENOMEM;
798
799 dev_info(&client->dev, "requesting fw name = %s\n", fw_name);
800 error = request_firmware(&fw, fw_name, &client->dev);
801 kfree(fw_name);
66aee900 802 if (error) {
37dee1ac
CM
803 dev_err(&client->dev, "failed to request firmware: %d\n",
804 error);
66aee900
SL
805 return error;
806 }
807
808 if (fw->size % ELAN_FW_PAGESIZE) {
809 dev_err(&client->dev, "invalid firmware length: %zu\n",
810 fw->size);
811 error = -EINVAL;
812 goto out;
813 }
814
815 disable_irq(client->irq);
816
817 error = elants_i2c_do_update_firmware(client, fw,
818 ts->iap_mode == ELAN_IAP_RECOVERY);
819 if (error) {
820 dev_err(&client->dev, "firmware update failed: %d\n", error);
821 ts->iap_mode = ELAN_IAP_RECOVERY;
822 goto out_enable_irq;
823 }
824
825 error = elants_i2c_initialize(ts);
826 if (error) {
827 dev_err(&client->dev,
828 "failed to initialize device after firmware update: %d\n",
829 error);
830 ts->iap_mode = ELAN_IAP_RECOVERY;
831 goto out_enable_irq;
832 }
833
834 ts->iap_mode = ELAN_IAP_OPERATIONAL;
835
836out_enable_irq:
837 ts->state = ELAN_STATE_NORMAL;
838 enable_irq(client->irq);
839 msleep(100);
840
841 if (!error)
842 elants_i2c_calibrate(ts);
843out:
844 release_firmware(fw);
845 return error;
846}
847
848/*
849 * Event reporting.
850 */
851
852static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
853{
854 struct input_dev *input = ts->input;
855 unsigned int n_fingers;
f27ad893 856 unsigned int tool_type;
66aee900
SL
857 u16 finger_state;
858 int i;
859
860 n_fingers = buf[FW_POS_STATE + 1] & 0x0f;
861 finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) |
862 buf[FW_POS_STATE];
863
864 dev_dbg(&ts->client->dev,
865 "n_fingers: %u, state: %04x\n", n_fingers, finger_state);
866
f27ad893
JC
867 /* Note: all fingers have the same tool type */
868 tool_type = buf[FW_POS_TOOL_TYPE] & BIT(0) ?
869 MT_TOOL_FINGER : MT_TOOL_PALM;
870
66aee900
SL
871 for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) {
872 if (finger_state & 1) {
873 unsigned int x, y, p, w;
874 u8 *pos;
875
876 pos = &buf[FW_POS_XY + i * 3];
877 x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
878 y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
879 p = buf[FW_POS_PRESSURE + i];
880 w = buf[FW_POS_WIDTH + i];
881
882 dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
883 i, x, y, p, w);
884
885 input_mt_slot(input, i);
f27ad893 886 input_mt_report_slot_state(input, tool_type, true);
68334dba 887 touchscreen_report_pos(input, &ts->prop, x, y, true);
66aee900
SL
888 input_event(input, EV_ABS, ABS_MT_PRESSURE, p);
889 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w);
890
891 n_fingers--;
892 }
893
894 finger_state >>= 1;
895 }
896
897 input_mt_sync_frame(input);
898 input_sync(input);
899}
900
901static u8 elants_i2c_calculate_checksum(u8 *buf)
902{
903 u8 checksum = 0;
904 u8 i;
905
906 for (i = 0; i < FW_POS_CHECKSUM; i++)
907 checksum += buf[i];
908
909 return checksum;
910}
911
912static void elants_i2c_event(struct elants_data *ts, u8 *buf)
913{
914 u8 checksum = elants_i2c_calculate_checksum(buf);
915
916 if (unlikely(buf[FW_POS_CHECKSUM] != checksum))
917 dev_warn(&ts->client->dev,
918 "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
919 __func__, buf[FW_POS_HEADER],
920 checksum, buf[FW_POS_CHECKSUM]);
921 else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER))
922 dev_warn(&ts->client->dev,
923 "%s: unknown packet type: %02x\n",
924 __func__, buf[FW_POS_HEADER]);
925 else
926 elants_i2c_mt_event(ts, buf);
927}
928
929static irqreturn_t elants_i2c_irq(int irq, void *_dev)
930{
931 const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 };
932 struct elants_data *ts = _dev;
933 struct i2c_client *client = ts->client;
934 int report_count, report_len;
935 int i;
936 int len;
937
00f73f97 938 len = i2c_master_recv_dmasafe(client, ts->buf, sizeof(ts->buf));
66aee900
SL
939 if (len < 0) {
940 dev_err(&client->dev, "%s: failed to read data: %d\n",
941 __func__, len);
942 goto out;
943 }
944
945 dev_dbg(&client->dev, "%s: packet %*ph\n",
946 __func__, HEADER_SIZE, ts->buf);
947
948 switch (ts->state) {
949 case ELAN_WAIT_RECALIBRATION:
950 if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) {
951 memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp));
952 complete(&ts->cmd_done);
953 ts->state = ELAN_STATE_NORMAL;
954 }
955 break;
956
957 case ELAN_WAIT_QUEUE_HEADER:
958 if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL)
959 break;
960
961 ts->state = ELAN_STATE_NORMAL;
6f49c4f5 962 fallthrough;
66aee900
SL
963
964 case ELAN_STATE_NORMAL:
965
966 switch (ts->buf[FW_HDR_TYPE]) {
967 case CMD_HEADER_HELLO:
968 case CMD_HEADER_RESP:
969 case CMD_HEADER_REK:
970 break;
971
972 case QUEUE_HEADER_WAIT:
973 if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) {
974 dev_err(&client->dev,
975 "invalid wait packet %*ph\n",
976 HEADER_SIZE, ts->buf);
977 } else {
978 ts->state = ELAN_WAIT_QUEUE_HEADER;
979 udelay(30);
980 }
981 break;
982
983 case QUEUE_HEADER_SINGLE:
984 elants_i2c_event(ts, &ts->buf[HEADER_SIZE]);
985 break;
986
987 case QUEUE_HEADER_NORMAL:
988 report_count = ts->buf[FW_HDR_COUNT];
1c3415a0 989 if (report_count == 0 || report_count > 3) {
66aee900 990 dev_err(&client->dev,
1c3415a0 991 "bad report count: %*ph\n",
66aee900
SL
992 HEADER_SIZE, ts->buf);
993 break;
994 }
995
996 report_len = ts->buf[FW_HDR_LENGTH] / report_count;
997 if (report_len != PACKET_SIZE) {
998 dev_err(&client->dev,
999 "mismatching report length: %*ph\n",
1000 HEADER_SIZE, ts->buf);
1001 break;
1002 }
1003
1004 for (i = 0; i < report_count; i++) {
1005 u8 *buf = ts->buf + HEADER_SIZE +
1006 i * PACKET_SIZE;
1007 elants_i2c_event(ts, buf);
1008 }
1009 break;
1010
1011 default:
1012 dev_err(&client->dev, "unknown packet %*ph\n",
1013 HEADER_SIZE, ts->buf);
1014 break;
1015 }
1016 break;
1017 }
1018
1019out:
1020 return IRQ_HANDLED;
1021}
1022
1023/*
1024 * sysfs interface
1025 */
1026static ssize_t calibrate_store(struct device *dev,
1027 struct device_attribute *attr,
cf520c64 1028 const char *buf, size_t count)
66aee900
SL
1029{
1030 struct i2c_client *client = to_i2c_client(dev);
1031 struct elants_data *ts = i2c_get_clientdata(client);
1032 int error;
1033
1034 error = mutex_lock_interruptible(&ts->sysfs_mutex);
1035 if (error)
1036 return error;
1037
1038 error = elants_i2c_calibrate(ts);
1039
1040 mutex_unlock(&ts->sysfs_mutex);
1041 return error ?: count;
1042}
1043
1044static ssize_t write_update_fw(struct device *dev,
1045 struct device_attribute *attr,
1046 const char *buf, size_t count)
1047{
1048 struct i2c_client *client = to_i2c_client(dev);
1049 struct elants_data *ts = i2c_get_clientdata(client);
1050 int error;
1051
1052 error = mutex_lock_interruptible(&ts->sysfs_mutex);
1053 if (error)
1054 return error;
1055
1056 error = elants_i2c_fw_update(ts);
1057 dev_dbg(dev, "firmware update result: %d\n", error);
1058
1059 mutex_unlock(&ts->sysfs_mutex);
1060 return error ?: count;
1061}
1062
1063static ssize_t show_iap_mode(struct device *dev,
1064 struct device_attribute *attr, char *buf)
1065{
1066 struct i2c_client *client = to_i2c_client(dev);
1067 struct elants_data *ts = i2c_get_clientdata(client);
1068
1069 return sprintf(buf, "%s\n",
1070 ts->iap_mode == ELAN_IAP_OPERATIONAL ?
1071 "Normal" : "Recovery");
1072}
1073
cf520c64
JC
1074static ssize_t show_calibration_count(struct device *dev,
1075 struct device_attribute *attr, char *buf)
1076{
1077 struct i2c_client *client = to_i2c_client(dev);
1078 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_REK, 0x00, 0x01 };
1079 u8 resp[HEADER_SIZE];
1080 u16 rek_count;
1081 int error;
1082
1083 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
918e2844
MM
1084 resp, sizeof(resp), 1,
1085 "read ReK status");
1086 if (error)
cf520c64 1087 return sprintf(buf, "%d\n", error);
cf520c64
JC
1088
1089 rek_count = get_unaligned_be16(&resp[2]);
cf520c64
JC
1090 return sprintf(buf, "0x%04x\n", rek_count);
1091}
1092
6cbaefb4 1093static DEVICE_ATTR_WO(calibrate);
66aee900 1094static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL);
cf520c64 1095static DEVICE_ATTR(calibration_count, S_IRUGO, show_calibration_count, NULL);
66aee900
SL
1096static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw);
1097
1098struct elants_version_attribute {
1099 struct device_attribute dattr;
1100 size_t field_offset;
1101 size_t field_size;
1102};
1103
1104#define __ELANTS_FIELD_SIZE(_field) \
1105 sizeof(((struct elants_data *)NULL)->_field)
1106#define __ELANTS_VERIFY_SIZE(_field) \
1107 (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
1108 __ELANTS_FIELD_SIZE(_field))
1109#define ELANTS_VERSION_ATTR(_field) \
1110 struct elants_version_attribute elants_ver_attr_##_field = { \
1111 .dattr = __ATTR(_field, S_IRUGO, \
1112 elants_version_attribute_show, NULL), \
1113 .field_offset = offsetof(struct elants_data, _field), \
1114 .field_size = __ELANTS_VERIFY_SIZE(_field), \
1115 }
1116
1117static ssize_t elants_version_attribute_show(struct device *dev,
1118 struct device_attribute *dattr,
1119 char *buf)
1120{
1121 struct i2c_client *client = to_i2c_client(dev);
1122 struct elants_data *ts = i2c_get_clientdata(client);
1123 struct elants_version_attribute *attr =
1124 container_of(dattr, struct elants_version_attribute, dattr);
1125 u8 *field = (u8 *)((char *)ts + attr->field_offset);
1126 unsigned int fmt_size;
1127 unsigned int val;
1128
1129 if (attr->field_size == 1) {
1130 val = *field;
1131 fmt_size = 2; /* 2 HEX digits */
1132 } else {
1133 val = *(u16 *)field;
1134 fmt_size = 4; /* 4 HEX digits */
1135 }
1136
1137 return sprintf(buf, "%0*x\n", fmt_size, val);
1138}
1139
1140static ELANTS_VERSION_ATTR(fw_version);
1141static ELANTS_VERSION_ATTR(hw_version);
1142static ELANTS_VERSION_ATTR(test_version);
1143static ELANTS_VERSION_ATTR(solution_version);
1144static ELANTS_VERSION_ATTR(bc_version);
1145static ELANTS_VERSION_ATTR(iap_version);
1146
1147static struct attribute *elants_attributes[] = {
1148 &dev_attr_calibrate.attr,
1149 &dev_attr_update_fw.attr,
1150 &dev_attr_iap_mode.attr,
cf520c64 1151 &dev_attr_calibration_count.attr,
66aee900
SL
1152
1153 &elants_ver_attr_fw_version.dattr.attr,
1154 &elants_ver_attr_hw_version.dattr.attr,
1155 &elants_ver_attr_test_version.dattr.attr,
1156 &elants_ver_attr_solution_version.dattr.attr,
1157 &elants_ver_attr_bc_version.dattr.attr,
1158 &elants_ver_attr_iap_version.dattr.attr,
1159 NULL
1160};
1161
48f960dd 1162static const struct attribute_group elants_attribute_group = {
66aee900
SL
1163 .attrs = elants_attributes,
1164};
1165
afe10358
DT
1166static int elants_i2c_power_on(struct elants_data *ts)
1167{
1168 int error;
1169
1170 /*
1171 * If we do not have reset gpio assume platform firmware
1172 * controls regulators and does power them on for us.
1173 */
1174 if (IS_ERR_OR_NULL(ts->reset_gpio))
1175 return 0;
1176
1177 gpiod_set_value_cansleep(ts->reset_gpio, 1);
1178
1179 error = regulator_enable(ts->vcc33);
1180 if (error) {
1181 dev_err(&ts->client->dev,
1182 "failed to enable vcc33 regulator: %d\n",
1183 error);
1184 goto release_reset_gpio;
1185 }
1186
1187 error = regulator_enable(ts->vccio);
1188 if (error) {
1189 dev_err(&ts->client->dev,
1190 "failed to enable vccio regulator: %d\n",
1191 error);
1192 regulator_disable(ts->vcc33);
1193 goto release_reset_gpio;
1194 }
1195
1196 /*
1197 * We need to wait a bit after powering on controller before
1198 * we are allowed to release reset GPIO.
1199 */
1200 udelay(ELAN_POWERON_DELAY_USEC);
1201
1202release_reset_gpio:
1203 gpiod_set_value_cansleep(ts->reset_gpio, 0);
1204 if (error)
1205 return error;
1206
1207 msleep(ELAN_RESET_DELAY_MSEC);
1208
1209 return 0;
1210}
1211
1212static void elants_i2c_power_off(void *_data)
1213{
1214 struct elants_data *ts = _data;
1215
1216 if (!IS_ERR_OR_NULL(ts->reset_gpio)) {
1217 /*
1218 * Activate reset gpio to prevent leakage through the
1219 * pin once we shut off power to the controller.
1220 */
1221 gpiod_set_value_cansleep(ts->reset_gpio, 1);
1222 regulator_disable(ts->vccio);
1223 regulator_disable(ts->vcc33);
1224 }
1225}
1226
66aee900
SL
1227static int elants_i2c_probe(struct i2c_client *client,
1228 const struct i2c_device_id *id)
1229{
1230 union i2c_smbus_data dummy;
1231 struct elants_data *ts;
1232 unsigned long irqflags;
1233 int error;
1234
1235 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1236 dev_err(&client->dev,
1237 "%s: i2c check functionality error\n", DEVICE_NAME);
1238 return -ENXIO;
1239 }
1240
66aee900
SL
1241 ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL);
1242 if (!ts)
1243 return -ENOMEM;
1244
1245 mutex_init(&ts->sysfs_mutex);
1246 init_completion(&ts->cmd_done);
1247
1248 ts->client = client;
1249 i2c_set_clientdata(client, ts);
1250
afe10358
DT
1251 ts->vcc33 = devm_regulator_get(&client->dev, "vcc33");
1252 if (IS_ERR(ts->vcc33)) {
1253 error = PTR_ERR(ts->vcc33);
1254 if (error != -EPROBE_DEFER)
1255 dev_err(&client->dev,
1256 "Failed to get 'vcc33' regulator: %d\n",
1257 error);
1258 return error;
1259 }
1260
1261 ts->vccio = devm_regulator_get(&client->dev, "vccio");
1262 if (IS_ERR(ts->vccio)) {
1263 error = PTR_ERR(ts->vccio);
1264 if (error != -EPROBE_DEFER)
1265 dev_err(&client->dev,
1266 "Failed to get 'vccio' regulator: %d\n",
1267 error);
1268 return error;
1269 }
1270
7229b87b 1271 ts->reset_gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW);
afe10358
DT
1272 if (IS_ERR(ts->reset_gpio)) {
1273 error = PTR_ERR(ts->reset_gpio);
1274
1275 if (error == -EPROBE_DEFER)
1276 return error;
1277
1278 if (error != -ENOENT && error != -ENOSYS) {
1279 dev_err(&client->dev,
1280 "failed to get reset gpio: %d\n",
1281 error);
1282 return error;
1283 }
1284
1285 ts->keep_power_in_suspend = true;
afe10358
DT
1286 }
1287
1288 error = elants_i2c_power_on(ts);
1289 if (error)
1290 return error;
1291
1292 error = devm_add_action(&client->dev, elants_i2c_power_off, ts);
1293 if (error) {
1294 dev_err(&client->dev,
1295 "failed to install power off action: %d\n", error);
1296 elants_i2c_power_off(ts);
1297 return error;
1298 }
1299
1300 /* Make sure there is something at this address */
1301 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1302 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1303 dev_err(&client->dev, "nothing at this address\n");
1304 return -ENXIO;
1305 }
1306
66aee900
SL
1307 error = elants_i2c_initialize(ts);
1308 if (error) {
1309 dev_err(&client->dev, "failed to initialize: %d\n", error);
1310 return error;
1311 }
1312
1313 ts->input = devm_input_allocate_device(&client->dev);
1314 if (!ts->input) {
1315 dev_err(&client->dev, "Failed to allocate input device\n");
1316 return -ENOMEM;
1317 }
1318
1319 ts->input->name = "Elan Touchscreen";
1320 ts->input->id.bustype = BUS_I2C;
1321
66aee900 1322 /* Multitouch input params setup */
66aee900
SL
1323
1324 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
1325 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
1326 input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1327 input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
f27ad893
JC
1328 input_set_abs_params(ts->input, ABS_MT_TOOL_TYPE,
1329 0, MT_TOOL_PALM, 0, 0);
66aee900
SL
1330 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
1331 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
4238e52c
JC
1332 if (ts->major_res > 0)
1333 input_abs_set_res(ts->input, ABS_MT_TOUCH_MAJOR, ts->major_res);
66aee900 1334
68334dba
MM
1335 touchscreen_parse_properties(ts->input, true, &ts->prop);
1336
6def17b1
MM
1337 error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
1338 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1339 if (error) {
1340 dev_err(&client->dev,
1341 "failed to initialize MT slots: %d\n", error);
1342 return error;
1343 }
1344
66aee900
SL
1345 error = input_register_device(ts->input);
1346 if (error) {
1347 dev_err(&client->dev,
1348 "unable to register input device: %d\n", error);
1349 return error;
1350 }
1351
1352 /*
4c83c071
DT
1353 * Platform code (ACPI, DTS) should normally set up interrupt
1354 * for us, but in case it did not let's fall back to using falling
1355 * edge to be compatible with older Chromebooks.
66aee900 1356 */
4c83c071
DT
1357 irqflags = irq_get_trigger_type(client->irq);
1358 if (!irqflags)
1359 irqflags = IRQF_TRIGGER_FALLING;
66aee900
SL
1360
1361 error = devm_request_threaded_irq(&client->dev, client->irq,
1362 NULL, elants_i2c_irq,
1363 irqflags | IRQF_ONESHOT,
1364 client->name, ts);
1365 if (error) {
1366 dev_err(&client->dev, "Failed to register interrupt\n");
1367 return error;
1368 }
1369
1370 /*
1371 * Systems using device tree should set up wakeup via DTS,
1372 * the rest will configure device as wakeup source by default.
1373 */
1374 if (!client->dev.of_node)
1375 device_init_wakeup(&client->dev, true);
1376
8db69a9a 1377 error = devm_device_add_group(&client->dev, &elants_attribute_group);
66aee900
SL
1378 if (error) {
1379 dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1380 error);
1381 return error;
1382 }
1383
66aee900
SL
1384 return 0;
1385}
1386
1387static int __maybe_unused elants_i2c_suspend(struct device *dev)
1388{
1389 struct i2c_client *client = to_i2c_client(dev);
1390 struct elants_data *ts = i2c_get_clientdata(client);
1391 const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 };
1392 int retry_cnt;
1393 int error;
1394
1395 /* Command not support in IAP recovery mode */
1396 if (ts->iap_mode != ELAN_IAP_OPERATIONAL)
1397 return -EBUSY;
1398
1399 disable_irq(client->irq);
1400
478e5ed1
JC
1401 if (device_may_wakeup(dev)) {
1402 /*
1403 * The device will automatically enter idle mode
1404 * that has reduced power consumption.
1405 */
1406 ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1407 } else if (ts->keep_power_in_suspend) {
afe10358
DT
1408 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1409 error = elants_i2c_send(client, set_sleep_cmd,
1410 sizeof(set_sleep_cmd));
1411 if (!error)
1412 break;
66aee900 1413
afe10358
DT
1414 dev_err(&client->dev,
1415 "suspend command failed: %d\n", error);
1416 }
afe10358
DT
1417 } else {
1418 elants_i2c_power_off(ts);
1419 }
66aee900
SL
1420
1421 return 0;
1422}
1423
1424static int __maybe_unused elants_i2c_resume(struct device *dev)
1425{
1426 struct i2c_client *client = to_i2c_client(dev);
1427 struct elants_data *ts = i2c_get_clientdata(client);
1428 const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 };
1429 int retry_cnt;
1430 int error;
1431
478e5ed1
JC
1432 if (device_may_wakeup(dev)) {
1433 if (ts->wake_irq_enabled)
1434 disable_irq_wake(client->irq);
1435 elants_i2c_sw_reset(client);
1436 } else if (ts->keep_power_in_suspend) {
afe10358
DT
1437 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1438 error = elants_i2c_send(client, set_active_cmd,
1439 sizeof(set_active_cmd));
1440 if (!error)
1441 break;
66aee900 1442
afe10358
DT
1443 dev_err(&client->dev,
1444 "resume command failed: %d\n", error);
1445 }
1446 } else {
1447 elants_i2c_power_on(ts);
1448 elants_i2c_initialize(ts);
66aee900
SL
1449 }
1450
1451 ts->state = ELAN_STATE_NORMAL;
1452 enable_irq(client->irq);
1453
1454 return 0;
1455}
1456
1457static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
1458 elants_i2c_suspend, elants_i2c_resume);
1459
1460static const struct i2c_device_id elants_i2c_id[] = {
1461 { DEVICE_NAME, 0 },
1462 { }
1463};
1464MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
1465
1466#ifdef CONFIG_ACPI
1467static const struct acpi_device_id elants_acpi_id[] = {
1468 { "ELAN0001", 0 },
1469 { }
1470};
1471MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
1472#endif
1473
1474#ifdef CONFIG_OF
1475static const struct of_device_id elants_of_match[] = {
1476 { .compatible = "elan,ekth3500" },
1477 { /* sentinel */ }
1478};
1479MODULE_DEVICE_TABLE(of, elants_of_match);
1480#endif
1481
1482static struct i2c_driver elants_i2c_driver = {
1483 .probe = elants_i2c_probe,
1484 .id_table = elants_i2c_id,
1485 .driver = {
1486 .name = DEVICE_NAME,
66aee900
SL
1487 .pm = &elants_i2c_pm_ops,
1488 .acpi_match_table = ACPI_PTR(elants_acpi_id),
1489 .of_match_table = of_match_ptr(elants_of_match),
9f6a07b6 1490 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
66aee900
SL
1491 },
1492};
1493module_i2c_driver(elants_i2c_driver);
1494
1495MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
1496MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
66aee900 1497MODULE_LICENSE("GPL");