1 // SPDX-License-Identifier: GPL-2.0-only
3 * Raydium touchscreen I2C driver.
5 * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
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.
11 * Contact Raydium Semiconductor Corporation at www.rad-ic.com
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>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <asm/unaligned.h>
29 #define RM_BOOT_BLDR 0x02
30 #define RM_BOOT_MAIN 0x03
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*/
39 #define RM_BOOT_RDY 0xFF /* bl data ready */
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
47 #define RM_RESET_MSG_ADDR 0x40000004
49 #define RM_MAX_READ_SIZE 56
50 #define RM_PACKET_CRC_SIZE 2
52 /* Touch relative info */
53 #define RM_MAX_RETRIES 3
54 #define RM_MAX_TOUCH_NUM 10
55 #define RM_BOOT_DELAY_MS 100
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
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
73 #define RM_POWERON_DELAY_USEC 500
74 #define RM_RESET_DELAY_MSEC 50
89 enum raydium_boot_mode
{
94 /* Response to RM_CMD_DATA_BANK request */
95 struct raydium_data_info
{
96 __le32 data_bank_addr
;
101 struct raydium_info
{
102 __le32 hw_ver
; /*device version */
105 __le16 ft_ver
; /* test version */
110 u8 x_res
; /* units/mm */
111 u8 y_res
; /* units/mm */
114 /* struct raydium_data - represents state of Raydium touchscreen device */
115 struct raydium_data
{
116 struct i2c_client
*client
;
117 struct input_dev
*input
;
119 struct regulator
*avdd
;
120 struct regulator
*vccio
;
121 struct gpio_desc
*reset_gpio
;
123 struct raydium_info info
;
125 struct mutex sysfs_mutex
;
134 enum raydium_boot_mode boot_mode
;
136 bool wake_irq_enabled
;
139 static int raydium_i2c_send(struct i2c_client
*client
,
140 u8 addr
, const void *data
, size_t len
)
146 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
151 memcpy(buf
+ 1, data
, len
);
154 ret
= i2c_master_send(client
, buf
, len
+ 1);
155 if (likely(ret
== len
+ 1))
159 } while (++tries
< RM_MAX_RETRIES
);
163 if (unlikely(ret
!= len
+ 1)) {
166 dev_err(&client
->dev
, "%s failed: %d\n", __func__
, ret
);
173 static int raydium_i2c_read(struct i2c_client
*client
,
174 u8 addr
, void *data
, size_t len
)
176 struct i2c_msg xfer
[] = {
178 .addr
= client
->addr
,
183 .addr
= client
->addr
,
191 ret
= i2c_transfer(client
->adapter
, xfer
, ARRAY_SIZE(xfer
));
192 if (unlikely(ret
!= ARRAY_SIZE(xfer
)))
193 return ret
< 0 ? ret
: -EIO
;
198 static int raydium_i2c_read_message(struct i2c_client
*client
,
199 u32 addr
, void *data
, size_t len
)
206 xfer_len
= min_t(size_t, len
, RM_MAX_READ_SIZE
);
208 be_addr
= cpu_to_be32(addr
);
210 error
= raydium_i2c_send(client
, RM_CMD_BANK_SWITCH
,
211 &be_addr
, sizeof(be_addr
));
213 error
= raydium_i2c_read(client
, addr
& 0xff,
226 static int raydium_i2c_send_message(struct i2c_client
*client
,
227 u32 addr
, const void *data
, size_t len
)
229 __be32 be_addr
= cpu_to_be32(addr
);
232 error
= raydium_i2c_send(client
, RM_CMD_BANK_SWITCH
,
233 &be_addr
, sizeof(be_addr
));
235 error
= raydium_i2c_send(client
, addr
& 0xff, data
, len
);
240 static int raydium_i2c_sw_reset(struct i2c_client
*client
)
242 const u8 soft_rst_cmd
= 0x01;
245 error
= raydium_i2c_send_message(client
, RM_RESET_MSG_ADDR
,
246 &soft_rst_cmd
, sizeof(soft_rst_cmd
));
248 dev_err(&client
->dev
, "software reset failed: %d\n", error
);
252 msleep(RM_RESET_DELAY_MSEC
);
257 static int raydium_i2c_query_ts_info(struct raydium_data
*ts
)
259 struct i2c_client
*client
= ts
->client
;
260 struct raydium_data_info data_info
;
261 __le32 query_bank_addr
;
263 int error
, retry_cnt
;
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
));
272 * Warn user if we already allocated memory for reports and
273 * then the size changed (due to firmware update?) and keep
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
);
281 ts
->pkg_size
= data_info
.pkg_size
;
282 ts
->report_size
= ts
->pkg_size
- RM_PACKET_CRC_SIZE
;
285 ts
->contact_size
= data_info
.tp_info_size
;
286 ts
->data_bank_addr
= le32_to_cpu(data_info
.data_bank_addr
);
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
);
292 error
= raydium_i2c_read(client
, RM_CMD_QUERY_BANK
,
294 sizeof(query_bank_addr
));
298 error
= raydium_i2c_read_message(client
,
299 le32_to_cpu(query_bank_addr
),
300 &ts
->info
, sizeof(ts
->info
));
307 dev_err(&client
->dev
, "failed to query device parameters: %d\n", error
);
311 static int raydium_i2c_check_fw_status(struct raydium_data
*ts
)
313 struct i2c_client
*client
= ts
->client
;
314 static const u8 bl_ack
= 0x62;
315 static const u8 main_ack
= 0x66;
319 error
= raydium_i2c_read(client
, RM_CMD_BOOT_READ
, buf
, sizeof(buf
));
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
;
331 static int raydium_i2c_initialize(struct raydium_data
*ts
)
333 struct i2c_client
*client
= ts
->client
;
334 int error
, retry_cnt
;
336 for (retry_cnt
= 0; retry_cnt
< RM_MAX_RETRIES
; retry_cnt
++) {
337 /* Wait for Hello packet */
338 msleep(RM_BOOT_DELAY_MS
);
340 error
= raydium_i2c_check_fw_status(ts
);
342 dev_err(&client
->dev
,
343 "failed to read 'hello' packet: %d\n", error
);
347 if (ts
->boot_mode
== RAYDIUM_TS_BLDR
||
348 ts
->boot_mode
== RAYDIUM_TS_MAIN
) {
354 ts
->boot_mode
= RAYDIUM_TS_BLDR
;
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;
361 raydium_i2c_query_ts_info(ts
);
367 static int raydium_i2c_bl_chk_state(struct i2c_client
*client
,
368 enum raydium_bl_ack state
)
370 static const u8 ack_ok
[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
371 u8 rbuf
[sizeof(ack_ok
)];
375 for (retry
= 0; retry
< RM_MAX_FW_RETRIES
; retry
++) {
377 case RAYDIUM_ACK_NULL
:
380 case RAYDIUM_WAIT_READY
:
381 error
= raydium_i2c_read(client
, RM_CMD_BOOT_CHK
,
383 if (!error
&& rbuf
[0] == RM_BOOT_RDY
)
388 case RAYDIUM_PATH_READY
:
389 error
= raydium_i2c_read(client
, RM_CMD_BOOT_CHK
,
391 if (!error
&& !memcmp(rbuf
, ack_ok
, sizeof(ack_ok
)))
397 dev_err(&client
->dev
, "%s: invalid target state %d\n",
408 static int raydium_i2c_write_object(struct i2c_client
*client
,
409 const void *data
, size_t len
,
410 enum raydium_bl_ack state
)
414 error
= raydium_i2c_send(client
, RM_CMD_BOOT_WRT
, data
, len
);
416 dev_err(&client
->dev
, "WRT obj command failed: %d\n",
421 error
= raydium_i2c_send(client
, RM_CMD_BOOT_ACK
, NULL
, 0);
423 dev_err(&client
->dev
, "Ack obj command failed: %d\n", error
);
427 error
= raydium_i2c_bl_chk_state(client
, state
);
429 dev_err(&client
->dev
, "BL check state failed: %d\n", error
);
435 static bool raydium_i2c_boot_trigger(struct i2c_client
*client
)
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 },
449 for (i
= 0; i
< 7; i
++) {
450 error
= raydium_i2c_write_object(client
, cmd
[i
], sizeof(cmd
[i
]),
453 dev_err(&client
->dev
,
454 "boot trigger failed at step %d: %d\n",
463 static bool raydium_i2c_fw_trigger(struct i2c_client
*client
)
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 },
475 for (i
= 0; i
< 5; i
++) {
476 error
= raydium_i2c_write_object(client
, cmd
[i
], sizeof(cmd
[i
]),
479 dev_err(&client
->dev
,
480 "fw trigger failed at step %d: %d\n",
489 static int raydium_i2c_check_path(struct i2c_client
*client
)
491 static const u8 cmd
[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
494 error
= raydium_i2c_write_object(client
, cmd
, sizeof(cmd
),
497 dev_err(&client
->dev
, "check path command failed: %d\n", error
);
504 static int raydium_i2c_enter_bl(struct i2c_client
*client
)
506 static const u8 cal_cmd
[] = { 0x00, 0x01, 0x52 };
509 error
= raydium_i2c_write_object(client
, cal_cmd
, sizeof(cal_cmd
),
512 dev_err(&client
->dev
, "enter bl command failed: %d\n", error
);
516 msleep(RM_BOOT_DELAY_MS
);
520 static int raydium_i2c_leave_bl(struct i2c_client
*client
)
522 static const u8 leave_cmd
[] = { 0x05, 0x00 };
525 error
= raydium_i2c_write_object(client
, leave_cmd
, sizeof(leave_cmd
),
528 dev_err(&client
->dev
, "leave bl command failed: %d\n", error
);
532 msleep(RM_BOOT_DELAY_MS
);
536 static int raydium_i2c_write_checksum(struct i2c_client
*client
,
537 size_t length
, u16 checksum
)
539 u8 checksum_cmd
[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
542 put_unaligned_le16(length
, &checksum_cmd
[3]);
543 put_unaligned_le16(checksum
, &checksum_cmd
[5]);
545 error
= raydium_i2c_write_object(client
,
546 checksum_cmd
, sizeof(checksum_cmd
),
549 dev_err(&client
->dev
, "failed to write checksum: %d\n",
557 static int raydium_i2c_disable_watch_dog(struct i2c_client
*client
)
559 static const u8 cmd
[] = { 0x0A, 0xAA };
562 error
= raydium_i2c_write_object(client
, cmd
, sizeof(cmd
),
565 dev_err(&client
->dev
, "disable watchdog command failed: %d\n",
573 static int raydium_i2c_fw_write_page(struct i2c_client
*client
,
574 u16 page_idx
, const void *data
, size_t len
)
576 u8 buf
[RM_BL_WRT_LEN
];
581 BUILD_BUG_ON((RM_FW_PAGE_SIZE
% RM_BL_WRT_PKG_SIZE
) != 0);
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;
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
);
594 error
= raydium_i2c_write_object(client
, buf
, RM_BL_WRT_LEN
,
597 dev_err(&client
->dev
,
598 "page write command failed for page %d, chunk %d: %d\n",
610 static u16
raydium_calc_chksum(const u8
*buf
, u16 len
)
615 for (i
= 0; i
< len
; i
++)
621 static int raydium_i2c_do_update_firmware(struct raydium_data
*ts
,
622 const struct firmware
*fw
)
624 struct i2c_client
*client
= ts
->client
;
633 if (fw
->size
== 0 || fw
->size
> RM_MAX_FW_SIZE
) {
634 dev_err(&client
->dev
, "Invalid firmware length\n");
638 error
= raydium_i2c_check_fw_status(ts
);
640 dev_err(&client
->dev
, "Unable to access IC %d\n", error
);
644 if (ts
->boot_mode
== RAYDIUM_TS_MAIN
) {
645 for (i
= 0; i
< RM_MAX_RETRIES
; i
++) {
646 error
= raydium_i2c_enter_bl(client
);
648 error
= raydium_i2c_check_fw_status(ts
);
650 dev_err(&client
->dev
,
651 "unable to access IC: %d\n",
656 if (ts
->boot_mode
== RAYDIUM_TS_BLDR
)
661 if (ts
->boot_mode
== RAYDIUM_TS_MAIN
) {
662 dev_err(&client
->dev
,
663 "failed to jump to boot loader: %d\n",
669 error
= raydium_i2c_disable_watch_dog(client
);
673 error
= raydium_i2c_check_path(client
);
677 error
= raydium_i2c_boot_trigger(client
);
679 dev_err(&client
->dev
, "send boot trigger fail: %d\n", error
);
683 msleep(RM_BOOT_DELAY_MS
);
690 len
= min_t(size_t, data_len
, RM_FW_PAGE_SIZE
);
692 error
= raydium_i2c_fw_write_page(client
, page_nr
++, data
, len
);
702 error
= raydium_i2c_leave_bl(client
);
704 dev_err(&client
->dev
,
705 "failed to leave boot loader: %d\n", error
);
709 dev_dbg(&client
->dev
, "left boot loader mode\n");
710 msleep(RM_BOOT_DELAY_MS
);
712 error
= raydium_i2c_check_fw_status(ts
);
714 dev_err(&client
->dev
,
715 "failed to check fw status after write: %d\n",
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",
727 error
= raydium_i2c_fw_trigger(client
);
729 dev_err(&client
->dev
, "failed to trigger fw: %d\n", error
);
733 fw_checksum
= raydium_calc_chksum(fw
->data
, fw
->size
);
735 error
= raydium_i2c_write_checksum(client
, fw
->size
, fw_checksum
);
742 static int raydium_i2c_fw_update(struct raydium_data
*ts
)
744 struct i2c_client
*client
= ts
->client
;
745 const struct firmware
*fw
= NULL
;
749 fw_file
= kasprintf(GFP_KERNEL
, "raydium_%#04x.fw",
750 le32_to_cpu(ts
->info
.hw_ver
));
754 dev_dbg(&client
->dev
, "firmware name: %s\n", fw_file
);
756 error
= request_firmware(&fw
, fw_file
, &client
->dev
);
758 dev_err(&client
->dev
, "Unable to open firmware %s\n", fw_file
);
759 goto out_free_fw_file
;
762 disable_irq(client
->irq
);
764 error
= raydium_i2c_do_update_firmware(ts
, fw
);
766 dev_err(&client
->dev
, "firmware update failed: %d\n", error
);
767 ts
->boot_mode
= RAYDIUM_TS_BLDR
;
771 error
= raydium_i2c_initialize(ts
);
773 dev_err(&client
->dev
,
774 "failed to initialize device after firmware update: %d\n",
776 ts
->boot_mode
= RAYDIUM_TS_BLDR
;
780 ts
->boot_mode
= RAYDIUM_TS_MAIN
;
783 enable_irq(client
->irq
);
786 release_firmware(fw
);
794 static void raydium_mt_event(struct raydium_data
*ts
)
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
];
803 input_mt_slot(ts
->input
, i
);
804 input_mt_report_slot_state(ts
->input
, MT_TOOL_FINGER
, state
);
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
]);
816 wx
= contact
[RM_CONTACT_WIDTH_X_POS
];
817 wy
= contact
[RM_CONTACT_WIDTH_Y_POS
];
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
));
823 input_mt_sync_frame(ts
->input
);
824 input_sync(ts
->input
);
827 static irqreturn_t
raydium_i2c_irq(int irq
, void *_dev
)
829 struct raydium_data
*ts
= _dev
;
834 if (ts
->boot_mode
!= RAYDIUM_TS_MAIN
)
837 error
= raydium_i2c_read_message(ts
->client
, ts
->data_bank_addr
,
838 ts
->report_data
, ts
->pkg_size
);
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
);
851 raydium_mt_event(ts
);
857 static ssize_t
raydium_i2c_fw_ver_show(struct device
*dev
,
858 struct device_attribute
*attr
, char *buf
)
860 struct i2c_client
*client
= to_i2c_client(dev
);
861 struct raydium_data
*ts
= i2c_get_clientdata(client
);
863 return sprintf(buf
, "%d.%d\n", ts
->info
.main_ver
, ts
->info
.sub_ver
);
866 static ssize_t
raydium_i2c_hw_ver_show(struct device
*dev
,
867 struct device_attribute
*attr
, char *buf
)
869 struct i2c_client
*client
= to_i2c_client(dev
);
870 struct raydium_data
*ts
= i2c_get_clientdata(client
);
872 return sprintf(buf
, "%#04x\n", le32_to_cpu(ts
->info
.hw_ver
));
875 static ssize_t
raydium_i2c_boot_mode_show(struct device
*dev
,
876 struct device_attribute
*attr
,
879 struct i2c_client
*client
= to_i2c_client(dev
);
880 struct raydium_data
*ts
= i2c_get_clientdata(client
);
882 return sprintf(buf
, "%s\n",
883 ts
->boot_mode
== RAYDIUM_TS_MAIN
?
884 "Normal" : "Recovery");
887 static ssize_t
raydium_i2c_update_fw_store(struct device
*dev
,
888 struct device_attribute
*attr
,
889 const char *buf
, size_t count
)
891 struct i2c_client
*client
= to_i2c_client(dev
);
892 struct raydium_data
*ts
= i2c_get_clientdata(client
);
895 error
= mutex_lock_interruptible(&ts
->sysfs_mutex
);
899 error
= raydium_i2c_fw_update(ts
);
901 mutex_unlock(&ts
->sysfs_mutex
);
903 return error
?: count
;
906 static ssize_t
raydium_i2c_calibrate_store(struct device
*dev
,
907 struct device_attribute
*attr
,
908 const char *buf
, size_t count
)
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 };
915 error
= mutex_lock_interruptible(&ts
->sysfs_mutex
);
919 error
= raydium_i2c_write_object(client
, cal_cmd
, sizeof(cal_cmd
),
922 dev_err(&client
->dev
, "calibrate command failed: %d\n", error
);
924 mutex_unlock(&ts
->sysfs_mutex
);
925 return error
?: count
;
928 static DEVICE_ATTR(fw_version
, S_IRUGO
, raydium_i2c_fw_ver_show
, NULL
);
929 static DEVICE_ATTR(hw_version
, S_IRUGO
, raydium_i2c_hw_ver_show
, NULL
);
930 static DEVICE_ATTR(boot_mode
, S_IRUGO
, raydium_i2c_boot_mode_show
, NULL
);
931 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, raydium_i2c_update_fw_store
);
932 static DEVICE_ATTR(calibrate
, S_IWUSR
, NULL
, raydium_i2c_calibrate_store
);
934 static struct attribute
*raydium_i2c_attributes
[] = {
935 &dev_attr_update_fw
.attr
,
936 &dev_attr_boot_mode
.attr
,
937 &dev_attr_fw_version
.attr
,
938 &dev_attr_hw_version
.attr
,
939 &dev_attr_calibrate
.attr
,
943 static const struct attribute_group raydium_i2c_attribute_group
= {
944 .attrs
= raydium_i2c_attributes
,
947 static int raydium_i2c_power_on(struct raydium_data
*ts
)
954 gpiod_set_value_cansleep(ts
->reset_gpio
, 1);
956 error
= regulator_enable(ts
->avdd
);
958 dev_err(&ts
->client
->dev
,
959 "failed to enable avdd regulator: %d\n", error
);
960 goto release_reset_gpio
;
963 error
= regulator_enable(ts
->vccio
);
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
;
971 udelay(RM_POWERON_DELAY_USEC
);
974 gpiod_set_value_cansleep(ts
->reset_gpio
, 0);
979 msleep(RM_RESET_DELAY_MSEC
);
984 static void raydium_i2c_power_off(void *_data
)
986 struct raydium_data
*ts
= _data
;
988 if (ts
->reset_gpio
) {
989 gpiod_set_value_cansleep(ts
->reset_gpio
, 1);
990 regulator_disable(ts
->vccio
);
991 regulator_disable(ts
->avdd
);
995 static int raydium_i2c_probe(struct i2c_client
*client
,
996 const struct i2c_device_id
*id
)
998 union i2c_smbus_data dummy
;
999 struct raydium_data
*ts
;
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");
1008 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
1012 mutex_init(&ts
->sysfs_mutex
);
1014 ts
->client
= client
;
1015 i2c_set_clientdata(client
, ts
);
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
);
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
);
1035 ts
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
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
);
1045 error
= raydium_i2c_power_on(ts
);
1049 error
= devm_add_action(&client
->dev
, raydium_i2c_power_off
, ts
);
1051 dev_err(&client
->dev
,
1052 "failed to install power off action: %d\n", error
);
1053 raydium_i2c_power_off(ts
);
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");
1064 error
= raydium_i2c_initialize(ts
);
1066 dev_err(&client
->dev
, "failed to initialize: %d\n", error
);
1070 ts
->report_data
= devm_kmalloc(&client
->dev
,
1071 ts
->pkg_size
, GFP_KERNEL
);
1072 if (!ts
->report_data
)
1075 ts
->input
= devm_input_allocate_device(&client
->dev
);
1077 dev_err(&client
->dev
, "Failed to allocate input device\n");
1081 ts
->input
->name
= "Raydium Touchscreen";
1082 ts
->input
->id
.bustype
= BUS_I2C
;
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
);
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);
1094 error
= input_mt_init_slots(ts
->input
, RM_MAX_TOUCH_NUM
,
1095 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
1097 dev_err(&client
->dev
,
1098 "failed to initialize MT slots: %d\n", error
);
1102 error
= input_register_device(ts
->input
);
1104 dev_err(&client
->dev
,
1105 "unable to register input device: %d\n", error
);
1109 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1110 NULL
, raydium_i2c_irq
,
1111 IRQF_ONESHOT
, client
->name
, ts
);
1113 dev_err(&client
->dev
, "Failed to register interrupt\n");
1117 error
= devm_device_add_group(&client
->dev
,
1118 &raydium_i2c_attribute_group
);
1120 dev_err(&client
->dev
, "failed to create sysfs attributes: %d\n",
1128 static void __maybe_unused
raydium_enter_sleep(struct i2c_client
*client
)
1130 static const u8 sleep_cmd
[] = { 0x5A, 0xff, 0x00, 0x0f };
1133 error
= raydium_i2c_send(client
, RM_CMD_ENTER_SLEEP
,
1134 sleep_cmd
, sizeof(sleep_cmd
));
1136 dev_err(&client
->dev
,
1137 "sleep command failed: %d\n", error
);
1140 static int __maybe_unused
raydium_i2c_suspend(struct device
*dev
)
1142 struct i2c_client
*client
= to_i2c_client(dev
);
1143 struct raydium_data
*ts
= i2c_get_clientdata(client
);
1145 /* Sleep is not available in BLDR recovery mode */
1146 if (ts
->boot_mode
!= RAYDIUM_TS_MAIN
)
1149 disable_irq(client
->irq
);
1151 if (device_may_wakeup(dev
)) {
1152 raydium_enter_sleep(client
);
1154 ts
->wake_irq_enabled
= (enable_irq_wake(client
->irq
) == 0);
1156 raydium_i2c_power_off(ts
);
1162 static int __maybe_unused
raydium_i2c_resume(struct device
*dev
)
1164 struct i2c_client
*client
= to_i2c_client(dev
);
1165 struct raydium_data
*ts
= i2c_get_clientdata(client
);
1167 if (device_may_wakeup(dev
)) {
1168 if (ts
->wake_irq_enabled
)
1169 disable_irq_wake(client
->irq
);
1170 raydium_i2c_sw_reset(client
);
1172 raydium_i2c_power_on(ts
);
1173 raydium_i2c_initialize(ts
);
1176 enable_irq(client
->irq
);
1181 static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops
,
1182 raydium_i2c_suspend
, raydium_i2c_resume
);
1184 static const struct i2c_device_id raydium_i2c_id
[] = {
1185 { "raydium_i2c" , 0 },
1189 MODULE_DEVICE_TABLE(i2c
, raydium_i2c_id
);
1192 static const struct acpi_device_id raydium_acpi_id
[] = {
1196 MODULE_DEVICE_TABLE(acpi
, raydium_acpi_id
);
1200 static const struct of_device_id raydium_of_match
[] = {
1201 { .compatible
= "raydium,rm32380", },
1204 MODULE_DEVICE_TABLE(of
, raydium_of_match
);
1207 static struct i2c_driver raydium_i2c_driver
= {
1208 .probe
= raydium_i2c_probe
,
1209 .id_table
= raydium_i2c_id
,
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
),
1217 module_i2c_driver(raydium_i2c_driver
);
1219 MODULE_AUTHOR("Raydium");
1220 MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1221 MODULE_LICENSE("GPL v2");