// SPDX-License-Identifier: GPL-2.0-only
/*
* Raydium touchscreen I2C driver.
*
* Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
*
* Raydium reserves the right to make changes without further notice
* to the materials described herein. Raydium does not assume any
* liability arising out of the application described herein.
*
* Contact Raydium Semiconductor Corporation at www.rad-ic.com
*/
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
/* Slave I2C mode */
#define RM_BOOT_BLDR 0x02
#define RM_BOOT_MAIN 0x03
/* I2C bootoloader commands */
#define RM_CMD_BOOT_PAGE_WRT 0x0B /* send bl page write */
#define RM_CMD_BOOT_WRT 0x11 /* send bl write */
#define RM_CMD_BOOT_ACK 0x22 /* send ack*/
#define RM_CMD_BOOT_CHK 0x33 /* send data check */
#define RM_CMD_BOOT_READ 0x44 /* send wait bl data ready*/
#define RM_BOOT_RDY 0xFF /* bl data ready */
/* I2C main commands */
#define RM_CMD_QUERY_BANK 0x2B
#define RM_CMD_DATA_BANK 0x4D
#define RM_CMD_ENTER_SLEEP 0x4E
#define RM_CMD_BANK_SWITCH 0xAA
#define RM_RESET_MSG_ADDR 0x40000004
#define RM_MAX_READ_SIZE 56
#define RM_PACKET_CRC_SIZE 2
/* Touch relative info */
#define RM_MAX_RETRIES 3
#define RM_MAX_TOUCH_NUM 10
#define RM_BOOT_DELAY_MS 100
/* Offsets in contact data */
#define RM_CONTACT_STATE_POS 0
#define RM_CONTACT_X_POS 1
#define RM_CONTACT_Y_POS 3
#define RM_CONTACT_PRESSURE_POS 5
#define RM_CONTACT_WIDTH_X_POS 6
#define RM_CONTACT_WIDTH_Y_POS 7
/* Bootloader relative info */
#define RM_BL_WRT_CMD_SIZE 3 /* bl flash wrt cmd size */
#define RM_BL_WRT_PKG_SIZE 32 /* bl wrt pkg size */
#define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
#define RM_FW_PAGE_SIZE 128
#define RM_MAX_FW_RETRIES 30
#define RM_MAX_FW_SIZE 0xD000
#define RM_POWERON_DELAY_USEC 500
#define RM_RESET_DELAY_MSEC 50
enum raydium_bl_cmd {
BL_HEADER = 0,
BL_PAGE_STR,
BL_PKG_IDX,
BL_DATA_STR,
};
enum raydium_bl_ack {
RAYDIUM_ACK_NULL = 0,
RAYDIUM_WAIT_READY,
RAYDIUM_PATH_READY,
};
enum raydium_boot_mode {
RAYDIUM_TS_MAIN = 0,
RAYDIUM_TS_BLDR,
};
/* Response to RM_CMD_DATA_BANK request */
struct raydium_data_info {
__le32 data_bank_addr;
u8 pkg_size;
u8 tp_info_size;
};
struct raydium_info {
__le32 hw_ver; /*device version */
u8 main_ver;
u8 sub_ver;
__le16 ft_ver; /* test version */
u8 x_num;
u8 y_num;
__le16 x_max;
__le16 y_max;
u8 x_res; /* units/mm */
u8 y_res; /* units/mm */
};
/* struct raydium_data - represents state of Raydium touchscreen device */
struct raydium_data {
struct i2c_client *client;
struct input_dev *input;
struct regulator *avdd;
struct regulator *vccio;
struct gpio_desc *reset_gpio;
struct raydium_info info;
struct mutex sysfs_mutex;
u8 *report_data;
u32 data_bank_addr;
u8 report_size;
u8 contact_size;
u8 pkg_size;
enum raydium_boot_mode boot_mode;
bool wake_irq_enabled;
};
static int raydium_i2c_send(struct i2c_client *client,
u8 addr, const void *data, size_t len)
{
u8 *buf;
int tries = 0;
int ret;
buf = kmalloc(len + 1, GFP_KERNEL);
if (!buf)
return -ENOMEM;
buf[0] = addr;
memcpy(buf + 1, data, len);
do {
ret = i2c_master_send(client, buf, len + 1);
if (likely(ret == len + 1))
break;
msleep(20);
} while (++tries < RM_MAX_RETRIES);
kfree(buf);
if (unlikely(ret != len + 1)) {
if (ret >= 0)
ret = -EIO;
dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
return ret;
}
return 0;
}
static int raydium_i2c_read(struct i2c_client *client,
u8 addr, void *data, size_t len)
{
struct i2c_msg xfer[] = {
{
.addr = client->addr