cregit-Linux how code gets into the kernel

Release 4.11 drivers/input/touchscreen/elants_i2c.c

/*
 * Elan Microelectronics touch panels with I2C interface
 *
 * Copyright (C) 2014 Elan Microelectronics Corporation.
 * Scott Liu <scott.liu@emc.com.tw>
 *
 * This code is partly based on hid-multitouch.c:
 *
 *  Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
 *  Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
 *  Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
 *
 *
 * This code is partly based on i2c-hid.c:
 *
 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
 * Copyright (c) 2012 Red Hat, Inc
 */

/*
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 */

#include <linux/module.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/async.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/firmware.h>
#include <linux/input/mt.h>
#include <linux/acpi.h>
#include <linux/of.h>
#include <linux/gpio/consumer.h>
#include <linux/regulator/consumer.h>
#include <asm/unaligned.h>

/* Device, Driver information */

#define DEVICE_NAME	"elants_i2c"

#define DRV_VERSION	"1.0.9"

/* Convert from rows or columns into resolution */

#define ELAN_TS_RESOLUTION(n, m)   (((n) - 1) * (m))

/* FW header data */

#define HEADER_SIZE		4

#define FW_HDR_TYPE		0

#define FW_HDR_COUNT		1

#define FW_HDR_LENGTH		2

/* Buffer mode Queue Header information */

#define QUEUE_HEADER_SINGLE	0x62

#define QUEUE_HEADER_NORMAL	0X63

#define QUEUE_HEADER_WAIT	0x64

/* Command header definition */

#define CMD_HEADER_WRITE	0x54

#define CMD_HEADER_READ		0x53

#define CMD_HEADER_6B_READ	0x5B

#define CMD_HEADER_RESP		0x52

#define CMD_HEADER_6B_RESP	0x9B

#define CMD_HEADER_HELLO	0x55

#define CMD_HEADER_REK		0x66

/* FW position data */

#define PACKET_SIZE		55

#define MAX_CONTACT_NUM		10

#define FW_POS_HEADER		0

#define FW_POS_STATE		1

#define FW_POS_TOTAL		2

#define FW_POS_XY		3

#define FW_POS_CHECKSUM		34

#define FW_POS_WIDTH		35

#define FW_POS_PRESSURE		45


#define HEADER_REPORT_10_FINGER	0x62

/* Header (4 bytes) plus 3 fill 10-finger packets */

#define MAX_PACKET_SIZE		169


#define BOOT_TIME_DELAY_MS	50

/* FW read command, 0x53 0x?? 0x0, 0x01 */

#define E_ELAN_INFO_FW_VER	0x00

#define E_ELAN_INFO_BC_VER	0x10

#define E_ELAN_INFO_TEST_VER	0xE0

#define E_ELAN_INFO_FW_ID	0xF0

#define E_INFO_OSR		0xD6

#define E_INFO_PHY_SCAN		0xD7

#define E_INFO_PHY_DRIVER	0xD8


#define MAX_RETRIES		3

#define MAX_FW_UPDATE_RETRIES	30


#define ELAN_FW_PAGESIZE	132

/* calibration timeout definition */

#define ELAN_CALI_TIMEOUT_MSEC	12000


#define ELAN_POWERON_DELAY_USEC	500

#define ELAN_RESET_DELAY_MSEC	20


enum elants_state {
	
ELAN_STATE_NORMAL,
	
ELAN_WAIT_QUEUE_HEADER,
	
ELAN_WAIT_RECALIBRATION,
};


enum elants_iap_mode {
	
ELAN_IAP_OPERATIONAL,
	
ELAN_IAP_RECOVERY,
};

/* struct elants_data - represents state of Elan touchscreen device */

struct elants_data {
	
struct i2c_client *client;
	
struct input_dev *input;

	
struct regulator *vcc33;
	
struct regulator *vccio;
	
struct gpio_desc *reset_gpio;

	
u16 fw_version;
	
u8 test_version;
	
u8 solution_version;
	
u8 bc_version;
	
u8 iap_version;
	
u16 hw_version;
	
unsigned int x_res;	/* resolution in units/mm */
	
unsigned int y_res;
	
unsigned int x_max;
	
unsigned int y_max;

	
enum elants_state state;
	
enum elants_iap_mode iap_mode;

