xref: /openbmc/linux/drivers/input/mouse/elan_i2c_smbus.c (revision 04d5ce620f794f1df69b5f1b9ad62910fea547f1)
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
486696777cSDuson Lin #define ETP_SMBUS_REPORT_OFFSET		2
496696777cSDuson Lin #define ETP_SMBUS_HELLOPACKET_LEN	5
506696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD		0x1234
516696777cSDuson Lin #define ETP_SMBUS_IAP_MODE_ON		(1 << 6)
526696777cSDuson Lin 
536696777cSDuson Lin static int elan_smbus_initialize(struct i2c_client *client)
546696777cSDuson Lin {
556696777cSDuson Lin 	u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 };
5650fc7b61SBen Hutchings 	u8 values[I2C_SMBUS_BLOCK_MAX] = {0};
576696777cSDuson Lin 	int len, error;
586696777cSDuson Lin 
596696777cSDuson Lin 	/* Get hello packet */
606696777cSDuson Lin 	len = i2c_smbus_read_block_data(client,
616696777cSDuson Lin 					ETP_SMBUS_HELLOPACKET_CMD, values);
626696777cSDuson Lin 	if (len != ETP_SMBUS_HELLOPACKET_LEN) {
636696777cSDuson Lin 		dev_err(&client->dev, "hello packet length fail: %d\n", len);
646696777cSDuson Lin 		error = len < 0 ? len : -EIO;
656696777cSDuson Lin 		return error;
666696777cSDuson Lin 	}
676696777cSDuson Lin 
686696777cSDuson Lin 	/* compare hello packet */
696696777cSDuson Lin 	if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) {
7098a4a59eSRasmus Villemoes 		dev_err(&client->dev, "hello packet fail [%*ph]\n",
716696777cSDuson Lin 			ETP_SMBUS_HELLOPACKET_LEN, values);
726696777cSDuson Lin 		return -ENXIO;
736696777cSDuson Lin 	}
746696777cSDuson Lin 
756696777cSDuson Lin 	/* enable tp */
766696777cSDuson Lin 	error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP);
776696777cSDuson Lin 	if (error) {
786696777cSDuson Lin 		dev_err(&client->dev, "failed to enable touchpad: %d\n", error);
796696777cSDuson Lin 		return error;
806696777cSDuson Lin 	}
816696777cSDuson Lin 
826696777cSDuson Lin 	return 0;
836696777cSDuson Lin }
846696777cSDuson Lin 
856696777cSDuson Lin static int elan_smbus_set_mode(struct i2c_client *client, u8 mode)
866696777cSDuson Lin {
876696777cSDuson Lin 	u8 cmd[4] = { 0x00, 0x07, 0x00, mode };
886696777cSDuson Lin 
896696777cSDuson Lin 	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
906696777cSDuson Lin 					  sizeof(cmd), cmd);
916696777cSDuson Lin }
926696777cSDuson Lin 
936696777cSDuson Lin static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep)
946696777cSDuson Lin {
956696777cSDuson Lin 	if (sleep)
966696777cSDuson Lin 		return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD);
976696777cSDuson Lin 	else
986696777cSDuson Lin 		return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */
996696777cSDuson Lin }
1006696777cSDuson Lin 
1016696777cSDuson Lin static int elan_smbus_power_control(struct i2c_client *client, bool enable)
1026696777cSDuson Lin {
1036696777cSDuson Lin 	return 0; /* A no-op */
1046696777cSDuson Lin }
1056696777cSDuson Lin 
1066696777cSDuson Lin static int elan_smbus_calibrate(struct i2c_client *client)
1076696777cSDuson Lin {
1086696777cSDuson Lin 	u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 };
1096696777cSDuson Lin 
1106696777cSDuson Lin 	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
1116696777cSDuson Lin 					  sizeof(cmd), cmd);
1126696777cSDuson Lin }
1136696777cSDuson Lin 
1146696777cSDuson Lin static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val)
1156696777cSDuson Lin {
1166696777cSDuson Lin 	int error;
11750fc7b61SBen Hutchings 	u8 buf[I2C_SMBUS_BLOCK_MAX] = {0};
11850fc7b61SBen Hutchings 
11950fc7b61SBen Hutchings 	BUILD_BUG_ON(ETP_CALIBRATE_MAX_LEN > sizeof(buf));
1206696777cSDuson Lin 
1216696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
12250fc7b61SBen Hutchings 					  ETP_SMBUS_CALIBRATE_QUERY, buf);
1236696777cSDuson Lin 	if (error < 0)
1246696777cSDuson Lin 		return error;
1256696777cSDuson Lin 
12650fc7b61SBen Hutchings 	memcpy(val, buf, ETP_CALIBRATE_MAX_LEN);
1276696777cSDuson Lin 	return 0;
1286696777cSDuson Lin }
1296696777cSDuson Lin 
1306696777cSDuson Lin static int elan_smbus_get_baseline_data(struct i2c_client *client,
1316696777cSDuson Lin 					bool max_baseline, u8 *value)
1326696777cSDuson Lin {
1336696777cSDuson Lin 	int error;
13440f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1356696777cSDuson Lin 
1366696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1376696777cSDuson Lin 					  max_baseline ?
1386696777cSDuson Lin 						ETP_SMBUS_MAX_BASELINE_CMD :
1396696777cSDuson Lin 						ETP_SMBUS_MIN_BASELINE_CMD,
1406696777cSDuson Lin 					  val);
1416696777cSDuson Lin 	if (error < 0)
1426696777cSDuson Lin 		return error;
1436696777cSDuson Lin 
1446696777cSDuson Lin 	*value = be16_to_cpup((__be16 *)val);
1456696777cSDuson Lin 
1466696777cSDuson Lin 	return 0;
1476696777cSDuson Lin }
1486696777cSDuson Lin 
1496696777cSDuson Lin static int elan_smbus_get_version(struct i2c_client *client,
1503d712af6SDmitry Torokhov 				  u8 pattern, bool iap, u8 *version)
1516696777cSDuson Lin {
1526696777cSDuson Lin 	int error;
15340f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1546696777cSDuson Lin 
1556696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1566696777cSDuson Lin 					  iap ? ETP_SMBUS_IAP_VERSION_CMD :
1576696777cSDuson Lin 						ETP_SMBUS_FW_VERSION_CMD,
1586696777cSDuson Lin 					  val);
1596696777cSDuson Lin 	if (error < 0) {
1606696777cSDuson Lin 		dev_err(&client->dev, "failed to get %s version: %d\n",
1616696777cSDuson Lin 			iap ? "IAP" : "FW", error);
1626696777cSDuson Lin 		return error;
1636696777cSDuson Lin 	}
1646696777cSDuson Lin 
1656696777cSDuson Lin 	*version = val[2];
1666696777cSDuson Lin 	return 0;
1676696777cSDuson Lin }
1686696777cSDuson Lin 
1693d712af6SDmitry Torokhov static int elan_smbus_get_sm_version(struct i2c_client *client, u8 pattern,
1703d712af6SDmitry Torokhov 				     u16 *ic_type, u8 *version, u8 *clickpad)
1716696777cSDuson Lin {
1726696777cSDuson Lin 	int error;
17340f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1746696777cSDuson Lin 
1756696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1766696777cSDuson Lin 					  ETP_SMBUS_SM_VERSION_CMD, val);
1776696777cSDuson Lin 	if (error < 0) {
1786696777cSDuson Lin 		dev_err(&client->dev, "failed to get SM version: %d\n", error);
1796696777cSDuson Lin 		return error;
1806696777cSDuson Lin 	}
1816696777cSDuson Lin 
18212018ac3SDuson Lin 	*version = val[0];
18312018ac3SDuson Lin 	*ic_type = val[1];
18499136881SKT Liao 	*clickpad = val[0] & 0x10;
1856696777cSDuson Lin 	return 0;
1866696777cSDuson Lin }
1876696777cSDuson Lin 
188ed75a14eSDuson Lin static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
1896696777cSDuson Lin {
1906696777cSDuson Lin 	int error;
19140f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
1926696777cSDuson Lin 
1936696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
1946696777cSDuson Lin 					  ETP_SMBUS_UNIQUEID_CMD, val);
1956696777cSDuson Lin 	if (error < 0) {
1966696777cSDuson Lin 		dev_err(&client->dev, "failed to get product ID: %d\n", error);
1976696777cSDuson Lin 		return error;
1986696777cSDuson Lin 	}
1996696777cSDuson Lin 
200ed75a14eSDuson Lin 	*id = be16_to_cpup((__be16 *)val);
2016696777cSDuson Lin 	return 0;
2026696777cSDuson Lin }
2036696777cSDuson Lin 
2046696777cSDuson Lin static int elan_smbus_get_checksum(struct i2c_client *client,
2056696777cSDuson Lin 				   bool iap, u16 *csum)
2066696777cSDuson Lin {
2076696777cSDuson Lin 	int error;
20840f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2096696777cSDuson Lin 
2106696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
2116696777cSDuson Lin 					  iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
2126696777cSDuson Lin 						ETP_SMBUS_IAP_CHECKSUM_CMD,
2136696777cSDuson Lin 					  val);
2146696777cSDuson Lin 	if (error < 0) {
2156696777cSDuson Lin 		dev_err(&client->dev, "failed to get %s checksum: %d\n",
2166696777cSDuson Lin 			iap ? "IAP" : "FW", error);
2176696777cSDuson Lin 		return error;
2186696777cSDuson Lin 	}
2196696777cSDuson Lin 
2206696777cSDuson Lin 	*csum = be16_to_cpup((__be16 *)val);
2216696777cSDuson Lin 	return 0;
2226696777cSDuson Lin }
2236696777cSDuson Lin 
2246696777cSDuson Lin static int elan_smbus_get_max(struct i2c_client *client,
2256696777cSDuson Lin 			      unsigned int *max_x, unsigned int *max_y)
2266696777cSDuson Lin {
227265d426dSBenjamin Tissoires 	int ret;
2286696777cSDuson Lin 	int error;
22940f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2306696777cSDuson Lin 
231265d426dSBenjamin Tissoires 	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
232265d426dSBenjamin Tissoires 	if (ret != 3) {
233265d426dSBenjamin Tissoires 		error = ret < 0 ? ret : -EIO;
2346696777cSDuson Lin 		dev_err(&client->dev, "failed to get dimensions: %d\n", error);
2356696777cSDuson Lin 		return error;
2366696777cSDuson Lin 	}
2376696777cSDuson Lin 
2386696777cSDuson Lin 	*max_x = (0x0f & val[0]) << 8 | val[1];
2396696777cSDuson Lin 	*max_y = (0xf0 & val[0]) << 4 | val[2];
2406696777cSDuson Lin 
2416696777cSDuson Lin 	return 0;
2426696777cSDuson Lin }
2436696777cSDuson Lin 
2446696777cSDuson Lin static int elan_smbus_get_resolution(struct i2c_client *client,
2456696777cSDuson Lin 				     u8 *hw_res_x, u8 *hw_res_y)
2466696777cSDuson Lin {
247265d426dSBenjamin Tissoires 	int ret;
2486696777cSDuson Lin 	int error;
24940f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2506696777cSDuson Lin 
251265d426dSBenjamin Tissoires 	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val);
252265d426dSBenjamin Tissoires 	if (ret != 3) {
253265d426dSBenjamin Tissoires 		error = ret < 0 ? ret : -EIO;
2546696777cSDuson Lin 		dev_err(&client->dev, "failed to get resolution: %d\n", error);
2556696777cSDuson Lin 		return error;
2566696777cSDuson Lin 	}
2576696777cSDuson Lin 
2586696777cSDuson Lin 	*hw_res_x = val[1] & 0x0F;
2596696777cSDuson Lin 	*hw_res_y = (val[1] & 0xF0) >> 4;
2606696777cSDuson Lin 
2616696777cSDuson Lin 	return 0;
2626696777cSDuson Lin }
2636696777cSDuson Lin 
2646696777cSDuson Lin static int elan_smbus_get_num_traces(struct i2c_client *client,
2656696777cSDuson Lin 				     unsigned int *x_traces,
2666696777cSDuson Lin 				     unsigned int *y_traces)
2676696777cSDuson Lin {
268265d426dSBenjamin Tissoires 	int ret;
2696696777cSDuson Lin 	int error;
27040f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2716696777cSDuson Lin 
272265d426dSBenjamin Tissoires 	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val);
273265d426dSBenjamin Tissoires 	if (ret != 3) {
274265d426dSBenjamin Tissoires 		error = ret < 0 ? ret : -EIO;
2756696777cSDuson Lin 		dev_err(&client->dev, "failed to get trace info: %d\n", error);
2766696777cSDuson Lin 		return error;
2776696777cSDuson Lin 	}
2786696777cSDuson Lin 
2799f423801SDuson Lin 	*x_traces = val[1];
2809f423801SDuson Lin 	*y_traces = val[2];
2816696777cSDuson Lin 
2826696777cSDuson Lin 	return 0;
2836696777cSDuson Lin }
2846696777cSDuson Lin 
285b9bced0eSduson static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
286b9bced0eSduson 					      int *adjustment)
287b9bced0eSduson {
288b9bced0eSduson 	*adjustment = ETP_PRESSURE_OFFSET;
289b9bced0eSduson 	return 0;
290b9bced0eSduson }
291b9bced0eSduson 
2926696777cSDuson Lin static int elan_smbus_iap_get_mode(struct i2c_client *client,
2936696777cSDuson Lin 				   enum tp_mode *mode)
2946696777cSDuson Lin {
2956696777cSDuson Lin 	int error;
2966696777cSDuson Lin 	u16 constant;
29740f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
2986696777cSDuson Lin 
2996696777cSDuson Lin 	error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
3006696777cSDuson Lin 	if (error < 0) {
3016696777cSDuson Lin 		dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
3026696777cSDuson Lin 			error);
3036696777cSDuson Lin 		return error;
3046696777cSDuson Lin 	}
3056696777cSDuson Lin 
3066696777cSDuson Lin 	constant = be16_to_cpup((__be16 *)val);
3076696777cSDuson Lin 	dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
3086696777cSDuson Lin 
3096696777cSDuson Lin 	*mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
3106696777cSDuson Lin 
3116696777cSDuson Lin 	return 0;
3126696777cSDuson Lin }
3136696777cSDuson Lin 
3146696777cSDuson Lin static int elan_smbus_iap_reset(struct i2c_client *client)
3156696777cSDuson Lin {
3166696777cSDuson Lin 	int error;
3176696777cSDuson Lin 
3186696777cSDuson Lin 	error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
3196696777cSDuson Lin 	if (error) {
3206696777cSDuson Lin 		dev_err(&client->dev, "cannot reset IC: %d\n", error);
3216696777cSDuson Lin 		return error;
3226696777cSDuson Lin 	}
3236696777cSDuson Lin 
3246696777cSDuson Lin 	return 0;
3256696777cSDuson Lin }
3266696777cSDuson Lin 
3276696777cSDuson Lin static int elan_smbus_set_flash_key(struct i2c_client *client)
3286696777cSDuson Lin {
3296696777cSDuson Lin 	int error;
3306696777cSDuson Lin 	u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
3316696777cSDuson Lin 
3326696777cSDuson Lin 	error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
3336696777cSDuson Lin 					   sizeof(cmd), cmd);
3346696777cSDuson Lin 	if (error) {
3356696777cSDuson Lin 		dev_err(&client->dev, "cannot set flash key: %d\n", error);
3366696777cSDuson Lin 		return error;
3376696777cSDuson Lin 	}
3386696777cSDuson Lin 
3396696777cSDuson Lin 	return 0;
3406696777cSDuson Lin }
3416696777cSDuson Lin 
342bfd9b92bSJingle Wu static int elan_smbus_prepare_fw_update(struct i2c_client *client, u16 ic_type,
343bfd9b92bSJingle Wu 					u8 iap_version)
3446696777cSDuson Lin {
3456696777cSDuson Lin 	struct device *dev = &client->dev;
3466696777cSDuson Lin 	int len;
3476696777cSDuson Lin 	int error;
3486696777cSDuson Lin 	enum tp_mode mode;
34940f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
3506696777cSDuson Lin 	u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
3516696777cSDuson Lin 	u16 password;
3526696777cSDuson Lin 
3536696777cSDuson Lin 	/* Get FW in which mode	(IAP_MODE/MAIN_MODE)  */
3546696777cSDuson Lin 	error = elan_smbus_iap_get_mode(client, &mode);
3556696777cSDuson Lin 	if (error)
3566696777cSDuson Lin 		return error;
3576696777cSDuson Lin 
3586696777cSDuson Lin 	if (mode == MAIN_MODE) {
3596696777cSDuson Lin 
3606696777cSDuson Lin 		/* set flash key */
3616696777cSDuson Lin 		error = elan_smbus_set_flash_key(client);
3626696777cSDuson Lin 		if (error)
3636696777cSDuson Lin 			return error;
3646696777cSDuson Lin 
3656696777cSDuson Lin 		/* write iap password */
3666696777cSDuson Lin 		if (i2c_smbus_write_byte(client,
3676696777cSDuson Lin 					 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
3686696777cSDuson Lin 			dev_err(dev, "cannot write iap password\n");
3696696777cSDuson Lin 			return -EIO;
3706696777cSDuson Lin 		}
3716696777cSDuson Lin 
3726696777cSDuson Lin 		error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
3736696777cSDuson Lin 						   sizeof(cmd), cmd);
3746696777cSDuson Lin 		if (error) {
3756696777cSDuson Lin 			dev_err(dev, "failed to write iap password: %d\n",
3766696777cSDuson Lin 				error);
3776696777cSDuson Lin 			return error;
3786696777cSDuson Lin 		}
3796696777cSDuson Lin 
3806696777cSDuson Lin 		/*
3816696777cSDuson Lin 		 * Read back password to make sure we enabled flash
3826696777cSDuson Lin 		 * successfully.
3836696777cSDuson Lin 		 */
3846696777cSDuson Lin 		len = i2c_smbus_read_block_data(client,
3856696777cSDuson Lin 						ETP_SMBUS_IAP_PASSWORD_READ,
3866696777cSDuson Lin 						val);
387ce1d6f22SJulia Lawall 		if (len < (int)sizeof(u16)) {
3886696777cSDuson Lin 			error = len < 0 ? len : -EIO;
3896696777cSDuson Lin 			dev_err(dev, "failed to read iap password: %d\n",
3906696777cSDuson Lin 				error);
3916696777cSDuson Lin 			return error;
3926696777cSDuson Lin 		}
3936696777cSDuson Lin 
3946696777cSDuson Lin 		password = be16_to_cpup((__be16 *)val);
3956696777cSDuson Lin 		if (password != ETP_SMBUS_IAP_PASSWORD) {
3966696777cSDuson Lin 			dev_err(dev, "wrong iap password = 0x%X\n", password);
3976696777cSDuson Lin 			return -EIO;
3986696777cSDuson Lin 		}
3996696777cSDuson Lin 
4006696777cSDuson Lin 		/* Wait 30ms for MAIN_MODE change to IAP_MODE */
4016696777cSDuson Lin 		msleep(30);
4026696777cSDuson Lin 	}
4036696777cSDuson Lin 
4046696777cSDuson Lin 	error = elan_smbus_set_flash_key(client);
4056696777cSDuson Lin 	if (error)
4066696777cSDuson Lin 		return error;
4076696777cSDuson Lin 
4086696777cSDuson Lin 	/* Reset IC */
4096696777cSDuson Lin 	error = elan_smbus_iap_reset(client);
4106696777cSDuson Lin 	if (error)
4116696777cSDuson Lin 		return error;
4126696777cSDuson Lin 
4136696777cSDuson Lin 	return 0;
4146696777cSDuson Lin }
4156696777cSDuson Lin 
4166696777cSDuson Lin 
417059d6c2dSJingle Wu static int elan_smbus_write_fw_block(struct i2c_client *client, u16 fw_page_size,
4186696777cSDuson Lin 				     const u8 *page, u16 checksum, int idx)
4196696777cSDuson Lin {
4206696777cSDuson Lin 	struct device *dev = &client->dev;
4216696777cSDuson Lin 	int error;
4226696777cSDuson Lin 	u16 result;
42340f7090bSBenjamin Tissoires 	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
4246696777cSDuson Lin 
4256696777cSDuson Lin 	/*
4266696777cSDuson Lin 	 * Due to the limitation of smbus protocol limiting
4276696777cSDuson Lin 	 * transfer to 32 bytes at a time, we must split block
4286696777cSDuson Lin 	 * in 2 transfers.
4296696777cSDuson Lin 	 */
4306696777cSDuson Lin 	error = i2c_smbus_write_block_data(client,
4316696777cSDuson Lin 					   ETP_SMBUS_WRITE_FW_BLOCK,
432059d6c2dSJingle Wu 					   fw_page_size / 2,
4336696777cSDuson Lin 					   page);
4346696777cSDuson Lin 	if (error) {
4356696777cSDuson Lin 		dev_err(dev, "Failed to write page %d (part %d): %d\n",
4366696777cSDuson Lin 			idx, 1, error);
4376696777cSDuson Lin 		return error;
4386696777cSDuson Lin 	}
4396696777cSDuson Lin 
4406696777cSDuson Lin 	error = i2c_smbus_write_block_data(client,
4416696777cSDuson Lin 					   ETP_SMBUS_WRITE_FW_BLOCK,
442059d6c2dSJingle Wu 					   fw_page_size / 2,
443059d6c2dSJingle Wu 					   page + fw_page_size / 2);
4446696777cSDuson Lin 	if (error) {
4456696777cSDuson Lin 		dev_err(dev, "Failed to write page %d (part %d): %d\n",
4466696777cSDuson Lin 			idx, 2, error);
4476696777cSDuson Lin 		return error;
4486696777cSDuson Lin 	}
4496696777cSDuson Lin 
4506696777cSDuson Lin 
4516696777cSDuson Lin 	/* Wait for F/W to update one page ROM data. */
4526696777cSDuson Lin 	usleep_range(8000, 10000);
4536696777cSDuson Lin 
4546696777cSDuson Lin 	error = i2c_smbus_read_block_data(client,
4556696777cSDuson Lin 					  ETP_SMBUS_IAP_CTRL_CMD, val);
4566696777cSDuson Lin 	if (error < 0) {
4576696777cSDuson Lin 		dev_err(dev, "Failed to read IAP write result: %d\n",
4586696777cSDuson Lin 			error);
4596696777cSDuson Lin 		return error;
4606696777cSDuson Lin 	}
4616696777cSDuson Lin 
4626696777cSDuson Lin 	result = be16_to_cpup((__be16 *)val);
4636696777cSDuson Lin 	if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
4646696777cSDuson Lin 		dev_err(dev, "IAP reports failed write: %04hx\n",
4656696777cSDuson Lin 			result);
4666696777cSDuson Lin 		return -EIO;
4676696777cSDuson Lin 	}
4686696777cSDuson Lin 
4696696777cSDuson Lin 	return 0;
4706696777cSDuson Lin }
4716696777cSDuson Lin 
472*04d5ce62SJingle Wu static int elan_smbus_get_report_features(struct i2c_client *client, u8 pattern,
473*04d5ce62SJingle Wu 					  unsigned int *features,
474*04d5ce62SJingle Wu 					  unsigned int *report_len)
475*04d5ce62SJingle Wu {
476*04d5ce62SJingle Wu 	/*
477*04d5ce62SJingle Wu 	 * SMBus controllers with pattern 2 lack area info, as newer
478*04d5ce62SJingle Wu 	 * high-precision packets use that space for coordinates.
479*04d5ce62SJingle Wu 	 */
480*04d5ce62SJingle Wu 	*features = pattern <= 0x01 ? ETP_FEATURE_REPORT_MK : 0;
481*04d5ce62SJingle Wu 	*report_len = ETP_SMBUS_REPORT_LEN;
482*04d5ce62SJingle Wu 	return 0;
483*04d5ce62SJingle Wu }
484*04d5ce62SJingle Wu 
485*04d5ce62SJingle Wu static int elan_smbus_get_report(struct i2c_client *client,
486*04d5ce62SJingle Wu 				 u8 *report, unsigned int report_len)
4876696777cSDuson Lin {
4886696777cSDuson Lin 	int len;
4896696777cSDuson Lin 
49050fc7b61SBen Hutchings 	BUILD_BUG_ON(I2C_SMBUS_BLOCK_MAX > ETP_SMBUS_REPORT_LEN);
49150fc7b61SBen Hutchings 
4926696777cSDuson Lin 	len = i2c_smbus_read_block_data(client,
4936696777cSDuson Lin 					ETP_SMBUS_PACKET_QUERY,
4946696777cSDuson Lin 					&report[ETP_SMBUS_REPORT_OFFSET]);
4956696777cSDuson Lin 	if (len < 0) {
4966696777cSDuson Lin 		dev_err(&client->dev, "failed to read report data: %d\n", len);
4976696777cSDuson Lin 		return len;
4986696777cSDuson Lin 	}
4996696777cSDuson Lin 
5006696777cSDuson Lin 	if (len != ETP_SMBUS_REPORT_LEN) {
5016696777cSDuson Lin 		dev_err(&client->dev,
5026696777cSDuson Lin 			"wrong report length (%d vs %d expected)\n",
5036696777cSDuson Lin 			len, ETP_SMBUS_REPORT_LEN);
5046696777cSDuson Lin 		return -EIO;
5056696777cSDuson Lin 	}
5066696777cSDuson Lin 
5076696777cSDuson Lin 	return 0;
5086696777cSDuson Lin }
5096696777cSDuson Lin 
5106696777cSDuson Lin static int elan_smbus_finish_fw_update(struct i2c_client *client,
5116696777cSDuson Lin 				       struct completion *fw_completion)
5126696777cSDuson Lin {
5136696777cSDuson Lin 	/* No special handling unlike I2C transport */
5146696777cSDuson Lin 	return 0;
5156696777cSDuson Lin }
5166696777cSDuson Lin 
517a2eaf299SKT Liao static int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern)
518a2eaf299SKT Liao {
519a2eaf299SKT Liao 	*pattern = 0;
520a2eaf299SKT Liao 	return 0;
521a2eaf299SKT Liao }
522a2eaf299SKT Liao 
5236696777cSDuson Lin const struct elan_transport_ops elan_smbus_ops = {
5246696777cSDuson Lin 	.initialize		= elan_smbus_initialize,
5256696777cSDuson Lin 	.sleep_control		= elan_smbus_sleep_control,
5266696777cSDuson Lin 	.power_control		= elan_smbus_power_control,
5276696777cSDuson Lin 	.set_mode		= elan_smbus_set_mode,
5286696777cSDuson Lin 
5296696777cSDuson Lin 	.calibrate		= elan_smbus_calibrate,
5306696777cSDuson Lin 	.calibrate_result	= elan_smbus_calibrate_result,
5316696777cSDuson Lin 
5326696777cSDuson Lin 	.get_baseline_data	= elan_smbus_get_baseline_data,
5336696777cSDuson Lin 
5346696777cSDuson Lin 	.get_version		= elan_smbus_get_version,
5356696777cSDuson Lin 	.get_sm_version		= elan_smbus_get_sm_version,
5366696777cSDuson Lin 	.get_product_id		= elan_smbus_get_product_id,
5376696777cSDuson Lin 	.get_checksum		= elan_smbus_get_checksum,
538b9bced0eSduson 	.get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
5396696777cSDuson Lin 
5406696777cSDuson Lin 	.get_max		= elan_smbus_get_max,
5416696777cSDuson Lin 	.get_resolution		= elan_smbus_get_resolution,
5426696777cSDuson Lin 	.get_num_traces		= elan_smbus_get_num_traces,
5436696777cSDuson Lin 
5446696777cSDuson Lin 	.iap_get_mode		= elan_smbus_iap_get_mode,
5456696777cSDuson Lin 	.iap_reset		= elan_smbus_iap_reset,
5466696777cSDuson Lin 
5476696777cSDuson Lin 	.prepare_fw_update	= elan_smbus_prepare_fw_update,
5486696777cSDuson Lin 	.write_fw_block		= elan_smbus_write_fw_block,
5496696777cSDuson Lin 	.finish_fw_update	= elan_smbus_finish_fw_update,
5506696777cSDuson Lin 
551*04d5ce62SJingle Wu 	.get_report_features	= elan_smbus_get_report_features,
5526696777cSDuson Lin 	.get_report		= elan_smbus_get_report,
553a2eaf299SKT Liao 	.get_pattern		= elan_smbus_get_pattern,
5546696777cSDuson Lin };
555