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