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