xref: /openbmc/linux/drivers/input/mouse/elan_i2c_smbus.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26696777cSDuson Lin /*
36696777cSDuson Lin  * Elan I2C/SMBus Touchpad driver - SMBus interface
46696777cSDuson Lin  *
56696777cSDuson Lin  * Copyright (c) 2013 ELAN Microelectronics Corp.
66696777cSDuson Lin  *
76696777cSDuson Lin  * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
86696777cSDuson Lin  *
96696777cSDuson Lin  * Based on cyapa driver:
106696777cSDuson Lin  * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
116696777cSDuson Lin  * copyright (c) 2011-2012 Google, Inc.
126696777cSDuson Lin  *
136696777cSDuson Lin  * Trademarks are the property of their respective owners.
146696777cSDuson Lin  */
156696777cSDuson Lin 
166696777cSDuson Lin #include <linux/delay.h>
176696777cSDuson Lin #include <linux/i2c.h>
186696777cSDuson Lin #include <linux/init.h>
196696777cSDuson Lin #include <linux/kernel.h>
206696777cSDuson Lin 
216696777cSDuson Lin #include "elan_i2c.h"
226696777cSDuson Lin 
236696777cSDuson Lin /* Elan SMbus commands */
246696777cSDuson Lin #define ETP_SMBUS_IAP_CMD		0x00
256696777cSDuson Lin #define ETP_SMBUS_ENABLE_TP		0x20
266696777cSDuson Lin #define ETP_SMBUS_SLEEP_CMD		0x21
276696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD_WRITE	0x29
286696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD_READ	0x80
296696777cSDuson Lin #define ETP_SMBUS_WRITE_FW_BLOCK	0x2A
306696777cSDuson Lin #define ETP_SMBUS_IAP_RESET_CMD		0x2B
316696777cSDuson Lin #define ETP_SMBUS_RANGE_CMD		0xA0
326696777cSDuson Lin #define ETP_SMBUS_FW_VERSION_CMD	0xA1
336696777cSDuson Lin #define ETP_SMBUS_XY_TRACENUM_CMD	0xA2
346696777cSDuson Lin #define ETP_SMBUS_SM_VERSION_CMD	0xA3
356696777cSDuson Lin #define ETP_SMBUS_UNIQUEID_CMD		0xA3
366696777cSDuson Lin #define ETP_SMBUS_RESOLUTION_CMD	0xA4
376696777cSDuson Lin #define ETP_SMBUS_HELLOPACKET_CMD	0xA7
386696777cSDuson Lin #define ETP_SMBUS_PACKET_QUERY		0xA8
396696777cSDuson Lin #define ETP_SMBUS_IAP_VERSION_CMD	0xAC
406696777cSDuson Lin #define ETP_SMBUS_IAP_CTRL_CMD		0xAD
416696777cSDuson Lin #define ETP_SMBUS_IAP_CHECKSUM_CMD	0xAE
426696777cSDuson Lin #define ETP_SMBUS_FW_CHECKSUM_CMD	0xAF
436696777cSDuson Lin #define ETP_SMBUS_MAX_BASELINE_CMD	0xC3
446696777cSDuson Lin #define ETP_SMBUS_MIN_BASELINE_CMD	0xC4
456696777cSDuson Lin #define ETP_SMBUS_CALIBRATE_QUERY	0xC5
466696777cSDuson Lin 
476696777cSDuson Lin #define ETP_SMBUS_REPORT_LEN		32
48*056115daSJingle Wu #define ETP_SMBUS_REPORT_LEN2		7
496696777cSDuson Lin #define ETP_SMBUS_REPORT_OFFSET		2
506696777cSDuson Lin #define ETP_SMBUS_HELLOPACKET_LEN	5
516696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD		0x1234
526696777cSDuson Lin #define ETP_SMBUS_IAP_MODE_ON		(1 << 6)
536696777cSDuson Lin 
elan_smbus_initialize(struct i2c_client * client)546696777cSDuson Lin static int elan_smbus_initialize(struct i2c_client *client)
556696777cSDuson Lin {
566696777cSDuson Lin 	u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 };
5750fc7b61SBen Hutchings 	u8 values[I2C_SMBUS_BLOCK_MAX] = {0};
586696777cSDuson Lin 	int len, error;
596696777cSDuson Lin 
606696777cSDuson Lin 	/* Get hello packet */
616696777cSDuson Lin 	len = i2c_smbus_read_block_data(client,
626696777cSDuson Lin 					ETP_SMBUS_HELLOPACKET_CMD, values);
636696777cSDuson Lin 	if (len != ETP_SMBUS_HELLOPACKET_LEN) {
646696777cSDuson Lin 		dev_err(&client->dev, "hello packet length fail: %d\n", len);
656696777cSDuson Lin 		error = len < 0 ? len : -EIO;
666696777cSDuson Lin 		return error;
676696777cSDuson Lin 	}
686696777cSDuson Lin 
696696777cSDuson Lin 	/* compare hello packet */
706696777cSDuson Lin 	if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) {
7198a4a59eSRasmus Villemoes 		dev_err(&client->dev, "hello packet fail [%*ph]\n",
726696777cSDuson Lin 			ETP_SMBUS_HELLOPACKET_LEN, values);
736696777cSDuson Lin 		return -ENXIO;
746696777cSDuson Lin 	}
756696777cSDuson Lin 
766696777cSDuson Lin 	/* enable tp */
776696777cSDuson Lin 	error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP);
786696777cSDuson Lin 	if (error) {
796696777cSDuson Lin 		dev_err(&client->dev, "failed to enable touchpad: %d\n", error);
806696777cSDuson Lin 		return error;
816696777cSDuson Lin 	}
826696777cSDuson Lin 
836696777cSDuson Lin 	return 0;
846696777cSDuson Lin }
856696777cSDuson Lin 
elan_smbus_set_mode(struct i2c_client * client,u8 mode)866696777cSDuson Lin static int elan_smbus_set_mode(struct i2c_client *client, u8 mode)
876696777cSDuson Lin {
886696777cSDuson Lin 	u8 cmd[4] = { 0x00, 0x07, 0x00, mode };
896696777cSDuson Lin 
906696777cSDuson Lin 	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
916696777cSDuson Lin 					  sizeof(cmd), cmd);
926696777cSDuson Lin }
936696777cSDuson Lin 
elan_smbus_sleep_control(struct i2c_client * client,bool sleep)946696777cSDuson Lin static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep)
956696777cSDuson Lin {
966696777cSDuson Lin 	if (sleep)
976696777cSDuson Lin 		return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD);
986696777cSDuson Lin 	else
996696777cSDuson Lin 		return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */
1006696777cSDuson Lin }
1016696777cSDuson Lin 
elan_smbus_power_control(struct i2c_client * client,bool enable)1026696777cSDuson Lin static int elan_smbus_power_control(struct i2c_client *client, bool enable)
1036696777cSDuson Lin {
1046696777cSDuson Lin 	return 0; /* A no-op */
1056696777cSDuson Lin }
1066696777cSDuson Lin 
elan_smbus_calibrate(struct i2c_client * client)1076696777cSDuson Lin static int elan_smbus_calibrate(struct i2c_client *client)
1086696777cSDuson Lin {
1096696777cSDuson Lin 	u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 };
1106696777cSDuson Lin 
1116696777cSDuson Lin 	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
1126696777cSDuson Lin 					  sizeof(cmd), cmd);
1136696777cSDuson Lin }
1146696777cSDuson Lin 
elan_smbus_calibrate_result(struct i2c_client * client,u8 * val)1156696777cSDuson Lin static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val)
1166696777cSDuson Lin {
1176696777cSDuson Lin 	int error;
11850fc7b61SBen Hutchings 	u8 buf[I2C_SMBUS_BLOCK_MAX] = {0};
11950fc7b61SBen Hutchings 
12050fc7b61SBen Hutchings 	BUILD_BUG_ON(ETP_CALIBRATE_MAX_LEN > sizeof(buf));
1216696777cSDuson Lin 
1226696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
12350fc7b61SBen Hutchings 					  ETP_SMBUS_CALIBRATE_QUERY, buf);
1246696777cSDuson Lin 	if (error < 0)
1256696777cSDuson Lin 		return error;
1266696777cSDuson Lin 
12750fc7b61SBen Hutchings 	memcpy(val, buf, ETP_CALIBRATE_MAX_LEN);
1286696777cSDuson Lin 	return 0;
1296696777cSDuson Lin }
1306696777cSDuson Lin 
elan_smbus_get_baseline_data(struct i2c_client * client,bool max_baseline,u8 * value)1316696777cSDuson Lin static int elan_smbus_get_baseline_data(struct i2c_client *client,
1326696777cSDuson Lin 					bool max_baseline, u8 *value)
1336696777cSDuson Lin {
1346696777cSDuson Lin 	int error;
13540f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1366696777cSDuson Lin 
1376696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1386696777cSDuson Lin 					  max_baseline ?
1396696777cSDuson Lin 						ETP_SMBUS_MAX_BASELINE_CMD :
1406696777cSDuson Lin 						ETP_SMBUS_MIN_BASELINE_CMD,
1416696777cSDuson Lin 					  val);
1426696777cSDuson Lin 	if (error < 0)
1436696777cSDuson Lin 		return error;
1446696777cSDuson Lin 
1456696777cSDuson Lin 	*value = be16_to_cpup((__be16 *)val);
1466696777cSDuson Lin 
1476696777cSDuson Lin 	return 0;
1486696777cSDuson Lin }
1496696777cSDuson Lin 
elan_smbus_get_version(struct i2c_client * client,u8 pattern,bool iap,u8 * version)1506696777cSDuson Lin static int elan_smbus_get_version(struct i2c_client *client,
1513d712af6SDmitry Torokhov 				  u8 pattern, bool iap, u8 *version)
1526696777cSDuson Lin {
1536696777cSDuson Lin 	int error;
15440f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1556696777cSDuson Lin 
1566696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1576696777cSDuson Lin 					  iap ? ETP_SMBUS_IAP_VERSION_CMD :
1586696777cSDuson Lin 						ETP_SMBUS_FW_VERSION_CMD,
1596696777cSDuson Lin 					  val);
1606696777cSDuson Lin 	if (error < 0) {
1616696777cSDuson Lin 		dev_err(&client->dev, "failed to get %s version: %d\n",
1626696777cSDuson Lin 			iap ? "IAP" : "FW", error);
1636696777cSDuson Lin 		return error;
1646696777cSDuson Lin 	}
1656696777cSDuson Lin 
1666696777cSDuson Lin 	*version = val[2];
1676696777cSDuson Lin 	return 0;
1686696777cSDuson Lin }
1696696777cSDuson Lin 
elan_smbus_get_sm_version(struct i2c_client * client,u8 pattern,u16 * ic_type,u8 * version,u8 * clickpad)1703d712af6SDmitry Torokhov static int elan_smbus_get_sm_version(struct i2c_client *client, u8 pattern,
1713d712af6SDmitry Torokhov 				     u16 *ic_type, u8 *version, u8 *clickpad)
1726696777cSDuson Lin {
1736696777cSDuson Lin 	int error;
17440f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1756696777cSDuson Lin 
1766696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1776696777cSDuson Lin 					  ETP_SMBUS_SM_VERSION_CMD, val);
1786696777cSDuson Lin 	if (error < 0) {
1796696777cSDuson Lin 		dev_err(&client->dev, "failed to get SM version: %d\n", error);
1806696777cSDuson Lin 		return error;
1816696777cSDuson Lin 	}
1826696777cSDuson Lin 
18312018ac3SDuson Lin 	*version = val[0];
18412018ac3SDuson Lin 	*ic_type = val[1];
18599136881SKT Liao 	*clickpad = val[0] & 0x10;
1866696777cSDuson Lin 	return 0;
1876696777cSDuson Lin }
1886696777cSDuson Lin 
elan_smbus_get_product_id(struct i2c_client * client,u16 * id)189ed75a14eSDuson Lin static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
1906696777cSDuson Lin {
1916696777cSDuson Lin 	int error;
19240f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1936696777cSDuson Lin 
1946696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1956696777cSDuson Lin 					  ETP_SMBUS_UNIQUEID_CMD, val);
1966696777cSDuson Lin 	if (error < 0) {
1976696777cSDuson Lin 		dev_err(&client->dev, "failed to get product ID: %d\n", error);
1986696777cSDuson Lin 		return error;
1996696777cSDuson Lin 	}
2006696777cSDuson Lin 
201ed75a14eSDuson Lin 	*id = be16_to_cpup((__be16 *)val);
2026696777cSDuson Lin 	return 0;
2036696777cSDuson Lin }
2046696777cSDuson Lin 
elan_smbus_get_checksum(struct i2c_client * client,bool iap,u16 * csum)2056696777cSDuson Lin static int elan_smbus_get_checksum(struct i2c_client *client,
2066696777cSDuson Lin 				   bool iap, u16 *csum)
2076696777cSDuson Lin {
2086696777cSDuson Lin 	int error;
20940f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2106696777cSDuson Lin 
2116696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
2126696777cSDuson Lin 					  iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
2136696777cSDuson Lin 						ETP_SMBUS_IAP_CHECKSUM_CMD,
2146696777cSDuson Lin 					  val);
2156696777cSDuson Lin 	if (error < 0) {
2166696777cSDuson Lin 		dev_err(&client->dev, "failed to get %s checksum: %d\n",
2176696777cSDuson Lin 			iap ? "IAP" : "FW", error);
2186696777cSDuson Lin 		return error;
2196696777cSDuson Lin 	}
2206696777cSDuson Lin 
2216696777cSDuson Lin 	*csum = be16_to_cpup((__be16 *)val);
2226696777cSDuson Lin 	return 0;
2236696777cSDuson Lin }
2246696777cSDuson Lin 
elan_smbus_get_max(struct i2c_client * client,unsigned int * max_x,unsigned int * max_y)2256696777cSDuson Lin static int elan_smbus_get_max(struct i2c_client *client,
2266696777cSDuson Lin 			      unsigned int *max_x, unsigned int *max_y)
2276696777cSDuson Lin {
228265d426dSBenjamin Tissoires 	int ret;
2296696777cSDuson Lin 	int error;
23040f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2316696777cSDuson Lin 
232265d426dSBenjamin Tissoires 	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
233265d426dSBenjamin Tissoires 	if (ret != 3) {
234265d426dSBenjamin Tissoires 		error = ret < 0 ? ret : -EIO;
2356696777cSDuson Lin 		dev_err(&client->dev, "failed to get dimensions: %d\n", error);
2366696777cSDuson Lin 		return error;
2376696777cSDuson Lin 	}
2386696777cSDuson Lin 
2396696777cSDuson Lin 	*max_x = (0x0f & val[0]) << 8 | val[1];
2406696777cSDuson Lin 	*max_y = (0xf0 & val[0]) << 4 | val[2];
2416696777cSDuson Lin 
2426696777cSDuson Lin 	return 0;
2436696777cSDuson Lin }
2446696777cSDuson Lin 
elan_smbus_get_resolution(struct i2c_client * client,u8 * hw_res_x,u8 * hw_res_y)2456696777cSDuson Lin static int elan_smbus_get_resolution(struct i2c_client *client,
2466696777cSDuson Lin 				     u8 *hw_res_x, u8 *hw_res_y)
2476696777cSDuson Lin {
248265d426dSBenjamin Tissoires 	int ret;
2496696777cSDuson Lin 	int error;
25040f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2516696777cSDuson Lin 
252265d426dSBenjamin Tissoires 	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val);
253265d426dSBenjamin Tissoires 	if (ret != 3) {
254265d426dSBenjamin Tissoires 		error = ret < 0 ? ret : -EIO;
2556696777cSDuson Lin 		dev_err(&client->dev, "failed to get resolution: %d\n", error);
2566696777cSDuson Lin 		return error;
2576696777cSDuson Lin 	}
2586696777cSDuson Lin 
2596696777cSDuson Lin 	*hw_res_x = val[1] & 0x0F;
2606696777cSDuson Lin 	*hw_res_y = (val[1] & 0xF0) >> 4;
2616696777cSDuson Lin 
2626696777cSDuson Lin 	return 0;
2636696777cSDuson Lin }
2646696777cSDuson Lin 
elan_smbus_get_num_traces(struct i2c_client * client,unsigned int * x_traces,unsigned int * y_traces)2656696777cSDuson Lin static int elan_smbus_get_num_traces(struct i2c_client *client,
2666696777cSDuson Lin 				     unsigned int *x_traces,
2676696777cSDuson Lin 				     unsigned int *y_traces)
2686696777cSDuson Lin {
269265d426dSBenjamin Tissoires 	int ret;
2706696777cSDuson Lin 	int error;
27140f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2726696777cSDuson Lin 
273265d426dSBenjamin Tissoires 	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val);
274265d426dSBenjamin Tissoires 	if (ret != 3) {
275265d426dSBenjamin Tissoires 		error = ret < 0 ? ret : -EIO;
2766696777cSDuson Lin 		dev_err(&client->dev, "failed to get trace info: %d\n", error);
2776696777cSDuson Lin 		return error;
2786696777cSDuson Lin 	}
2796696777cSDuson Lin 
2809f423801SDuson Lin 	*x_traces = val[1];
2819f423801SDuson Lin 	*y_traces = val[2];
2826696777cSDuson Lin 
2836696777cSDuson Lin 	return 0;
2846696777cSDuson Lin }
2856696777cSDuson Lin 
elan_smbus_get_pressure_adjustment(struct i2c_client * client,int * adjustment)286b9bced0eSduson static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
287b9bced0eSduson 					      int *adjustment)
288b9bced0eSduson {
289b9bced0eSduson 	*adjustment = ETP_PRESSURE_OFFSET;
290b9bced0eSduson 	return 0;
291b9bced0eSduson }
292b9bced0eSduson 
elan_smbus_iap_get_mode(struct i2c_client * client,enum tp_mode * mode)2936696777cSDuson Lin static int elan_smbus_iap_get_mode(struct i2c_client *client,
2946696777cSDuson Lin 				   enum tp_mode *mode)
2956696777cSDuson Lin {
2966696777cSDuson Lin 	int error;
2976696777cSDuson Lin 	u16 constant;
29840f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2996696777cSDuson Lin 
3006696777cSDuson Lin 	error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
3016696777cSDuson Lin 	if (error < 0) {
3026696777cSDuson Lin 		dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
3036696777cSDuson Lin 			error);
3046696777cSDuson Lin 		return error;
3056696777cSDuson Lin 	}
3066696777cSDuson Lin 
3076696777cSDuson Lin 	constant = be16_to_cpup((__be16 *)val);
3086696777cSDuson Lin 	dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
3096696777cSDuson Lin 
3106696777cSDuson Lin 	*mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
3116696777cSDuson Lin 
3126696777cSDuson Lin 	return 0;
3136696777cSDuson Lin }
3146696777cSDuson Lin 
elan_smbus_iap_reset(struct i2c_client * client)3156696777cSDuson Lin static int elan_smbus_iap_reset(struct i2c_client *client)
3166696777cSDuson Lin {
3176696777cSDuson Lin 	int error;
3186696777cSDuson Lin 
3196696777cSDuson Lin 	error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
3206696777cSDuson Lin 	if (error) {
3216696777cSDuson Lin 		dev_err(&client->dev, "cannot reset IC: %d\n", error);
3226696777cSDuson Lin 		return error;
3236696777cSDuson Lin 	}
3246696777cSDuson Lin 
3256696777cSDuson Lin 	return 0;
3266696777cSDuson Lin }
3276696777cSDuson Lin 
elan_smbus_set_flash_key(struct i2c_client * client)3286696777cSDuson Lin static int elan_smbus_set_flash_key(struct i2c_client *client)
3296696777cSDuson Lin {
3306696777cSDuson Lin 	int error;
3316696777cSDuson Lin 	u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
3326696777cSDuson Lin 
3336696777cSDuson Lin 	error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
3346696777cSDuson Lin 					   sizeof(cmd), cmd);
3356696777cSDuson Lin 	if (error) {
3366696777cSDuson Lin 		dev_err(&client->dev, "cannot set flash key: %d\n", error);
3376696777cSDuson Lin 		return error;
3386696777cSDuson Lin 	}
3396696777cSDuson Lin 
3406696777cSDuson Lin 	return 0;
3416696777cSDuson Lin }
3426696777cSDuson Lin 
elan_smbus_prepare_fw_update(struct i2c_client * client,u16 ic_type,u8 iap_version,u16 fw_page_size)343bfd9b92bSJingle Wu static int elan_smbus_prepare_fw_update(struct i2c_client *client, u16 ic_type,
344ae3d6083Sjingle.wu 					u8 iap_version, u16 fw_page_size)
3456696777cSDuson Lin {
3466696777cSDuson Lin 	struct device *dev = &client->dev;
3476696777cSDuson Lin 	int len;
3486696777cSDuson Lin 	int error;
3496696777cSDuson Lin 	enum tp_mode mode;
35040f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
3516696777cSDuson Lin 	u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
3526696777cSDuson Lin 	u16 password;
3536696777cSDuson Lin 
3546696777cSDuson Lin 	/* Get FW in which mode	(IAP_MODE/MAIN_MODE)  */
3556696777cSDuson Lin 	error = elan_smbus_iap_get_mode(client, &mode);
3566696777cSDuson Lin 	if (error)
3576696777cSDuson Lin 		return error;
3586696777cSDuson Lin 
3596696777cSDuson Lin 	if (mode == MAIN_MODE) {
3606696777cSDuson Lin 
3616696777cSDuson Lin 		/* set flash key */
3626696777cSDuson Lin 		error = elan_smbus_set_flash_key(client);
3636696777cSDuson Lin 		if (error)
3646696777cSDuson Lin 			return error;
3656696777cSDuson Lin 
3666696777cSDuson Lin 		/* write iap password */
3676696777cSDuson Lin 		if (i2c_smbus_write_byte(client,
3686696777cSDuson Lin 					 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
3696696777cSDuson Lin 			dev_err(dev, "cannot write iap password\n");
3706696777cSDuson Lin 			return -EIO;
3716696777cSDuson Lin 		}
3726696777cSDuson Lin 
3736696777cSDuson Lin 		error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
3746696777cSDuson Lin 						   sizeof(cmd), cmd);
3756696777cSDuson Lin 		if (error) {
3766696777cSDuson Lin 			dev_err(dev, "failed to write iap password: %d\n",
3776696777cSDuson Lin 				error);
3786696777cSDuson Lin 			return error;
3796696777cSDuson Lin 		}
3806696777cSDuson Lin 
3816696777cSDuson Lin 		/*
3826696777cSDuson Lin 		 * Read back password to make sure we enabled flash
3836696777cSDuson Lin 		 * successfully.
3846696777cSDuson Lin 		 */
3856696777cSDuson Lin 		len = i2c_smbus_read_block_data(client,
3866696777cSDuson Lin 						ETP_SMBUS_IAP_PASSWORD_READ,
3876696777cSDuson Lin 						val);
388ce1d6f22SJulia Lawall 		if (len < (int)sizeof(u16)) {
3896696777cSDuson Lin 			error = len < 0 ? len : -EIO;
3906696777cSDuson Lin 			dev_err(dev, "failed to read iap password: %d\n",
3916696777cSDuson Lin 				error);
3926696777cSDuson Lin 			return error;
3936696777cSDuson Lin 		}
3946696777cSDuson Lin 
3956696777cSDuson Lin 		password = be16_to_cpup((__be16 *)val);
3966696777cSDuson Lin 		if (password != ETP_SMBUS_IAP_PASSWORD) {
3976696777cSDuson Lin 			dev_err(dev, "wrong iap password = 0x%X\n", password);
3986696777cSDuson Lin 			return -EIO;
3996696777cSDuson Lin 		}
4006696777cSDuson Lin 
4016696777cSDuson Lin 		/* Wait 30ms for MAIN_MODE change to IAP_MODE */
4026696777cSDuson Lin 		msleep(30);
4036696777cSDuson Lin 	}
4046696777cSDuson Lin 
4056696777cSDuson Lin 	error = elan_smbus_set_flash_key(client);
4066696777cSDuson Lin 	if (error)
4076696777cSDuson Lin 		return error;
4086696777cSDuson Lin 
4096696777cSDuson Lin 	/* Reset IC */
4106696777cSDuson Lin 	error = elan_smbus_iap_reset(client);
4116696777cSDuson Lin 	if (error)
4126696777cSDuson Lin 		return error;
4136696777cSDuson Lin 
4146696777cSDuson Lin 	return 0;
4156696777cSDuson Lin }
4166696777cSDuson Lin 
4176696777cSDuson Lin 
elan_smbus_write_fw_block(struct i2c_client * client,u16 fw_page_size,const u8 * page,u16 checksum,int idx)418059d6c2dSJingle Wu static int elan_smbus_write_fw_block(struct i2c_client *client, u16 fw_page_size,
4196696777cSDuson Lin 				     const u8 *page, u16 checksum, int idx)
4206696777cSDuson Lin {
4216696777cSDuson Lin 	struct device *dev = &client->dev;
4226696777cSDuson Lin 	int error;
4236696777cSDuson Lin 	u16 result;
42440f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
4256696777cSDuson Lin 
4266696777cSDuson Lin 	/*
4276696777cSDuson Lin 	 * Due to the limitation of smbus protocol limiting
4286696777cSDuson Lin 	 * transfer to 32 bytes at a time, we must split block
4296696777cSDuson Lin 	 * in 2 transfers.
4306696777cSDuson Lin 	 */
4316696777cSDuson Lin 	error = i2c_smbus_write_block_data(client,
4326696777cSDuson Lin 					   ETP_SMBUS_WRITE_FW_BLOCK,
433059d6c2dSJingle Wu 					   fw_page_size / 2,
4346696777cSDuson Lin 					   page);
4356696777cSDuson Lin 	if (error) {
4366696777cSDuson Lin 		dev_err(dev, "Failed to write page %d (part %d): %d\n",
4376696777cSDuson Lin 			idx, 1, error);
4386696777cSDuson Lin 		return error;
4396696777cSDuson Lin 	}
4406696777cSDuson Lin 
4416696777cSDuson Lin 	error = i2c_smbus_write_block_data(client,
4426696777cSDuson Lin 					   ETP_SMBUS_WRITE_FW_BLOCK,
443059d6c2dSJingle Wu 					   fw_page_size / 2,
444059d6c2dSJingle Wu 					   page + fw_page_size / 2);
4456696777cSDuson Lin 	if (error) {
4466696777cSDuson Lin 		dev_err(dev, "Failed to write page %d (part %d): %d\n",
4476696777cSDuson Lin 			idx, 2, error);
4486696777cSDuson Lin 		return error;
4496696777cSDuson Lin 	}
4506696777cSDuson Lin 
4516696777cSDuson Lin 
4526696777cSDuson Lin 	/* Wait for F/W to update one page ROM data. */
4536696777cSDuson Lin 	usleep_range(8000, 10000);
4546696777cSDuson Lin 
4556696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
4566696777cSDuson Lin 					  ETP_SMBUS_IAP_CTRL_CMD, val);
4576696777cSDuson Lin 	if (error < 0) {
4586696777cSDuson Lin 		dev_err(dev, "Failed to read IAP write result: %d\n",
4596696777cSDuson Lin 			error);
4606696777cSDuson Lin 		return error;
4616696777cSDuson Lin 	}
4626696777cSDuson Lin 
4636696777cSDuson Lin 	result = be16_to_cpup((__be16 *)val);
4646696777cSDuson Lin 	if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
4656696777cSDuson Lin 		dev_err(dev, "IAP reports failed write: %04hx\n",
4666696777cSDuson Lin 			result);
4676696777cSDuson Lin 		return -EIO;
4686696777cSDuson Lin 	}
4696696777cSDuson Lin 
4706696777cSDuson Lin 	return 0;
4716696777cSDuson Lin }
4726696777cSDuson Lin 
elan_smbus_get_report_features(struct i2c_client * client,u8 pattern,unsigned int * features,unsigned int * report_len)47304d5ce62SJingle Wu static int elan_smbus_get_report_features(struct i2c_client *client, u8 pattern,
47404d5ce62SJingle Wu 					  unsigned int *features,
47504d5ce62SJingle Wu 					  unsigned int *report_len)
47604d5ce62SJingle Wu {
47704d5ce62SJingle Wu 	/*
47804d5ce62SJingle Wu 	 * SMBus controllers with pattern 2 lack area info, as newer
47904d5ce62SJingle Wu 	 * high-precision packets use that space for coordinates.
48004d5ce62SJingle Wu 	 */
48104d5ce62SJingle Wu 	*features = pattern <= 0x01 ? ETP_FEATURE_REPORT_MK : 0;
48204d5ce62SJingle Wu 	*report_len = ETP_SMBUS_REPORT_LEN;
48304d5ce62SJingle Wu 	return 0;
48404d5ce62SJingle Wu }
48504d5ce62SJingle Wu 
elan_smbus_get_report(struct i2c_client * client,u8 * report,unsigned int report_len)48604d5ce62SJingle Wu static int elan_smbus_get_report(struct i2c_client *client,
48704d5ce62SJingle Wu 				 u8 *report, unsigned int report_len)
4886696777cSDuson Lin {
4896696777cSDuson Lin 	int len;
4906696777cSDuson Lin 
49150fc7b61SBen Hutchings 	BUILD_BUG_ON(I2C_SMBUS_BLOCK_MAX > ETP_SMBUS_REPORT_LEN);
49250fc7b61SBen Hutchings 
4936696777cSDuson Lin 	len = i2c_smbus_read_block_data(client,
4946696777cSDuson Lin 					ETP_SMBUS_PACKET_QUERY,
4956696777cSDuson Lin 					&report[ETP_SMBUS_REPORT_OFFSET]);
4966696777cSDuson Lin 	if (len < 0) {
4976696777cSDuson Lin 		dev_err(&client->dev, "failed to read report data: %d\n", len);
4986696777cSDuson Lin 		return len;
4996696777cSDuson Lin 	}
5006696777cSDuson Lin 
501*056115daSJingle Wu 	if (report[ETP_REPORT_ID_OFFSET] == ETP_TP_REPORT_ID2)
502*056115daSJingle Wu 		report_len = ETP_SMBUS_REPORT_LEN2;
503*056115daSJingle Wu 
504*056115daSJingle Wu 	if (len != report_len) {
5056696777cSDuson Lin 		dev_err(&client->dev,
5066696777cSDuson Lin 			"wrong report length (%d vs %d expected)\n",
507*056115daSJingle Wu 			len, report_len);
5086696777cSDuson Lin 		return -EIO;
5096696777cSDuson Lin 	}
5106696777cSDuson Lin 
5116696777cSDuson Lin 	return 0;
5126696777cSDuson Lin }
5136696777cSDuson Lin 
elan_smbus_finish_fw_update(struct i2c_client * client,struct completion * fw_completion)5146696777cSDuson Lin static int elan_smbus_finish_fw_update(struct i2c_client *client,
5156696777cSDuson Lin 				       struct completion *fw_completion)
5166696777cSDuson Lin {
5176696777cSDuson Lin 	/* No special handling unlike I2C transport */
5186696777cSDuson Lin 	return 0;
5196696777cSDuson Lin }
5206696777cSDuson Lin 
elan_smbus_get_pattern(struct i2c_client * client,u8 * pattern)521a2eaf299SKT Liao static int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern)
522a2eaf299SKT Liao {
523a2eaf299SKT Liao 	*pattern = 0;
524a2eaf299SKT Liao 	return 0;
525a2eaf299SKT Liao }
526a2eaf299SKT Liao 
5276696777cSDuson Lin const struct elan_transport_ops elan_smbus_ops = {
5286696777cSDuson Lin 	.initialize		= elan_smbus_initialize,
5296696777cSDuson Lin 	.sleep_control		= elan_smbus_sleep_control,
5306696777cSDuson Lin 	.power_control		= elan_smbus_power_control,
5316696777cSDuson Lin 	.set_mode		= elan_smbus_set_mode,
5326696777cSDuson Lin 
5336696777cSDuson Lin 	.calibrate		= elan_smbus_calibrate,
5346696777cSDuson Lin 	.calibrate_result	= elan_smbus_calibrate_result,
5356696777cSDuson Lin 
5366696777cSDuson Lin 	.get_baseline_data	= elan_smbus_get_baseline_data,
5376696777cSDuson Lin 
5386696777cSDuson Lin 	.get_version		= elan_smbus_get_version,
5396696777cSDuson Lin 	.get_sm_version		= elan_smbus_get_sm_version,
5406696777cSDuson Lin 	.get_product_id		= elan_smbus_get_product_id,
5416696777cSDuson Lin 	.get_checksum		= elan_smbus_get_checksum,
542b9bced0eSduson 	.get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
5436696777cSDuson Lin 
5446696777cSDuson Lin 	.get_max		= elan_smbus_get_max,
5456696777cSDuson Lin 	.get_resolution		= elan_smbus_get_resolution,
5466696777cSDuson Lin 	.get_num_traces		= elan_smbus_get_num_traces,
5476696777cSDuson Lin 
5486696777cSDuson Lin 	.iap_get_mode		= elan_smbus_iap_get_mode,
5496696777cSDuson Lin 	.iap_reset		= elan_smbus_iap_reset,
5506696777cSDuson Lin 
5516696777cSDuson Lin 	.prepare_fw_update	= elan_smbus_prepare_fw_update,
5526696777cSDuson Lin 	.write_fw_block		= elan_smbus_write_fw_block,
5536696777cSDuson Lin 	.finish_fw_update	= elan_smbus_finish_fw_update,
5546696777cSDuson Lin 
55504d5ce62SJingle Wu 	.get_report_features	= elan_smbus_get_report_features,
5566696777cSDuson Lin 	.get_report		= elan_smbus_get_report,
557a2eaf299SKT Liao 	.get_pattern		= elan_smbus_get_pattern,
5586696777cSDuson Lin };
559