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/pm_wakeirq.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/unaligned.h>
30 #define RM_BOOT_BLDR 0x02
31 #define RM_BOOT_MAIN 0x03
33 /* I2C bootoloader commands */
34 #define RM_CMD_BOOT_PAGE_WRT 0x0B /* send bl page write */
35 #define RM_CMD_BOOT_WRT 0x11 /* send bl write */
36 #define RM_CMD_BOOT_ACK 0x22 /* send ack*/
37 #define RM_CMD_BOOT_CHK 0x33 /* send data check */
38 #define RM_CMD_BOOT_READ 0x44 /* send wait bl data ready*/
40 #define RM_BOOT_RDY 0xFF /* bl data ready */
41 #define RM_BOOT_CMD_READHWID 0x0E /* read hwid */
43 /* I2C main commands */
44 #define RM_CMD_QUERY_BANK 0x2B
45 #define RM_CMD_DATA_BANK 0x4D
46 #define RM_CMD_ENTER_SLEEP 0x4E
47 #define RM_CMD_BANK_SWITCH 0xAA
49 #define RM_RESET_MSG_ADDR 0x40000004
51 #define RM_MAX_READ_SIZE 56
52 #define RM_PACKET_CRC_SIZE 2
54 /* Touch relative info */
55 #define RM_MAX_RETRIES 3
56 #define RM_RETRY_DELAY_MS 20
57 #define RM_MAX_TOUCH_NUM 10
58 #define RM_BOOT_DELAY_MS 100
60 /* Offsets in contact data */
61 #define RM_CONTACT_STATE_POS 0
62 #define RM_CONTACT_X_POS 1
63 #define RM_CONTACT_Y_POS 3
64 #define RM_CONTACT_PRESSURE_POS 5
65 #define RM_CONTACT_WIDTH_X_POS 6
66 #define RM_CONTACT_WIDTH_Y_POS 7
68 /* Bootloader relative info */
69 #define RM_BL_WRT_CMD_SIZE 3 /* bl flash wrt cmd size */
70 #define RM_BL_WRT_PKG_SIZE 32 /* bl wrt pkg size */
71 #define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
72 #define RM_FW_PAGE_SIZE 128
73 #define RM_MAX_FW_RETRIES 30
74 #define RM_MAX_FW_SIZE 0xD000
76 #define RM_POWERON_DELAY_USEC 500
77 #define RM_RESET_DELAY_MSEC 50
92 enum raydium_boot_mode {
97 /* Response to RM_CMD_DATA_BANK request */
98 struct raydium_data_info {
99 __le32 data_bank_addr;
104 struct raydium_info {
105 __le32 hw_ver; /*device version */
108 __le16 ft_ver; /* test version */
113 u8 x_res; /* units/mm */
114 u8 y_res; /* units/mm */
117 /* struct raydium_data - represents state of Raydium touchscreen device */
118 struct raydium_data {
119 struct i2c_client *client;
120 struct input_dev *input;
122 struct regulator *avdd;
123 struct regulator *vccio;
124 struct gpio_desc *reset_gpio;
126 struct raydium_info info;
128 struct mutex sysfs_mutex;
137 enum raydium_boot_mode boot_mode;
141 * Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
142 * raydium_i2c_{read|send} below.
144 struct __packed raydium_bank_switch_header {
149 static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
150 struct i2c_msg *xfer, size_t xfer_count)
154 * If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
155 * sent first. Else, skip the header i.e. xfer[0].
157 int xfer_start_idx = (addr > 0xff) ? 0 : 1;
158 xfer_count -= xfer_start_idx;
160 ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
161 if (likely(ret == xfer_count))
164 return ret < 0 ? ret : -EIO;
167 static int raydium_i2c_send(struct i2c_client *client,
168 u32 addr, const void *data, size_t len)
173 u8 reg_addr = addr & 0xff;
175 tx_buf = kmalloc(len + 1, GFP_KERNEL);
179 tx_buf[0] = reg_addr;
180 memcpy(tx_buf + 1, data, len);
183 struct raydium_bank_switch_header header = {
184 .cmd = RM_CMD_BANK_SWITCH,
185 .be_addr = cpu_to_be32(addr),
189 * Perform as a single i2c_transfer transaction to ensure that
190 * no other I2C transactions are initiated on the bus to any
191 * other device in between. Initiating transacations to other
192 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
193 * issues. This is also why regmap infrastructure cannot be used
194 * for this driver. Regmap handles page(bank) switch and reads
195 * as separate i2c_transfer() operations. This can result in
196 * problems if the Raydium device is on a shared I2C bus.
198 struct i2c_msg xfer[] = {
200 .addr = client->addr,
201 .len = sizeof(header),
202 .buf = (u8 *)&header,
205 .addr = client->addr,
211 error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
215 msleep(RM_RETRY_DELAY_MS);
216 } while (++tries < RM_MAX_RETRIES);
218 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
224 static int raydium_i2c_read(struct i2c_client *client,
225 u32 addr, void *data, size_t len)
230 u8 reg_addr = addr & 0xff;
231 struct raydium_bank_switch_header header = {
232 .cmd = RM_CMD_BANK_SWITCH,
233 .be_addr = cpu_to_be32(addr),
235 size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
238 * Perform as a single i2c_transfer transaction to ensure that
239 * no other I2C transactions are initiated on the bus to any
240 * other device in between. Initiating transacations to other
241 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
242 * issues. This is also why regmap infrastructure cannot be used
243 * for this driver. Regmap handles page(bank) switch and writes
244 * as separate i2c_transfer() operations. This can result in
245 * problems if the Raydium device is on a shared I2C bus.
247 struct i2c_msg xfer[] = {
249 .addr = client->addr,
250 .len = sizeof(header),
251 .buf = (u8 *)&header,
254 .addr = client->addr,
259 .addr = client->addr,
266 error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
278 static int raydium_i2c_sw_reset(struct i2c_client *client)
280 const u8 soft_rst_cmd = 0x01;
283 error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
284 sizeof(soft_rst_cmd));
286 dev_err(&client->dev, "software reset failed: %d\n", error);
290 msleep(RM_RESET_DELAY_MSEC);
295 static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
297 struct i2c_client *client = ts->client;
298 static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
299 0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
304 error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
305 get_hwid, sizeof(get_hwid));
307 dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
311 error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
313 dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
317 error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
319 dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
321 hw_ver = 0xffffffffUL;
323 hw_ver = get_unaligned_be32(rbuf + 1);
326 ts->info.hw_ver = cpu_to_le32(hw_ver);
327 ts->info.main_ver = 0xff;
328 ts->info.sub_ver = 0xff;
333 static int raydium_i2c_query_ts_info(struct raydium_data *ts)
335 struct i2c_client *client = ts->client;
336 struct raydium_data_info data_info;
337 __le32 query_bank_addr;
339 int error, retry_cnt;
341 for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
342 error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
343 &data_info, sizeof(data_info));
348 * Warn user if we already allocated memory for reports and
349 * then the size changed (due to firmware update?) and keep
352 if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
353 dev_warn(&client->dev,
354 "report size changes, was: %d, new: %d\n",
355 ts->pkg_size, data_info.pkg_size);
357 ts->pkg_size = data_info.pkg_size;
358 ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
361 ts->contact_size = data_info.tp_info_size;
362 ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
364 dev_dbg(&client->dev,
365 "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
366 ts->data_bank_addr, ts->report_size, ts->contact_size);
368 error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
370 sizeof(query_bank_addr));
374 error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
375 &ts->info, sizeof(ts->info));
382 dev_err(&client->dev, "failed to query device parameters: %d\n", error);
386 static int raydium_i2c_check_fw_status(struct raydium_data *ts)
388 struct i2c_client *client = ts->client;
389 static const u8 bl_ack = 0x62;
390 static const u8 main_ack = 0x66;
394 error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
396 if (buf[0] == bl_ack)
397 ts->boot_mode = RAYDIUM_TS_BLDR;
398 else if (buf[0] == main_ack)
399 ts->boot_mode = RAYDIUM_TS_MAIN;
406 static int raydium_i2c_initialize(struct raydium_data *ts)
408 struct i2c_client *client = ts->client;
409 int error, retry_cnt;
411 for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
412 /* Wait for Hello packet */
413 msleep(RM_BOOT_DELAY_MS);
415 error = raydium_i2c_check_fw_status(ts);
417 dev_err(&client->dev,
418 "failed to read 'hello' packet: %d\n", error);
422 if (ts->boot_mode == RAYDIUM_TS_BLDR ||
423 ts->boot_mode == RAYDIUM_TS_MAIN) {
429 ts->boot_mode = RAYDIUM_TS_BLDR;
431 if (ts->boot_mode == RAYDIUM_TS_BLDR)
432 raydium_i2c_query_ts_bootloader_info(ts);
434 raydium_i2c_query_ts_info(ts);
439 static int raydium_i2c_bl_chk_state(struct i2c_client *client,
440 enum raydium_bl_ack state)
442 static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
443 u8 rbuf[sizeof(ack_ok)];
447 for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
449 case RAYDIUM_ACK_NULL:
452 case RAYDIUM_WAIT_READY:
453 error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
455 if (!error && rbuf[0] == RM_BOOT_RDY)
460 case RAYDIUM_PATH_READY:
461 error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
463 if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
469 dev_err(&client->dev, "%s: invalid target state %d\n",
480 static int raydium_i2c_write_object(struct i2c_client *client,
481 const void *data, size_t len,
482 enum raydium_bl_ack state)
485 static const u8 cmd[] = { 0xFF, 0x39 };
487 error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
489 dev_err(&client->dev, "WRT obj command failed: %d\n",
494 error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
496 dev_err(&client->dev, "Ack obj command failed: %d\n", error);
500 error = raydium_i2c_bl_chk_state(client, state);
502 dev_err(&client->dev, "BL check state failed: %d\n", error);
508 static int raydium_i2c_boot_trigger(struct i2c_client *client)
510 static const u8 cmd[7][6] = {
511 { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
512 { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
513 { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
514 { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
515 { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
516 { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
517 { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
522 for (i = 0; i < 7; i++) {
523 error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
526 dev_err(&client->dev,
527 "boot trigger failed at step %d: %d\n",
536 static int raydium_i2c_fw_trigger(struct i2c_client *client)
538 static const u8 cmd[5][11] = {
539 { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
540 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
541 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
542 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
543 { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
548 for (i = 0; i < 5; i++) {
549 error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
552 dev_err(&client->dev,
553 "fw trigger failed at step %d: %d\n",
562 static int raydium_i2c_check_path(struct i2c_client *client)
564 static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
567 error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
570 dev_err(&client->dev, "check path command failed: %d\n", error);
577 static int raydium_i2c_enter_bl(struct i2c_client *client)
579 static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
582 error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
585 dev_err(&client->dev, "enter bl command failed: %d\n", error);
589 msleep(RM_BOOT_DELAY_MS);
593 static int raydium_i2c_leave_bl(struct i2c_client *client)
595 static const u8 leave_cmd[] = { 0x05, 0x00 };
598 error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
601 dev_err(&client->dev, "leave bl command failed: %d\n", error);
605 msleep(RM_BOOT_DELAY_MS);
609 static int raydium_i2c_write_checksum(struct i2c_client *client,
610 size_t length, u16 checksum)
612 u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
615 put_unaligned_le16(length, &checksum_cmd[3]);
616 put_unaligned_le16(checksum, &checksum_cmd[5]);
618 error = raydium_i2c_write_object(client,
619 checksum_cmd, sizeof(checksum_cmd),
622 dev_err(&client->dev, "failed to write checksum: %d\n",
630 static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
632 static const u8 cmd[] = { 0x0A, 0xAA };
635 error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
638 dev_err(&client->dev, "disable watchdog command failed: %d\n",
646 static int raydium_i2c_fw_write_page(struct i2c_client *client,
647 u16 page_idx, const void *data, size_t len)
649 u8 buf[RM_BL_WRT_LEN];
654 BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
656 for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
657 buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
658 buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
659 buf[BL_PKG_IDX] = i + 1;
661 xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
662 memcpy(&buf[BL_DATA_STR], data, xfer_len);
663 if (len < RM_BL_WRT_PKG_SIZE)
664 memset(&buf[BL_DATA_STR + xfer_len], 0xff,
665 RM_BL_WRT_PKG_SIZE - xfer_len);
667 error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
670 dev_err(&client->dev,
671 "page write command failed for page %d, chunk %d: %d\n",
683 static u16 raydium_calc_chksum(const u8 *buf, u16 len)
688 for (i = 0; i < len; i++)
694 static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
695 const struct firmware *fw)
697 struct i2c_client *client = ts->client;
706 if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
707 dev_err(&client->dev, "Invalid firmware length\n");
711 error = raydium_i2c_check_fw_status(ts);
713 dev_err(&client->dev, "Unable to access IC %d\n", error);
717 if (ts->boot_mode == RAYDIUM_TS_MAIN) {
718 for (i = 0; i < RM_MAX_RETRIES; i++) {
719 error = raydium_i2c_enter_bl(client);
721 error = raydium_i2c_check_fw_status(ts);
723 dev_err(&client->dev,
724 "unable to access IC: %d\n",
729 if (ts->boot_mode == RAYDIUM_TS_BLDR)
734 if (ts->boot_mode == RAYDIUM_TS_MAIN) {
735 dev_err(&client->dev,
736 "failed to jump to boot loader: %d\n",
742 error = raydium_i2c_disable_watch_dog(client);
746 error = raydium_i2c_check_path(client);
750 error = raydium_i2c_boot_trigger(client);
752 dev_err(&client->dev, "send boot trigger fail: %d\n", error);
756 msleep(RM_BOOT_DELAY_MS);
763 len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
765 error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
775 error = raydium_i2c_leave_bl(client);
777 dev_err(&client->dev,
778 "failed to leave boot loader: %d\n", error);
782 dev_dbg(&client->dev, "left boot loader mode\n");
783 msleep(RM_BOOT_DELAY_MS);
785 error = raydium_i2c_check_fw_status(ts);
787 dev_err(&client->dev,
788 "failed to check fw status after write: %d\n",
793 if (ts->boot_mode != RAYDIUM_TS_MAIN) {
794 dev_err(&client->dev,
795 "failed to switch to main fw after writing firmware: %d\n",
800 error = raydium_i2c_fw_trigger(client);
802 dev_err(&client->dev, "failed to trigger fw: %d\n", error);
806 fw_checksum = raydium_calc_chksum(fw->data, fw->size);
808 error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
815 static int raydium_i2c_fw_update(struct raydium_data *ts)
817 struct i2c_client *client = ts->client;
818 const struct firmware *fw = NULL;
822 fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
823 le32_to_cpu(ts->info.hw_ver));
827 dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
829 error = request_firmware(&fw, fw_file, &client->dev);
831 dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
832 goto out_free_fw_file;
835 disable_irq(client->irq);
837 error = raydium_i2c_do_update_firmware(ts, fw);
839 dev_err(&client->dev, "firmware update failed: %d\n", error);
840 ts->boot_mode = RAYDIUM_TS_BLDR;
844 error = raydium_i2c_initialize(ts);
846 dev_err(&client->dev,
847 "failed to initialize device after firmware update: %d\n",
849 ts->boot_mode = RAYDIUM_TS_BLDR;
853 ts->boot_mode = RAYDIUM_TS_MAIN;
856 enable_irq(client->irq);
859 release_firmware(fw);
867 static void raydium_mt_event(struct raydium_data *ts)
871 for (i = 0; i < ts->report_size / ts->contact_size; i++) {
872 u8 *contact = &ts->report_data[ts->contact_size * i];
873 bool state = contact[RM_CONTACT_STATE_POS];
876 input_mt_slot(ts->input, i);
877 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
882 input_report_abs(ts->input, ABS_MT_POSITION_X,
883 get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
884 input_report_abs(ts->input, ABS_MT_POSITION_Y,
885 get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
886 input_report_abs(ts->input, ABS_MT_PRESSURE,
887 contact[RM_CONTACT_PRESSURE_POS]);
889 wx = contact[RM_CONTACT_WIDTH_X_POS];
890 wy = contact[RM_CONTACT_WIDTH_Y_POS];
892 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
893 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
896 input_mt_sync_frame(ts->input);
897 input_sync(ts->input);
900 static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
902 struct raydium_data *ts = _dev;
907 if (ts->boot_mode != RAYDIUM_TS_MAIN)
910 error = raydium_i2c_read(ts->client, ts->data_bank_addr,
911 ts->report_data, ts->pkg_size);
915 fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
916 calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
917 if (unlikely(fw_crc != calc_crc)) {
918 dev_warn(&ts->client->dev,
919 "%s: invalid packet crc %#04x vs %#04x\n",
920 __func__, calc_crc, fw_crc);
924 raydium_mt_event(ts);
930 static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
931 struct device_attribute *attr, char *buf)
933 struct i2c_client *client = to_i2c_client(dev);
934 struct raydium_data *ts = i2c_get_clientdata(client);
936 return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
939 static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
940 struct device_attribute *attr, char *buf)
942 struct i2c_client *client = to_i2c_client(dev);
943 struct raydium_data *ts = i2c_get_clientdata(client);
945 return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
948 static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
949 struct device_attribute *attr,
952 struct i2c_client *client = to_i2c_client(dev);
953 struct raydium_data *ts = i2c_get_clientdata(client);
955 return sprintf(buf, "%s\n",
956 ts->boot_mode == RAYDIUM_TS_MAIN ?
957 "Normal" : "Recovery");
960 static ssize_t raydium_i2c_update_fw_store(struct device *dev,
961 struct device_attribute *attr,
962 const char *buf, size_t count)
964 struct i2c_client *client = to_i2c_client(dev);
965 struct raydium_data *ts = i2c_get_clientdata(client);
968 error = mutex_lock_interruptible(&ts->sysfs_mutex);
972 error = raydium_i2c_fw_update(ts);
974 mutex_unlock(&ts->sysfs_mutex);
976 return error ?: count;
979 static ssize_t raydium_i2c_calibrate_store(struct device *dev,
980 struct device_attribute *attr,
981 const char *buf, size_t count)
983 struct i2c_client *client = to_i2c_client(dev);
984 struct raydium_data *ts = i2c_get_clientdata(client);
985 static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
988 error = mutex_lock_interruptible(&ts->sysfs_mutex);
992 error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
995 dev_err(&client->dev, "calibrate command failed: %d\n", error);
997 mutex_unlock(&ts->sysfs_mutex);
998 return error ?: count;
1001 static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
1002 static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
1003 static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
1004 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
1005 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
1007 static struct attribute *raydium_i2c_attrs[] = {
1008 &dev_attr_update_fw.attr,
1009 &dev_attr_boot_mode.attr,
1010 &dev_attr_fw_version.attr,
1011 &dev_attr_hw_version.attr,
1012 &dev_attr_calibrate.attr,
1015 ATTRIBUTE_GROUPS(raydium_i2c);
1017 static int raydium_i2c_power_on(struct raydium_data *ts)
1021 if (!ts->reset_gpio)
1024 gpiod_set_value_cansleep(ts->reset_gpio, 1);
1026 error = regulator_enable(ts->avdd);
1028 dev_err(&ts->client->dev,
1029 "failed to enable avdd regulator: %d\n", error);
1030 goto release_reset_gpio;
1033 error = regulator_enable(ts->vccio);
1035 regulator_disable(ts->avdd);
1036 dev_err(&ts->client->dev,
1037 "failed to enable vccio regulator: %d\n", error);
1038 goto release_reset_gpio;
1041 udelay(RM_POWERON_DELAY_USEC);
1044 gpiod_set_value_cansleep(ts->reset_gpio, 0);
1049 msleep(RM_RESET_DELAY_MSEC);
1054 static void raydium_i2c_power_off(void *_data)
1056 struct raydium_data *ts = _data;
1058 if (ts->reset_gpio) {
1059 gpiod_set_value_cansleep(ts->reset_gpio, 1);
1060 regulator_disable(ts->vccio);
1061 regulator_disable(ts->avdd);
1065 static int raydium_i2c_probe(struct i2c_client *client)
1067 union i2c_smbus_data dummy;
1068 struct raydium_data *ts;
1071 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1072 dev_err(&client->dev,
1073 "i2c check functionality error (need I2C_FUNC_I2C)\n");
1077 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1081 mutex_init(&ts->sysfs_mutex);
1083 ts->client = client;
1084 i2c_set_clientdata(client, ts);
1086 ts->avdd = devm_regulator_get(&client->dev, "avdd");
1087 if (IS_ERR(ts->avdd))
1088 return dev_err_probe(&client->dev, PTR_ERR(ts->avdd),
1089 "Failed to get 'avdd' regulator\n");
1091 ts->vccio = devm_regulator_get(&client->dev, "vccio");
1092 if (IS_ERR(ts->vccio))
1093 return dev_err_probe(&client->dev, PTR_ERR(ts->vccio),
1094 "Failed to get 'vccio' regulator\n");
1096 ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1098 if (IS_ERR(ts->reset_gpio))
1099 return dev_err_probe(&client->dev, PTR_ERR(ts->reset_gpio),
1100 "Failed to get reset gpio\n");
1102 error = raydium_i2c_power_on(ts);
1106 error = devm_add_action_or_reset(&client->dev,
1107 raydium_i2c_power_off, ts);
1109 dev_err(&client->dev,
1110 "failed to install power off action: %d\n", error);
1114 /* Make sure there is something at this address */
1115 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1116 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1117 dev_err(&client->dev, "nothing at this address\n");
1121 error = raydium_i2c_initialize(ts);
1123 dev_err(&client->dev, "failed to initialize: %d\n", error);
1127 ts->report_data = devm_kmalloc(&client->dev,
1128 ts->pkg_size, GFP_KERNEL);
1129 if (!ts->report_data)
1132 ts->input = devm_input_allocate_device(&client->dev);
1134 dev_err(&client->dev, "Failed to allocate input device\n");
1138 ts->input->name = "Raydium Touchscreen";
1139 ts->input->id.bustype = BUS_I2C;
1141 input_set_abs_params(ts->input, ABS_MT_POSITION_X,
1142 0, le16_to_cpu(ts->info.x_max), 0, 0);
1143 input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
1144 0, le16_to_cpu(ts->info.y_max), 0, 0);
1145 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
1146 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
1148 input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1149 input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1151 error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
1152 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1154 dev_err(&client->dev,
1155 "failed to initialize MT slots: %d\n", error);
1159 error = input_register_device(ts->input);
1161 dev_err(&client->dev,
1162 "unable to register input device: %d\n", error);
1166 error = devm_request_threaded_irq(&client->dev, client->irq,
1167 NULL, raydium_i2c_irq,
1168 IRQF_ONESHOT, client->name, ts);
1170 dev_err(&client->dev, "Failed to register interrupt\n");
1177 static void raydium_enter_sleep(struct i2c_client *client)
1179 static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
1182 error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
1183 sleep_cmd, sizeof(sleep_cmd));
1185 dev_err(&client->dev,
1186 "sleep command failed: %d\n", error);
1189 static int raydium_i2c_suspend(struct device *dev)
1191 struct i2c_client *client = to_i2c_client(dev);
1192 struct raydium_data *ts = i2c_get_clientdata(client);
1194 /* Sleep is not available in BLDR recovery mode */
1195 if (ts->boot_mode != RAYDIUM_TS_MAIN)
1198 disable_irq(client->irq);
1200 if (device_may_wakeup(dev)) {
1201 raydium_enter_sleep(client);
1203 raydium_i2c_power_off(ts);
1209 static int raydium_i2c_resume(struct device *dev)
1211 struct i2c_client *client = to_i2c_client(dev);
1212 struct raydium_data *ts = i2c_get_clientdata(client);
1214 if (device_may_wakeup(dev)) {
1215 raydium_i2c_sw_reset(client);
1217 raydium_i2c_power_on(ts);
1218 raydium_i2c_initialize(ts);
1221 enable_irq(client->irq);
1226 static DEFINE_SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1227 raydium_i2c_suspend, raydium_i2c_resume);
1229 static const struct i2c_device_id raydium_i2c_id[] = {
1234 MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1237 static const struct acpi_device_id raydium_acpi_id[] = {
1241 MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1245 static const struct of_device_id raydium_of_match[] = {
1246 { .compatible = "raydium,rm32380", },
1249 MODULE_DEVICE_TABLE(of, raydium_of_match);
1252 static struct i2c_driver raydium_i2c_driver = {
1253 .probe = raydium_i2c_probe,
1254 .id_table = raydium_i2c_id,
1256 .name = "raydium_ts",
1257 .dev_groups = raydium_i2c_groups,
1258 .pm = pm_sleep_ptr(&raydium_i2c_pm_ops),
1259 .acpi_match_table = ACPI_PTR(raydium_acpi_id),
1260 .of_match_table = of_match_ptr(raydium_of_match),
1263 module_i2c_driver(raydium_i2c_driver);
1265 MODULE_AUTHOR("Raydium");
1266 MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1267 MODULE_LICENSE("GPL v2");