	/* Guards against concurrent access to the device via sysfs */
	
struct mutex sysfs_mutex;

	
u8 cmd_resp[HEADER_SIZE];
	
struct completion cmd_done;

	
u8 buf[MAX_PACKET_SIZE];

	
bool wake_irq_enabled;
	
bool keep_power_in_suspend;
};


static int elants_i2c_send(struct i2c_client *client, const void *data, size_t size) { int ret; ret = i2c_master_send(client, data, size); if (ret == size) return 0; if (ret >= 0) ret = -EIO; dev_err(&client->dev, "%s failed (%*ph): %d\n", __func__, (int)size, data, ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu77100.00%1100.00%
Total77100.00%1100.00%


static int elants_i2c_read(struct i2c_client *client, void *data, size_t size) { int ret; ret = i2c_master_recv(client, data, size); if (ret == size) return 0; if (ret >= 0) ret = -EIO; dev_err(&client->dev, "%s failed: %d\n", __func__, ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu69100.00%1100.00%
Total69100.00%1100.00%


static int elants_i2c_execute_command(struct i2c_client *client, const u8 *cmd, size_t cmd_size, u8 *resp, size_t resp_size) { struct i2c_msg msgs[2]; int ret; u8 expected_response; switch (cmd[0]) { case CMD_HEADER_READ: expected_response = CMD_HEADER_RESP; break; case CMD_HEADER_6B_READ: expected_response = CMD_HEADER_6B_RESP; break; default: dev_err(&client->dev, "%s: invalid command %*ph\n", __func__, (int)cmd_size, cmd); return -EINVAL; } msgs[0].addr = client->addr; msgs[0].flags = client->flags & I2C_M_TEN; msgs[0].len = cmd_size; msgs[0].buf = (u8 *)cmd; msgs[1].addr = client->addr; msgs[1].flags = client->flags & I2C_M_TEN; msgs[1].flags |= I2C_M_RD; msgs[1].len = resp_size; msgs[1].buf = resp; ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); if (ret < 0) return ret; if (ret != ARRAY_SIZE(msgs) || resp[FW_HDR_TYPE] != expected_response) return -EIO; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu233100.00%1100.00%
Total233100.00%1100.00%


static int elants_i2c_calibrate(struct elants_data *ts) { struct i2c_client *client = ts->client; int ret, error; static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A }; static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 }; static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 }; disable_irq(client->irq); ts->state = ELAN_WAIT_RECALIBRATION; reinit_completion(&ts->cmd_done); elants_i2c_send(client, w_flashkey, sizeof(w_flashkey)); elants_i2c_send(client, rek, sizeof(rek)); enable_irq(client->irq); ret = wait_for_completion_interruptible_timeout(&ts->cmd_done, msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC)); ts->state = ELAN_STATE_NORMAL; if (ret <= 0) { error = ret < 0 ? ret : -ETIMEDOUT; dev_err(&client->dev, "error while waiting for calibration to complete: %d\n", error); return error; } if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) { dev_err(&client->dev, "unexpected calibration response: %*ph\n", (int)sizeof(ts->cmd_resp), ts->cmd_resp); return -EINVAL; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu229100.00%1100.00%
Total229100.00%1100.00%


static int elants_i2c_sw_reset(struct i2c_client *client) { const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 }; int error; error = elants_i2c_send(client, soft_rst_cmd, sizeof(soft_rst_cmd)); if (error) { dev_err(&client->dev, "software reset failed: %d\n", error); return error; } /* * We should wait at least 10 msec (but no more than 40) before * sending fastboot or IAP command to the device. */ msleep(30); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu73100.00%1100.00%
Total73100.00%1100.00%


static u16 elants_i2c_parse_version(u8 *buf) { return get_unaligned_be32(buf) >> 4; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu18100.00%1100.00%
Total18100.00%1100.00%


static int elants_i2c_query_hw_version(struct elants_data *ts) { struct i2c_client *client = ts->client; int error, retry_cnt; const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 }; u8 resp[HEADER_SIZE]; for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { error = elants_i2c_execute_command(client, cmd, sizeof(cmd), resp, sizeof(resp)); if (!error) { ts->hw_version = elants_i2c_parse_version(resp); if (ts->hw_version != 0xffff) return 0; } dev_dbg(&client->dev, "read fw id error=%d, buf=%*phC\n", error, (int)sizeof(resp), resp); } if (error) { dev_err(&client->dev, "Failed to read fw id: %d\n", error); return error; } dev_err(&client->dev, "Invalid fw id: %#04x\n", ts->hw_version); return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu14384.12%150.00%
Johnny Chuang2715.88%150.00%
Total170100.00%2100.00%


static int elants_i2c_query_fw_version(struct elants_data *ts) { struct i2c_client *client = ts->client; int error, retry_cnt; const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 }; u8 resp[HEADER_SIZE]; for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { error = elants_i2c_execute_command(client, cmd, sizeof(cmd), resp, sizeof(resp)); if (!error) { ts->fw_version = elants_i2c_parse_version(resp); if (ts->fw_version != 0x0000 && ts->fw_version != 0xffff) return 0; } dev_dbg(&client->dev, "read fw version error=%d, buf=%*phC\n", error, (int)sizeof(resp), resp); } dev_err(&client->dev, "Failed to read fw version or fw version is invalid\n"); return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu151100.00%1100.00%
Total151100.00%1100.00%


static int elants_i2c_query_test_version(struct elants_data *ts) { struct i2c_client *client = ts->client; int error, retry_cnt; u16 version; const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 }; u8 resp[HEADER_SIZE]; for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { error = elants_i2c_execute_command(client, cmd, sizeof(cmd), resp, sizeof(resp)); if (!error) { version = elants_i2c_parse_version(resp); ts->test_version = version >> 8; ts->solution_version = version & 0xff; return 0; } dev_dbg(&client->dev, "read test version error rc=%d, buf=%*phC\n", error, (int)sizeof(resp), resp); } dev_err(&client->dev, "Failed to read test version\n"); return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu154100.00%1100.00%
Total154100.00%1100.00%


static int elants_i2c_query_bc_version(struct elants_data *ts) { struct i2c_client *client = ts->client; const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 }; u8 resp[HEADER_SIZE]; u16 version; int error; error = elants_i2c_execute_command(client, cmd, sizeof(cmd), resp, sizeof(resp)); if (error) { dev_err(&client->dev, "read BC version error=%d, buf=%*phC\n", error, (int)sizeof(resp), resp); return error; } version = elants_i2c_parse_version(resp); ts->bc_version = version >> 8; ts->iap_version = version & 0xff; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu125100.00%1100.00%
Total125100.00%1100.00%


static int elants_i2c_query_ts_info(struct elants_data *ts) { struct i2c_client *client = ts->client; int error; u8 resp[17]; u16 phy_x, phy_y, rows, cols, osr; const u8 get_resolution_cmd[] = { CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00 }; const u8 get_osr_cmd[] = { CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01 }; const u8 get_physical_scan_cmd[] = { CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01 }; const u8 get_physical_drive_cmd[] = { CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01 }; /* Get trace number */ error = elants_i2c_execute_command(client, get_resolution_cmd, sizeof(get_resolution_cmd), resp, sizeof(resp)); if (error) { dev_err(&client->dev, "get resolution command failed: %d\n", error); return error; } rows = resp[2] + resp[6] + resp[10]; cols = resp[3] + resp[7] + resp[11]; /* Process mm_to_pixel information */ error = elants_i2c_execute_command(client, get_osr_cmd, sizeof(get_osr_cmd), resp, sizeof(resp)); if (error) { dev_err(&client->dev, "get osr command failed: %d\n", error); return error; } osr = resp[3]; error = elants_i2c_execute_command(client, get_physical_scan_cmd, sizeof(get_physical_scan_cmd), resp, sizeof(resp)); if (error) { dev_err(&client->dev, "get physical scan command failed: %d\n", error); return error; } phy_x = get_unaligned_be16(&resp[2]); error = elants_i2c_execute_command(client, get_physical_drive_cmd, sizeof(get_physical_drive_cmd), resp, sizeof(resp)); if (error) { dev_err(&client->dev, "get physical drive command failed: %d\n", error); return error; } phy_y = get_unaligned_be16(&resp[2]); dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y); if (rows == 0 || cols == 0 || osr == 0) { dev_warn(&client->dev, "invalid trace number data: %d, %d, %d\n", rows, cols, osr); } else { /* translate trace number to TS resolution */ ts->x_max = ELAN_TS_RESOLUTION(rows, osr); ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x); ts->y_max = ELAN_TS_RESOLUTION(cols, osr); ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu438100.00%1100.00%
Total438100.00%1100.00%


static int elants_i2c_fastboot(struct i2c_client *client) { const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E }; int error; error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd)); if (error) { dev_err(&client->dev, "boot failed: %d\n", error); return error; } dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu81100.00%1100.00%
Total81100.00%1100.00%


