xref: /openbmc/linux/drivers/input/mouse/elan_i2c_smbus.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas 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,
1506696777cSDuson Lin 				  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 
16912018ac3SDuson Lin static int elan_smbus_get_sm_version(struct i2c_client *client,
17099136881SKT Liao 				     u16 *ic_type, u8 *version,
17199136881SKT Liao 				     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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
3436696777cSDuson Lin static int elan_smbus_prepare_fw_update(struct i2c_client *client)
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 
4176696777cSDuson Lin static int elan_smbus_write_fw_block(struct i2c_client *client,
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,
4326696777cSDuson Lin 					   ETP_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,
4426696777cSDuson Lin 					   ETP_FW_PAGE_SIZE / 2,
4436696777cSDuson Lin 					   page + ETP_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 
4726696777cSDuson Lin static int elan_smbus_get_report(struct i2c_client *client, u8 *report)
4736696777cSDuson Lin {
4746696777cSDuson Lin 	int len;
4756696777cSDuson Lin 
47650fc7b61SBen Hutchings 	BUILD_BUG_ON(I2C_SMBUS_BLOCK_MAX > ETP_SMBUS_REPORT_LEN);
47750fc7b61SBen Hutchings 
4786696777cSDuson Lin 	len = i2c_smbus_read_block_data(client,
4796696777cSDuson Lin 					ETP_SMBUS_PACKET_QUERY,
4806696777cSDuson Lin 					&report[ETP_SMBUS_REPORT_OFFSET]);
4816696777cSDuson Lin 	if (len < 0) {
4826696777cSDuson Lin 		dev_err(&client->dev, "failed to read report data: %d\n", len);
4836696777cSDuson Lin 		return len;
4846696777cSDuson Lin 	}
4856696777cSDuson Lin 
4866696777cSDuson Lin 	if (len != ETP_SMBUS_REPORT_LEN) {
4876696777cSDuson Lin 		dev_err(&client->dev,
4886696777cSDuson Lin 			"wrong report length (%d vs %d expected)\n",
4896696777cSDuson Lin 			len, ETP_SMBUS_REPORT_LEN);
4906696777cSDuson Lin 		return -EIO;
4916696777cSDuson Lin 	}
4926696777cSDuson Lin 
4936696777cSDuson Lin 	return 0;
4946696777cSDuson Lin }
4956696777cSDuson Lin 
4966696777cSDuson Lin static int elan_smbus_finish_fw_update(struct i2c_client *client,
4976696777cSDuson Lin 				       struct completion *fw_completion)
4986696777cSDuson Lin {
4996696777cSDuson Lin 	/* No special handling unlike I2C transport */
5006696777cSDuson Lin 	return 0;
5016696777cSDuson Lin }
5026696777cSDuson Lin 
503a2eaf299SKT Liao static int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern)
504a2eaf299SKT Liao {
505a2eaf299SKT Liao 	*pattern = 0;
506a2eaf299SKT Liao 	return 0;
507a2eaf299SKT Liao }
508a2eaf299SKT Liao 
5096696777cSDuson Lin const struct elan_transport_ops elan_smbus_ops = {
5106696777cSDuson Lin 	.initialize		= elan_smbus_initialize,
5116696777cSDuson Lin 	.sleep_control		= elan_smbus_sleep_control,
5126696777cSDuson Lin 	.power_control		= elan_smbus_power_control,
5136696777cSDuson Lin 	.set_mode		= elan_smbus_set_mode,
5146696777cSDuson Lin 
5156696777cSDuson Lin 	.calibrate		= elan_smbus_calibrate,
5166696777cSDuson Lin 	.calibrate_result	= elan_smbus_calibrate_result,
5176696777cSDuson Lin 
5186696777cSDuson Lin 	.get_baseline_data	= elan_smbus_get_baseline_data,
5196696777cSDuson Lin 
5206696777cSDuson Lin 	.get_version		= elan_smbus_get_version,
5216696777cSDuson Lin 	.get_sm_version		= elan_smbus_get_sm_version,
5226696777cSDuson Lin 	.get_product_id		= elan_smbus_get_product_id,
5236696777cSDuson Lin 	.get_checksum		= elan_smbus_get_checksum,
524b9bced0eSduson 	.get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
5256696777cSDuson Lin 
5266696777cSDuson Lin 	.get_max		= elan_smbus_get_max,
5276696777cSDuson Lin 	.get_resolution		= elan_smbus_get_resolution,
5286696777cSDuson Lin 	.get_num_traces		= elan_smbus_get_num_traces,
5296696777cSDuson Lin 
5306696777cSDuson Lin 	.iap_get_mode		= elan_smbus_iap_get_mode,
5316696777cSDuson Lin 	.iap_reset		= elan_smbus_iap_reset,
5326696777cSDuson Lin 
5336696777cSDuson Lin 	.prepare_fw_update	= elan_smbus_prepare_fw_update,
5346696777cSDuson Lin 	.write_fw_block		= elan_smbus_write_fw_block,
5356696777cSDuson Lin 	.finish_fw_update	= elan_smbus_finish_fw_update,
5366696777cSDuson Lin 
5376696777cSDuson Lin 	.get_report		= elan_smbus_get_report,
538a2eaf299SKT Liao 	.get_pattern		= elan_smbus_get_pattern,
5396696777cSDuson Lin };
540