// SPDX-License-Identifier: GPL-2.0-only
#include <linux/crc-ccitt.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/ihex.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/input/touchscreen.h>
#include <linux/interrupt.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
#define ILI2XXX_POLL_PERIOD 15
#define ILI210X_DATA_SIZE 64
#define ILI211X_DATA_SIZE 43
#define ILI251X_DATA_SIZE1 31
#define ILI251X_DATA_SIZE2 20
/* Touchscreen commands */
#define REG_TOUCHDATA 0x10
#define REG_PANEL_INFO 0x20
#define REG_FIRMWARE_VERSION 0x40
#define REG_PROTOCOL_VERSION 0x42
#define REG_KERNEL_VERSION 0x61
#define REG_IC_BUSY 0x80
#define REG_IC_BUSY_NOT_BUSY 0x50
#define REG_GET_MODE 0xc0
#define REG_GET_MODE_AP 0x5a
#define REG_GET_MODE_BL 0x55
#define REG_SET_MODE_AP 0xc1
#define REG_SET_MODE_BL 0xc2
#define REG_WRITE_DATA 0xc3
#define REG_WRITE_ENABLE 0xc4
#define REG_READ_DATA_CRC 0xc7
#define REG_CALIBRATE 0xcc
#define ILI251X_FW_FILENAME "ilitek/ili251x.bin"
struct ili2xxx_chip {
int (*read_reg)(struct i2c_client *client, u8 reg,
void *buf, size_t len);
int (*get_touch_data)(struct i2c_client *client, u8 *data);
bool (*parse_touch_data)(const u8 *data, unsigned int finger,
unsigned int *x, unsigned int *y,
unsigned int *z);
bool (*continue_polling)(const u8 *data, bool touch);
unsigned int max_touches;
unsigned int resolution;
bool has_calibrate_reg;
bool has_firmware_proto;
bool has_pressure_reg;
};
struct ili210x {
struct i2c_client *client;
struct input_dev *input;
struct gpio_desc *reset_gpio;
struct touchscreen_properties prop;
const struct ili2xxx_chip *chip;
u8 version_firmware[8];
u8 version_kernel[5];
u8 version_proto[2];
u8 ic_mode[2];
bool stop;
};
static int ili210x_read_reg(struct i2c_client *client,
u8 reg, void *buf, size_t len)
{
struct i2c_msg msg[] = {
{
.addr = client->addr,
.flags = 0,
.len = 1,
.buf = ®,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = len,
.buf = buf,
}
};
int error, ret;
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if (ret != ARRAY_SIZE(msg)) {
error = ret < 0 ? ret : -EIO;
dev_err(&client->dev, "%s failed: %d\n", __func__, error);
return error;
}
return 0;
}
static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
{
return ili210x_read_reg(client, REG_TOUCHDATA,
data, ILI210X_DATA_SIZE);
}
static bool ili210x_touchdata_to_coords(const u8 *touchdata,
unsigned int finger,
unsigned int *x, unsigned int *y,
unsigned int *z)
{
if (!(touchdata[0] & BIT(finger)))
return false;
*x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
*y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
return true;
}
static bool ili210x_check_continue_polling(const u8 *data, bool touch)
{
return data[0] & 0xf3;
}
static const struct ili2xxx_chip ili210x_chip = {
.read_reg = ili210x_read_reg,
.get_touch_data = ili210x_read_touch_data,
.parse_touch_data = ili210x_touchdata_to_coords,
.continue_polling = ili210x_check_continue_polling,
.max_touches = 2,
.has_calibrate_reg = true,
};
static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
{
s16 sum = 0;
int error;
int ret;
int i;
ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE);
if (ret != ILI211X_DATA_SIZE) {
error = ret < 0 ? ret : -EIO;
dev_err(&client->dev, "%s failed: %d\n", __func__, error);
return error;
}
/* This chip uses custom checksum at the end of data */
for (i = 0; i < ILI211X_DATA_SIZE - 1; i++)
sum = (sum + data[i]) & 0xff;
if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) {
dev_err(&client->dev,
"CRC error (crc=0x%02x expected=0x%02x)\n",
sum, data[ILI211X_DATA_SIZE - 1]);
return -EIO;
}
return 0;
}
static bool ili211x_touchdata_to_coords(const u8 *touchdata,
unsigned int finger,
unsigned int *x, unsigned int *y,
unsigned int *z)
{<