2 * Weida HiTech WDT87xx TouchScreen I2C driver
4 * Copyright (c) 2015 Weida Hi-Tech Co., Ltd.
5 * HN Chen <hn.chen@weidahitech.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
12 #include <linux/i2c.h>
13 #include <linux/input.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/irq.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/firmware.h>
21 #include <linux/input/mt.h>
22 #include <linux/acpi.h>
23 #include <asm/unaligned.h>
25 #define WDT87XX_NAME "wdt87xx_i2c"
26 #define WDT87XX_DRV_VER "0.9.8"
27 #define WDT87XX_FW_NAME "wdt87xx_fw.bin"
28 #define WDT87XX_CFG_NAME "wdt87xx_cfg.bin"
30 #define MODE_ACTIVE 0x01
31 #define MODE_READY 0x02
32 #define MODE_IDLE 0x03
33 #define MODE_SLEEP 0x04
34 #define MODE_STOP 0xFF
36 #define WDT_MAX_FINGER 10
37 #define WDT_RAW_BUF_COUNT 54
38 #define WDT_V1_RAW_BUF_COUNT 74
39 #define WDT_FIRMWARE_ID 0xa9e368f5
41 #define PG_SIZE 0x1000
44 #define MAX_UNIT_AXIS 0x7FFF
46 #define PKT_READ_SIZE 72
47 #define PKT_WRITE_SIZE 80
49 /* the finger definition of the report event */
50 #define FINGER_EV_OFFSET_ID 0
51 #define FINGER_EV_OFFSET_X 1
52 #define FINGER_EV_OFFSET_Y 3
53 #define FINGER_EV_SIZE 5
55 #define FINGER_EV_V1_OFFSET_ID 0
56 #define FINGER_EV_V1_OFFSET_W 1
57 #define FINGER_EV_V1_OFFSET_P 2
58 #define FINGER_EV_V1_OFFSET_X 3
59 #define FINGER_EV_V1_OFFSET_Y 5
60 #define FINGER_EV_V1_SIZE 7
62 /* The definition of a report packet */
63 #define TOUCH_PK_OFFSET_REPORT_ID 0
64 #define TOUCH_PK_OFFSET_EVENT 1
65 #define TOUCH_PK_OFFSET_SCAN_TIME 51
66 #define TOUCH_PK_OFFSET_FNGR_NUM 53
68 #define TOUCH_PK_V1_OFFSET_REPORT_ID 0
69 #define TOUCH_PK_V1_OFFSET_EVENT 1
70 #define TOUCH_PK_V1_OFFSET_SCAN_TIME 71
71 #define TOUCH_PK_V1_OFFSET_FNGR_NUM 73
73 /* The definition of the controller parameters */
74 #define CTL_PARAM_OFFSET_FW_ID 0
75 #define CTL_PARAM_OFFSET_PLAT_ID 2
76 #define CTL_PARAM_OFFSET_XMLS_ID1 4
77 #define CTL_PARAM_OFFSET_XMLS_ID2 6
78 #define CTL_PARAM_OFFSET_PHY_CH_X 8
79 #define CTL_PARAM_OFFSET_PHY_CH_Y 10
80 #define CTL_PARAM_OFFSET_PHY_X0 12
81 #define CTL_PARAM_OFFSET_PHY_X1 14
82 #define CTL_PARAM_OFFSET_PHY_Y0 16
83 #define CTL_PARAM_OFFSET_PHY_Y1 18
84 #define CTL_PARAM_OFFSET_PHY_W 22
85 #define CTL_PARAM_OFFSET_PHY_H 24
86 #define CTL_PARAM_OFFSET_FACTOR 32
88 /* The definition of the device descriptor */
89 #define WDT_GD_DEVICE 1
90 #define DEV_DESC_OFFSET_VID 8
91 #define DEV_DESC_OFFSET_PID 10
93 /* Communication commands */
94 #define PACKET_SIZE 56
95 #define VND_REQ_READ 0x06
96 #define VND_READ_DATA 0x07
97 #define VND_REQ_WRITE 0x08
99 #define VND_CMD_START 0x00
100 #define VND_CMD_STOP 0x01
101 #define VND_CMD_RESET 0x09
103 #define VND_CMD_ERASE 0x1A
105 #define VND_GET_CHECKSUM 0x66
107 #define VND_SET_DATA 0x83
108 #define VND_SET_COMMAND_DATA 0x84
109 #define VND_SET_CHECKSUM_CALC 0x86
110 #define VND_SET_CHECKSUM_LENGTH 0x87
112 #define VND_CMD_SFLCK 0xFC
113 #define VND_CMD_SFUNL 0xFD
115 #define CMD_SFLCK_KEY 0xC39B
116 #define CMD_SFUNL_KEY 0x95DA
118 #define STRIDX_PLATFORM_ID 0x80
119 #define STRIDX_PARAMETERS 0x81
121 #define CMD_BUF_SIZE 8
122 #define PKT_BUF_SIZE 64
124 /* The definition of the command packet */
125 #define CMD_REPORT_ID_OFFSET 0x0
126 #define CMD_TYPE_OFFSET 0x1
127 #define CMD_INDEX_OFFSET 0x2
128 #define CMD_KEY_OFFSET 0x3
129 #define CMD_LENGTH_OFFSET 0x4
130 #define CMD_DATA_OFFSET 0x8
132 /* The definition of firmware chunk tags */
133 #define FOURCC_ID_RIFF 0x46464952
134 #define FOURCC_ID_WHIF 0x46494857
135 #define FOURCC_ID_FRMT 0x544D5246
136 #define FOURCC_ID_FRWR 0x52575246
137 #define FOURCC_ID_CNFG 0x47464E43
139 #define CHUNK_ID_FRMT FOURCC_ID_FRMT
140 #define CHUNK_ID_FRWR FOURCC_ID_FRWR
141 #define CHUNK_ID_CNFG FOURCC_ID_CNFG
143 #define FW_FOURCC1_OFFSET 0
144 #define FW_SIZE_OFFSET 4
145 #define FW_FOURCC2_OFFSET 8
146 #define FW_PAYLOAD_OFFSET 40
148 #define FW_CHUNK_ID_OFFSET 0
149 #define FW_CHUNK_SIZE_OFFSET 4
150 #define FW_CHUNK_TGT_START_OFFSET 8
151 #define FW_CHUNK_PAYLOAD_LEN_OFFSET 12
152 #define FW_CHUNK_SRC_START_OFFSET 16
153 #define FW_CHUNK_VERSION_OFFSET 20
154 #define FW_CHUNK_ATTR_OFFSET 24
155 #define FW_CHUNK_PAYLOAD_OFFSET 32
157 /* Controller requires minimum 300us between commands */
158 #define WDT_COMMAND_DELAY_MS 2
159 #define WDT_FLASH_WRITE_DELAY_MS 4
160 #define WDT_FLASH_ERASE_DELAY_MS 200
161 #define WDT_FW_RESET_TIME 2500
163 struct wdt87xx_sys_param
{
179 struct wdt87xx_data
{
180 struct i2c_client
*client
;
181 struct input_dev
*input
;
182 /* Mutex for fw update to prevent concurrent access */
183 struct mutex fw_mutex
;
184 struct wdt87xx_sys_param param
;
188 static int wdt87xx_i2c_xfer(struct i2c_client
*client
,
189 void *txdata
, size_t txlen
,
190 void *rxdata
, size_t rxlen
)
192 struct i2c_msg msgs
[] = {
194 .addr
= client
->addr
,
200 .addr
= client
->addr
,
209 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
210 if (ret
!= ARRAY_SIZE(msgs
)) {
211 error
= ret
< 0 ? ret
: -EIO
;
212 dev_err(&client
->dev
, "%s: i2c transfer failed: %d\n",
220 static int wdt87xx_get_desc(struct i2c_client
*client
, u8 desc_idx
,
223 u8 tx_buf
[] = { 0x22, 0x00, 0x10, 0x0E, 0x23, 0x00 };
226 tx_buf
[2] |= desc_idx
& 0xF;
228 error
= wdt87xx_i2c_xfer(client
, tx_buf
, sizeof(tx_buf
),
231 dev_err(&client
->dev
, "get desc failed: %d\n", error
);
236 dev_err(&client
->dev
, "unexpected response to get desc: %d\n",
241 mdelay(WDT_COMMAND_DELAY_MS
);
246 static int wdt87xx_get_string(struct i2c_client
*client
, u8 str_idx
,
249 u8 tx_buf
[] = { 0x22, 0x00, 0x13, 0x0E, str_idx
, 0x23, 0x00 };
250 u8 rx_buf
[PKT_WRITE_SIZE
];
251 size_t rx_len
= len
+ 2;
254 if (rx_len
> sizeof(rx_buf
))
257 error
= wdt87xx_i2c_xfer(client
, tx_buf
, sizeof(tx_buf
),
260 dev_err(&client
->dev
, "get string failed: %d\n", error
);
264 if (rx_buf
[1] != 0x03) {
265 dev_err(&client
->dev
, "unexpected response to get string: %d\n",
270 rx_len
= min_t(size_t, len
, rx_buf
[0]);
271 memcpy(buf
, &rx_buf
[2], rx_len
);
273 mdelay(WDT_COMMAND_DELAY_MS
);
278 static int wdt87xx_get_feature(struct i2c_client
*client
,
279 u8
*buf
, size_t buf_size
)
282 u8 rx_buf
[PKT_WRITE_SIZE
];
284 size_t rx_len
= buf_size
+ 2;
287 if (rx_len
> sizeof(rx_buf
))
290 /* Get feature command packet */
291 tx_buf
[tx_len
++] = 0x22;
292 tx_buf
[tx_len
++] = 0x00;
293 if (buf
[CMD_REPORT_ID_OFFSET
] > 0xF) {
294 tx_buf
[tx_len
++] = 0x30;
295 tx_buf
[tx_len
++] = 0x02;
296 tx_buf
[tx_len
++] = buf
[CMD_REPORT_ID_OFFSET
];
298 tx_buf
[tx_len
++] = 0x30 | buf
[CMD_REPORT_ID_OFFSET
];
299 tx_buf
[tx_len
++] = 0x02;
301 tx_buf
[tx_len
++] = 0x23;
302 tx_buf
[tx_len
++] = 0x00;
304 error
= wdt87xx_i2c_xfer(client
, tx_buf
, tx_len
, rx_buf
, rx_len
);
306 dev_err(&client
->dev
, "get feature failed: %d\n", error
);
310 rx_len
= min_t(size_t, buf_size
, get_unaligned_le16(rx_buf
));
311 memcpy(buf
, &rx_buf
[2], rx_len
);
313 mdelay(WDT_COMMAND_DELAY_MS
);
318 static int wdt87xx_set_feature(struct i2c_client
*client
,
319 const u8
*buf
, size_t buf_size
)
321 u8 tx_buf
[PKT_WRITE_SIZE
];
325 /* Set feature command packet */
326 tx_buf
[tx_len
++] = 0x22;
327 tx_buf
[tx_len
++] = 0x00;
328 if (buf
[CMD_REPORT_ID_OFFSET
] > 0xF) {
329 tx_buf
[tx_len
++] = 0x30;
330 tx_buf
[tx_len
++] = 0x03;
331 tx_buf
[tx_len
++] = buf
[CMD_REPORT_ID_OFFSET
];
333 tx_buf
[tx_len
++] = 0x30 | buf
[CMD_REPORT_ID_OFFSET
];
334 tx_buf
[tx_len
++] = 0x03;
336 tx_buf
[tx_len
++] = 0x23;
337 tx_buf
[tx_len
++] = 0x00;
338 tx_buf
[tx_len
++] = (buf_size
& 0xFF);
339 tx_buf
[tx_len
++] = ((buf_size
& 0xFF00) >> 8);
341 if (tx_len
+ buf_size
> sizeof(tx_buf
))
344 memcpy(&tx_buf
[tx_len
], buf
, buf_size
);
347 error
= i2c_master_send(client
, tx_buf
, tx_len
);
349 dev_err(&client
->dev
, "set feature failed: %d\n", error
);
353 mdelay(WDT_COMMAND_DELAY_MS
);
358 static int wdt87xx_send_command(struct i2c_client
*client
, int cmd
, int value
)
360 u8 cmd_buf
[CMD_BUF_SIZE
];
362 /* Set the command packet */
363 cmd_buf
[CMD_REPORT_ID_OFFSET
] = VND_REQ_WRITE
;
364 cmd_buf
[CMD_TYPE_OFFSET
] = VND_SET_COMMAND_DATA
;
365 put_unaligned_le16((u16
)cmd
, &cmd_buf
[CMD_INDEX_OFFSET
]);
372 put_unaligned_le32((value
& 0xFF), &cmd_buf
[CMD_LENGTH_OFFSET
]);
376 put_unaligned_le16(CMD_SFLCK_KEY
, &cmd_buf
[CMD_KEY_OFFSET
]);
380 put_unaligned_le16(CMD_SFUNL_KEY
, &cmd_buf
[CMD_KEY_OFFSET
]);
384 case VND_SET_CHECKSUM_CALC
:
385 case VND_SET_CHECKSUM_LENGTH
:
386 put_unaligned_le32(value
, &cmd_buf
[CMD_KEY_OFFSET
]);
390 cmd_buf
[CMD_REPORT_ID_OFFSET
] = 0;
391 dev_err(&client
->dev
, "Invalid command: %d\n", cmd
);
395 return wdt87xx_set_feature(client
, cmd_buf
, sizeof(cmd_buf
));
398 static int wdt87xx_sw_reset(struct i2c_client
*client
)
402 dev_dbg(&client
->dev
, "resetting device now\n");
404 error
= wdt87xx_send_command(client
, VND_CMD_RESET
, 0);
406 dev_err(&client
->dev
, "reset failed\n");
410 /* Wait the device to be ready */
411 msleep(WDT_FW_RESET_TIME
);
416 static const void *wdt87xx_get_fw_chunk(const struct firmware
*fw
, u32 id
)
418 size_t pos
= FW_PAYLOAD_OFFSET
;
419 u32 chunk_id
, chunk_size
;
421 while (pos
< fw
->size
) {
422 chunk_id
= get_unaligned_le32(fw
->data
+
423 pos
+ FW_CHUNK_ID_OFFSET
);
425 return fw
->data
+ pos
;
427 chunk_size
= get_unaligned_le32(fw
->data
+
428 pos
+ FW_CHUNK_SIZE_OFFSET
);
429 pos
+= chunk_size
+ 2 * sizeof(u32
); /* chunk ID + size */
435 static int wdt87xx_get_sysparam(struct i2c_client
*client
,
436 struct wdt87xx_sys_param
*param
)
438 u8 buf
[PKT_READ_SIZE
];
441 error
= wdt87xx_get_desc(client
, WDT_GD_DEVICE
, buf
, 18);
443 dev_err(&client
->dev
, "failed to get device desc\n");
447 param
->vendor_id
= get_unaligned_le16(buf
+ DEV_DESC_OFFSET_VID
);
448 param
->product_id
= get_unaligned_le16(buf
+ DEV_DESC_OFFSET_PID
);
450 error
= wdt87xx_get_string(client
, STRIDX_PARAMETERS
, buf
, 34);
452 dev_err(&client
->dev
, "failed to get parameters\n");
456 param
->xmls_id1
= get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_XMLS_ID1
);
457 param
->xmls_id2
= get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_XMLS_ID2
);
458 param
->phy_ch_x
= get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_PHY_CH_X
);
459 param
->phy_ch_y
= get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_PHY_CH_Y
);
460 param
->phy_w
= get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_PHY_W
) / 10;
461 param
->phy_h
= get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_PHY_H
) / 10;
463 /* Get the scaling factor of pixel to logical coordinate */
464 param
->scaling_factor
=
465 get_unaligned_le16(buf
+ CTL_PARAM_OFFSET_FACTOR
);
467 param
->max_x
= MAX_UNIT_AXIS
;
468 param
->max_y
= DIV_ROUND_CLOSEST(MAX_UNIT_AXIS
* param
->phy_h
,
471 error
= wdt87xx_get_string(client
, STRIDX_PLATFORM_ID
, buf
, 8);
473 dev_err(&client
->dev
, "failed to get platform id\n");
477 param
->plat_id
= buf
[1];
480 error
= wdt87xx_get_feature(client
, buf
, 16);
482 dev_err(&client
->dev
, "failed to get firmware id\n");
486 if (buf
[0] != 0xf2) {
487 dev_err(&client
->dev
, "wrong id of fw response: 0x%x\n",
492 param
->fw_id
= get_unaligned_le16(&buf
[1]);
494 dev_info(&client
->dev
,
495 "fw_id: 0x%x, plat_id: 0x%x, xml_id1: %04x, xml_id2: %04x\n",
496 param
->fw_id
, param
->plat_id
,
497 param
->xmls_id1
, param
->xmls_id2
);
502 static int wdt87xx_validate_firmware(struct wdt87xx_data
*wdt
,
503 const struct firmware
*fw
)
505 const void *fw_chunk
;
511 data1
= get_unaligned_le32(fw
->data
+ FW_FOURCC1_OFFSET
);
512 data2
= get_unaligned_le32(fw
->data
+ FW_FOURCC2_OFFSET
);
513 if (data1
!= FOURCC_ID_RIFF
|| data2
!= FOURCC_ID_WHIF
) {
514 dev_err(&wdt
->client
->dev
, "check fw tag failed\n");
518 size
= get_unaligned_le32(fw
->data
+ FW_SIZE_OFFSET
);
519 if (size
!= fw
->size
) {
520 dev_err(&wdt
->client
->dev
,
521 "fw size mismatch: expected %d, actual %zu\n",
527 * Get the chip_id from the firmware. Make sure that it is the
528 * right controller to do the firmware and config update.
530 fw_chunk
= wdt87xx_get_fw_chunk(fw
, CHUNK_ID_FRWR
);
532 dev_err(&wdt
->client
->dev
,
533 "unable to locate firmware chunk\n");
537 fw_chip_id
= (get_unaligned_le32(fw_chunk
+
538 FW_CHUNK_VERSION_OFFSET
) >> 12) & 0xF;
539 chip_id
= (wdt
->param
.fw_id
>> 12) & 0xF;
541 if (fw_chip_id
!= chip_id
) {
542 dev_err(&wdt
->client
->dev
,
543 "fw version mismatch: fw %d vs. chip %d\n",
544 fw_chip_id
, chip_id
);
551 static int wdt87xx_validate_fw_chunk(const void *data
, int id
)
553 if (id
== CHUNK_ID_FRWR
) {
556 fw_id
= get_unaligned_le32(data
+ FW_CHUNK_PAYLOAD_OFFSET
);
557 if (fw_id
!= WDT_FIRMWARE_ID
)
564 static int wdt87xx_write_data(struct i2c_client
*client
, const char *data
,
565 u32 address
, int length
)
570 u8 pkt_buf
[PKT_BUF_SIZE
];
572 /* Address and length should be 4 bytes aligned */
573 if ((address
& 0x3) != 0 || (length
& 0x3) != 0) {
574 dev_err(&client
->dev
,
575 "addr & len must be 4 bytes aligned %x, %x\n",
581 packet_size
= min(length
, PACKET_SIZE
);
583 pkt_buf
[CMD_REPORT_ID_OFFSET
] = VND_REQ_WRITE
;
584 pkt_buf
[CMD_TYPE_OFFSET
] = VND_SET_DATA
;
585 put_unaligned_le16(packet_size
, &pkt_buf
[CMD_INDEX_OFFSET
]);
586 put_unaligned_le32(address
, &pkt_buf
[CMD_LENGTH_OFFSET
]);
587 memcpy(&pkt_buf
[CMD_DATA_OFFSET
], data
, packet_size
);
589 error
= wdt87xx_set_feature(client
, pkt_buf
, sizeof(pkt_buf
));
593 length
-= packet_size
;
595 address
+= packet_size
;
597 /* Wait for the controller to finish the write */
598 mdelay(WDT_FLASH_WRITE_DELAY_MS
);
600 if ((++count
% 32) == 0) {
601 /* Delay for fw to clear watch dog */
609 static u16
misr(u16 cur_value
, u8 new_value
)
626 y
= (y
& ~1) | (bit0
& 1);
631 static u16
wdt87xx_calculate_checksum(const u8
*data
, size_t length
)
636 for (i
= 0; i
< length
; i
++)
637 checksum
= misr(checksum
, data
[i
]);
642 static int wdt87xx_get_checksum(struct i2c_client
*client
, u16
*checksum
,
643 u32 address
, int length
)
647 u8 pkt_buf
[PKT_BUF_SIZE
];
648 u8 cmd_buf
[CMD_BUF_SIZE
];
650 error
= wdt87xx_send_command(client
, VND_SET_CHECKSUM_LENGTH
, length
);
652 dev_err(&client
->dev
, "failed to set checksum length\n");
656 error
= wdt87xx_send_command(client
, VND_SET_CHECKSUM_CALC
, address
);
658 dev_err(&client
->dev
, "failed to set checksum address\n");
662 /* Wait the operation to complete */
663 time_delay
= DIV_ROUND_UP(length
, 1024);
664 msleep(time_delay
* 30);
666 memset(cmd_buf
, 0, sizeof(cmd_buf
));
667 cmd_buf
[CMD_REPORT_ID_OFFSET
] = VND_REQ_READ
;
668 cmd_buf
[CMD_TYPE_OFFSET
] = VND_GET_CHECKSUM
;
669 error
= wdt87xx_set_feature(client
, cmd_buf
, sizeof(cmd_buf
));
671 dev_err(&client
->dev
, "failed to request checksum\n");
675 memset(pkt_buf
, 0, sizeof(pkt_buf
));
676 pkt_buf
[CMD_REPORT_ID_OFFSET
] = VND_READ_DATA
;
677 error
= wdt87xx_get_feature(client
, pkt_buf
, sizeof(pkt_buf
));
679 dev_err(&client
->dev
, "failed to read checksum\n");
683 *checksum
= get_unaligned_le16(&pkt_buf
[CMD_DATA_OFFSET
]);
687 static int wdt87xx_write_firmware(struct i2c_client
*client
, const void *chunk
)
689 u32 start_addr
= get_unaligned_le32(chunk
+ FW_CHUNK_TGT_START_OFFSET
);
690 u32 size
= get_unaligned_le32(chunk
+ FW_CHUNK_PAYLOAD_LEN_OFFSET
);
691 const void *data
= chunk
+ FW_CHUNK_PAYLOAD_OFFSET
;
696 u16 device_checksum
, firmware_checksum
;
698 dev_dbg(&client
->dev
, "start 4k page program\n");
700 error
= wdt87xx_send_command(client
, VND_CMD_STOP
, MODE_STOP
);
702 dev_err(&client
->dev
, "stop report mode failed\n");
706 error
= wdt87xx_send_command(client
, VND_CMD_SFUNL
, 0);
708 dev_err(&client
->dev
, "unlock failed\n");
709 goto out_enable_reporting
;
715 dev_dbg(&client
->dev
, "%s: %x, %x\n", __func__
,
718 page_size
= min_t(u32
, size
, PG_SIZE
);
721 for (retry
= 0; retry
< MAX_RETRIES
; retry
++) {
722 error
= wdt87xx_send_command(client
, VND_CMD_ERASE
,
725 dev_err(&client
->dev
,
726 "erase failed at %#08x\n", start_addr
);
730 msleep(WDT_FLASH_ERASE_DELAY_MS
);
732 error
= wdt87xx_write_data(client
, data
, start_addr
,
735 dev_err(&client
->dev
,
736 "write failed at %#08x (%d bytes)\n",
737 start_addr
, page_size
);
741 error
= wdt87xx_get_checksum(client
, &device_checksum
,
742 start_addr
, page_size
);
744 dev_err(&client
->dev
,
745 "failed to retrieve checksum for %#08x (len: %d)\n",
746 start_addr
, page_size
);
751 wdt87xx_calculate_checksum(data
, page_size
);
753 if (device_checksum
== firmware_checksum
)
756 dev_err(&client
->dev
,
757 "checksum fail: %d vs %d, retry %d\n",
758 device_checksum
, firmware_checksum
, retry
);
761 if (retry
== MAX_RETRIES
) {
762 dev_err(&client
->dev
, "page write failed\n");
764 goto out_lock_device
;
767 start_addr
= start_addr
+ page_size
;
768 data
= data
+ page_size
;
772 err1
= wdt87xx_send_command(client
, VND_CMD_SFLCK
, 0);
774 dev_err(&client
->dev
, "lock failed\n");
778 out_enable_reporting
:
779 err1
= wdt87xx_send_command(client
, VND_CMD_START
, 0);
781 dev_err(&client
->dev
, "start to report failed\n");
783 return error
? error
: err1
;
786 static int wdt87xx_load_chunk(struct i2c_client
*client
,
787 const struct firmware
*fw
, u32 ck_id
)
792 chunk
= wdt87xx_get_fw_chunk(fw
, ck_id
);
794 dev_err(&client
->dev
, "unable to locate chunk (type %d)\n",
799 error
= wdt87xx_validate_fw_chunk(chunk
, ck_id
);
801 dev_err(&client
->dev
, "invalid chunk (type %d): %d\n",
806 error
= wdt87xx_write_firmware(client
, chunk
);
808 dev_err(&client
->dev
,
809 "failed to write fw chunk (type %d): %d\n",
817 static int wdt87xx_do_update_firmware(struct i2c_client
*client
,
818 const struct firmware
*fw
,
819 unsigned int chunk_id
)
821 struct wdt87xx_data
*wdt
= i2c_get_clientdata(client
);
824 error
= wdt87xx_validate_firmware(wdt
, fw
);
828 error
= mutex_lock_interruptible(&wdt
->fw_mutex
);
832 disable_irq(client
->irq
);
834 error
= wdt87xx_load_chunk(client
, fw
, chunk_id
);
836 dev_err(&client
->dev
,
837 "firmware load failed (type: %d): %d\n",
842 error
= wdt87xx_sw_reset(client
);
844 dev_err(&client
->dev
, "soft reset failed: %d\n", error
);
848 /* Refresh the parameters */
849 error
= wdt87xx_get_sysparam(client
, &wdt
->param
);
851 dev_err(&client
->dev
,
852 "failed to refresh system parameters: %d\n", error
);
854 enable_irq(client
->irq
);
855 mutex_unlock(&wdt
->fw_mutex
);
857 return error
? error
: 0;
860 static int wdt87xx_update_firmware(struct device
*dev
,
861 const char *fw_name
, unsigned int chunk_id
)
863 struct i2c_client
*client
= to_i2c_client(dev
);
864 const struct firmware
*fw
;
867 error
= request_firmware(&fw
, fw_name
, dev
);
869 dev_err(&client
->dev
, "unable to retrieve firmware %s: %d\n",
874 error
= wdt87xx_do_update_firmware(client
, fw
, chunk_id
);
876 release_firmware(fw
);
878 return error
? error
: 0;
881 static ssize_t
config_csum_show(struct device
*dev
,
882 struct device_attribute
*attr
, char *buf
)
884 struct i2c_client
*client
= to_i2c_client(dev
);
885 struct wdt87xx_data
*wdt
= i2c_get_clientdata(client
);
888 cfg_csum
= wdt
->param
.xmls_id1
;
889 cfg_csum
= (cfg_csum
<< 16) | wdt
->param
.xmls_id2
;
891 return scnprintf(buf
, PAGE_SIZE
, "%x\n", cfg_csum
);
894 static ssize_t
fw_version_show(struct device
*dev
,
895 struct device_attribute
*attr
, char *buf
)
897 struct i2c_client
*client
= to_i2c_client(dev
);
898 struct wdt87xx_data
*wdt
= i2c_get_clientdata(client
);
900 return scnprintf(buf
, PAGE_SIZE
, "%x\n", wdt
->param
.fw_id
);
903 static ssize_t
plat_id_show(struct device
*dev
,
904 struct device_attribute
*attr
, char *buf
)
906 struct i2c_client
*client
= to_i2c_client(dev
);
907 struct wdt87xx_data
*wdt
= i2c_get_clientdata(client
);
909 return scnprintf(buf
, PAGE_SIZE
, "%x\n", wdt
->param
.plat_id
);
912 static ssize_t
update_config_store(struct device
*dev
,
913 struct device_attribute
*attr
,
914 const char *buf
, size_t count
)
918 error
= wdt87xx_update_firmware(dev
, WDT87XX_CFG_NAME
, CHUNK_ID_CNFG
);
920 return error
? error
: count
;
923 static ssize_t
update_fw_store(struct device
*dev
,
924 struct device_attribute
*attr
,
925 const char *buf
, size_t count
)
929 error
= wdt87xx_update_firmware(dev
, WDT87XX_FW_NAME
, CHUNK_ID_FRWR
);
931 return error
? error
: count
;
934 static DEVICE_ATTR_RO(config_csum
);
935 static DEVICE_ATTR_RO(fw_version
);
936 static DEVICE_ATTR_RO(plat_id
);
937 static DEVICE_ATTR_WO(update_config
);
938 static DEVICE_ATTR_WO(update_fw
);
940 static struct attribute
*wdt87xx_attrs
[] = {
941 &dev_attr_config_csum
.attr
,
942 &dev_attr_fw_version
.attr
,
943 &dev_attr_plat_id
.attr
,
944 &dev_attr_update_config
.attr
,
945 &dev_attr_update_fw
.attr
,
949 static const struct attribute_group wdt87xx_attr_group
= {
950 .attrs
= wdt87xx_attrs
,
953 static void wdt87xx_report_contact(struct input_dev
*input
,
954 struct wdt87xx_sys_param
*param
,
961 finger_id
= (buf
[FINGER_EV_V1_OFFSET_ID
] >> 3) - 1;
965 /* Check if this is an active contact */
966 if (!(buf
[FINGER_EV_V1_OFFSET_ID
] & 0x1))
969 w
= buf
[FINGER_EV_V1_OFFSET_W
];
970 w
*= param
->scaling_factor
;
972 p
= buf
[FINGER_EV_V1_OFFSET_P
];
974 x
= get_unaligned_le16(buf
+ FINGER_EV_V1_OFFSET_X
);
976 y
= get_unaligned_le16(buf
+ FINGER_EV_V1_OFFSET_Y
);
977 y
= DIV_ROUND_CLOSEST(y
* param
->phy_h
, param
->phy_w
);
979 /* Refuse incorrect coordinates */
980 if (x
> param
->max_x
|| y
> param
->max_y
)
983 dev_dbg(input
->dev
.parent
, "tip on (%d), x(%d), y(%d)\n",
986 input_mt_slot(input
, finger_id
);
987 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, 1);
988 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, w
);
989 input_report_abs(input
, ABS_MT_PRESSURE
, p
);
990 input_report_abs(input
, ABS_MT_POSITION_X
, x
);
991 input_report_abs(input
, ABS_MT_POSITION_Y
, y
);
994 static irqreturn_t
wdt87xx_ts_interrupt(int irq
, void *dev_id
)
996 struct wdt87xx_data
*wdt
= dev_id
;
997 struct i2c_client
*client
= wdt
->client
;
1000 u8 raw_buf
[WDT_V1_RAW_BUF_COUNT
] = {0};
1002 error
= i2c_master_recv(client
, raw_buf
, WDT_V1_RAW_BUF_COUNT
);
1004 dev_err(&client
->dev
, "read v1 raw data failed: %d\n", error
);
1008 fingers
= raw_buf
[TOUCH_PK_V1_OFFSET_FNGR_NUM
];
1012 for (i
= 0; i
< WDT_MAX_FINGER
; i
++)
1013 wdt87xx_report_contact(wdt
->input
,
1015 &raw_buf
[TOUCH_PK_V1_OFFSET_EVENT
+
1016 i
* FINGER_EV_V1_SIZE
]);
1018 input_mt_sync_frame(wdt
->input
);
1019 input_sync(wdt
->input
);
1025 static int wdt87xx_ts_create_input_device(struct wdt87xx_data
*wdt
)
1027 struct device
*dev
= &wdt
->client
->dev
;
1028 struct input_dev
*input
;
1029 unsigned int res
= DIV_ROUND_CLOSEST(MAX_UNIT_AXIS
, wdt
->param
.phy_w
);
1032 input
= devm_input_allocate_device(dev
);
1034 dev_err(dev
, "failed to allocate input device\n");
1039 input
->name
= "WDT87xx Touchscreen";
1040 input
->id
.bustype
= BUS_I2C
;
1041 input
->id
.vendor
= wdt
->param
.vendor_id
;
1042 input
->id
.product
= wdt
->param
.product_id
;
1043 input
->phys
= wdt
->phys
;
1045 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0,
1046 wdt
->param
.max_x
, 0, 0);
1047 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0,
1048 wdt
->param
.max_y
, 0, 0);
1049 input_abs_set_res(input
, ABS_MT_POSITION_X
, res
);
1050 input_abs_set_res(input
, ABS_MT_POSITION_Y
, res
);
1052 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
,
1053 0, wdt
->param
.max_x
, 0, 0);
1054 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, 0xFF, 0, 0);
1056 input_mt_init_slots(input
, WDT_MAX_FINGER
,
1057 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
1059 error
= input_register_device(input
);
1061 dev_err(dev
, "failed to register input device: %d\n", error
);
1068 static int wdt87xx_ts_probe(struct i2c_client
*client
,
1069 const struct i2c_device_id
*id
)
1071 struct wdt87xx_data
*wdt
;
1074 dev_dbg(&client
->dev
, "adapter=%d, client irq: %d\n",
1075 client
->adapter
->nr
, client
->irq
);
1077 /* Check if the I2C function is ok in this adaptor */
1078 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
1081 wdt
= devm_kzalloc(&client
->dev
, sizeof(*wdt
), GFP_KERNEL
);
1085 wdt
->client
= client
;
1086 mutex_init(&wdt
->fw_mutex
);
1087 i2c_set_clientdata(client
, wdt
);
1089 snprintf(wdt
->phys
, sizeof(wdt
->phys
), "i2c-%u-%04x/input0",
1090 client
->adapter
->nr
, client
->addr
);
1092 error
= wdt87xx_get_sysparam(client
, &wdt
->param
);
1096 error
= wdt87xx_ts_create_input_device(wdt
);
1100 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1101 NULL
, wdt87xx_ts_interrupt
,
1105 dev_err(&client
->dev
, "request irq failed: %d\n", error
);
1109 error
= sysfs_create_group(&client
->dev
.kobj
, &wdt87xx_attr_group
);
1111 dev_err(&client
->dev
, "create sysfs failed: %d\n", error
);
1118 static int wdt87xx_ts_remove(struct i2c_client
*client
)
1120 sysfs_remove_group(&client
->dev
.kobj
, &wdt87xx_attr_group
);
1125 static int __maybe_unused
wdt87xx_suspend(struct device
*dev
)
1127 struct i2c_client
*client
= to_i2c_client(dev
);
1130 disable_irq(client
->irq
);
1132 error
= wdt87xx_send_command(client
, VND_CMD_STOP
, MODE_IDLE
);
1134 enable_irq(client
->irq
);
1135 dev_err(&client
->dev
,
1136 "failed to stop device when suspending: %d\n",
1144 static int __maybe_unused
wdt87xx_resume(struct device
*dev
)
1146 struct i2c_client
*client
= to_i2c_client(dev
);
1150 * The chip may have been reset while system is resuming,
1151 * give it some time to settle.
1155 error
= wdt87xx_send_command(client
, VND_CMD_START
, 0);
1157 dev_err(&client
->dev
,
1158 "failed to start device when resuming: %d\n",
1161 enable_irq(client
->irq
);
1166 static SIMPLE_DEV_PM_OPS(wdt87xx_pm_ops
, wdt87xx_suspend
, wdt87xx_resume
);
1168 static const struct i2c_device_id wdt87xx_dev_id
[] = {
1169 { WDT87XX_NAME
, 0 },
1172 MODULE_DEVICE_TABLE(i2c
, wdt87xx_dev_id
);
1174 static const struct acpi_device_id wdt87xx_acpi_id
[] = {
1178 MODULE_DEVICE_TABLE(acpi
, wdt87xx_acpi_id
);
1180 static struct i2c_driver wdt87xx_driver
= {
1181 .probe
= wdt87xx_ts_probe
,
1182 .remove
= wdt87xx_ts_remove
,
1183 .id_table
= wdt87xx_dev_id
,
1185 .name
= WDT87XX_NAME
,
1186 .pm
= &wdt87xx_pm_ops
,
1187 .acpi_match_table
= ACPI_PTR(wdt87xx_acpi_id
),
1190 module_i2c_driver(wdt87xx_driver
);
1192 MODULE_AUTHOR("HN Chen <hn.chen@weidahitech.com>");
1193 MODULE_DESCRIPTION("WeidaHiTech WDT87XX Touchscreen driver");
1194 MODULE_VERSION(WDT87XX_DRV_VER
);
1195 MODULE_LICENSE("GPL");