148a2b783SJeffrey Lin /* 248a2b783SJeffrey Lin * Raydium touchscreen I2C driver. 348a2b783SJeffrey Lin * 448a2b783SJeffrey Lin * Copyright (C) 2012-2014, Raydium Semiconductor Corporation. 548a2b783SJeffrey Lin * 648a2b783SJeffrey Lin * This program is free software; you can redistribute it and/or 748a2b783SJeffrey Lin * modify it under the terms of the GNU General Public License 848a2b783SJeffrey Lin * version 2, and only version 2, as published by the 948a2b783SJeffrey Lin * Free Software Foundation. 1048a2b783SJeffrey Lin * 1148a2b783SJeffrey Lin * This program is distributed in the hope that it will be useful, 1248a2b783SJeffrey Lin * but WITHOUT ANY WARRANTY; without even the implied warranty of 1348a2b783SJeffrey Lin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1448a2b783SJeffrey Lin * GNU General Public License for more details. 1548a2b783SJeffrey Lin * 1648a2b783SJeffrey Lin * Raydium reserves the right to make changes without further notice 1748a2b783SJeffrey Lin * to the materials described herein. Raydium does not assume any 1848a2b783SJeffrey Lin * liability arising out of the application described herein. 1948a2b783SJeffrey Lin * 2048a2b783SJeffrey Lin * Contact Raydium Semiconductor Corporation at www.rad-ic.com 2148a2b783SJeffrey Lin */ 2248a2b783SJeffrey Lin 2348a2b783SJeffrey Lin #include <linux/acpi.h> 2448a2b783SJeffrey Lin #include <linux/delay.h> 2548a2b783SJeffrey Lin #include <linux/firmware.h> 2648a2b783SJeffrey Lin #include <linux/gpio/consumer.h> 2748a2b783SJeffrey Lin #include <linux/i2c.h> 2848a2b783SJeffrey Lin #include <linux/input.h> 2948a2b783SJeffrey Lin #include <linux/input/mt.h> 3048a2b783SJeffrey Lin #include <linux/interrupt.h> 3148a2b783SJeffrey Lin #include <linux/module.h> 3248a2b783SJeffrey Lin #include <linux/of.h> 3348a2b783SJeffrey Lin #include <linux/regulator/consumer.h> 3448a2b783SJeffrey Lin #include <linux/slab.h> 3548a2b783SJeffrey Lin #include <asm/unaligned.h> 3648a2b783SJeffrey Lin 3748a2b783SJeffrey Lin /* Slave I2C mode */ 3848a2b783SJeffrey Lin #define RM_BOOT_BLDR 0x02 3948a2b783SJeffrey Lin #define RM_BOOT_MAIN 0x03 4048a2b783SJeffrey Lin 4148a2b783SJeffrey Lin /* I2C bootoloader commands */ 4248a2b783SJeffrey Lin #define RM_CMD_BOOT_PAGE_WRT 0x0B /* send bl page write */ 4348a2b783SJeffrey Lin #define RM_CMD_BOOT_WRT 0x11 /* send bl write */ 4448a2b783SJeffrey Lin #define RM_CMD_BOOT_ACK 0x22 /* send ack*/ 4548a2b783SJeffrey Lin #define RM_CMD_BOOT_CHK 0x33 /* send data check */ 4648a2b783SJeffrey Lin #define RM_CMD_BOOT_READ 0x44 /* send wait bl data ready*/ 4748a2b783SJeffrey Lin 4848a2b783SJeffrey Lin #define RM_BOOT_RDY 0xFF /* bl data ready */ 4948a2b783SJeffrey Lin 5048a2b783SJeffrey Lin /* I2C main commands */ 5148a2b783SJeffrey Lin #define RM_CMD_QUERY_BANK 0x2B 5248a2b783SJeffrey Lin #define RM_CMD_DATA_BANK 0x4D 5348a2b783SJeffrey Lin #define RM_CMD_ENTER_SLEEP 0x4E 5448a2b783SJeffrey Lin #define RM_CMD_BANK_SWITCH 0xAA 5548a2b783SJeffrey Lin 5648a2b783SJeffrey Lin #define RM_RESET_MSG_ADDR 0x40000004 5748a2b783SJeffrey Lin 5848a2b783SJeffrey Lin #define RM_MAX_READ_SIZE 56 59e66475ebSJeffrey Lin #define RM_PACKET_CRC_SIZE 2 6048a2b783SJeffrey Lin 6148a2b783SJeffrey Lin /* Touch relative info */ 6248a2b783SJeffrey Lin #define RM_MAX_RETRIES 3 6348a2b783SJeffrey Lin #define RM_MAX_TOUCH_NUM 10 6448a2b783SJeffrey Lin #define RM_BOOT_DELAY_MS 100 6548a2b783SJeffrey Lin 6648a2b783SJeffrey Lin /* Offsets in contact data */ 6748a2b783SJeffrey Lin #define RM_CONTACT_STATE_POS 0 6848a2b783SJeffrey Lin #define RM_CONTACT_X_POS 1 6948a2b783SJeffrey Lin #define RM_CONTACT_Y_POS 3 7048a2b783SJeffrey Lin #define RM_CONTACT_PRESSURE_POS 5 7148a2b783SJeffrey Lin #define RM_CONTACT_WIDTH_X_POS 6 7248a2b783SJeffrey Lin #define RM_CONTACT_WIDTH_Y_POS 7 7348a2b783SJeffrey Lin 7448a2b783SJeffrey Lin /* Bootloader relative info */ 7548a2b783SJeffrey Lin #define RM_BL_WRT_CMD_SIZE 3 /* bl flash wrt cmd size */ 7648a2b783SJeffrey Lin #define RM_BL_WRT_PKG_SIZE 32 /* bl wrt pkg size */ 7748a2b783SJeffrey Lin #define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE) 7848a2b783SJeffrey Lin #define RM_FW_PAGE_SIZE 128 7948a2b783SJeffrey Lin #define RM_MAX_FW_RETRIES 30 8048a2b783SJeffrey Lin #define RM_MAX_FW_SIZE 0xD000 8148a2b783SJeffrey Lin 8248a2b783SJeffrey Lin #define RM_POWERON_DELAY_USEC 500 8348a2b783SJeffrey Lin #define RM_RESET_DELAY_MSEC 50 8448a2b783SJeffrey Lin 8548a2b783SJeffrey Lin enum raydium_bl_cmd { 8648a2b783SJeffrey Lin BL_HEADER = 0, 8748a2b783SJeffrey Lin BL_PAGE_STR, 8848a2b783SJeffrey Lin BL_PKG_IDX, 8948a2b783SJeffrey Lin BL_DATA_STR, 9048a2b783SJeffrey Lin }; 9148a2b783SJeffrey Lin 9248a2b783SJeffrey Lin enum raydium_bl_ack { 9348a2b783SJeffrey Lin RAYDIUM_ACK_NULL = 0, 9448a2b783SJeffrey Lin RAYDIUM_WAIT_READY, 9548a2b783SJeffrey Lin RAYDIUM_PATH_READY, 9648a2b783SJeffrey Lin }; 9748a2b783SJeffrey Lin 9848a2b783SJeffrey Lin enum raydium_boot_mode { 9948a2b783SJeffrey Lin RAYDIUM_TS_MAIN = 0, 10048a2b783SJeffrey Lin RAYDIUM_TS_BLDR, 10148a2b783SJeffrey Lin }; 10248a2b783SJeffrey Lin 10348a2b783SJeffrey Lin /* Response to RM_CMD_DATA_BANK request */ 10448a2b783SJeffrey Lin struct raydium_data_info { 10548a2b783SJeffrey Lin __le32 data_bank_addr; 10648a2b783SJeffrey Lin u8 pkg_size; 10748a2b783SJeffrey Lin u8 tp_info_size; 10848a2b783SJeffrey Lin }; 10948a2b783SJeffrey Lin 11048a2b783SJeffrey Lin struct raydium_info { 11148a2b783SJeffrey Lin __le32 hw_ver; /*device version */ 11248a2b783SJeffrey Lin u8 main_ver; 11348a2b783SJeffrey Lin u8 sub_ver; 11448a2b783SJeffrey Lin __le16 ft_ver; /* test version */ 11548a2b783SJeffrey Lin u8 x_num; 11648a2b783SJeffrey Lin u8 y_num; 11748a2b783SJeffrey Lin __le16 x_max; 11848a2b783SJeffrey Lin __le16 y_max; 11948a2b783SJeffrey Lin u8 x_res; /* units/mm */ 12048a2b783SJeffrey Lin u8 y_res; /* units/mm */ 12148a2b783SJeffrey Lin }; 12248a2b783SJeffrey Lin 12348a2b783SJeffrey Lin /* struct raydium_data - represents state of Raydium touchscreen device */ 12448a2b783SJeffrey Lin struct raydium_data { 12548a2b783SJeffrey Lin struct i2c_client *client; 12648a2b783SJeffrey Lin struct input_dev *input; 12748a2b783SJeffrey Lin 12848a2b783SJeffrey Lin struct regulator *avdd; 12948a2b783SJeffrey Lin struct regulator *vccio; 13048a2b783SJeffrey Lin struct gpio_desc *reset_gpio; 13148a2b783SJeffrey Lin 13248a2b783SJeffrey Lin struct raydium_info info; 13348a2b783SJeffrey Lin 13448a2b783SJeffrey Lin struct mutex sysfs_mutex; 13548a2b783SJeffrey Lin 13648a2b783SJeffrey Lin u8 *report_data; 13748a2b783SJeffrey Lin 13848a2b783SJeffrey Lin u32 data_bank_addr; 13948a2b783SJeffrey Lin u8 report_size; 14048a2b783SJeffrey Lin u8 contact_size; 141e66475ebSJeffrey Lin u8 pkg_size; 14248a2b783SJeffrey Lin 14348a2b783SJeffrey Lin enum raydium_boot_mode boot_mode; 14448a2b783SJeffrey Lin 14548a2b783SJeffrey Lin bool wake_irq_enabled; 14648a2b783SJeffrey Lin }; 14748a2b783SJeffrey Lin 14848a2b783SJeffrey Lin static int raydium_i2c_send(struct i2c_client *client, 14948a2b783SJeffrey Lin u8 addr, const void *data, size_t len) 15048a2b783SJeffrey Lin { 15148a2b783SJeffrey Lin u8 *buf; 15248a2b783SJeffrey Lin int tries = 0; 15348a2b783SJeffrey Lin int ret; 15448a2b783SJeffrey Lin 15548a2b783SJeffrey Lin buf = kmalloc(len + 1, GFP_KERNEL); 15648a2b783SJeffrey Lin if (!buf) 15748a2b783SJeffrey Lin return -ENOMEM; 15848a2b783SJeffrey Lin 15948a2b783SJeffrey Lin buf[0] = addr; 16048a2b783SJeffrey Lin memcpy(buf + 1, data, len); 16148a2b783SJeffrey Lin 16248a2b783SJeffrey Lin do { 16348a2b783SJeffrey Lin ret = i2c_master_send(client, buf, len + 1); 16448a2b783SJeffrey Lin if (likely(ret == len + 1)) 16548a2b783SJeffrey Lin break; 16648a2b783SJeffrey Lin 16748a2b783SJeffrey Lin msleep(20); 16848a2b783SJeffrey Lin } while (++tries < RM_MAX_RETRIES); 16948a2b783SJeffrey Lin 17048a2b783SJeffrey Lin kfree(buf); 17148a2b783SJeffrey Lin 17248a2b783SJeffrey Lin if (unlikely(ret != len + 1)) { 17348a2b783SJeffrey Lin if (ret >= 0) 17448a2b783SJeffrey Lin ret = -EIO; 17548a2b783SJeffrey Lin dev_err(&client->dev, "%s failed: %d\n", __func__, ret); 17648a2b783SJeffrey Lin return ret; 17748a2b783SJeffrey Lin } 17848a2b783SJeffrey Lin 17948a2b783SJeffrey Lin return 0; 18048a2b783SJeffrey Lin } 18148a2b783SJeffrey Lin 18248a2b783SJeffrey Lin static int raydium_i2c_read(struct i2c_client *client, 18348a2b783SJeffrey Lin u8 addr, void *data, size_t len) 18448a2b783SJeffrey Lin { 18548a2b783SJeffrey Lin struct i2c_msg xfer[] = { 18648a2b783SJeffrey Lin { 18748a2b783SJeffrey Lin .addr = client->addr, 18848a2b783SJeffrey Lin .len = 1, 18948a2b783SJeffrey Lin .buf = &addr, 19048a2b783SJeffrey Lin }, 19148a2b783SJeffrey Lin { 19248a2b783SJeffrey Lin .addr = client->addr, 19348a2b783SJeffrey Lin .flags = I2C_M_RD, 19448a2b783SJeffrey Lin .len = len, 19548a2b783SJeffrey Lin .buf = data, 19648a2b783SJeffrey Lin } 19748a2b783SJeffrey Lin }; 19848a2b783SJeffrey Lin int ret; 19948a2b783SJeffrey Lin 20048a2b783SJeffrey Lin ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer)); 20148a2b783SJeffrey Lin if (unlikely(ret != ARRAY_SIZE(xfer))) 20248a2b783SJeffrey Lin return ret < 0 ? ret : -EIO; 20348a2b783SJeffrey Lin 20448a2b783SJeffrey Lin return 0; 20548a2b783SJeffrey Lin } 20648a2b783SJeffrey Lin 20748a2b783SJeffrey Lin static int raydium_i2c_read_message(struct i2c_client *client, 20848a2b783SJeffrey Lin u32 addr, void *data, size_t len) 20948a2b783SJeffrey Lin { 21048a2b783SJeffrey Lin __be32 be_addr; 21148a2b783SJeffrey Lin size_t xfer_len; 21248a2b783SJeffrey Lin int error; 21348a2b783SJeffrey Lin 21448a2b783SJeffrey Lin while (len) { 21548a2b783SJeffrey Lin xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE); 21648a2b783SJeffrey Lin 21748a2b783SJeffrey Lin be_addr = cpu_to_be32(addr); 21848a2b783SJeffrey Lin 21948a2b783SJeffrey Lin error = raydium_i2c_send(client, RM_CMD_BANK_SWITCH, 22048a2b783SJeffrey Lin &be_addr, sizeof(be_addr)); 22148a2b783SJeffrey Lin if (!error) 22248a2b783SJeffrey Lin error = raydium_i2c_read(client, addr & 0xff, 22348a2b783SJeffrey Lin data, xfer_len); 22448a2b783SJeffrey Lin if (error) 22548a2b783SJeffrey Lin return error; 22648a2b783SJeffrey Lin 22748a2b783SJeffrey Lin len -= xfer_len; 22848a2b783SJeffrey Lin data += xfer_len; 22948a2b783SJeffrey Lin addr += xfer_len; 23048a2b783SJeffrey Lin } 23148a2b783SJeffrey Lin 23248a2b783SJeffrey Lin return 0; 23348a2b783SJeffrey Lin } 23448a2b783SJeffrey Lin 23548a2b783SJeffrey Lin static int raydium_i2c_send_message(struct i2c_client *client, 23648a2b783SJeffrey Lin u32 addr, const void *data, size_t len) 23748a2b783SJeffrey Lin { 23848a2b783SJeffrey Lin __be32 be_addr = cpu_to_be32(addr); 23948a2b783SJeffrey Lin int error; 24048a2b783SJeffrey Lin 24148a2b783SJeffrey Lin error = raydium_i2c_send(client, RM_CMD_BANK_SWITCH, 24248a2b783SJeffrey Lin &be_addr, sizeof(be_addr)); 24348a2b783SJeffrey Lin if (!error) 24448a2b783SJeffrey Lin error = raydium_i2c_send(client, addr & 0xff, data, len); 24548a2b783SJeffrey Lin 24648a2b783SJeffrey Lin return error; 24748a2b783SJeffrey Lin } 24848a2b783SJeffrey Lin 24948a2b783SJeffrey Lin static int raydium_i2c_sw_reset(struct i2c_client *client) 25048a2b783SJeffrey Lin { 25148a2b783SJeffrey Lin const u8 soft_rst_cmd = 0x01; 25248a2b783SJeffrey Lin int error; 25348a2b783SJeffrey Lin 25448a2b783SJeffrey Lin error = raydium_i2c_send_message(client, RM_RESET_MSG_ADDR, 25548a2b783SJeffrey Lin &soft_rst_cmd, sizeof(soft_rst_cmd)); 25648a2b783SJeffrey Lin if (error) { 25748a2b783SJeffrey Lin dev_err(&client->dev, "software reset failed: %d\n", error); 25848a2b783SJeffrey Lin return error; 25948a2b783SJeffrey Lin } 26048a2b783SJeffrey Lin 26148a2b783SJeffrey Lin msleep(RM_RESET_DELAY_MSEC); 26248a2b783SJeffrey Lin 26348a2b783SJeffrey Lin return 0; 26448a2b783SJeffrey Lin } 26548a2b783SJeffrey Lin 26648a2b783SJeffrey Lin static int raydium_i2c_query_ts_info(struct raydium_data *ts) 26748a2b783SJeffrey Lin { 26848a2b783SJeffrey Lin struct i2c_client *client = ts->client; 26948a2b783SJeffrey Lin struct raydium_data_info data_info; 27048a2b783SJeffrey Lin __le32 query_bank_addr; 27148a2b783SJeffrey Lin 27248a2b783SJeffrey Lin int error, retry_cnt; 27348a2b783SJeffrey Lin 27448a2b783SJeffrey Lin for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) { 27548a2b783SJeffrey Lin error = raydium_i2c_read(client, RM_CMD_DATA_BANK, 27648a2b783SJeffrey Lin &data_info, sizeof(data_info)); 27748a2b783SJeffrey Lin if (error) 27848a2b783SJeffrey Lin continue; 27948a2b783SJeffrey Lin 28048a2b783SJeffrey Lin /* 28148a2b783SJeffrey Lin * Warn user if we already allocated memory for reports and 28248a2b783SJeffrey Lin * then the size changed (due to firmware update?) and keep 28348a2b783SJeffrey Lin * old size instead. 28448a2b783SJeffrey Lin */ 285e66475ebSJeffrey Lin if (ts->report_data && ts->pkg_size != data_info.pkg_size) { 28648a2b783SJeffrey Lin dev_warn(&client->dev, 28748a2b783SJeffrey Lin "report size changes, was: %d, new: %d\n", 288e66475ebSJeffrey Lin ts->pkg_size, data_info.pkg_size); 289e66475ebSJeffrey Lin } else { 290e66475ebSJeffrey Lin ts->pkg_size = data_info.pkg_size; 291e66475ebSJeffrey Lin ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE; 292e66475ebSJeffrey Lin } 29348a2b783SJeffrey Lin 29448a2b783SJeffrey Lin ts->contact_size = data_info.tp_info_size; 29548a2b783SJeffrey Lin ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr); 29648a2b783SJeffrey Lin 29748a2b783SJeffrey Lin dev_dbg(&client->dev, 29848a2b783SJeffrey Lin "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n", 29948a2b783SJeffrey Lin ts->data_bank_addr, ts->report_size, ts->contact_size); 30048a2b783SJeffrey Lin 30148a2b783SJeffrey Lin error = raydium_i2c_read(client, RM_CMD_QUERY_BANK, 30248a2b783SJeffrey Lin &query_bank_addr, 30348a2b783SJeffrey Lin sizeof(query_bank_addr)); 30448a2b783SJeffrey Lin if (error) 30548a2b783SJeffrey Lin continue; 30648a2b783SJeffrey Lin 30748a2b783SJeffrey Lin error = raydium_i2c_read_message(client, 30848a2b783SJeffrey Lin le32_to_cpu(query_bank_addr), 30948a2b783SJeffrey Lin &ts->info, sizeof(ts->info)); 31048a2b783SJeffrey Lin if (error) 31148a2b783SJeffrey Lin continue; 31248a2b783SJeffrey Lin 31348a2b783SJeffrey Lin return 0; 31448a2b783SJeffrey Lin } 31548a2b783SJeffrey Lin 31648a2b783SJeffrey Lin dev_err(&client->dev, "failed to query device parameters: %d\n", error); 31748a2b783SJeffrey Lin return error; 31848a2b783SJeffrey Lin } 31948a2b783SJeffrey Lin 32048a2b783SJeffrey Lin static int raydium_i2c_check_fw_status(struct raydium_data *ts) 32148a2b783SJeffrey Lin { 32248a2b783SJeffrey Lin struct i2c_client *client = ts->client; 32348a2b783SJeffrey Lin static const u8 bl_ack = 0x62; 32448a2b783SJeffrey Lin static const u8 main_ack = 0x66; 32548a2b783SJeffrey Lin u8 buf[4]; 32648a2b783SJeffrey Lin int error; 32748a2b783SJeffrey Lin 32848a2b783SJeffrey Lin error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf)); 32948a2b783SJeffrey Lin if (!error) { 33048a2b783SJeffrey Lin if (buf[0] == bl_ack) 33148a2b783SJeffrey Lin ts->boot_mode = RAYDIUM_TS_BLDR; 33248a2b783SJeffrey Lin else if (buf[0] == main_ack) 33348a2b783SJeffrey Lin ts->boot_mode = RAYDIUM_TS_MAIN; 33448a2b783SJeffrey Lin return 0; 33548a2b783SJeffrey Lin } 33648a2b783SJeffrey Lin 33748a2b783SJeffrey Lin return error; 33848a2b783SJeffrey Lin } 33948a2b783SJeffrey Lin 34048a2b783SJeffrey Lin static int raydium_i2c_initialize(struct raydium_data *ts) 34148a2b783SJeffrey Lin { 34248a2b783SJeffrey Lin struct i2c_client *client = ts->client; 34348a2b783SJeffrey Lin int error, retry_cnt; 34448a2b783SJeffrey Lin 34548a2b783SJeffrey Lin for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) { 34648a2b783SJeffrey Lin /* Wait for Hello packet */ 34748a2b783SJeffrey Lin msleep(RM_BOOT_DELAY_MS); 34848a2b783SJeffrey Lin 34948a2b783SJeffrey Lin error = raydium_i2c_check_fw_status(ts); 35048a2b783SJeffrey Lin if (error) { 35148a2b783SJeffrey Lin dev_err(&client->dev, 35248a2b783SJeffrey Lin "failed to read 'hello' packet: %d\n", error); 35348a2b783SJeffrey Lin continue; 35448a2b783SJeffrey Lin } 35548a2b783SJeffrey Lin 35648a2b783SJeffrey Lin if (ts->boot_mode == RAYDIUM_TS_BLDR || 35748a2b783SJeffrey Lin ts->boot_mode == RAYDIUM_TS_MAIN) { 35848a2b783SJeffrey Lin break; 35948a2b783SJeffrey Lin } 36048a2b783SJeffrey Lin } 36148a2b783SJeffrey Lin 36248a2b783SJeffrey Lin if (error) 36348a2b783SJeffrey Lin ts->boot_mode = RAYDIUM_TS_BLDR; 36448a2b783SJeffrey Lin 36548a2b783SJeffrey Lin if (ts->boot_mode == RAYDIUM_TS_BLDR) { 36648a2b783SJeffrey Lin ts->info.hw_ver = cpu_to_le32(0xffffffffUL); 36748a2b783SJeffrey Lin ts->info.main_ver = 0xff; 36848a2b783SJeffrey Lin ts->info.sub_ver = 0xff; 36948a2b783SJeffrey Lin } else { 37048a2b783SJeffrey Lin raydium_i2c_query_ts_info(ts); 37148a2b783SJeffrey Lin } 37248a2b783SJeffrey Lin 37348a2b783SJeffrey Lin return error; 37448a2b783SJeffrey Lin } 37548a2b783SJeffrey Lin 37648a2b783SJeffrey Lin static int raydium_i2c_bl_chk_state(struct i2c_client *client, 37748a2b783SJeffrey Lin enum raydium_bl_ack state) 37848a2b783SJeffrey Lin { 37948a2b783SJeffrey Lin static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 }; 38048a2b783SJeffrey Lin u8 rbuf[sizeof(ack_ok)]; 38148a2b783SJeffrey Lin u8 retry; 38248a2b783SJeffrey Lin int error; 38348a2b783SJeffrey Lin 38448a2b783SJeffrey Lin for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) { 38548a2b783SJeffrey Lin switch (state) { 38648a2b783SJeffrey Lin case RAYDIUM_ACK_NULL: 38748a2b783SJeffrey Lin return 0; 38848a2b783SJeffrey Lin 38948a2b783SJeffrey Lin case RAYDIUM_WAIT_READY: 39048a2b783SJeffrey Lin error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, 39148a2b783SJeffrey Lin &rbuf[0], 1); 39248a2b783SJeffrey Lin if (!error && rbuf[0] == RM_BOOT_RDY) 39348a2b783SJeffrey Lin return 0; 39448a2b783SJeffrey Lin 39548a2b783SJeffrey Lin break; 39648a2b783SJeffrey Lin 39748a2b783SJeffrey Lin case RAYDIUM_PATH_READY: 39848a2b783SJeffrey Lin error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, 39948a2b783SJeffrey Lin rbuf, sizeof(rbuf)); 40048a2b783SJeffrey Lin if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok))) 40148a2b783SJeffrey Lin return 0; 40248a2b783SJeffrey Lin 40348a2b783SJeffrey Lin break; 40448a2b783SJeffrey Lin 40548a2b783SJeffrey Lin default: 40648a2b783SJeffrey Lin dev_err(&client->dev, "%s: invalid target state %d\n", 40748a2b783SJeffrey Lin __func__, state); 40848a2b783SJeffrey Lin return -EINVAL; 40948a2b783SJeffrey Lin } 41048a2b783SJeffrey Lin 41148a2b783SJeffrey Lin msleep(20); 41248a2b783SJeffrey Lin } 41348a2b783SJeffrey Lin 41448a2b783SJeffrey Lin return -ETIMEDOUT; 41548a2b783SJeffrey Lin } 41648a2b783SJeffrey Lin 41748a2b783SJeffrey Lin static int raydium_i2c_write_object(struct i2c_client *client, 41848a2b783SJeffrey Lin const void *data, size_t len, 41948a2b783SJeffrey Lin enum raydium_bl_ack state) 42048a2b783SJeffrey Lin { 42148a2b783SJeffrey Lin int error; 42248a2b783SJeffrey Lin 42348a2b783SJeffrey Lin error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len); 42448a2b783SJeffrey Lin if (error) { 42548a2b783SJeffrey Lin dev_err(&client->dev, "WRT obj command failed: %d\n", 42648a2b783SJeffrey Lin error); 42748a2b783SJeffrey Lin return error; 42848a2b783SJeffrey Lin } 42948a2b783SJeffrey Lin 43048a2b783SJeffrey Lin error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, NULL, 0); 43148a2b783SJeffrey Lin if (error) { 43248a2b783SJeffrey Lin dev_err(&client->dev, "Ack obj command failed: %d\n", error); 43348a2b783SJeffrey Lin return error; 43448a2b783SJeffrey Lin } 43548a2b783SJeffrey Lin 43648a2b783SJeffrey Lin error = raydium_i2c_bl_chk_state(client, state); 43748a2b783SJeffrey Lin if (error) { 43848a2b783SJeffrey Lin dev_err(&client->dev, "BL check state failed: %d\n", error); 43948a2b783SJeffrey Lin return error; 44048a2b783SJeffrey Lin } 44148a2b783SJeffrey Lin return 0; 44248a2b783SJeffrey Lin } 44348a2b783SJeffrey Lin 44448a2b783SJeffrey Lin static bool raydium_i2c_boot_trigger(struct i2c_client *client) 44548a2b783SJeffrey Lin { 44648a2b783SJeffrey Lin static const u8 cmd[7][6] = { 44748a2b783SJeffrey Lin { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 }, 44848a2b783SJeffrey Lin { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 }, 44948a2b783SJeffrey Lin { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 }, 45048a2b783SJeffrey Lin { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 }, 45148a2b783SJeffrey Lin { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 }, 45248a2b783SJeffrey Lin { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 }, 45348a2b783SJeffrey Lin { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 }, 45448a2b783SJeffrey Lin }; 45548a2b783SJeffrey Lin int i; 45648a2b783SJeffrey Lin int error; 45748a2b783SJeffrey Lin 45848a2b783SJeffrey Lin for (i = 0; i < 7; i++) { 45948a2b783SJeffrey Lin error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]), 46048a2b783SJeffrey Lin RAYDIUM_WAIT_READY); 46148a2b783SJeffrey Lin if (error) { 46248a2b783SJeffrey Lin dev_err(&client->dev, 46348a2b783SJeffrey Lin "boot trigger failed at step %d: %d\n", 46448a2b783SJeffrey Lin i, error); 46548a2b783SJeffrey Lin return error; 46648a2b783SJeffrey Lin } 46748a2b783SJeffrey Lin } 46848a2b783SJeffrey Lin 46948a2b783SJeffrey Lin return 0; 47048a2b783SJeffrey Lin } 47148a2b783SJeffrey Lin 47248a2b783SJeffrey Lin static bool raydium_i2c_fw_trigger(struct i2c_client *client) 47348a2b783SJeffrey Lin { 47448a2b783SJeffrey Lin static const u8 cmd[5][11] = { 47548a2b783SJeffrey Lin { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 }, 47648a2b783SJeffrey Lin { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 }, 47748a2b783SJeffrey Lin { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 }, 47848a2b783SJeffrey Lin { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 }, 47948a2b783SJeffrey Lin { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 }, 48048a2b783SJeffrey Lin }; 48148a2b783SJeffrey Lin int i; 48248a2b783SJeffrey Lin int error; 48348a2b783SJeffrey Lin 48448a2b783SJeffrey Lin for (i = 0; i < 5; i++) { 48548a2b783SJeffrey Lin error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]), 48648a2b783SJeffrey Lin RAYDIUM_ACK_NULL); 48748a2b783SJeffrey Lin if (error) { 48848a2b783SJeffrey Lin dev_err(&client->dev, 48948a2b783SJeffrey Lin "fw trigger failed at step %d: %d\n", 49048a2b783SJeffrey Lin i, error); 49148a2b783SJeffrey Lin return error; 49248a2b783SJeffrey Lin } 49348a2b783SJeffrey Lin } 49448a2b783SJeffrey Lin 49548a2b783SJeffrey Lin return 0; 49648a2b783SJeffrey Lin } 49748a2b783SJeffrey Lin 49848a2b783SJeffrey Lin static int raydium_i2c_check_path(struct i2c_client *client) 49948a2b783SJeffrey Lin { 50048a2b783SJeffrey Lin static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 }; 50148a2b783SJeffrey Lin int error; 50248a2b783SJeffrey Lin 50348a2b783SJeffrey Lin error = raydium_i2c_write_object(client, cmd, sizeof(cmd), 50448a2b783SJeffrey Lin RAYDIUM_PATH_READY); 50548a2b783SJeffrey Lin if (error) { 50648a2b783SJeffrey Lin dev_err(&client->dev, "check path command failed: %d\n", error); 50748a2b783SJeffrey Lin return error; 50848a2b783SJeffrey Lin } 50948a2b783SJeffrey Lin 51048a2b783SJeffrey Lin return 0; 51148a2b783SJeffrey Lin } 51248a2b783SJeffrey Lin 51348a2b783SJeffrey Lin static int raydium_i2c_enter_bl(struct i2c_client *client) 51448a2b783SJeffrey Lin { 51548a2b783SJeffrey Lin static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 }; 51648a2b783SJeffrey Lin int error; 51748a2b783SJeffrey Lin 51848a2b783SJeffrey Lin error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd), 51948a2b783SJeffrey Lin RAYDIUM_ACK_NULL); 52048a2b783SJeffrey Lin if (error) { 52148a2b783SJeffrey Lin dev_err(&client->dev, "enter bl command failed: %d\n", error); 52248a2b783SJeffrey Lin return error; 52348a2b783SJeffrey Lin } 52448a2b783SJeffrey Lin 52548a2b783SJeffrey Lin msleep(RM_BOOT_DELAY_MS); 52648a2b783SJeffrey Lin return 0; 52748a2b783SJeffrey Lin } 52848a2b783SJeffrey Lin 52948a2b783SJeffrey Lin static int raydium_i2c_leave_bl(struct i2c_client *client) 53048a2b783SJeffrey Lin { 53148a2b783SJeffrey Lin static const u8 leave_cmd[] = { 0x05, 0x00 }; 53248a2b783SJeffrey Lin int error; 53348a2b783SJeffrey Lin 53448a2b783SJeffrey Lin error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd), 53548a2b783SJeffrey Lin RAYDIUM_ACK_NULL); 53648a2b783SJeffrey Lin if (error) { 53748a2b783SJeffrey Lin dev_err(&client->dev, "leave bl command failed: %d\n", error); 53848a2b783SJeffrey Lin return error; 53948a2b783SJeffrey Lin } 54048a2b783SJeffrey Lin 54148a2b783SJeffrey Lin msleep(RM_BOOT_DELAY_MS); 54248a2b783SJeffrey Lin return 0; 54348a2b783SJeffrey Lin } 54448a2b783SJeffrey Lin 54548a2b783SJeffrey Lin static int raydium_i2c_write_checksum(struct i2c_client *client, 54648a2b783SJeffrey Lin size_t length, u16 checksum) 54748a2b783SJeffrey Lin { 54848a2b783SJeffrey Lin u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 }; 54948a2b783SJeffrey Lin int error; 55048a2b783SJeffrey Lin 55148a2b783SJeffrey Lin put_unaligned_le16(length, &checksum_cmd[3]); 55248a2b783SJeffrey Lin put_unaligned_le16(checksum, &checksum_cmd[5]); 55348a2b783SJeffrey Lin 55448a2b783SJeffrey Lin error = raydium_i2c_write_object(client, 55548a2b783SJeffrey Lin checksum_cmd, sizeof(checksum_cmd), 55648a2b783SJeffrey Lin RAYDIUM_ACK_NULL); 55748a2b783SJeffrey Lin if (error) { 55848a2b783SJeffrey Lin dev_err(&client->dev, "failed to write checksum: %d\n", 55948a2b783SJeffrey Lin error); 56048a2b783SJeffrey Lin return error; 56148a2b783SJeffrey Lin } 56248a2b783SJeffrey Lin 56348a2b783SJeffrey Lin return 0; 56448a2b783SJeffrey Lin } 56548a2b783SJeffrey Lin 56648a2b783SJeffrey Lin static int raydium_i2c_disable_watch_dog(struct i2c_client *client) 56748a2b783SJeffrey Lin { 56848a2b783SJeffrey Lin static const u8 cmd[] = { 0x0A, 0xAA }; 56948a2b783SJeffrey Lin int error; 57048a2b783SJeffrey Lin 57148a2b783SJeffrey Lin error = raydium_i2c_write_object(client, cmd, sizeof(cmd), 57248a2b783SJeffrey Lin RAYDIUM_WAIT_READY); 57348a2b783SJeffrey Lin if (error) { 57448a2b783SJeffrey Lin dev_err(&client->dev, "disable watchdog command failed: %d\n", 57548a2b783SJeffrey Lin error); 57648a2b783SJeffrey Lin return error; 57748a2b783SJeffrey Lin } 57848a2b783SJeffrey Lin 57948a2b783SJeffrey Lin return 0; 58048a2b783SJeffrey Lin } 58148a2b783SJeffrey Lin 58248a2b783SJeffrey Lin static int raydium_i2c_fw_write_page(struct i2c_client *client, 58348a2b783SJeffrey Lin u16 page_idx, const void *data, size_t len) 58448a2b783SJeffrey Lin { 58548a2b783SJeffrey Lin u8 buf[RM_BL_WRT_LEN]; 58648a2b783SJeffrey Lin size_t xfer_len; 58748a2b783SJeffrey Lin int error; 58848a2b783SJeffrey Lin int i; 58948a2b783SJeffrey Lin 59048a2b783SJeffrey Lin BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0); 59148a2b783SJeffrey Lin 59248a2b783SJeffrey Lin for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) { 59348a2b783SJeffrey Lin buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT; 59448a2b783SJeffrey Lin buf[BL_PAGE_STR] = page_idx ? 0xff : 0; 59548a2b783SJeffrey Lin buf[BL_PKG_IDX] = i + 1; 59648a2b783SJeffrey Lin 59748a2b783SJeffrey Lin xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE); 59848a2b783SJeffrey Lin memcpy(&buf[BL_DATA_STR], data, xfer_len); 59948a2b783SJeffrey Lin if (len < RM_BL_WRT_PKG_SIZE) 60048a2b783SJeffrey Lin memset(&buf[BL_DATA_STR + xfer_len], 0xff, 60148a2b783SJeffrey Lin RM_BL_WRT_PKG_SIZE - xfer_len); 60248a2b783SJeffrey Lin 60348a2b783SJeffrey Lin error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN, 60448a2b783SJeffrey Lin RAYDIUM_WAIT_READY); 60548a2b783SJeffrey Lin if (error) { 60648a2b783SJeffrey Lin dev_err(&client->dev, 60748a2b783SJeffrey Lin "page write command failed for page %d, chunk %d: %d\n", 60848a2b783SJeffrey Lin page_idx, i, error); 60948a2b783SJeffrey Lin return error; 61048a2b783SJeffrey Lin } 61148a2b783SJeffrey Lin 61248a2b783SJeffrey Lin data += xfer_len; 61348a2b783SJeffrey Lin len -= xfer_len; 61448a2b783SJeffrey Lin } 61548a2b783SJeffrey Lin 61648a2b783SJeffrey Lin return error; 61748a2b783SJeffrey Lin } 61848a2b783SJeffrey Lin 619e66475ebSJeffrey Lin static u16 raydium_calc_chksum(const u8 *buf, u16 len) 620e66475ebSJeffrey Lin { 621e66475ebSJeffrey Lin u16 checksum = 0; 622e66475ebSJeffrey Lin u16 i; 623e66475ebSJeffrey Lin 624e66475ebSJeffrey Lin for (i = 0; i < len; i++) 625e66475ebSJeffrey Lin checksum += buf[i]; 626e66475ebSJeffrey Lin 627e66475ebSJeffrey Lin return checksum; 628e66475ebSJeffrey Lin } 629e66475ebSJeffrey Lin 63048a2b783SJeffrey Lin static int raydium_i2c_do_update_firmware(struct raydium_data *ts, 63148a2b783SJeffrey Lin const struct firmware *fw) 63248a2b783SJeffrey Lin { 63348a2b783SJeffrey Lin struct i2c_client *client = ts->client; 63448a2b783SJeffrey Lin const void *data; 63548a2b783SJeffrey Lin size_t data_len; 63648a2b783SJeffrey Lin size_t len; 63748a2b783SJeffrey Lin int page_nr; 63848a2b783SJeffrey Lin int i; 63948a2b783SJeffrey Lin int error; 64048a2b783SJeffrey Lin u16 fw_checksum; 64148a2b783SJeffrey Lin 64248a2b783SJeffrey Lin if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) { 64348a2b783SJeffrey Lin dev_err(&client->dev, "Invalid firmware length\n"); 64448a2b783SJeffrey Lin return -EINVAL; 64548a2b783SJeffrey Lin } 64648a2b783SJeffrey Lin 64748a2b783SJeffrey Lin error = raydium_i2c_check_fw_status(ts); 64848a2b783SJeffrey Lin if (error) { 64948a2b783SJeffrey Lin dev_err(&client->dev, "Unable to access IC %d\n", error); 65048a2b783SJeffrey Lin return error; 65148a2b783SJeffrey Lin } 65248a2b783SJeffrey Lin 65348a2b783SJeffrey Lin if (ts->boot_mode == RAYDIUM_TS_MAIN) { 65448a2b783SJeffrey Lin for (i = 0; i < RM_MAX_RETRIES; i++) { 65548a2b783SJeffrey Lin error = raydium_i2c_enter_bl(client); 65648a2b783SJeffrey Lin if (!error) { 65748a2b783SJeffrey Lin error = raydium_i2c_check_fw_status(ts); 65848a2b783SJeffrey Lin if (error) { 65948a2b783SJeffrey Lin dev_err(&client->dev, 66048a2b783SJeffrey Lin "unable to access IC: %d\n", 66148a2b783SJeffrey Lin error); 66248a2b783SJeffrey Lin return error; 66348a2b783SJeffrey Lin } 66448a2b783SJeffrey Lin 66548a2b783SJeffrey Lin if (ts->boot_mode == RAYDIUM_TS_BLDR) 66648a2b783SJeffrey Lin break; 66748a2b783SJeffrey Lin } 66848a2b783SJeffrey Lin } 66948a2b783SJeffrey Lin 67048a2b783SJeffrey Lin if (ts->boot_mode == RAYDIUM_TS_MAIN) { 67148a2b783SJeffrey Lin dev_err(&client->dev, 672fac96cc9SColin Ian King "failed to jump to boot loader: %d\n", 67348a2b783SJeffrey Lin error); 67448a2b783SJeffrey Lin return -EIO; 67548a2b783SJeffrey Lin } 67648a2b783SJeffrey Lin } 67748a2b783SJeffrey Lin 67848a2b783SJeffrey Lin error = raydium_i2c_disable_watch_dog(client); 67948a2b783SJeffrey Lin if (error) 68048a2b783SJeffrey Lin return error; 68148a2b783SJeffrey Lin 68248a2b783SJeffrey Lin error = raydium_i2c_check_path(client); 68348a2b783SJeffrey Lin if (error) 68448a2b783SJeffrey Lin return error; 68548a2b783SJeffrey Lin 68648a2b783SJeffrey Lin error = raydium_i2c_boot_trigger(client); 68748a2b783SJeffrey Lin if (error) { 68848a2b783SJeffrey Lin dev_err(&client->dev, "send boot trigger fail: %d\n", error); 68948a2b783SJeffrey Lin return error; 69048a2b783SJeffrey Lin } 69148a2b783SJeffrey Lin 69248a2b783SJeffrey Lin msleep(RM_BOOT_DELAY_MS); 69348a2b783SJeffrey Lin 69448a2b783SJeffrey Lin data = fw->data; 69548a2b783SJeffrey Lin data_len = fw->size; 69648a2b783SJeffrey Lin page_nr = 0; 69748a2b783SJeffrey Lin 69848a2b783SJeffrey Lin while (data_len) { 69948a2b783SJeffrey Lin len = min_t(size_t, data_len, RM_FW_PAGE_SIZE); 70048a2b783SJeffrey Lin 70148a2b783SJeffrey Lin error = raydium_i2c_fw_write_page(client, page_nr++, data, len); 70248a2b783SJeffrey Lin if (error) 70348a2b783SJeffrey Lin return error; 70448a2b783SJeffrey Lin 70548a2b783SJeffrey Lin msleep(20); 70648a2b783SJeffrey Lin 70748a2b783SJeffrey Lin data += len; 70848a2b783SJeffrey Lin data_len -= len; 70948a2b783SJeffrey Lin } 71048a2b783SJeffrey Lin 71148a2b783SJeffrey Lin error = raydium_i2c_leave_bl(client); 71248a2b783SJeffrey Lin if (error) { 71348a2b783SJeffrey Lin dev_err(&client->dev, 71448a2b783SJeffrey Lin "failed to leave boot loader: %d\n", error); 71548a2b783SJeffrey Lin return error; 71648a2b783SJeffrey Lin } 71748a2b783SJeffrey Lin 71848a2b783SJeffrey Lin dev_dbg(&client->dev, "left boot loader mode\n"); 71948a2b783SJeffrey Lin msleep(RM_BOOT_DELAY_MS); 72048a2b783SJeffrey Lin 72148a2b783SJeffrey Lin error = raydium_i2c_check_fw_status(ts); 72248a2b783SJeffrey Lin if (error) { 72348a2b783SJeffrey Lin dev_err(&client->dev, 72448a2b783SJeffrey Lin "failed to check fw status after write: %d\n", 72548a2b783SJeffrey Lin error); 72648a2b783SJeffrey Lin return error; 72748a2b783SJeffrey Lin } 72848a2b783SJeffrey Lin 72948a2b783SJeffrey Lin if (ts->boot_mode != RAYDIUM_TS_MAIN) { 73048a2b783SJeffrey Lin dev_err(&client->dev, 73148a2b783SJeffrey Lin "failed to switch to main fw after writing firmware: %d\n", 73248a2b783SJeffrey Lin error); 73348a2b783SJeffrey Lin return -EINVAL; 73448a2b783SJeffrey Lin } 73548a2b783SJeffrey Lin 73648a2b783SJeffrey Lin error = raydium_i2c_fw_trigger(client); 73748a2b783SJeffrey Lin if (error) { 73848a2b783SJeffrey Lin dev_err(&client->dev, "failed to trigger fw: %d\n", error); 73948a2b783SJeffrey Lin return error; 74048a2b783SJeffrey Lin } 74148a2b783SJeffrey Lin 742e66475ebSJeffrey Lin fw_checksum = raydium_calc_chksum(fw->data, fw->size); 74348a2b783SJeffrey Lin 74448a2b783SJeffrey Lin error = raydium_i2c_write_checksum(client, fw->size, fw_checksum); 74548a2b783SJeffrey Lin if (error) 74648a2b783SJeffrey Lin return error; 74748a2b783SJeffrey Lin 74848a2b783SJeffrey Lin return 0; 74948a2b783SJeffrey Lin } 75048a2b783SJeffrey Lin 75148a2b783SJeffrey Lin static int raydium_i2c_fw_update(struct raydium_data *ts) 75248a2b783SJeffrey Lin { 75348a2b783SJeffrey Lin struct i2c_client *client = ts->client; 75448a2b783SJeffrey Lin const struct firmware *fw = NULL; 755*92a37438SJeffrey Lin char *fw_file; 75648a2b783SJeffrey Lin int error; 75748a2b783SJeffrey Lin 758*92a37438SJeffrey Lin fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw", 759*92a37438SJeffrey Lin le32_to_cpu(ts->info.hw_ver)); 760*92a37438SJeffrey Lin if (!fw_file) 761*92a37438SJeffrey Lin return -ENOMEM; 762*92a37438SJeffrey Lin 763*92a37438SJeffrey Lin dev_dbg(&client->dev, "firmware name: %s\n", fw_file); 764*92a37438SJeffrey Lin 76548a2b783SJeffrey Lin error = request_firmware(&fw, fw_file, &client->dev); 76648a2b783SJeffrey Lin if (error) { 76748a2b783SJeffrey Lin dev_err(&client->dev, "Unable to open firmware %s\n", fw_file); 768*92a37438SJeffrey Lin goto out_free_fw_file; 76948a2b783SJeffrey Lin } 77048a2b783SJeffrey Lin 77148a2b783SJeffrey Lin disable_irq(client->irq); 77248a2b783SJeffrey Lin 77348a2b783SJeffrey Lin error = raydium_i2c_do_update_firmware(ts, fw); 77448a2b783SJeffrey Lin if (error) { 77548a2b783SJeffrey Lin dev_err(&client->dev, "firmware update failed: %d\n", error); 77648a2b783SJeffrey Lin ts->boot_mode = RAYDIUM_TS_BLDR; 77748a2b783SJeffrey Lin goto out_enable_irq; 77848a2b783SJeffrey Lin } 77948a2b783SJeffrey Lin 78048a2b783SJeffrey Lin error = raydium_i2c_initialize(ts); 78148a2b783SJeffrey Lin if (error) { 78248a2b783SJeffrey Lin dev_err(&client->dev, 78348a2b783SJeffrey Lin "failed to initialize device after firmware update: %d\n", 78448a2b783SJeffrey Lin error); 78548a2b783SJeffrey Lin ts->boot_mode = RAYDIUM_TS_BLDR; 78648a2b783SJeffrey Lin goto out_enable_irq; 78748a2b783SJeffrey Lin } 78848a2b783SJeffrey Lin 78948a2b783SJeffrey Lin ts->boot_mode = RAYDIUM_TS_MAIN; 79048a2b783SJeffrey Lin 79148a2b783SJeffrey Lin out_enable_irq: 79248a2b783SJeffrey Lin enable_irq(client->irq); 79348a2b783SJeffrey Lin msleep(100); 79448a2b783SJeffrey Lin 79548a2b783SJeffrey Lin release_firmware(fw); 79648a2b783SJeffrey Lin 797*92a37438SJeffrey Lin out_free_fw_file: 798*92a37438SJeffrey Lin kfree(fw_file); 799*92a37438SJeffrey Lin 80048a2b783SJeffrey Lin return error; 80148a2b783SJeffrey Lin } 80248a2b783SJeffrey Lin 80348a2b783SJeffrey Lin static void raydium_mt_event(struct raydium_data *ts) 80448a2b783SJeffrey Lin { 80548a2b783SJeffrey Lin int i; 80648a2b783SJeffrey Lin 80748a2b783SJeffrey Lin for (i = 0; i < ts->report_size / ts->contact_size; i++) { 80848a2b783SJeffrey Lin u8 *contact = &ts->report_data[ts->contact_size * i]; 80948a2b783SJeffrey Lin bool state = contact[RM_CONTACT_STATE_POS]; 81048a2b783SJeffrey Lin u8 wx, wy; 81148a2b783SJeffrey Lin 81248a2b783SJeffrey Lin input_mt_slot(ts->input, i); 81348a2b783SJeffrey Lin input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state); 81448a2b783SJeffrey Lin 81548a2b783SJeffrey Lin if (!state) 81648a2b783SJeffrey Lin continue; 81748a2b783SJeffrey Lin 81848a2b783SJeffrey Lin input_report_abs(ts->input, ABS_MT_POSITION_X, 81948a2b783SJeffrey Lin get_unaligned_le16(&contact[RM_CONTACT_X_POS])); 82048a2b783SJeffrey Lin input_report_abs(ts->input, ABS_MT_POSITION_Y, 82148a2b783SJeffrey Lin get_unaligned_le16(&contact[RM_CONTACT_Y_POS])); 82248a2b783SJeffrey Lin input_report_abs(ts->input, ABS_MT_PRESSURE, 82348a2b783SJeffrey Lin contact[RM_CONTACT_PRESSURE_POS]); 82448a2b783SJeffrey Lin 82548a2b783SJeffrey Lin wx = contact[RM_CONTACT_WIDTH_X_POS]; 82648a2b783SJeffrey Lin wy = contact[RM_CONTACT_WIDTH_Y_POS]; 82748a2b783SJeffrey Lin 82848a2b783SJeffrey Lin input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy)); 82948a2b783SJeffrey Lin input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy)); 83048a2b783SJeffrey Lin } 83148a2b783SJeffrey Lin 83248a2b783SJeffrey Lin input_mt_sync_frame(ts->input); 83348a2b783SJeffrey Lin input_sync(ts->input); 83448a2b783SJeffrey Lin } 83548a2b783SJeffrey Lin 83648a2b783SJeffrey Lin static irqreturn_t raydium_i2c_irq(int irq, void *_dev) 83748a2b783SJeffrey Lin { 83848a2b783SJeffrey Lin struct raydium_data *ts = _dev; 839e66475ebSJeffrey Lin int error; 840e66475ebSJeffrey Lin u16 fw_crc; 841e66475ebSJeffrey Lin u16 calc_crc; 84248a2b783SJeffrey Lin 843e66475ebSJeffrey Lin if (ts->boot_mode != RAYDIUM_TS_MAIN) 844e66475ebSJeffrey Lin goto out; 845e66475ebSJeffrey Lin 846e66475ebSJeffrey Lin error = raydium_i2c_read_message(ts->client, ts->data_bank_addr, 847e66475ebSJeffrey Lin ts->report_data, ts->pkg_size); 848e66475ebSJeffrey Lin if (error) 849e66475ebSJeffrey Lin goto out; 850e66475ebSJeffrey Lin 851e66475ebSJeffrey Lin fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]); 852e66475ebSJeffrey Lin calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size); 853e66475ebSJeffrey Lin if (unlikely(fw_crc != calc_crc)) { 854e66475ebSJeffrey Lin dev_warn(&ts->client->dev, 855e66475ebSJeffrey Lin "%s: invalid packet crc %#04x vs %#04x\n", 856e66475ebSJeffrey Lin __func__, calc_crc, fw_crc); 857e66475ebSJeffrey Lin goto out; 858e66475ebSJeffrey Lin } 859e66475ebSJeffrey Lin 86048a2b783SJeffrey Lin raydium_mt_event(ts); 86148a2b783SJeffrey Lin 862e66475ebSJeffrey Lin out: 86348a2b783SJeffrey Lin return IRQ_HANDLED; 86448a2b783SJeffrey Lin } 86548a2b783SJeffrey Lin 86648a2b783SJeffrey Lin static ssize_t raydium_i2c_fw_ver_show(struct device *dev, 86748a2b783SJeffrey Lin struct device_attribute *attr, char *buf) 86848a2b783SJeffrey Lin { 86948a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 87048a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 87148a2b783SJeffrey Lin 87248a2b783SJeffrey Lin return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver); 87348a2b783SJeffrey Lin } 87448a2b783SJeffrey Lin 87548a2b783SJeffrey Lin static ssize_t raydium_i2c_hw_ver_show(struct device *dev, 87648a2b783SJeffrey Lin struct device_attribute *attr, char *buf) 87748a2b783SJeffrey Lin { 87848a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 87948a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 88048a2b783SJeffrey Lin 88148a2b783SJeffrey Lin return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver)); 88248a2b783SJeffrey Lin } 88348a2b783SJeffrey Lin 88448a2b783SJeffrey Lin static ssize_t raydium_i2c_boot_mode_show(struct device *dev, 88548a2b783SJeffrey Lin struct device_attribute *attr, 88648a2b783SJeffrey Lin char *buf) 88748a2b783SJeffrey Lin { 88848a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 88948a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 89048a2b783SJeffrey Lin 89148a2b783SJeffrey Lin return sprintf(buf, "%s\n", 89248a2b783SJeffrey Lin ts->boot_mode == RAYDIUM_TS_MAIN ? 89348a2b783SJeffrey Lin "Normal" : "Recovery"); 89448a2b783SJeffrey Lin } 89548a2b783SJeffrey Lin 89648a2b783SJeffrey Lin static ssize_t raydium_i2c_update_fw_store(struct device *dev, 89748a2b783SJeffrey Lin struct device_attribute *attr, 89848a2b783SJeffrey Lin const char *buf, size_t count) 89948a2b783SJeffrey Lin { 90048a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 90148a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 90248a2b783SJeffrey Lin int error; 90348a2b783SJeffrey Lin 90448a2b783SJeffrey Lin error = mutex_lock_interruptible(&ts->sysfs_mutex); 90548a2b783SJeffrey Lin if (error) 90648a2b783SJeffrey Lin return error; 90748a2b783SJeffrey Lin 90848a2b783SJeffrey Lin error = raydium_i2c_fw_update(ts); 90948a2b783SJeffrey Lin 91048a2b783SJeffrey Lin mutex_unlock(&ts->sysfs_mutex); 91148a2b783SJeffrey Lin 91248a2b783SJeffrey Lin return error ?: count; 91348a2b783SJeffrey Lin } 91448a2b783SJeffrey Lin 91548a2b783SJeffrey Lin static ssize_t raydium_i2c_calibrate_store(struct device *dev, 91648a2b783SJeffrey Lin struct device_attribute *attr, 91748a2b783SJeffrey Lin const char *buf, size_t count) 91848a2b783SJeffrey Lin { 91948a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 92048a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 92148a2b783SJeffrey Lin static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E }; 92248a2b783SJeffrey Lin int error; 92348a2b783SJeffrey Lin 92448a2b783SJeffrey Lin error = mutex_lock_interruptible(&ts->sysfs_mutex); 92548a2b783SJeffrey Lin if (error) 92648a2b783SJeffrey Lin return error; 92748a2b783SJeffrey Lin 92848a2b783SJeffrey Lin error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd), 92948a2b783SJeffrey Lin RAYDIUM_WAIT_READY); 93048a2b783SJeffrey Lin if (error) 93148a2b783SJeffrey Lin dev_err(&client->dev, "calibrate command failed: %d\n", error); 93248a2b783SJeffrey Lin 93348a2b783SJeffrey Lin mutex_unlock(&ts->sysfs_mutex); 93448a2b783SJeffrey Lin return error ?: count; 93548a2b783SJeffrey Lin } 93648a2b783SJeffrey Lin 93748a2b783SJeffrey Lin static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL); 93848a2b783SJeffrey Lin static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL); 93948a2b783SJeffrey Lin static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL); 94048a2b783SJeffrey Lin static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store); 94148a2b783SJeffrey Lin static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store); 94248a2b783SJeffrey Lin 94348a2b783SJeffrey Lin static struct attribute *raydium_i2c_attributes[] = { 94448a2b783SJeffrey Lin &dev_attr_update_fw.attr, 94548a2b783SJeffrey Lin &dev_attr_boot_mode.attr, 94648a2b783SJeffrey Lin &dev_attr_fw_version.attr, 94748a2b783SJeffrey Lin &dev_attr_hw_version.attr, 94848a2b783SJeffrey Lin &dev_attr_calibrate.attr, 94948a2b783SJeffrey Lin NULL 95048a2b783SJeffrey Lin }; 95148a2b783SJeffrey Lin 952d653386dSArvind Yadav static const struct attribute_group raydium_i2c_attribute_group = { 95348a2b783SJeffrey Lin .attrs = raydium_i2c_attributes, 95448a2b783SJeffrey Lin }; 95548a2b783SJeffrey Lin 95648a2b783SJeffrey Lin static int raydium_i2c_power_on(struct raydium_data *ts) 95748a2b783SJeffrey Lin { 95848a2b783SJeffrey Lin int error; 95948a2b783SJeffrey Lin 960cd467299SDmitry Torokhov if (!ts->reset_gpio) 96148a2b783SJeffrey Lin return 0; 96248a2b783SJeffrey Lin 96348a2b783SJeffrey Lin gpiod_set_value_cansleep(ts->reset_gpio, 1); 96448a2b783SJeffrey Lin 96548a2b783SJeffrey Lin error = regulator_enable(ts->avdd); 96648a2b783SJeffrey Lin if (error) { 96748a2b783SJeffrey Lin dev_err(&ts->client->dev, 96848a2b783SJeffrey Lin "failed to enable avdd regulator: %d\n", error); 96948a2b783SJeffrey Lin goto release_reset_gpio; 97048a2b783SJeffrey Lin } 97148a2b783SJeffrey Lin 97248a2b783SJeffrey Lin error = regulator_enable(ts->vccio); 97348a2b783SJeffrey Lin if (error) { 97448a2b783SJeffrey Lin regulator_disable(ts->avdd); 97548a2b783SJeffrey Lin dev_err(&ts->client->dev, 97648a2b783SJeffrey Lin "failed to enable vccio regulator: %d\n", error); 97748a2b783SJeffrey Lin goto release_reset_gpio; 97848a2b783SJeffrey Lin } 97948a2b783SJeffrey Lin 98048a2b783SJeffrey Lin udelay(RM_POWERON_DELAY_USEC); 98148a2b783SJeffrey Lin 98248a2b783SJeffrey Lin release_reset_gpio: 98348a2b783SJeffrey Lin gpiod_set_value_cansleep(ts->reset_gpio, 0); 98448a2b783SJeffrey Lin 98548a2b783SJeffrey Lin if (error) 98648a2b783SJeffrey Lin return error; 98748a2b783SJeffrey Lin 98848a2b783SJeffrey Lin msleep(RM_RESET_DELAY_MSEC); 98948a2b783SJeffrey Lin 99048a2b783SJeffrey Lin return 0; 99148a2b783SJeffrey Lin } 99248a2b783SJeffrey Lin 99348a2b783SJeffrey Lin static void raydium_i2c_power_off(void *_data) 99448a2b783SJeffrey Lin { 99548a2b783SJeffrey Lin struct raydium_data *ts = _data; 99648a2b783SJeffrey Lin 997cd467299SDmitry Torokhov if (ts->reset_gpio) { 99848a2b783SJeffrey Lin gpiod_set_value_cansleep(ts->reset_gpio, 1); 99948a2b783SJeffrey Lin regulator_disable(ts->vccio); 100048a2b783SJeffrey Lin regulator_disable(ts->avdd); 100148a2b783SJeffrey Lin } 100248a2b783SJeffrey Lin } 100348a2b783SJeffrey Lin 100448a2b783SJeffrey Lin static int raydium_i2c_probe(struct i2c_client *client, 100548a2b783SJeffrey Lin const struct i2c_device_id *id) 100648a2b783SJeffrey Lin { 100748a2b783SJeffrey Lin union i2c_smbus_data dummy; 100848a2b783SJeffrey Lin struct raydium_data *ts; 100948a2b783SJeffrey Lin int error; 101048a2b783SJeffrey Lin 101148a2b783SJeffrey Lin if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 101248a2b783SJeffrey Lin dev_err(&client->dev, 101348a2b783SJeffrey Lin "i2c check functionality error (need I2C_FUNC_I2C)\n"); 101448a2b783SJeffrey Lin return -ENXIO; 101548a2b783SJeffrey Lin } 101648a2b783SJeffrey Lin 101748a2b783SJeffrey Lin ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 101848a2b783SJeffrey Lin if (!ts) 101948a2b783SJeffrey Lin return -ENOMEM; 102048a2b783SJeffrey Lin 102148a2b783SJeffrey Lin mutex_init(&ts->sysfs_mutex); 102248a2b783SJeffrey Lin 102348a2b783SJeffrey Lin ts->client = client; 102448a2b783SJeffrey Lin i2c_set_clientdata(client, ts); 102548a2b783SJeffrey Lin 102648a2b783SJeffrey Lin ts->avdd = devm_regulator_get(&client->dev, "avdd"); 102748a2b783SJeffrey Lin if (IS_ERR(ts->avdd)) { 102848a2b783SJeffrey Lin error = PTR_ERR(ts->avdd); 102948a2b783SJeffrey Lin if (error != -EPROBE_DEFER) 103048a2b783SJeffrey Lin dev_err(&client->dev, 103148a2b783SJeffrey Lin "Failed to get 'avdd' regulator: %d\n", error); 103248a2b783SJeffrey Lin return error; 103348a2b783SJeffrey Lin } 103448a2b783SJeffrey Lin 103548a2b783SJeffrey Lin ts->vccio = devm_regulator_get(&client->dev, "vccio"); 103648a2b783SJeffrey Lin if (IS_ERR(ts->vccio)) { 103748a2b783SJeffrey Lin error = PTR_ERR(ts->vccio); 103848a2b783SJeffrey Lin if (error != -EPROBE_DEFER) 103948a2b783SJeffrey Lin dev_err(&client->dev, 104048a2b783SJeffrey Lin "Failed to get 'vccio' regulator: %d\n", error); 104148a2b783SJeffrey Lin return error; 104248a2b783SJeffrey Lin } 104348a2b783SJeffrey Lin 104448a2b783SJeffrey Lin ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", 104548a2b783SJeffrey Lin GPIOD_OUT_LOW); 104648a2b783SJeffrey Lin if (IS_ERR(ts->reset_gpio)) { 104748a2b783SJeffrey Lin error = PTR_ERR(ts->reset_gpio); 1048cd467299SDmitry Torokhov if (error != -EPROBE_DEFER) 104948a2b783SJeffrey Lin dev_err(&client->dev, 105048a2b783SJeffrey Lin "failed to get reset gpio: %d\n", error); 105148a2b783SJeffrey Lin return error; 105248a2b783SJeffrey Lin } 105348a2b783SJeffrey Lin 105448a2b783SJeffrey Lin error = raydium_i2c_power_on(ts); 105548a2b783SJeffrey Lin if (error) 105648a2b783SJeffrey Lin return error; 105748a2b783SJeffrey Lin 105848a2b783SJeffrey Lin error = devm_add_action(&client->dev, raydium_i2c_power_off, ts); 105948a2b783SJeffrey Lin if (error) { 106048a2b783SJeffrey Lin dev_err(&client->dev, 106148a2b783SJeffrey Lin "failed to install power off action: %d\n", error); 106248a2b783SJeffrey Lin raydium_i2c_power_off(ts); 106348a2b783SJeffrey Lin return error; 106448a2b783SJeffrey Lin } 106548a2b783SJeffrey Lin 106648a2b783SJeffrey Lin /* Make sure there is something at this address */ 106748a2b783SJeffrey Lin if (i2c_smbus_xfer(client->adapter, client->addr, 0, 106848a2b783SJeffrey Lin I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) { 106948a2b783SJeffrey Lin dev_err(&client->dev, "nothing at this address\n"); 107048a2b783SJeffrey Lin return -ENXIO; 107148a2b783SJeffrey Lin } 107248a2b783SJeffrey Lin 107348a2b783SJeffrey Lin error = raydium_i2c_initialize(ts); 107448a2b783SJeffrey Lin if (error) { 107548a2b783SJeffrey Lin dev_err(&client->dev, "failed to initialize: %d\n", error); 107648a2b783SJeffrey Lin return error; 107748a2b783SJeffrey Lin } 107848a2b783SJeffrey Lin 107948a2b783SJeffrey Lin ts->report_data = devm_kmalloc(&client->dev, 1080e66475ebSJeffrey Lin ts->pkg_size, GFP_KERNEL); 108148a2b783SJeffrey Lin if (!ts->report_data) 108248a2b783SJeffrey Lin return -ENOMEM; 108348a2b783SJeffrey Lin 108448a2b783SJeffrey Lin ts->input = devm_input_allocate_device(&client->dev); 108548a2b783SJeffrey Lin if (!ts->input) { 108648a2b783SJeffrey Lin dev_err(&client->dev, "Failed to allocate input device\n"); 108748a2b783SJeffrey Lin return -ENOMEM; 108848a2b783SJeffrey Lin } 108948a2b783SJeffrey Lin 109048a2b783SJeffrey Lin ts->input->name = "Raydium Touchscreen"; 109148a2b783SJeffrey Lin ts->input->id.bustype = BUS_I2C; 109248a2b783SJeffrey Lin 109348a2b783SJeffrey Lin input_set_abs_params(ts->input, ABS_MT_POSITION_X, 109448a2b783SJeffrey Lin 0, le16_to_cpu(ts->info.x_max), 0, 0); 109548a2b783SJeffrey Lin input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 109648a2b783SJeffrey Lin 0, le16_to_cpu(ts->info.y_max), 0, 0); 109748a2b783SJeffrey Lin input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res); 109848a2b783SJeffrey Lin input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res); 109948a2b783SJeffrey Lin 110048a2b783SJeffrey Lin input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 110148a2b783SJeffrey Lin input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0); 110248a2b783SJeffrey Lin 110348a2b783SJeffrey Lin error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM, 110448a2b783SJeffrey Lin INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 110548a2b783SJeffrey Lin if (error) { 110648a2b783SJeffrey Lin dev_err(&client->dev, 110748a2b783SJeffrey Lin "failed to initialize MT slots: %d\n", error); 110848a2b783SJeffrey Lin return error; 110948a2b783SJeffrey Lin } 111048a2b783SJeffrey Lin 111148a2b783SJeffrey Lin error = input_register_device(ts->input); 111248a2b783SJeffrey Lin if (error) { 111348a2b783SJeffrey Lin dev_err(&client->dev, 111448a2b783SJeffrey Lin "unable to register input device: %d\n", error); 111548a2b783SJeffrey Lin return error; 111648a2b783SJeffrey Lin } 111748a2b783SJeffrey Lin 111848a2b783SJeffrey Lin error = devm_request_threaded_irq(&client->dev, client->irq, 111948a2b783SJeffrey Lin NULL, raydium_i2c_irq, 112048a2b783SJeffrey Lin IRQF_ONESHOT, client->name, ts); 112148a2b783SJeffrey Lin if (error) { 112248a2b783SJeffrey Lin dev_err(&client->dev, "Failed to register interrupt\n"); 112348a2b783SJeffrey Lin return error; 112448a2b783SJeffrey Lin } 112548a2b783SJeffrey Lin 1126b887df52SAndi Shyti error = devm_device_add_group(&client->dev, 112748a2b783SJeffrey Lin &raydium_i2c_attribute_group); 112848a2b783SJeffrey Lin if (error) { 112948a2b783SJeffrey Lin dev_err(&client->dev, "failed to create sysfs attributes: %d\n", 113048a2b783SJeffrey Lin error); 113148a2b783SJeffrey Lin return error; 113248a2b783SJeffrey Lin } 113348a2b783SJeffrey Lin 113448a2b783SJeffrey Lin return 0; 113548a2b783SJeffrey Lin } 113648a2b783SJeffrey Lin 113748a2b783SJeffrey Lin static void __maybe_unused raydium_enter_sleep(struct i2c_client *client) 113848a2b783SJeffrey Lin { 113948a2b783SJeffrey Lin static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f }; 114048a2b783SJeffrey Lin int error; 114148a2b783SJeffrey Lin 114248a2b783SJeffrey Lin error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP, 114348a2b783SJeffrey Lin sleep_cmd, sizeof(sleep_cmd)); 114448a2b783SJeffrey Lin if (error) 114548a2b783SJeffrey Lin dev_err(&client->dev, 114648a2b783SJeffrey Lin "sleep command failed: %d\n", error); 114748a2b783SJeffrey Lin } 114848a2b783SJeffrey Lin 114948a2b783SJeffrey Lin static int __maybe_unused raydium_i2c_suspend(struct device *dev) 115048a2b783SJeffrey Lin { 115148a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 115248a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 115348a2b783SJeffrey Lin 115448a2b783SJeffrey Lin /* Sleep is not available in BLDR recovery mode */ 115548a2b783SJeffrey Lin if (ts->boot_mode != RAYDIUM_TS_MAIN) 1156cd467299SDmitry Torokhov return -EBUSY; 115748a2b783SJeffrey Lin 115848a2b783SJeffrey Lin disable_irq(client->irq); 115948a2b783SJeffrey Lin 116048a2b783SJeffrey Lin if (device_may_wakeup(dev)) { 116148a2b783SJeffrey Lin raydium_enter_sleep(client); 116248a2b783SJeffrey Lin 116348a2b783SJeffrey Lin ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0); 116448a2b783SJeffrey Lin } else { 116548a2b783SJeffrey Lin raydium_i2c_power_off(ts); 116648a2b783SJeffrey Lin } 116748a2b783SJeffrey Lin 116848a2b783SJeffrey Lin return 0; 116948a2b783SJeffrey Lin } 117048a2b783SJeffrey Lin 117148a2b783SJeffrey Lin static int __maybe_unused raydium_i2c_resume(struct device *dev) 117248a2b783SJeffrey Lin { 117348a2b783SJeffrey Lin struct i2c_client *client = to_i2c_client(dev); 117448a2b783SJeffrey Lin struct raydium_data *ts = i2c_get_clientdata(client); 117548a2b783SJeffrey Lin 117648a2b783SJeffrey Lin if (device_may_wakeup(dev)) { 117748a2b783SJeffrey Lin if (ts->wake_irq_enabled) 117848a2b783SJeffrey Lin disable_irq_wake(client->irq); 117948a2b783SJeffrey Lin raydium_i2c_sw_reset(client); 118048a2b783SJeffrey Lin } else { 118148a2b783SJeffrey Lin raydium_i2c_power_on(ts); 118248a2b783SJeffrey Lin raydium_i2c_initialize(ts); 118348a2b783SJeffrey Lin } 118448a2b783SJeffrey Lin 118548a2b783SJeffrey Lin enable_irq(client->irq); 118648a2b783SJeffrey Lin 118748a2b783SJeffrey Lin return 0; 118848a2b783SJeffrey Lin } 118948a2b783SJeffrey Lin 119048a2b783SJeffrey Lin static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops, 119148a2b783SJeffrey Lin raydium_i2c_suspend, raydium_i2c_resume); 119248a2b783SJeffrey Lin 119348a2b783SJeffrey Lin static const struct i2c_device_id raydium_i2c_id[] = { 119448a2b783SJeffrey Lin { "raydium_i2c" , 0 }, 119548a2b783SJeffrey Lin { "rm32380", 0 }, 119648a2b783SJeffrey Lin { /* sentinel */ } 119748a2b783SJeffrey Lin }; 119848a2b783SJeffrey Lin MODULE_DEVICE_TABLE(i2c, raydium_i2c_id); 119948a2b783SJeffrey Lin 120048a2b783SJeffrey Lin #ifdef CONFIG_ACPI 120148a2b783SJeffrey Lin static const struct acpi_device_id raydium_acpi_id[] = { 120248a2b783SJeffrey Lin { "RAYD0001", 0 }, 120348a2b783SJeffrey Lin { /* sentinel */ } 120448a2b783SJeffrey Lin }; 120548a2b783SJeffrey Lin MODULE_DEVICE_TABLE(acpi, raydium_acpi_id); 120648a2b783SJeffrey Lin #endif 120748a2b783SJeffrey Lin 120848a2b783SJeffrey Lin #ifdef CONFIG_OF 120948a2b783SJeffrey Lin static const struct of_device_id raydium_of_match[] = { 121048a2b783SJeffrey Lin { .compatible = "raydium,rm32380", }, 121148a2b783SJeffrey Lin { /* sentinel */ } 121248a2b783SJeffrey Lin }; 121348a2b783SJeffrey Lin MODULE_DEVICE_TABLE(of, raydium_of_match); 121448a2b783SJeffrey Lin #endif 121548a2b783SJeffrey Lin 121648a2b783SJeffrey Lin static struct i2c_driver raydium_i2c_driver = { 121748a2b783SJeffrey Lin .probe = raydium_i2c_probe, 121848a2b783SJeffrey Lin .id_table = raydium_i2c_id, 121948a2b783SJeffrey Lin .driver = { 122048a2b783SJeffrey Lin .name = "raydium_ts", 122148a2b783SJeffrey Lin .pm = &raydium_i2c_pm_ops, 122248a2b783SJeffrey Lin .acpi_match_table = ACPI_PTR(raydium_acpi_id), 122348a2b783SJeffrey Lin .of_match_table = of_match_ptr(raydium_of_match), 122448a2b783SJeffrey Lin }, 122548a2b783SJeffrey Lin }; 122648a2b783SJeffrey Lin module_i2c_driver(raydium_i2c_driver); 122748a2b783SJeffrey Lin 122848a2b783SJeffrey Lin MODULE_AUTHOR("Raydium"); 122948a2b783SJeffrey Lin MODULE_DESCRIPTION("Raydium I2c Touchscreen driver"); 123048a2b783SJeffrey Lin MODULE_LICENSE("GPL v2"); 1231