static int elants_i2c_initialize(struct elants_data *ts) { struct i2c_client *client = ts->client; int error, error2, retry_cnt; const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 }; const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 }; u8 buf[HEADER_SIZE]; for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { error = elants_i2c_sw_reset(client); if (error) { /* Continue initializing if it's the last try */ if (retry_cnt < MAX_RETRIES - 1) continue; } error = elants_i2c_fastboot(client); if (error) { /* Continue initializing if it's the last try */ if (retry_cnt < MAX_RETRIES - 1) continue; } /* Wait for Hello packet */ msleep(BOOT_TIME_DELAY_MS); error = elants_i2c_read(client, buf, sizeof(buf)); if (error) { dev_err(&client->dev, "failed to read 'hello' packet: %d\n", error); } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) { ts->iap_mode = ELAN_IAP_OPERATIONAL; break; } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) { /* * Setting error code will mark device * in recovery mode below. */ error = -EIO; break; } else { error = -EINVAL; dev_err(&client->dev, "invalid 'hello' packet: %*ph\n", (int)sizeof(buf), buf); } } /* hw version is available even if device in recovery state */ error2 = elants_i2c_query_hw_version(ts); if (!error) error = error2; if (!error) error = elants_i2c_query_fw_version(ts); if (!error) error = elants_i2c_query_test_version(ts); if (!error) error = elants_i2c_query_bc_version(ts); if (!error) error = elants_i2c_query_ts_info(ts); if (error) ts->iap_mode = ELAN_IAP_RECOVERY; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu28188.36%150.00%
Johnny Chuang3711.64%150.00%
Total318100.00%2100.00%

