16696777cSDuson Lin /* 26696777cSDuson Lin * Elan I2C/SMBus Touchpad driver - SMBus interface 36696777cSDuson Lin * 46696777cSDuson Lin * Copyright (c) 2013 ELAN Microelectronics Corp. 56696777cSDuson Lin * 66696777cSDuson Lin * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> 76696777cSDuson Lin * 86696777cSDuson Lin * Based on cyapa driver: 96696777cSDuson Lin * copyright (c) 2011-2012 Cypress Semiconductor, Inc. 106696777cSDuson Lin * copyright (c) 2011-2012 Google, Inc. 116696777cSDuson Lin * 126696777cSDuson Lin * This program is free software; you can redistribute it and/or modify it 136696777cSDuson Lin * under the terms of the GNU General Public License version 2 as published 146696777cSDuson Lin * by the Free Software Foundation. 156696777cSDuson Lin * 166696777cSDuson Lin * Trademarks are the property of their respective owners. 176696777cSDuson Lin */ 186696777cSDuson Lin 196696777cSDuson Lin #include <linux/delay.h> 206696777cSDuson Lin #include <linux/i2c.h> 216696777cSDuson Lin #include <linux/init.h> 226696777cSDuson Lin #include <linux/kernel.h> 236696777cSDuson Lin 246696777cSDuson Lin #include "elan_i2c.h" 256696777cSDuson Lin 266696777cSDuson Lin /* Elan SMbus commands */ 276696777cSDuson Lin #define ETP_SMBUS_IAP_CMD 0x00 286696777cSDuson Lin #define ETP_SMBUS_ENABLE_TP 0x20 296696777cSDuson Lin #define ETP_SMBUS_SLEEP_CMD 0x21 306696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD_WRITE 0x29 316696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD_READ 0x80 326696777cSDuson Lin #define ETP_SMBUS_WRITE_FW_BLOCK 0x2A 336696777cSDuson Lin #define ETP_SMBUS_IAP_RESET_CMD 0x2B 346696777cSDuson Lin #define ETP_SMBUS_RANGE_CMD 0xA0 356696777cSDuson Lin #define ETP_SMBUS_FW_VERSION_CMD 0xA1 366696777cSDuson Lin #define ETP_SMBUS_XY_TRACENUM_CMD 0xA2 376696777cSDuson Lin #define ETP_SMBUS_SM_VERSION_CMD 0xA3 386696777cSDuson Lin #define ETP_SMBUS_UNIQUEID_CMD 0xA3 396696777cSDuson Lin #define ETP_SMBUS_RESOLUTION_CMD 0xA4 406696777cSDuson Lin #define ETP_SMBUS_HELLOPACKET_CMD 0xA7 416696777cSDuson Lin #define ETP_SMBUS_PACKET_QUERY 0xA8 426696777cSDuson Lin #define ETP_SMBUS_IAP_VERSION_CMD 0xAC 436696777cSDuson Lin #define ETP_SMBUS_IAP_CTRL_CMD 0xAD 446696777cSDuson Lin #define ETP_SMBUS_IAP_CHECKSUM_CMD 0xAE 456696777cSDuson Lin #define ETP_SMBUS_FW_CHECKSUM_CMD 0xAF 466696777cSDuson Lin #define ETP_SMBUS_MAX_BASELINE_CMD 0xC3 476696777cSDuson Lin #define ETP_SMBUS_MIN_BASELINE_CMD 0xC4 486696777cSDuson Lin #define ETP_SMBUS_CALIBRATE_QUERY 0xC5 496696777cSDuson Lin 506696777cSDuson Lin #define ETP_SMBUS_REPORT_LEN 32 516696777cSDuson Lin #define ETP_SMBUS_REPORT_OFFSET 2 526696777cSDuson Lin #define ETP_SMBUS_HELLOPACKET_LEN 5 536696777cSDuson Lin #define ETP_SMBUS_IAP_PASSWORD 0x1234 546696777cSDuson Lin #define ETP_SMBUS_IAP_MODE_ON (1 << 6) 556696777cSDuson Lin 566696777cSDuson Lin static int elan_smbus_initialize(struct i2c_client *client) 576696777cSDuson Lin { 586696777cSDuson Lin u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 }; 596696777cSDuson Lin u8 values[ETP_SMBUS_HELLOPACKET_LEN] = { 0, 0, 0, 0, 0 }; 606696777cSDuson Lin int len, error; 616696777cSDuson Lin 626696777cSDuson Lin /* Get hello packet */ 636696777cSDuson Lin len = i2c_smbus_read_block_data(client, 646696777cSDuson Lin ETP_SMBUS_HELLOPACKET_CMD, values); 656696777cSDuson Lin if (len != ETP_SMBUS_HELLOPACKET_LEN) { 666696777cSDuson Lin dev_err(&client->dev, "hello packet length fail: %d\n", len); 676696777cSDuson Lin error = len < 0 ? len : -EIO; 686696777cSDuson Lin return error; 696696777cSDuson Lin } 706696777cSDuson Lin 716696777cSDuson Lin /* compare hello packet */ 726696777cSDuson Lin if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) { 7398a4a59eSRasmus Villemoes dev_err(&client->dev, "hello packet fail [%*ph]\n", 746696777cSDuson Lin ETP_SMBUS_HELLOPACKET_LEN, values); 756696777cSDuson Lin return -ENXIO; 766696777cSDuson Lin } 776696777cSDuson Lin 786696777cSDuson Lin /* enable tp */ 796696777cSDuson Lin error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP); 806696777cSDuson Lin if (error) { 816696777cSDuson Lin dev_err(&client->dev, "failed to enable touchpad: %d\n", error); 826696777cSDuson Lin return error; 836696777cSDuson Lin } 846696777cSDuson Lin 856696777cSDuson Lin return 0; 866696777cSDuson Lin } 876696777cSDuson Lin 886696777cSDuson Lin static int elan_smbus_set_mode(struct i2c_client *client, u8 mode) 896696777cSDuson Lin { 906696777cSDuson Lin u8 cmd[4] = { 0x00, 0x07, 0x00, mode }; 916696777cSDuson Lin 926696777cSDuson Lin return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, 936696777cSDuson Lin sizeof(cmd), cmd); 946696777cSDuson Lin } 956696777cSDuson Lin 966696777cSDuson Lin static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep) 976696777cSDuson Lin { 986696777cSDuson Lin if (sleep) 996696777cSDuson Lin return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD); 1006696777cSDuson Lin else 1016696777cSDuson Lin return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */ 1026696777cSDuson Lin } 1036696777cSDuson Lin 1046696777cSDuson Lin static int elan_smbus_power_control(struct i2c_client *client, bool enable) 1056696777cSDuson Lin { 1066696777cSDuson Lin return 0; /* A no-op */ 1076696777cSDuson Lin } 1086696777cSDuson Lin 1096696777cSDuson Lin static int elan_smbus_calibrate(struct i2c_client *client) 1106696777cSDuson Lin { 1116696777cSDuson Lin u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 }; 1126696777cSDuson Lin 1136696777cSDuson Lin return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, 1146696777cSDuson Lin sizeof(cmd), cmd); 1156696777cSDuson Lin } 1166696777cSDuson Lin 1176696777cSDuson Lin static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val) 1186696777cSDuson Lin { 1196696777cSDuson Lin int error; 1206696777cSDuson Lin 1216696777cSDuson Lin error = i2c_smbus_read_block_data(client, 1226696777cSDuson Lin ETP_SMBUS_CALIBRATE_QUERY, val); 1236696777cSDuson Lin if (error < 0) 1246696777cSDuson Lin return error; 1256696777cSDuson Lin 1266696777cSDuson Lin return 0; 1276696777cSDuson Lin } 1286696777cSDuson Lin 1296696777cSDuson Lin static int elan_smbus_get_baseline_data(struct i2c_client *client, 1306696777cSDuson Lin bool max_baseline, u8 *value) 1316696777cSDuson Lin { 1326696777cSDuson Lin int error; 1336696777cSDuson Lin u8 val[3]; 1346696777cSDuson Lin 1356696777cSDuson Lin error = i2c_smbus_read_block_data(client, 1366696777cSDuson Lin max_baseline ? 1376696777cSDuson Lin ETP_SMBUS_MAX_BASELINE_CMD : 1386696777cSDuson Lin ETP_SMBUS_MIN_BASELINE_CMD, 1396696777cSDuson Lin val); 1406696777cSDuson Lin if (error < 0) 1416696777cSDuson Lin return error; 1426696777cSDuson Lin 1436696777cSDuson Lin *value = be16_to_cpup((__be16 *)val); 1446696777cSDuson Lin 1456696777cSDuson Lin return 0; 1466696777cSDuson Lin } 1476696777cSDuson Lin 1486696777cSDuson Lin static int elan_smbus_get_version(struct i2c_client *client, 1496696777cSDuson Lin bool iap, u8 *version) 1506696777cSDuson Lin { 1516696777cSDuson Lin int error; 1526696777cSDuson Lin u8 val[3]; 1536696777cSDuson Lin 1546696777cSDuson Lin error = i2c_smbus_read_block_data(client, 1556696777cSDuson Lin iap ? ETP_SMBUS_IAP_VERSION_CMD : 1566696777cSDuson Lin ETP_SMBUS_FW_VERSION_CMD, 1576696777cSDuson Lin val); 1586696777cSDuson Lin if (error < 0) { 1596696777cSDuson Lin dev_err(&client->dev, "failed to get %s version: %d\n", 1606696777cSDuson Lin iap ? "IAP" : "FW", error); 1616696777cSDuson Lin return error; 1626696777cSDuson Lin } 1636696777cSDuson Lin 1646696777cSDuson Lin *version = val[2]; 1656696777cSDuson Lin return 0; 1666696777cSDuson Lin } 1676696777cSDuson Lin 16812018ac3SDuson Lin static int elan_smbus_get_sm_version(struct i2c_client *client, 169*a2eaf299SKT Liao u16 *ic_type, u8 *version) 1706696777cSDuson Lin { 1716696777cSDuson Lin int error; 1726696777cSDuson Lin u8 val[3]; 1736696777cSDuson Lin 1746696777cSDuson Lin error = i2c_smbus_read_block_data(client, 1756696777cSDuson Lin ETP_SMBUS_SM_VERSION_CMD, val); 1766696777cSDuson Lin if (error < 0) { 1776696777cSDuson Lin dev_err(&client->dev, "failed to get SM version: %d\n", error); 1786696777cSDuson Lin return error; 1796696777cSDuson Lin } 1806696777cSDuson Lin 18112018ac3SDuson Lin *version = val[0]; 18212018ac3SDuson Lin *ic_type = val[1]; 1836696777cSDuson Lin return 0; 1846696777cSDuson Lin } 1856696777cSDuson Lin 186ed75a14eSDuson Lin static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id) 1876696777cSDuson Lin { 1886696777cSDuson Lin int error; 1896696777cSDuson Lin u8 val[3]; 1906696777cSDuson Lin 1916696777cSDuson Lin error = i2c_smbus_read_block_data(client, 1926696777cSDuson Lin ETP_SMBUS_UNIQUEID_CMD, val); 1936696777cSDuson Lin if (error < 0) { 1946696777cSDuson Lin dev_err(&client->dev, "failed to get product ID: %d\n", error); 1956696777cSDuson Lin return error; 1966696777cSDuson Lin } 1976696777cSDuson Lin 198ed75a14eSDuson Lin *id = be16_to_cpup((__be16 *)val); 1996696777cSDuson Lin return 0; 2006696777cSDuson Lin } 2016696777cSDuson Lin 2026696777cSDuson Lin static int elan_smbus_get_checksum(struct i2c_client *client, 2036696777cSDuson Lin bool iap, u16 *csum) 2046696777cSDuson Lin { 2056696777cSDuson Lin int error; 2066696777cSDuson Lin u8 val[3]; 2076696777cSDuson Lin 2086696777cSDuson Lin error = i2c_smbus_read_block_data(client, 2096696777cSDuson Lin iap ? ETP_SMBUS_FW_CHECKSUM_CMD : 2106696777cSDuson Lin ETP_SMBUS_IAP_CHECKSUM_CMD, 2116696777cSDuson Lin val); 2126696777cSDuson Lin if (error < 0) { 2136696777cSDuson Lin dev_err(&client->dev, "failed to get %s checksum: %d\n", 2146696777cSDuson Lin iap ? "IAP" : "FW", error); 2156696777cSDuson Lin return error; 2166696777cSDuson Lin } 2176696777cSDuson Lin 2186696777cSDuson Lin *csum = be16_to_cpup((__be16 *)val); 2196696777cSDuson Lin return 0; 2206696777cSDuson Lin } 2216696777cSDuson Lin 2226696777cSDuson Lin static int elan_smbus_get_max(struct i2c_client *client, 2236696777cSDuson Lin unsigned int *max_x, unsigned int *max_y) 2246696777cSDuson Lin { 225265d426dSBenjamin Tissoires int ret; 2266696777cSDuson Lin int error; 2276696777cSDuson Lin u8 val[3]; 2286696777cSDuson Lin 229265d426dSBenjamin Tissoires ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val); 230265d426dSBenjamin Tissoires if (ret != 3) { 231265d426dSBenjamin Tissoires error = ret < 0 ? ret : -EIO; 2326696777cSDuson Lin dev_err(&client->dev, "failed to get dimensions: %d\n", error); 2336696777cSDuson Lin return error; 2346696777cSDuson Lin } 2356696777cSDuson Lin 2366696777cSDuson Lin *max_x = (0x0f & val[0]) << 8 | val[1]; 2376696777cSDuson Lin *max_y = (0xf0 & val[0]) << 4 | val[2]; 2386696777cSDuson Lin 2396696777cSDuson Lin return 0; 2406696777cSDuson Lin } 2416696777cSDuson Lin 2426696777cSDuson Lin static int elan_smbus_get_resolution(struct i2c_client *client, 2436696777cSDuson Lin u8 *hw_res_x, u8 *hw_res_y) 2446696777cSDuson Lin { 245265d426dSBenjamin Tissoires int ret; 2466696777cSDuson Lin int error; 2476696777cSDuson Lin u8 val[3]; 2486696777cSDuson Lin 249265d426dSBenjamin Tissoires ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val); 250265d426dSBenjamin Tissoires if (ret != 3) { 251265d426dSBenjamin Tissoires error = ret < 0 ? ret : -EIO; 2526696777cSDuson Lin dev_err(&client->dev, "failed to get resolution: %d\n", error); 2536696777cSDuson Lin return error; 2546696777cSDuson Lin } 2556696777cSDuson Lin 2566696777cSDuson Lin *hw_res_x = val[1] & 0x0F; 2576696777cSDuson Lin *hw_res_y = (val[1] & 0xF0) >> 4; 2586696777cSDuson Lin 2596696777cSDuson Lin return 0; 2606696777cSDuson Lin } 2616696777cSDuson Lin 2626696777cSDuson Lin static int elan_smbus_get_num_traces(struct i2c_client *client, 2636696777cSDuson Lin unsigned int *x_traces, 2646696777cSDuson Lin unsigned int *y_traces) 2656696777cSDuson Lin { 266265d426dSBenjamin Tissoires int ret; 2676696777cSDuson Lin int error; 2686696777cSDuson Lin u8 val[3]; 2696696777cSDuson Lin 270265d426dSBenjamin Tissoires ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val); 271265d426dSBenjamin Tissoires if (ret != 3) { 272265d426dSBenjamin Tissoires error = ret < 0 ? ret : -EIO; 2736696777cSDuson Lin dev_err(&client->dev, "failed to get trace info: %d\n", error); 2746696777cSDuson Lin return error; 2756696777cSDuson Lin } 2766696777cSDuson Lin 2779f423801SDuson Lin *x_traces = val[1]; 2789f423801SDuson Lin *y_traces = val[2]; 2796696777cSDuson Lin 2806696777cSDuson Lin return 0; 2816696777cSDuson Lin } 2826696777cSDuson Lin 283b9bced0eSduson static int elan_smbus_get_pressure_adjustment(struct i2c_client *client, 284b9bced0eSduson int *adjustment) 285b9bced0eSduson { 286b9bced0eSduson *adjustment = ETP_PRESSURE_OFFSET; 287b9bced0eSduson return 0; 288b9bced0eSduson } 289b9bced0eSduson 2906696777cSDuson Lin static int elan_smbus_iap_get_mode(struct i2c_client *client, 2916696777cSDuson Lin enum tp_mode *mode) 2926696777cSDuson Lin { 2936696777cSDuson Lin int error; 2946696777cSDuson Lin u16 constant; 2956696777cSDuson Lin u8 val[3]; 2966696777cSDuson Lin 2976696777cSDuson Lin error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val); 2986696777cSDuson Lin if (error < 0) { 2996696777cSDuson Lin dev_err(&client->dev, "failed to read iap ctrol register: %d\n", 3006696777cSDuson Lin error); 3016696777cSDuson Lin return error; 3026696777cSDuson Lin } 3036696777cSDuson Lin 3046696777cSDuson Lin constant = be16_to_cpup((__be16 *)val); 3056696777cSDuson Lin dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant); 3066696777cSDuson Lin 3076696777cSDuson Lin *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE; 3086696777cSDuson Lin 3096696777cSDuson Lin return 0; 3106696777cSDuson Lin } 3116696777cSDuson Lin 3126696777cSDuson Lin static int elan_smbus_iap_reset(struct i2c_client *client) 3136696777cSDuson Lin { 3146696777cSDuson Lin int error; 3156696777cSDuson Lin 3166696777cSDuson Lin error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD); 3176696777cSDuson Lin if (error) { 3186696777cSDuson Lin dev_err(&client->dev, "cannot reset IC: %d\n", error); 3196696777cSDuson Lin return error; 3206696777cSDuson Lin } 3216696777cSDuson Lin 3226696777cSDuson Lin return 0; 3236696777cSDuson Lin } 3246696777cSDuson Lin 3256696777cSDuson Lin static int elan_smbus_set_flash_key(struct i2c_client *client) 3266696777cSDuson Lin { 3276696777cSDuson Lin int error; 3286696777cSDuson Lin u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A }; 3296696777cSDuson Lin 3306696777cSDuson Lin error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, 3316696777cSDuson Lin sizeof(cmd), cmd); 3326696777cSDuson Lin if (error) { 3336696777cSDuson Lin dev_err(&client->dev, "cannot set flash key: %d\n", error); 3346696777cSDuson Lin return error; 3356696777cSDuson Lin } 3366696777cSDuson Lin 3376696777cSDuson Lin return 0; 3386696777cSDuson Lin } 3396696777cSDuson Lin 3406696777cSDuson Lin static int elan_smbus_prepare_fw_update(struct i2c_client *client) 3416696777cSDuson Lin { 3426696777cSDuson Lin struct device *dev = &client->dev; 3436696777cSDuson Lin int len; 3446696777cSDuson Lin int error; 3456696777cSDuson Lin enum tp_mode mode; 3466696777cSDuson Lin u8 val[3]; 3476696777cSDuson Lin u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06}; 3486696777cSDuson Lin u16 password; 3496696777cSDuson Lin 3506696777cSDuson Lin /* Get FW in which mode (IAP_MODE/MAIN_MODE) */ 3516696777cSDuson Lin error = elan_smbus_iap_get_mode(client, &mode); 3526696777cSDuson Lin if (error) 3536696777cSDuson Lin return error; 3546696777cSDuson Lin 3556696777cSDuson Lin if (mode == MAIN_MODE) { 3566696777cSDuson Lin 3576696777cSDuson Lin /* set flash key */ 3586696777cSDuson Lin error = elan_smbus_set_flash_key(client); 3596696777cSDuson Lin if (error) 3606696777cSDuson Lin return error; 3616696777cSDuson Lin 3626696777cSDuson Lin /* write iap password */ 3636696777cSDuson Lin if (i2c_smbus_write_byte(client, 3646696777cSDuson Lin ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) { 3656696777cSDuson Lin dev_err(dev, "cannot write iap password\n"); 3666696777cSDuson Lin return -EIO; 3676696777cSDuson Lin } 3686696777cSDuson Lin 3696696777cSDuson Lin error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, 3706696777cSDuson Lin sizeof(cmd), cmd); 3716696777cSDuson Lin if (error) { 3726696777cSDuson Lin dev_err(dev, "failed to write iap password: %d\n", 3736696777cSDuson Lin error); 3746696777cSDuson Lin return error; 3756696777cSDuson Lin } 3766696777cSDuson Lin 3776696777cSDuson Lin /* 3786696777cSDuson Lin * Read back password to make sure we enabled flash 3796696777cSDuson Lin * successfully. 3806696777cSDuson Lin */ 3816696777cSDuson Lin len = i2c_smbus_read_block_data(client, 3826696777cSDuson Lin ETP_SMBUS_IAP_PASSWORD_READ, 3836696777cSDuson Lin val); 3846696777cSDuson Lin if (len < sizeof(u16)) { 3856696777cSDuson Lin error = len < 0 ? len : -EIO; 3866696777cSDuson Lin dev_err(dev, "failed to read iap password: %d\n", 3876696777cSDuson Lin error); 3886696777cSDuson Lin return error; 3896696777cSDuson Lin } 3906696777cSDuson Lin 3916696777cSDuson Lin password = be16_to_cpup((__be16 *)val); 3926696777cSDuson Lin if (password != ETP_SMBUS_IAP_PASSWORD) { 3936696777cSDuson Lin dev_err(dev, "wrong iap password = 0x%X\n", password); 3946696777cSDuson Lin return -EIO; 3956696777cSDuson Lin } 3966696777cSDuson Lin 3976696777cSDuson Lin /* Wait 30ms for MAIN_MODE change to IAP_MODE */ 3986696777cSDuson Lin msleep(30); 3996696777cSDuson Lin } 4006696777cSDuson Lin 4016696777cSDuson Lin error = elan_smbus_set_flash_key(client); 4026696777cSDuson Lin if (error) 4036696777cSDuson Lin return error; 4046696777cSDuson Lin 4056696777cSDuson Lin /* Reset IC */ 4066696777cSDuson Lin error = elan_smbus_iap_reset(client); 4076696777cSDuson Lin if (error) 4086696777cSDuson Lin return error; 4096696777cSDuson Lin 4106696777cSDuson Lin return 0; 4116696777cSDuson Lin } 4126696777cSDuson Lin 4136696777cSDuson Lin 4146696777cSDuson Lin static int elan_smbus_write_fw_block(struct i2c_client *client, 4156696777cSDuson Lin const u8 *page, u16 checksum, int idx) 4166696777cSDuson Lin { 4176696777cSDuson Lin struct device *dev = &client->dev; 4186696777cSDuson Lin int error; 4196696777cSDuson Lin u16 result; 4206696777cSDuson Lin u8 val[3]; 4216696777cSDuson Lin 4226696777cSDuson Lin /* 4236696777cSDuson Lin * Due to the limitation of smbus protocol limiting 4246696777cSDuson Lin * transfer to 32 bytes at a time, we must split block 4256696777cSDuson Lin * in 2 transfers. 4266696777cSDuson Lin */ 4276696777cSDuson Lin error = i2c_smbus_write_block_data(client, 4286696777cSDuson Lin ETP_SMBUS_WRITE_FW_BLOCK, 4296696777cSDuson Lin ETP_FW_PAGE_SIZE / 2, 4306696777cSDuson Lin page); 4316696777cSDuson Lin if (error) { 4326696777cSDuson Lin dev_err(dev, "Failed to write page %d (part %d): %d\n", 4336696777cSDuson Lin idx, 1, error); 4346696777cSDuson Lin return error; 4356696777cSDuson Lin } 4366696777cSDuson Lin 4376696777cSDuson Lin error = i2c_smbus_write_block_data(client, 4386696777cSDuson Lin ETP_SMBUS_WRITE_FW_BLOCK, 4396696777cSDuson Lin ETP_FW_PAGE_SIZE / 2, 4406696777cSDuson Lin page + ETP_FW_PAGE_SIZE / 2); 4416696777cSDuson Lin if (error) { 4426696777cSDuson Lin dev_err(dev, "Failed to write page %d (part %d): %d\n", 4436696777cSDuson Lin idx, 2, error); 4446696777cSDuson Lin return error; 4456696777cSDuson Lin } 4466696777cSDuson Lin 4476696777cSDuson Lin 4486696777cSDuson Lin /* Wait for F/W to update one page ROM data. */ 4496696777cSDuson Lin usleep_range(8000, 10000); 4506696777cSDuson Lin 4516696777cSDuson Lin error = i2c_smbus_read_block_data(client, 4526696777cSDuson Lin ETP_SMBUS_IAP_CTRL_CMD, val); 4536696777cSDuson Lin if (error < 0) { 4546696777cSDuson Lin dev_err(dev, "Failed to read IAP write result: %d\n", 4556696777cSDuson Lin error); 4566696777cSDuson Lin return error; 4576696777cSDuson Lin } 4586696777cSDuson Lin 4596696777cSDuson Lin result = be16_to_cpup((__be16 *)val); 4606696777cSDuson Lin if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) { 4616696777cSDuson Lin dev_err(dev, "IAP reports failed write: %04hx\n", 4626696777cSDuson Lin result); 4636696777cSDuson Lin return -EIO; 4646696777cSDuson Lin } 4656696777cSDuson Lin 4666696777cSDuson Lin return 0; 4676696777cSDuson Lin } 4686696777cSDuson Lin 4696696777cSDuson Lin static int elan_smbus_get_report(struct i2c_client *client, u8 *report) 4706696777cSDuson Lin { 4716696777cSDuson Lin int len; 4726696777cSDuson Lin 4736696777cSDuson Lin len = i2c_smbus_read_block_data(client, 4746696777cSDuson Lin ETP_SMBUS_PACKET_QUERY, 4756696777cSDuson Lin &report[ETP_SMBUS_REPORT_OFFSET]); 4766696777cSDuson Lin if (len < 0) { 4776696777cSDuson Lin dev_err(&client->dev, "failed to read report data: %d\n", len); 4786696777cSDuson Lin return len; 4796696777cSDuson Lin } 4806696777cSDuson Lin 4816696777cSDuson Lin if (len != ETP_SMBUS_REPORT_LEN) { 4826696777cSDuson Lin dev_err(&client->dev, 4836696777cSDuson Lin "wrong report length (%d vs %d expected)\n", 4846696777cSDuson Lin len, ETP_SMBUS_REPORT_LEN); 4856696777cSDuson Lin return -EIO; 4866696777cSDuson Lin } 4876696777cSDuson Lin 4886696777cSDuson Lin return 0; 4896696777cSDuson Lin } 4906696777cSDuson Lin 4916696777cSDuson Lin static int elan_smbus_finish_fw_update(struct i2c_client *client, 4926696777cSDuson Lin struct completion *fw_completion) 4936696777cSDuson Lin { 4946696777cSDuson Lin /* No special handling unlike I2C transport */ 4956696777cSDuson Lin return 0; 4966696777cSDuson Lin } 4976696777cSDuson Lin 498*a2eaf299SKT Liao static int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern) 499*a2eaf299SKT Liao { 500*a2eaf299SKT Liao *pattern = 0; 501*a2eaf299SKT Liao return 0; 502*a2eaf299SKT Liao } 503*a2eaf299SKT Liao 5046696777cSDuson Lin const struct elan_transport_ops elan_smbus_ops = { 5056696777cSDuson Lin .initialize = elan_smbus_initialize, 5066696777cSDuson Lin .sleep_control = elan_smbus_sleep_control, 5076696777cSDuson Lin .power_control = elan_smbus_power_control, 5086696777cSDuson Lin .set_mode = elan_smbus_set_mode, 5096696777cSDuson Lin 5106696777cSDuson Lin .calibrate = elan_smbus_calibrate, 5116696777cSDuson Lin .calibrate_result = elan_smbus_calibrate_result, 5126696777cSDuson Lin 5136696777cSDuson Lin .get_baseline_data = elan_smbus_get_baseline_data, 5146696777cSDuson Lin 5156696777cSDuson Lin .get_version = elan_smbus_get_version, 5166696777cSDuson Lin .get_sm_version = elan_smbus_get_sm_version, 5176696777cSDuson Lin .get_product_id = elan_smbus_get_product_id, 5186696777cSDuson Lin .get_checksum = elan_smbus_get_checksum, 519b9bced0eSduson .get_pressure_adjustment = elan_smbus_get_pressure_adjustment, 5206696777cSDuson Lin 5216696777cSDuson Lin .get_max = elan_smbus_get_max, 5226696777cSDuson Lin .get_resolution = elan_smbus_get_resolution, 5236696777cSDuson Lin .get_num_traces = elan_smbus_get_num_traces, 5246696777cSDuson Lin 5256696777cSDuson Lin .iap_get_mode = elan_smbus_iap_get_mode, 5266696777cSDuson Lin .iap_reset = elan_smbus_iap_reset, 5276696777cSDuson Lin 5286696777cSDuson Lin .prepare_fw_update = elan_smbus_prepare_fw_update, 5296696777cSDuson Lin .write_fw_block = elan_smbus_write_fw_block, 5306696777cSDuson Lin .finish_fw_update = elan_smbus_finish_fw_update, 5316696777cSDuson Lin 5326696777cSDuson Lin .get_report = elan_smbus_get_report, 533*a2eaf299SKT Liao .get_pattern = elan_smbus_get_pattern, 5346696777cSDuson Lin }; 535