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