/* * Firmware update interface. */
static int elants_i2c_fw_write_page(struct i2c_client *client, const void *page) { const u8 ack_ok[] = { 0xaa, 0xaa }; u8 buf[2]; int retry; int error; for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) { error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE); if (error) { dev_err(&client->dev, "IAP Write Page failed: %d\n", error); continue; } error = elants_i2c_read(client, buf, 2); if (error) { dev_err(&client->dev, "IAP Ack read failed: %d\n", error); return error; } if (!memcmp(buf, ack_ok, sizeof(ack_ok))) return 0; error = -EIO; dev_err(&client->dev, "IAP Get Ack Error [%02x:%02x]\n", buf[0], buf[1]); } return error; }

Contributors

PersonTokensPropCommitsCommitProp
Scott Liu162100.00%1100.00%
Total162100.00%1100.00%


static int elants_i2c_do_update_firmware(struct i2c_client *client, const struct firmware *fw, bool force) { const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 }; const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 }; const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc }; const u8 close_idle[] = {0x54, 0x2c, 0x01, 0x01}; u8 buf[HEADER_SIZE]; u16 send_id; int page, n_fw_pages; int error; /* Recovery mode detection! */ if (force) { dev_dbg(&client->dev, "Recovery mode procedure\n"); error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2)); } else { /* Start IAP Procedure */ dev_dbg(&client->dev, "Normal IAP procedure\n"); /* Close idle mode */ error = elants_i2c_send(client, close_idle, sizeof(close_idle)); if (error) dev_err(&client->dev, "Failed close idle: %d\n", error); msleep(60); elants_i2c_sw_reset(client); msleep(20); error = elants_i2c_send(client, enter_iap, sizeof(enter_iap)); } if (error) { dev_err(&client->dev, "failed to enter IAP mode: %d\n", error); return error; } msleep(20); /* check IAP state */ error = elants_i2c_read(client, buf, 4); if (error) { dev_err(&client->dev, "failed to read IAP acknowledgement: %d\n", error); return error; } if (memcmp(buf, iap_ack, sizeof(iap_ack))) { dev_err(&client->dev, "failed to enter IAP: %*ph (expected %*ph)\n", (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack); return -EIO; } dev_info(&client->dev, "successfully entered IAP mode"); send_id = client->addr; error = elants_i2c_send(client, &send_id, 1); if (error) { dev_err(&client->dev, "sending dummy byte failed: %d\n", error); return