18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Elan I2C/SMBus Touchpad driver - SMBus interface
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2013 ELAN Microelectronics Corp.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based on cyapa driver:
108c2ecf20Sopenharmony_ci * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
118c2ecf20Sopenharmony_ci * copyright (c) 2011-2012 Google, Inc.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Trademarks are the property of their respective owners.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/delay.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "elan_i2c.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Elan SMbus commands */
248c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_CMD		0x00
258c2ecf20Sopenharmony_ci#define ETP_SMBUS_ENABLE_TP		0x20
268c2ecf20Sopenharmony_ci#define ETP_SMBUS_SLEEP_CMD		0x21
278c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_PASSWORD_WRITE	0x29
288c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_PASSWORD_READ	0x80
298c2ecf20Sopenharmony_ci#define ETP_SMBUS_WRITE_FW_BLOCK	0x2A
308c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_RESET_CMD		0x2B
318c2ecf20Sopenharmony_ci#define ETP_SMBUS_RANGE_CMD		0xA0
328c2ecf20Sopenharmony_ci#define ETP_SMBUS_FW_VERSION_CMD	0xA1
338c2ecf20Sopenharmony_ci#define ETP_SMBUS_XY_TRACENUM_CMD	0xA2
348c2ecf20Sopenharmony_ci#define ETP_SMBUS_SM_VERSION_CMD	0xA3
358c2ecf20Sopenharmony_ci#define ETP_SMBUS_UNIQUEID_CMD		0xA3
368c2ecf20Sopenharmony_ci#define ETP_SMBUS_RESOLUTION_CMD	0xA4
378c2ecf20Sopenharmony_ci#define ETP_SMBUS_HELLOPACKET_CMD	0xA7
388c2ecf20Sopenharmony_ci#define ETP_SMBUS_PACKET_QUERY		0xA8
398c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_VERSION_CMD	0xAC
408c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_CTRL_CMD		0xAD
418c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_CHECKSUM_CMD	0xAE
428c2ecf20Sopenharmony_ci#define ETP_SMBUS_FW_CHECKSUM_CMD	0xAF
438c2ecf20Sopenharmony_ci#define ETP_SMBUS_MAX_BASELINE_CMD	0xC3
448c2ecf20Sopenharmony_ci#define ETP_SMBUS_MIN_BASELINE_CMD	0xC4
458c2ecf20Sopenharmony_ci#define ETP_SMBUS_CALIBRATE_QUERY	0xC5
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define ETP_SMBUS_REPORT_LEN		32
488c2ecf20Sopenharmony_ci#define ETP_SMBUS_REPORT_LEN2		7
498c2ecf20Sopenharmony_ci#define ETP_SMBUS_REPORT_OFFSET		2
508c2ecf20Sopenharmony_ci#define ETP_SMBUS_HELLOPACKET_LEN	5
518c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_PASSWORD		0x1234
528c2ecf20Sopenharmony_ci#define ETP_SMBUS_IAP_MODE_ON		(1 << 6)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int elan_smbus_initialize(struct i2c_client *client)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 };
578c2ecf20Sopenharmony_ci	u8 values[I2C_SMBUS_BLOCK_MAX] = {0};
588c2ecf20Sopenharmony_ci	int len, error;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* Get hello packet */
618c2ecf20Sopenharmony_ci	len = i2c_smbus_read_block_data(client,
628c2ecf20Sopenharmony_ci					ETP_SMBUS_HELLOPACKET_CMD, values);
638c2ecf20Sopenharmony_ci	if (len != ETP_SMBUS_HELLOPACKET_LEN) {
648c2ecf20Sopenharmony_ci		dev_err(&client->dev, "hello packet length fail: %d\n", len);
658c2ecf20Sopenharmony_ci		error = len < 0 ? len : -EIO;
668c2ecf20Sopenharmony_ci		return error;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* compare hello packet */
708c2ecf20Sopenharmony_ci	if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) {
718c2ecf20Sopenharmony_ci		dev_err(&client->dev, "hello packet fail [%*ph]\n",
728c2ecf20Sopenharmony_ci			ETP_SMBUS_HELLOPACKET_LEN, values);
738c2ecf20Sopenharmony_ci		return -ENXIO;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* enable tp */
778c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP);
788c2ecf20Sopenharmony_ci	if (error) {
798c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to enable touchpad: %d\n", error);
808c2ecf20Sopenharmony_ci		return error;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int elan_smbus_set_mode(struct i2c_client *client, u8 mode)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	u8 cmd[4] = { 0x00, 0x07, 0x00, mode };
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
918c2ecf20Sopenharmony_ci					  sizeof(cmd), cmd);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int elan_smbus_sleep_control(struct i2c_client *client, bool sleep)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	if (sleep)
978c2ecf20Sopenharmony_ci		return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD);
988c2ecf20Sopenharmony_ci	else
998c2ecf20Sopenharmony_ci		return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int elan_smbus_power_control(struct i2c_client *client, bool enable)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	return 0; /* A no-op */
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int elan_smbus_calibrate(struct i2c_client *client)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 };
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
1128c2ecf20Sopenharmony_ci					  sizeof(cmd), cmd);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	int error;
1188c2ecf20Sopenharmony_ci	u8 buf[I2C_SMBUS_BLOCK_MAX] = {0};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ETP_CALIBRATE_MAX_LEN > sizeof(buf));
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
1238c2ecf20Sopenharmony_ci					  ETP_SMBUS_CALIBRATE_QUERY, buf);
1248c2ecf20Sopenharmony_ci	if (error < 0)
1258c2ecf20Sopenharmony_ci		return error;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	memcpy(val, buf, ETP_CALIBRATE_MAX_LEN);
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic int elan_smbus_get_baseline_data(struct i2c_client *client,
1328c2ecf20Sopenharmony_ci					bool max_baseline, u8 *value)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	int error;
1358c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
1388c2ecf20Sopenharmony_ci					  max_baseline ?
1398c2ecf20Sopenharmony_ci						ETP_SMBUS_MAX_BASELINE_CMD :
1408c2ecf20Sopenharmony_ci						ETP_SMBUS_MIN_BASELINE_CMD,
1418c2ecf20Sopenharmony_ci					  val);
1428c2ecf20Sopenharmony_ci	if (error < 0)
1438c2ecf20Sopenharmony_ci		return error;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	*value = be16_to_cpup((__be16 *)val);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return 0;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic int elan_smbus_get_version(struct i2c_client *client,
1518c2ecf20Sopenharmony_ci				  u8 pattern, bool iap, u8 *version)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	int error;
1548c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
1578c2ecf20Sopenharmony_ci					  iap ? ETP_SMBUS_IAP_VERSION_CMD :
1588c2ecf20Sopenharmony_ci						ETP_SMBUS_FW_VERSION_CMD,
1598c2ecf20Sopenharmony_ci					  val);
1608c2ecf20Sopenharmony_ci	if (error < 0) {
1618c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get %s version: %d\n",
1628c2ecf20Sopenharmony_ci			iap ? "IAP" : "FW", error);
1638c2ecf20Sopenharmony_ci		return error;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	*version = val[2];
1678c2ecf20Sopenharmony_ci	return 0;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int elan_smbus_get_sm_version(struct i2c_client *client, u8 pattern,
1718c2ecf20Sopenharmony_ci				     u16 *ic_type, u8 *version, u8 *clickpad)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	int error;
1748c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
1778c2ecf20Sopenharmony_ci					  ETP_SMBUS_SM_VERSION_CMD, val);
1788c2ecf20Sopenharmony_ci	if (error < 0) {
1798c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get SM version: %d\n", error);
1808c2ecf20Sopenharmony_ci		return error;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	*version = val[0];
1848c2ecf20Sopenharmony_ci	*ic_type = val[1];
1858c2ecf20Sopenharmony_ci	*clickpad = val[0] & 0x10;
1868c2ecf20Sopenharmony_ci	return 0;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	int error;
1928c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
1958c2ecf20Sopenharmony_ci					  ETP_SMBUS_UNIQUEID_CMD, val);
1968c2ecf20Sopenharmony_ci	if (error < 0) {
1978c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get product ID: %d\n", error);
1988c2ecf20Sopenharmony_ci		return error;
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	*id = be16_to_cpup((__be16 *)val);
2028c2ecf20Sopenharmony_ci	return 0;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic int elan_smbus_get_checksum(struct i2c_client *client,
2068c2ecf20Sopenharmony_ci				   bool iap, u16 *csum)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	int error;
2098c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
2128c2ecf20Sopenharmony_ci					  iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
2138c2ecf20Sopenharmony_ci						ETP_SMBUS_IAP_CHECKSUM_CMD,
2148c2ecf20Sopenharmony_ci					  val);
2158c2ecf20Sopenharmony_ci	if (error < 0) {
2168c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get %s checksum: %d\n",
2178c2ecf20Sopenharmony_ci			iap ? "IAP" : "FW", error);
2188c2ecf20Sopenharmony_ci		return error;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	*csum = be16_to_cpup((__be16 *)val);
2228c2ecf20Sopenharmony_ci	return 0;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int elan_smbus_get_max(struct i2c_client *client,
2268c2ecf20Sopenharmony_ci			      unsigned int *max_x, unsigned int *max_y)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	int ret;
2298c2ecf20Sopenharmony_ci	int error;
2308c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
2338c2ecf20Sopenharmony_ci	if (ret != 3) {
2348c2ecf20Sopenharmony_ci		error = ret < 0 ? ret : -EIO;
2358c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get dimensions: %d\n", error);
2368c2ecf20Sopenharmony_ci		return error;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	*max_x = (0x0f & val[0]) << 8 | val[1];
2408c2ecf20Sopenharmony_ci	*max_y = (0xf0 & val[0]) << 4 | val[2];
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return 0;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic int elan_smbus_get_resolution(struct i2c_client *client,
2468c2ecf20Sopenharmony_ci				     u8 *hw_res_x, u8 *hw_res_y)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	int ret;
2498c2ecf20Sopenharmony_ci	int error;
2508c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val);
2538c2ecf20Sopenharmony_ci	if (ret != 3) {
2548c2ecf20Sopenharmony_ci		error = ret < 0 ? ret : -EIO;
2558c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get resolution: %d\n", error);
2568c2ecf20Sopenharmony_ci		return error;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	*hw_res_x = val[1] & 0x0F;
2608c2ecf20Sopenharmony_ci	*hw_res_y = (val[1] & 0xF0) >> 4;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	return 0;
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int elan_smbus_get_num_traces(struct i2c_client *client,
2668c2ecf20Sopenharmony_ci				     unsigned int *x_traces,
2678c2ecf20Sopenharmony_ci				     unsigned int *y_traces)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	int ret;
2708c2ecf20Sopenharmony_ci	int error;
2718c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val);
2748c2ecf20Sopenharmony_ci	if (ret != 3) {
2758c2ecf20Sopenharmony_ci		error = ret < 0 ? ret : -EIO;
2768c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to get trace info: %d\n", error);
2778c2ecf20Sopenharmony_ci		return error;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	*x_traces = val[1];
2818c2ecf20Sopenharmony_ci	*y_traces = val[2];
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return 0;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
2878c2ecf20Sopenharmony_ci					      int *adjustment)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	*adjustment = ETP_PRESSURE_OFFSET;
2908c2ecf20Sopenharmony_ci	return 0;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic int elan_smbus_iap_get_mode(struct i2c_client *client,
2948c2ecf20Sopenharmony_ci				   enum tp_mode *mode)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	int error;
2978c2ecf20Sopenharmony_ci	u16 constant;
2988c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
3018c2ecf20Sopenharmony_ci	if (error < 0) {
3028c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
3038c2ecf20Sopenharmony_ci			error);
3048c2ecf20Sopenharmony_ci		return error;
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	constant = be16_to_cpup((__be16 *)val);
3088c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	*mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return 0;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int elan_smbus_iap_reset(struct i2c_client *client)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	int error;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
3208c2ecf20Sopenharmony_ci	if (error) {
3218c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot reset IC: %d\n", error);
3228c2ecf20Sopenharmony_ci		return error;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	return 0;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int elan_smbus_set_flash_key(struct i2c_client *client)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	int error;
3318c2ecf20Sopenharmony_ci	u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
3348c2ecf20Sopenharmony_ci					   sizeof(cmd), cmd);
3358c2ecf20Sopenharmony_ci	if (error) {
3368c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot set flash key: %d\n", error);
3378c2ecf20Sopenharmony_ci		return error;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return 0;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic int elan_smbus_prepare_fw_update(struct i2c_client *client, u16 ic_type,
3448c2ecf20Sopenharmony_ci					u8 iap_version, u16 fw_page_size)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
3478c2ecf20Sopenharmony_ci	int len;
3488c2ecf20Sopenharmony_ci	int error;
3498c2ecf20Sopenharmony_ci	enum tp_mode mode;
3508c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
3518c2ecf20Sopenharmony_ci	u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
3528c2ecf20Sopenharmony_ci	u16 password;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* Get FW in which mode	(IAP_MODE/MAIN_MODE)  */
3558c2ecf20Sopenharmony_ci	error = elan_smbus_iap_get_mode(client, &mode);
3568c2ecf20Sopenharmony_ci	if (error)
3578c2ecf20Sopenharmony_ci		return error;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	if (mode == MAIN_MODE) {
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci		/* set flash key */
3628c2ecf20Sopenharmony_ci		error = elan_smbus_set_flash_key(client);
3638c2ecf20Sopenharmony_ci		if (error)
3648c2ecf20Sopenharmony_ci			return error;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci		/* write iap password */
3678c2ecf20Sopenharmony_ci		if (i2c_smbus_write_byte(client,
3688c2ecf20Sopenharmony_ci					 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
3698c2ecf20Sopenharmony_ci			dev_err(dev, "cannot write iap password\n");
3708c2ecf20Sopenharmony_ci			return -EIO;
3718c2ecf20Sopenharmony_ci		}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci		error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
3748c2ecf20Sopenharmony_ci						   sizeof(cmd), cmd);
3758c2ecf20Sopenharmony_ci		if (error) {
3768c2ecf20Sopenharmony_ci			dev_err(dev, "failed to write iap password: %d\n",
3778c2ecf20Sopenharmony_ci				error);
3788c2ecf20Sopenharmony_ci			return error;
3798c2ecf20Sopenharmony_ci		}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci		/*
3828c2ecf20Sopenharmony_ci		 * Read back password to make sure we enabled flash
3838c2ecf20Sopenharmony_ci		 * successfully.
3848c2ecf20Sopenharmony_ci		 */
3858c2ecf20Sopenharmony_ci		len = i2c_smbus_read_block_data(client,
3868c2ecf20Sopenharmony_ci						ETP_SMBUS_IAP_PASSWORD_READ,
3878c2ecf20Sopenharmony_ci						val);
3888c2ecf20Sopenharmony_ci		if (len < (int)sizeof(u16)) {
3898c2ecf20Sopenharmony_ci			error = len < 0 ? len : -EIO;
3908c2ecf20Sopenharmony_ci			dev_err(dev, "failed to read iap password: %d\n",
3918c2ecf20Sopenharmony_ci				error);
3928c2ecf20Sopenharmony_ci			return error;
3938c2ecf20Sopenharmony_ci		}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		password = be16_to_cpup((__be16 *)val);
3968c2ecf20Sopenharmony_ci		if (password != ETP_SMBUS_IAP_PASSWORD) {
3978c2ecf20Sopenharmony_ci			dev_err(dev, "wrong iap password = 0x%X\n", password);
3988c2ecf20Sopenharmony_ci			return -EIO;
3998c2ecf20Sopenharmony_ci		}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci		/* Wait 30ms for MAIN_MODE change to IAP_MODE */
4028c2ecf20Sopenharmony_ci		msleep(30);
4038c2ecf20Sopenharmony_ci	}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	error = elan_smbus_set_flash_key(client);
4068c2ecf20Sopenharmony_ci	if (error)
4078c2ecf20Sopenharmony_ci		return error;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* Reset IC */
4108c2ecf20Sopenharmony_ci	error = elan_smbus_iap_reset(client);
4118c2ecf20Sopenharmony_ci	if (error)
4128c2ecf20Sopenharmony_ci		return error;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	return 0;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic int elan_smbus_write_fw_block(struct i2c_client *client, u16 fw_page_size,
4198c2ecf20Sopenharmony_ci				     const u8 *page, u16 checksum, int idx)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
4228c2ecf20Sopenharmony_ci	int error;
4238c2ecf20Sopenharmony_ci	u16 result;
4248c2ecf20Sopenharmony_ci	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/*
4278c2ecf20Sopenharmony_ci	 * Due to the limitation of smbus protocol limiting
4288c2ecf20Sopenharmony_ci	 * transfer to 32 bytes at a time, we must split block
4298c2ecf20Sopenharmony_ci	 * in 2 transfers.
4308c2ecf20Sopenharmony_ci	 */
4318c2ecf20Sopenharmony_ci	error = i2c_smbus_write_block_data(client,
4328c2ecf20Sopenharmony_ci					   ETP_SMBUS_WRITE_FW_BLOCK,
4338c2ecf20Sopenharmony_ci					   fw_page_size / 2,
4348c2ecf20Sopenharmony_ci					   page);
4358c2ecf20Sopenharmony_ci	if (error) {
4368c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to write page %d (part %d): %d\n",
4378c2ecf20Sopenharmony_ci			idx, 1, error);
4388c2ecf20Sopenharmony_ci		return error;
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	error = i2c_smbus_write_block_data(client,
4428c2ecf20Sopenharmony_ci					   ETP_SMBUS_WRITE_FW_BLOCK,
4438c2ecf20Sopenharmony_ci					   fw_page_size / 2,
4448c2ecf20Sopenharmony_ci					   page + fw_page_size / 2);
4458c2ecf20Sopenharmony_ci	if (error) {
4468c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to write page %d (part %d): %d\n",
4478c2ecf20Sopenharmony_ci			idx, 2, error);
4488c2ecf20Sopenharmony_ci		return error;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	/* Wait for F/W to update one page ROM data. */
4538c2ecf20Sopenharmony_ci	usleep_range(8000, 10000);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	error = i2c_smbus_read_block_data(client,
4568c2ecf20Sopenharmony_ci					  ETP_SMBUS_IAP_CTRL_CMD, val);
4578c2ecf20Sopenharmony_ci	if (error < 0) {
4588c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to read IAP write result: %d\n",
4598c2ecf20Sopenharmony_ci			error);
4608c2ecf20Sopenharmony_ci		return error;
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	result = be16_to_cpup((__be16 *)val);
4648c2ecf20Sopenharmony_ci	if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
4658c2ecf20Sopenharmony_ci		dev_err(dev, "IAP reports failed write: %04hx\n",
4668c2ecf20Sopenharmony_ci			result);
4678c2ecf20Sopenharmony_ci		return -EIO;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	return 0;
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic int elan_smbus_get_report_features(struct i2c_client *client, u8 pattern,
4748c2ecf20Sopenharmony_ci					  unsigned int *features,
4758c2ecf20Sopenharmony_ci					  unsigned int *report_len)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	/*
4788c2ecf20Sopenharmony_ci	 * SMBus controllers with pattern 2 lack area info, as newer
4798c2ecf20Sopenharmony_ci	 * high-precision packets use that space for coordinates.
4808c2ecf20Sopenharmony_ci	 */
4818c2ecf20Sopenharmony_ci	*features = pattern <= 0x01 ? ETP_FEATURE_REPORT_MK : 0;
4828c2ecf20Sopenharmony_ci	*report_len = ETP_SMBUS_REPORT_LEN;
4838c2ecf20Sopenharmony_ci	return 0;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic int elan_smbus_get_report(struct i2c_client *client,
4878c2ecf20Sopenharmony_ci				 u8 *report, unsigned int report_len)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	int len;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	BUILD_BUG_ON(I2C_SMBUS_BLOCK_MAX > ETP_SMBUS_REPORT_LEN);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	len = i2c_smbus_read_block_data(client,
4948c2ecf20Sopenharmony_ci					ETP_SMBUS_PACKET_QUERY,
4958c2ecf20Sopenharmony_ci					&report[ETP_SMBUS_REPORT_OFFSET]);
4968c2ecf20Sopenharmony_ci	if (len < 0) {
4978c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to read report data: %d\n", len);
4988c2ecf20Sopenharmony_ci		return len;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (report[ETP_REPORT_ID_OFFSET] == ETP_TP_REPORT_ID2)
5028c2ecf20Sopenharmony_ci		report_len = ETP_SMBUS_REPORT_LEN2;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (len != report_len) {
5058c2ecf20Sopenharmony_ci		dev_err(&client->dev,
5068c2ecf20Sopenharmony_ci			"wrong report length (%d vs %d expected)\n",
5078c2ecf20Sopenharmony_ci			len, report_len);
5088c2ecf20Sopenharmony_ci		return -EIO;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	return 0;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic int elan_smbus_finish_fw_update(struct i2c_client *client,
5158c2ecf20Sopenharmony_ci				       struct completion *fw_completion)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	/* No special handling unlike I2C transport */
5188c2ecf20Sopenharmony_ci	return 0;
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	*pattern = 0;
5248c2ecf20Sopenharmony_ci	return 0;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ciconst struct elan_transport_ops elan_smbus_ops = {
5288c2ecf20Sopenharmony_ci	.initialize		= elan_smbus_initialize,
5298c2ecf20Sopenharmony_ci	.sleep_control		= elan_smbus_sleep_control,
5308c2ecf20Sopenharmony_ci	.power_control		= elan_smbus_power_control,
5318c2ecf20Sopenharmony_ci	.set_mode		= elan_smbus_set_mode,
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	.calibrate		= elan_smbus_calibrate,
5348c2ecf20Sopenharmony_ci	.calibrate_result	= elan_smbus_calibrate_result,
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	.get_baseline_data	= elan_smbus_get_baseline_data,
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	.get_version		= elan_smbus_get_version,
5398c2ecf20Sopenharmony_ci	.get_sm_version		= elan_smbus_get_sm_version,
5408c2ecf20Sopenharmony_ci	.get_product_id		= elan_smbus_get_product_id,
5418c2ecf20Sopenharmony_ci	.get_checksum		= elan_smbus_get_checksum,
5428c2ecf20Sopenharmony_ci	.get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	.get_max		= elan_smbus_get_max,
5458c2ecf20Sopenharmony_ci	.get_resolution		= elan_smbus_get_resolution,
5468c2ecf20Sopenharmony_ci	.get_num_traces		= elan_smbus_get_num_traces,
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	.iap_get_mode		= elan_smbus_iap_get_mode,
5498c2ecf20Sopenharmony_ci	.iap_reset		= elan_smbus_iap_reset,
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	.prepare_fw_update	= elan_smbus_prepare_fw_update,
5528c2ecf20Sopenharmony_ci	.write_fw_block		= elan_smbus_write_fw_block,
5538c2ecf20Sopenharmony_ci	.finish_fw_update	= elan_smbus_finish_fw_update,
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	.get_report_features	= elan_smbus_get_report_features,
5568c2ecf20Sopenharmony_ci	.get_report		= elan_smbus_get_report,
5578c2ecf20Sopenharmony_ci	.get_pattern		= elan_smbus_get_pattern,
5588c2ecf20Sopenharmony_ci};
559