GNU Linux-libre 6.1.24-gnu
[releases.git] / drivers / input / touchscreen / raydium_i2c_ts.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Raydium touchscreen I2C driver.
4  *
5  * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
6  *
7  * Raydium reserves the right to make changes without further notice
8  * to the materials described herein. Raydium does not assume any
9  * liability arising out of the application described herein.
10  *
11  * Contact Raydium Semiconductor Corporation at www.rad-ic.com
12  */
13
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <asm/unaligned.h>
27
28 /* Slave I2C mode */
29 #define RM_BOOT_BLDR            0x02
30 #define RM_BOOT_MAIN            0x03
31
32 /* I2C bootoloader commands */
33 #define RM_CMD_BOOT_PAGE_WRT    0x0B            /* send bl page write */
34 #define RM_CMD_BOOT_WRT         0x11            /* send bl write */
35 #define RM_CMD_BOOT_ACK         0x22            /* send ack*/
36 #define RM_CMD_BOOT_CHK         0x33            /* send data check */
37 #define RM_CMD_BOOT_READ        0x44            /* send wait bl data ready*/
38
39 #define RM_BOOT_RDY             0xFF            /* bl data ready */
40 #define RM_BOOT_CMD_READHWID    0x0E            /* read hwid */
41
42 /* I2C main commands */
43 #define RM_CMD_QUERY_BANK       0x2B
44 #define RM_CMD_DATA_BANK        0x4D
45 #define RM_CMD_ENTER_SLEEP      0x4E
46 #define RM_CMD_BANK_SWITCH      0xAA
47
48 #define RM_RESET_MSG_ADDR       0x40000004
49
50 #define RM_MAX_READ_SIZE        56
51 #define RM_PACKET_CRC_SIZE      2
52
53 /* Touch relative info */
54 #define RM_MAX_RETRIES          3
55 #define RM_RETRY_DELAY_MS       20
56 #define RM_MAX_TOUCH_NUM        10
57 #define RM_BOOT_DELAY_MS        100
58
59 /* Offsets in contact data */
60 #define RM_CONTACT_STATE_POS    0
61 #define RM_CONTACT_X_POS        1
62 #define RM_CONTACT_Y_POS        3
63 #define RM_CONTACT_PRESSURE_POS 5
64 #define RM_CONTACT_WIDTH_X_POS  6
65 #define RM_CONTACT_WIDTH_Y_POS  7
66
67 /* Bootloader relative info */
68 #define RM_BL_WRT_CMD_SIZE      3       /* bl flash wrt cmd size */
69 #define RM_BL_WRT_PKG_SIZE      32      /* bl wrt pkg size */
70 #define RM_BL_WRT_LEN           (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
71 #define RM_FW_PAGE_SIZE         128
72 #define RM_MAX_FW_RETRIES       30
73 #define RM_MAX_FW_SIZE          0xD000
74
75 #define RM_POWERON_DELAY_USEC   500
76 #define RM_RESET_DELAY_MSEC     50
77
78 enum raydium_bl_cmd {
79         BL_HEADER = 0,
80         BL_PAGE_STR,
81         BL_PKG_IDX,
82         BL_DATA_STR,
83 };
84
85 enum raydium_bl_ack {
86         RAYDIUM_ACK_NULL = 0,
87         RAYDIUM_WAIT_READY,
88         RAYDIUM_PATH_READY,
89 };
90
91 enum raydium_boot_mode {
92         RAYDIUM_TS_MAIN = 0,
93         RAYDIUM_TS_BLDR,
94 };
95
96 /* Response to RM_CMD_DATA_BANK request */
97 struct raydium_data_info {
98         __le32 data_bank_addr;
99         u8 pkg_size;
100         u8 tp_info_size;
101 };
102
103 struct raydium_info {
104         __le32 hw_ver;          /*device version */
105         u8 main_ver;
106         u8 sub_ver;
107         __le16 ft_ver;          /* test version */
108         u8 x_num;
109         u8 y_num;
110         __le16 x_max;
111         __le16 y_max;
112         u8 x_res;               /* units/mm */
113         u8 y_res;               /* units/mm */
114 };
115
116 /* struct raydium_data - represents state of Raydium touchscreen device */
117 struct raydium_data {
118         struct i2c_client *client;
119         struct input_dev *input;
120
121         struct regulator *avdd;
122         struct regulator *vccio;
123         struct gpio_desc *reset_gpio;
124
125         struct raydium_info info;
126
127         struct mutex sysfs_mutex;
128
129         u8 *report_data;
130
131         u32 data_bank_addr;
132         u8 report_size;
133         u8 contact_size;
134         u8 pkg_size;
135
136         enum raydium_boot_mode boot_mode;
137
138         bool wake_irq_enabled;
139 };
140
141 /*
142  * Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
143  * raydium_i2c_{read|send} below.
144  */
145 struct __packed raydium_bank_switch_header {
146         u8 cmd;
147         __be32 be_addr;
148 };
149
150 static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
151                             struct i2c_msg *xfer, size_t xfer_count)
152 {
153         int ret;
154         /*
155          * If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
156          * sent first. Else, skip the header i.e. xfer[0].
157          */
158         int xfer_start_idx = (addr > 0xff) ? 0 : 1;
159         xfer_count -= xfer_start_idx;
160
161         ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
162         if (likely(ret == xfer_count))
163                 return 0;
164
165         return ret < 0 ? ret : -EIO;
166 }
167
168 static int raydium_i2c_send(struct i2c_client *client,
169                             u32 addr, const void *data, size_t len)
170 {
171         int tries = 0;
172         int error;
173         u8 *tx_buf;
174         u8 reg_addr = addr & 0xff;
175
176         tx_buf = kmalloc(len + 1, GFP_KERNEL);
177         if (!tx_buf)
178                 return -ENOMEM;
179
180         tx_buf[0] = reg_addr;
181         memcpy(tx_buf + 1, data, len);
182
183         do {
184                 struct raydium_bank_switch_header header = {
185                         .cmd = RM_CMD_BANK_SWITCH,
186                         .be_addr = cpu_to_be32(addr),
187                 };
188
189                 /*
190                  * Perform as a single i2c_transfer transaction to ensure that
191                  * no other I2C transactions are initiated on the bus to any
192                  * other device in between. Initiating transacations to other
193                  * devices after RM_CMD_BANK_SWITCH is sent is known to cause
194                  * issues. This is also why regmap infrastructure cannot be used
195                  * for this driver. Regmap handles page(bank) switch and reads
196                  * as separate i2c_transfer() operations. This can result in
197                  * problems if the Raydium device is on a shared I2C bus.
198                  */
199                 struct i2c_msg xfer[] = {
200                         {
201                                 .addr = client->addr,
202                                 .len = sizeof(header),
203                                 .buf = (u8 *)&header,
204                         },
205                         {
206                                 .addr = client->addr,
207                                 .len = len + 1,
208                                 .buf = tx_buf,
209                         },
210                 };
211
212                 error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
213                 if (likely(!error))
214                         goto out;
215
216                 msleep(RM_RETRY_DELAY_MS);
217         } while (++tries < RM_MAX_RETRIES);
218
219         dev_err(&client->dev, "%s failed: %d\n", __func__, error);
220 out:
221         kfree(tx_buf);
222         return error;
223 }
224
225 static int raydium_i2c_read(struct i2c_client *client,
226                             u32 addr, void *data, size_t len)
227 {
228         int error;
229
230         while (len) {
231                 u8 reg_addr = addr & 0xff;
232                 struct raydium_bank_switch_header header = {
233                         .cmd = RM_CMD_BANK_SWITCH,
234                         .be_addr = cpu_to_be32(addr),
235                 };
236                 size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
237
238                 /*
239                  * Perform as a single i2c_transfer transaction to ensure that
240                  * no other I2C transactions are initiated on the bus to any
241                  * other device in between. Initiating transacations to other
242                  * devices after RM_CMD_BANK_SWITCH is sent is known to cause
243                  * issues. This is also why regmap infrastructure cannot be used
244                  * for this driver. Regmap handles page(bank) switch and writes
245                  * as separate i2c_transfer() operations. This can result in
246                  * problems if the Raydium device is on a shared I2C bus.
247                  */
248                 struct i2c_msg xfer[] = {
249                         {
250                                 .addr = client->addr,
251                                 .len = sizeof(header),
252                                 .buf = (u8 *)&header,
253                         },
254                         {
255                                 .addr = client->addr,
256                                 .len = 1,
257                                 .buf = &reg_addr,
258                         },
259                         {
260                                 .addr = client->addr,
261                                 .len = xfer_len,
262                                 .buf = data,
263                                 .flags = I2C_M_RD,
264                         }
265                 };
266
267                 error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
268                 if (unlikely(error))
269                         return error;
270
271                 len -= xfer_len;
272                 data += xfer_len;
273                 addr += xfer_len;
274         }
275
276         return 0;
277 }
278
279 static int raydium_i2c_sw_reset(struct i2c_client *client)
280 {
281         const u8 soft_rst_cmd = 0x01;
282         int error;
283
284         error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
285                                  sizeof(soft_rst_cmd));
286         if (error) {
287                 dev_err(&client->dev, "software reset failed: %d\n", error);
288                 return error;
289         }
290
291         msleep(RM_RESET_DELAY_MSEC);
292
293         return 0;
294 }
295
296 static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
297 {
298         struct i2c_client *client = ts->client;
299         static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
300                                        0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
301         u8 rbuf[5] = { 0 };
302         u32 hw_ver;
303         int error;
304
305         error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
306                                  get_hwid, sizeof(get_hwid));
307         if (error) {
308                 dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
309                 return error;
310         }
311
312         error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
313         if (error) {
314                 dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
315                 return error;
316         }
317
318         error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
319         if (error) {
320                 dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
321                         error, rbuf + 1);
322                 hw_ver = 0xffffffffUL;
323         } else {
324                 hw_ver = get_unaligned_be32(rbuf + 1);
325         }
326
327         ts->info.hw_ver = cpu_to_le32(hw_ver);
328         ts->info.main_ver = 0xff;
329         ts->info.sub_ver = 0xff;
330
331         return error;
332 }
333
334 static int raydium_i2c_query_ts_info(struct raydium_data *ts)
335 {
336         struct i2c_client *client = ts->client;
337         struct raydium_data_info data_info;
338         __le32 query_bank_addr;
339
340         int error, retry_cnt;
341
342         for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
343                 error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
344                                          &data_info, sizeof(data_info));
345                 if (error)
346                         continue;
347
348                 /*
349                  * Warn user if we already allocated memory for reports and
350                  * then the size changed (due to firmware update?) and keep
351                  * old size instead.
352                  */
353                 if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
354                         dev_warn(&client->dev,
355                                  "report size changes, was: %d, new: %d\n",
356                                  ts->pkg_size, data_info.pkg_size);
357                 } else {
358                         ts->pkg_size = data_info.pkg_size;
359                         ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
360                 }
361
362                 ts->contact_size = data_info.tp_info_size;
363                 ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
364
365                 dev_dbg(&client->dev,
366                         "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
367                         ts->data_bank_addr, ts->report_size, ts->contact_size);
368
369                 error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
370                                          &query_bank_addr,
371                                          sizeof(query_bank_addr));
372                 if (error)
373                         continue;
374
375                 error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
376                                          &ts->info, sizeof(ts->info));
377                 if (error)
378                         continue;
379
380                 return 0;
381         }
382
383         dev_err(&client->dev, "failed to query device parameters: %d\n", error);
384         return error;
385 }
386
387 static int raydium_i2c_check_fw_status(struct raydium_data *ts)
388 {
389         struct i2c_client *client = ts->client;
390         static const u8 bl_ack = 0x62;
391         static const u8 main_ack = 0x66;
392         u8 buf[4];
393         int error;
394
395         error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
396         if (!error) {
397                 if (buf[0] == bl_ack)
398                         ts->boot_mode = RAYDIUM_TS_BLDR;
399                 else if (buf[0] == main_ack)
400                         ts->boot_mode = RAYDIUM_TS_MAIN;
401                 return 0;
402         }
403
404         return error;
405 }
406
407 static int raydium_i2c_initialize(struct raydium_data *ts)
408 {
409         struct i2c_client *client = ts->client;
410         int error, retry_cnt;
411
412         for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
413                 /* Wait for Hello packet */
414                 msleep(RM_BOOT_DELAY_MS);
415
416                 error = raydium_i2c_check_fw_status(ts);
417                 if (error) {
418                         dev_err(&client->dev,
419                                 "failed to read 'hello' packet: %d\n", error);
420                         continue;
421                 }
422
423                 if (ts->boot_mode == RAYDIUM_TS_BLDR ||
424                     ts->boot_mode == RAYDIUM_TS_MAIN) {
425                         break;
426                 }
427         }
428
429         if (error)
430                 ts->boot_mode = RAYDIUM_TS_BLDR;
431
432         if (ts->boot_mode == RAYDIUM_TS_BLDR)
433                 raydium_i2c_query_ts_bootloader_info(ts);
434         else
435                 raydium_i2c_query_ts_info(ts);
436
437         return error;
438 }
439
440 static int raydium_i2c_bl_chk_state(struct i2c_client *client,
441                                     enum raydium_bl_ack state)
442 {
443         static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
444         u8 rbuf[sizeof(ack_ok)];
445         u8 retry;
446         int error;
447
448         for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
449                 switch (state) {
450                 case RAYDIUM_ACK_NULL:
451                         return 0;
452
453                 case RAYDIUM_WAIT_READY:
454                         error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
455                                                  &rbuf[0], 1);
456                         if (!error && rbuf[0] == RM_BOOT_RDY)
457                                 return 0;
458
459                         break;
460
461                 case RAYDIUM_PATH_READY:
462                         error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
463                                                  rbuf, sizeof(rbuf));
464                         if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
465                                 return 0;
466
467                         break;
468
469                 default:
470                         dev_err(&client->dev, "%s: invalid target state %d\n",
471                                 __func__, state);
472                         return -EINVAL;
473                 }
474
475                 msleep(20);
476         }
477
478         return -ETIMEDOUT;
479 }
480
481 static int raydium_i2c_write_object(struct i2c_client *client,
482                                     const void *data, size_t len,
483                                     enum raydium_bl_ack state)
484 {
485         int error;
486         static const u8 cmd[] = { 0xFF, 0x39 };
487
488         error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
489         if (error) {
490                 dev_err(&client->dev, "WRT obj command failed: %d\n",
491                         error);
492                 return error;
493         }
494
495         error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
496         if (error) {
497                 dev_err(&client->dev, "Ack obj command failed: %d\n", error);
498                 return error;
499         }
500
501         error = raydium_i2c_bl_chk_state(client, state);
502         if (error) {
503                 dev_err(&client->dev, "BL check state failed: %d\n", error);
504                 return error;
505         }
506         return 0;
507 }
508
509 static int raydium_i2c_boot_trigger(struct i2c_client *client)
510 {
511         static const u8 cmd[7][6] = {
512                 { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
513                 { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
514                 { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
515                 { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
516                 { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
517                 { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
518                 { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
519         };
520         int i;
521         int error;
522
523         for (i = 0; i < 7; i++) {
524                 error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
525                                                  RAYDIUM_WAIT_READY);
526                 if (error) {
527                         dev_err(&client->dev,
528                                 "boot trigger failed at step %d: %d\n",
529                                 i, error);
530                         return error;
531                 }
532         }
533
534         return 0;
535 }
536
537 static int raydium_i2c_fw_trigger(struct i2c_client *client)
538 {
539         static const u8 cmd[5][11] = {
540                 { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
541                 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
542                 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
543                 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
544                 { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
545         };
546         int i;
547         int error;
548
549         for (i = 0; i < 5; i++) {
550                 error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
551                                                  RAYDIUM_ACK_NULL);
552                 if (error) {
553                         dev_err(&client->dev,
554                                 "fw trigger failed at step %d: %d\n",
555                                 i, error);
556                         return error;
557                 }
558         }
559
560         return 0;
561 }
562
563 static int raydium_i2c_check_path(struct i2c_client *client)
564 {
565         static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
566         int error;
567
568         error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
569                                          RAYDIUM_PATH_READY);
570         if (error) {
571                 dev_err(&client->dev, "check path command failed: %d\n", error);
572                 return error;
573         }
574
575         return 0;
576 }
577
578 static int raydium_i2c_enter_bl(struct i2c_client *client)
579 {
580         static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
581         int error;
582
583         error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
584                                          RAYDIUM_ACK_NULL);
585         if (error) {
586                 dev_err(&client->dev, "enter bl command failed: %d\n", error);
587                 return error;
588         }
589
590         msleep(RM_BOOT_DELAY_MS);
591         return 0;
592 }
593
594 static int raydium_i2c_leave_bl(struct i2c_client *client)
595 {
596         static const u8 leave_cmd[] = { 0x05, 0x00 };
597         int error;
598
599         error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
600                                          RAYDIUM_ACK_NULL);
601         if (error) {
602                 dev_err(&client->dev, "leave bl command failed: %d\n", error);
603                 return error;
604         }
605
606         msleep(RM_BOOT_DELAY_MS);
607         return 0;
608 }
609
610 static int raydium_i2c_write_checksum(struct i2c_client *client,
611                                       size_t length, u16 checksum)
612 {
613         u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
614         int error;
615
616         put_unaligned_le16(length, &checksum_cmd[3]);
617         put_unaligned_le16(checksum, &checksum_cmd[5]);
618
619         error = raydium_i2c_write_object(client,
620                                          checksum_cmd, sizeof(checksum_cmd),
621                                          RAYDIUM_ACK_NULL);
622         if (error) {
623                 dev_err(&client->dev, "failed to write checksum: %d\n",
624                         error);
625                 return error;
626         }
627
628         return 0;
629 }
630
631 static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
632 {
633         static const u8 cmd[] = { 0x0A, 0xAA };
634         int error;
635
636         error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
637                                          RAYDIUM_WAIT_READY);
638         if (error) {
639                 dev_err(&client->dev, "disable watchdog command failed: %d\n",
640                         error);
641                 return error;
642         }
643
644         return 0;
645 }
646
647 static int raydium_i2c_fw_write_page(struct i2c_client *client,
648                                      u16 page_idx, const void *data, size_t len)
649 {
650         u8 buf[RM_BL_WRT_LEN];
651         size_t xfer_len;
652         int error;
653         int i;
654
655         BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
656
657         for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
658                 buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
659                 buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
660                 buf[BL_PKG_IDX] = i + 1;
661
662                 xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
663                 memcpy(&buf[BL_DATA_STR], data, xfer_len);
664                 if (len < RM_BL_WRT_PKG_SIZE)
665                         memset(&buf[BL_DATA_STR + xfer_len], 0xff,
666                                 RM_BL_WRT_PKG_SIZE - xfer_len);
667
668                 error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
669                                                  RAYDIUM_WAIT_READY);
670                 if (error) {
671                         dev_err(&client->dev,
672                                 "page write command failed for page %d, chunk %d: %d\n",
673                                 page_idx, i, error);
674                         return error;
675                 }
676
677                 data += xfer_len;
678                 len -= xfer_len;
679         }
680
681         return error;
682 }
683
684 static u16 raydium_calc_chksum(const u8 *buf, u16 len)
685 {
686         u16 checksum = 0;
687         u16 i;
688
689         for (i = 0; i < len; i++)
690                 checksum += buf[i];
691
692         return checksum;
693 }
694
695 static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
696                                          const struct firmware *fw)
697 {
698         struct i2c_client *client = ts->client;
699         const void *data;
700         size_t data_len;
701         size_t len;
702         int page_nr;
703         int i;
704         int error;
705         u16 fw_checksum;
706
707         if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
708                 dev_err(&client->dev, "Invalid firmware length\n");
709                 return -EINVAL;
710         }
711
712         error = raydium_i2c_check_fw_status(ts);
713         if (error) {
714                 dev_err(&client->dev, "Unable to access IC %d\n", error);
715                 return error;
716         }
717
718         if (ts->boot_mode == RAYDIUM_TS_MAIN) {
719                 for (i = 0; i < RM_MAX_RETRIES; i++) {
720                         error = raydium_i2c_enter_bl(client);
721                         if (!error) {
722                                 error = raydium_i2c_check_fw_status(ts);
723                                 if (error) {
724                                         dev_err(&client->dev,
725                                                 "unable to access IC: %d\n",
726                                                 error);
727                                         return error;
728                                 }
729
730                                 if (ts->boot_mode == RAYDIUM_TS_BLDR)
731                                         break;
732                         }
733                 }
734
735                 if (ts->boot_mode == RAYDIUM_TS_MAIN) {
736                         dev_err(&client->dev,
737                                 "failed to jump to boot loader: %d\n",
738                                 error);
739                         return -EIO;
740                 }
741         }
742
743         error = raydium_i2c_disable_watch_dog(client);
744         if (error)
745                 return error;
746
747         error = raydium_i2c_check_path(client);
748         if (error)
749                 return error;
750
751         error = raydium_i2c_boot_trigger(client);
752         if (error) {
753                 dev_err(&client->dev, "send boot trigger fail: %d\n", error);
754                 return error;
755         }
756
757         msleep(RM_BOOT_DELAY_MS);
758
759         data = fw->data;
760         data_len = fw->size;
761         page_nr = 0;
762
763         while (data_len) {
764                 len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
765
766                 error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
767                 if (error)
768                         return error;
769
770                 msleep(20);
771
772                 data += len;
773                 data_len -= len;
774         }
775
776         error = raydium_i2c_leave_bl(client);
777         if (error) {
778                 dev_err(&client->dev,
779                         "failed to leave boot loader: %d\n", error);
780                 return error;
781         }
782
783         dev_dbg(&client->dev, "left boot loader mode\n");
784         msleep(RM_BOOT_DELAY_MS);
785
786         error = raydium_i2c_check_fw_status(ts);
787         if (error) {
788                 dev_err(&client->dev,
789                         "failed to check fw status after write: %d\n",
790                         error);
791                 return error;
792         }
793
794         if (ts->boot_mode != RAYDIUM_TS_MAIN) {
795                 dev_err(&client->dev,
796                         "failed to switch to main fw after writing firmware: %d\n",
797                         error);
798                 return -EINVAL;
799         }
800
801         error = raydium_i2c_fw_trigger(client);
802         if (error) {
803                 dev_err(&client->dev, "failed to trigger fw: %d\n", error);
804                 return error;
805         }
806
807         fw_checksum = raydium_calc_chksum(fw->data, fw->size);
808
809         error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
810         if (error)
811                 return error;
812
813         return 0;
814 }
815
816 static int raydium_i2c_fw_update(struct raydium_data *ts)
817 {
818         struct i2c_client *client = ts->client;
819         const struct firmware *fw = NULL;
820         char *fw_file;
821         int error;
822
823         fw_file = kasprintf(GFP_KERNEL, "/*(DEBLOBBED)*/",
824                             le32_to_cpu(ts->info.hw_ver));
825         if (!fw_file)
826                 return -ENOMEM;
827
828         dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
829
830         error = reject_firmware(&fw, fw_file, &client->dev);
831         if (error) {
832                 dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
833                 goto out_free_fw_file;
834         }
835
836         disable_irq(client->irq);
837
838         error = raydium_i2c_do_update_firmware(ts, fw);
839         if (error) {
840                 dev_err(&client->dev, "firmware update failed: %d\n", error);
841                 ts->boot_mode = RAYDIUM_TS_BLDR;
842                 goto out_enable_irq;
843         }
844
845         error = raydium_i2c_initialize(ts);
846         if (error) {
847                 dev_err(&client->dev,
848                         "failed to initialize device after firmware update: %d\n",
849                         error);
850                 ts->boot_mode = RAYDIUM_TS_BLDR;
851                 goto out_enable_irq;
852         }
853
854         ts->boot_mode = RAYDIUM_TS_MAIN;
855
856 out_enable_irq:
857         enable_irq(client->irq);
858         msleep(100);
859
860         release_firmware(fw);
861
862 out_free_fw_file:
863         kfree(fw_file);
864
865         return error;
866 }
867
868 static void raydium_mt_event(struct raydium_data *ts)
869 {
870         int i;
871
872         for (i = 0; i < ts->report_size / ts->contact_size; i++) {
873                 u8 *contact = &ts->report_data[ts->contact_size * i];
874                 bool state = contact[RM_CONTACT_STATE_POS];
875                 u8 wx, wy;
876
877                 input_mt_slot(ts->input, i);
878                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
879
880                 if (!state)
881                         continue;
882
883                 input_report_abs(ts->input, ABS_MT_POSITION_X,
884                                 get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
885                 input_report_abs(ts->input, ABS_MT_POSITION_Y,
886                                 get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
887                 input_report_abs(ts->input, ABS_MT_PRESSURE,
888                                 contact[RM_CONTACT_PRESSURE_POS]);
889
890                 wx = contact[RM_CONTACT_WIDTH_X_POS];
891                 wy = contact[RM_CONTACT_WIDTH_Y_POS];
892
893                 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
894                 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
895         }
896
897         input_mt_sync_frame(ts->input);
898         input_sync(ts->input);
899 }
900
901 static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
902 {
903         struct raydium_data *ts = _dev;
904         int error;
905         u16 fw_crc;
906         u16 calc_crc;
907
908         if (ts->boot_mode != RAYDIUM_TS_MAIN)
909                 goto out;
910
911         error = raydium_i2c_read(ts->client, ts->data_bank_addr,
912                                  ts->report_data, ts->pkg_size);
913         if (error)
914                 goto out;
915
916         fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
917         calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
918         if (unlikely(fw_crc != calc_crc)) {
919                 dev_warn(&ts->client->dev,
920                          "%s: invalid packet crc %#04x vs %#04x\n",
921                          __func__, calc_crc, fw_crc);
922                 goto out;
923         }
924
925         raydium_mt_event(ts);
926
927 out:
928         return IRQ_HANDLED;
929 }
930
931 static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
932                                        struct device_attribute *attr, char *buf)
933 {
934         struct i2c_client *client = to_i2c_client(dev);
935         struct raydium_data *ts = i2c_get_clientdata(client);
936
937         return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
938 }
939
940 static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
941                                        struct device_attribute *attr, char *buf)
942 {
943         struct i2c_client *client = to_i2c_client(dev);
944         struct raydium_data *ts = i2c_get_clientdata(client);
945
946         return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
947 }
948
949 static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
950                                           struct device_attribute *attr,
951                                           char *buf)
952 {
953         struct i2c_client *client = to_i2c_client(dev);
954         struct raydium_data *ts = i2c_get_clientdata(client);
955
956         return sprintf(buf, "%s\n",
957                        ts->boot_mode == RAYDIUM_TS_MAIN ?
958                                 "Normal" : "Recovery");
959 }
960
961 static ssize_t raydium_i2c_update_fw_store(struct device *dev,
962                                            struct device_attribute *attr,
963                                            const char *buf, size_t count)
964 {
965         struct i2c_client *client = to_i2c_client(dev);
966         struct raydium_data *ts = i2c_get_clientdata(client);
967         int error;
968
969         error = mutex_lock_interruptible(&ts->sysfs_mutex);
970         if (error)
971                 return error;
972
973         error = raydium_i2c_fw_update(ts);
974
975         mutex_unlock(&ts->sysfs_mutex);
976
977         return error ?: count;
978 }
979
980 static ssize_t raydium_i2c_calibrate_store(struct device *dev,
981                                            struct device_attribute *attr,
982                                            const char *buf, size_t count)
983 {
984         struct i2c_client *client = to_i2c_client(dev);
985         struct raydium_data *ts = i2c_get_clientdata(client);
986         static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
987         int error;
988
989         error = mutex_lock_interruptible(&ts->sysfs_mutex);
990         if (error)
991                 return error;
992
993         error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
994                                          RAYDIUM_WAIT_READY);
995         if (error)
996                 dev_err(&client->dev, "calibrate command failed: %d\n", error);
997
998         mutex_unlock(&ts->sysfs_mutex);
999         return error ?: count;
1000 }
1001
1002 static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
1003 static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
1004 static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
1005 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
1006 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
1007
1008 static struct attribute *raydium_i2c_attributes[] = {
1009         &dev_attr_update_fw.attr,
1010         &dev_attr_boot_mode.attr,
1011         &dev_attr_fw_version.attr,
1012         &dev_attr_hw_version.attr,
1013         &dev_attr_calibrate.attr,
1014         NULL
1015 };
1016
1017 static const struct attribute_group raydium_i2c_attribute_group = {
1018         .attrs = raydium_i2c_attributes,
1019 };
1020
1021 static int raydium_i2c_power_on(struct raydium_data *ts)
1022 {
1023         int error;
1024
1025         if (!ts->reset_gpio)
1026                 return 0;
1027
1028         gpiod_set_value_cansleep(ts->reset_gpio, 1);
1029
1030         error = regulator_enable(ts->avdd);
1031         if (error) {
1032                 dev_err(&ts->client->dev,
1033                         "failed to enable avdd regulator: %d\n", error);
1034                 goto release_reset_gpio;
1035         }
1036
1037         error = regulator_enable(ts->vccio);
1038         if (error) {
1039                 regulator_disable(ts->avdd);
1040                 dev_err(&ts->client->dev,
1041                         "failed to enable vccio regulator: %d\n", error);
1042                 goto release_reset_gpio;
1043         }
1044
1045         udelay(RM_POWERON_DELAY_USEC);
1046
1047 release_reset_gpio:
1048         gpiod_set_value_cansleep(ts->reset_gpio, 0);
1049
1050         if (error)
1051                 return error;
1052
1053         msleep(RM_RESET_DELAY_MSEC);
1054
1055         return 0;
1056 }
1057
1058 static void raydium_i2c_power_off(void *_data)
1059 {
1060         struct raydium_data *ts = _data;
1061
1062         if (ts->reset_gpio) {
1063                 gpiod_set_value_cansleep(ts->reset_gpio, 1);
1064                 regulator_disable(ts->vccio);
1065                 regulator_disable(ts->avdd);
1066         }
1067 }
1068
1069 static int raydium_i2c_probe(struct i2c_client *client,
1070                              const struct i2c_device_id *id)
1071 {
1072         union i2c_smbus_data dummy;
1073         struct raydium_data *ts;
1074         int error;
1075
1076         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1077                 dev_err(&client->dev,
1078                         "i2c check functionality error (need I2C_FUNC_I2C)\n");
1079                 return -ENXIO;
1080         }
1081
1082         ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1083         if (!ts)
1084                 return -ENOMEM;
1085
1086         mutex_init(&ts->sysfs_mutex);
1087
1088         ts->client = client;
1089         i2c_set_clientdata(client, ts);
1090
1091         ts->avdd = devm_regulator_get(&client->dev, "avdd");
1092         if (IS_ERR(ts->avdd)) {
1093                 error = PTR_ERR(ts->avdd);
1094                 if (error != -EPROBE_DEFER)
1095                         dev_err(&client->dev,
1096                                 "Failed to get 'avdd' regulator: %d\n", error);
1097                 return error;
1098         }
1099
1100         ts->vccio = devm_regulator_get(&client->dev, "vccio");
1101         if (IS_ERR(ts->vccio)) {
1102                 error = PTR_ERR(ts->vccio);
1103                 if (error != -EPROBE_DEFER)
1104                         dev_err(&client->dev,
1105                                 "Failed to get 'vccio' regulator: %d\n", error);
1106                 return error;
1107         }
1108
1109         ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1110                                                  GPIOD_OUT_LOW);
1111         if (IS_ERR(ts->reset_gpio)) {
1112                 error = PTR_ERR(ts->reset_gpio);
1113                 if (error != -EPROBE_DEFER)
1114                         dev_err(&client->dev,
1115                                 "failed to get reset gpio: %d\n", error);
1116                 return error;
1117         }
1118
1119         error = raydium_i2c_power_on(ts);
1120         if (error)
1121                 return error;
1122
1123         error = devm_add_action_or_reset(&client->dev,
1124                                          raydium_i2c_power_off, ts);
1125         if (error) {
1126                 dev_err(&client->dev,
1127                         "failed to install power off action: %d\n", error);
1128                 return error;
1129         }
1130
1131         /* Make sure there is something at this address */
1132         if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1133                            I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1134                 dev_err(&client->dev, "nothing at this address\n");
1135                 return -ENXIO;
1136         }
1137
1138         error = raydium_i2c_initialize(ts);
1139         if (error) {
1140                 dev_err(&client->dev, "failed to initialize: %d\n", error);
1141                 return error;
1142         }
1143
1144         ts->report_data = devm_kmalloc(&client->dev,
1145                                        ts->pkg_size, GFP_KERNEL);
1146         if (!ts->report_data)
1147                 return -ENOMEM;
1148
1149         ts->input = devm_input_allocate_device(&client->dev);
1150         if (!ts->input) {
1151                 dev_err(&client->dev, "Failed to allocate input device\n");
1152                 return -ENOMEM;
1153         }
1154
1155         ts->input->name = "Raydium Touchscreen";
1156         ts->input->id.bustype = BUS_I2C;
1157
1158         input_set_abs_params(ts->input, ABS_MT_POSITION_X,
1159                              0, le16_to_cpu(ts->info.x_max), 0, 0);
1160         input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
1161                              0, le16_to_cpu(ts->info.y_max), 0, 0);
1162         input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
1163         input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
1164
1165         input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1166         input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1167
1168         error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
1169                                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1170         if (error) {
1171                 dev_err(&client->dev,
1172                         "failed to initialize MT slots: %d\n", error);
1173                 return error;
1174         }
1175
1176         error = input_register_device(ts->input);
1177         if (error) {
1178                 dev_err(&client->dev,
1179                         "unable to register input device: %d\n", error);
1180                 return error;
1181         }
1182
1183         error = devm_request_threaded_irq(&client->dev, client->irq,
1184                                           NULL, raydium_i2c_irq,
1185                                           IRQF_ONESHOT, client->name, ts);
1186         if (error) {
1187                 dev_err(&client->dev, "Failed to register interrupt\n");
1188                 return error;
1189         }
1190
1191         error = devm_device_add_group(&client->dev,
1192                                    &raydium_i2c_attribute_group);
1193         if (error) {
1194                 dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1195                         error);
1196                 return error;
1197         }
1198
1199         return 0;
1200 }
1201
1202 static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
1203 {
1204         static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
1205         int error;
1206
1207         error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
1208                                  sleep_cmd, sizeof(sleep_cmd));
1209         if (error)
1210                 dev_err(&client->dev,
1211                         "sleep command failed: %d\n", error);
1212 }
1213
1214 static int __maybe_unused raydium_i2c_suspend(struct device *dev)
1215 {
1216         struct i2c_client *client = to_i2c_client(dev);
1217         struct raydium_data *ts = i2c_get_clientdata(client);
1218
1219         /* Sleep is not available in BLDR recovery mode */
1220         if (ts->boot_mode != RAYDIUM_TS_MAIN)
1221                 return -EBUSY;
1222
1223         disable_irq(client->irq);
1224
1225         if (device_may_wakeup(dev)) {
1226                 raydium_enter_sleep(client);
1227
1228                 ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1229         } else {
1230                 raydium_i2c_power_off(ts);
1231         }
1232
1233         return 0;
1234 }
1235
1236 static int __maybe_unused raydium_i2c_resume(struct device *dev)
1237 {
1238         struct i2c_client *client = to_i2c_client(dev);
1239         struct raydium_data *ts = i2c_get_clientdata(client);
1240
1241         if (device_may_wakeup(dev)) {
1242                 if (ts->wake_irq_enabled)
1243                         disable_irq_wake(client->irq);
1244                 raydium_i2c_sw_reset(client);
1245         } else {
1246                 raydium_i2c_power_on(ts);
1247                 raydium_i2c_initialize(ts);
1248         }
1249
1250         enable_irq(client->irq);
1251
1252         return 0;
1253 }
1254
1255 static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1256                          raydium_i2c_suspend, raydium_i2c_resume);
1257
1258 static const struct i2c_device_id raydium_i2c_id[] = {
1259         { "raydium_i2c", 0 },
1260         { "rm32380", 0 },
1261         { /* sentinel */ }
1262 };
1263 MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1264
1265 #ifdef CONFIG_ACPI
1266 static const struct acpi_device_id raydium_acpi_id[] = {
1267         { "RAYD0001", 0 },
1268         { /* sentinel */ }
1269 };
1270 MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1271 #endif
1272
1273 #ifdef CONFIG_OF
1274 static const struct of_device_id raydium_of_match[] = {
1275         { .compatible = "raydium,rm32380", },
1276         { /* sentinel */ }
1277 };
1278 MODULE_DEVICE_TABLE(of, raydium_of_match);
1279 #endif
1280
1281 static struct i2c_driver raydium_i2c_driver = {
1282         .probe = raydium_i2c_probe,
1283         .id_table = raydium_i2c_id,
1284         .driver = {
1285                 .name = "raydium_ts",
1286                 .pm = &raydium_i2c_pm_ops,
1287                 .acpi_match_table = ACPI_PTR(raydium_acpi_id),
1288                 .of_match_table = of_match_ptr(raydium_of_match),
1289         },
1290 };
1291 module_i2c_driver(raydium_i2c_driver);
1292
1293 MODULE_AUTHOR("Raydium");
1294 MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1295 MODULE_LICENSE("GPL v2");