Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Elan I2C/SMBus Touchpad driver - SMBus interface
  4 *
  5 * Copyright (c) 2013 ELAN Microelectronics Corp.
  6 *
  7 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
  8 *
  9 * Based on cyapa driver:
 10 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
 11 * copyright (c) 2011-2012 Google, Inc.
 12 *
 
 
 
 
 13 * Trademarks are the property of their respective owners.
 14 */
 15
 16#include <linux/delay.h>
 17#include <linux/i2c.h>
 18#include <linux/init.h>
 19#include <linux/kernel.h>
 20
 21#include "elan_i2c.h"
 22
 23/* Elan SMbus commands */
 24#define ETP_SMBUS_IAP_CMD		0x00
 25#define ETP_SMBUS_ENABLE_TP		0x20
 26#define ETP_SMBUS_SLEEP_CMD		0x21
 27#define ETP_SMBUS_IAP_PASSWORD_WRITE	0x29
 28#define ETP_SMBUS_IAP_PASSWORD_READ	0x80
 29#define ETP_SMBUS_WRITE_FW_BLOCK	0x2A
 30#define ETP_SMBUS_IAP_RESET_CMD		0x2B
 31#define ETP_SMBUS_RANGE_CMD		0xA0
 32#define ETP_SMBUS_FW_VERSION_CMD	0xA1
 33#define ETP_SMBUS_XY_TRACENUM_CMD	0xA2
 34#define ETP_SMBUS_SM_VERSION_CMD	0xA3
 35#define ETP_SMBUS_UNIQUEID_CMD		0xA3
 36#define ETP_SMBUS_RESOLUTION_CMD	0xA4
 37#define ETP_SMBUS_HELLOPACKET_CMD	0xA7
 38#define ETP_SMBUS_PACKET_QUERY		0xA8
 39#define ETP_SMBUS_IAP_VERSION_CMD	0xAC
 40#define ETP_SMBUS_IAP_CTRL_CMD		0xAD
 41#define ETP_SMBUS_IAP_CHECKSUM_CMD	0xAE
 42#define ETP_SMBUS_FW_CHECKSUM_CMD	0xAF
 43#define ETP_SMBUS_MAX_BASELINE_CMD	0xC3
 44#define ETP_SMBUS_MIN_BASELINE_CMD	0xC4
 45#define ETP_SMBUS_CALIBRATE_QUERY	0xC5
 46
 47#define ETP_SMBUS_REPORT_LEN		32
 48#define ETP_SMBUS_REPORT_LEN2		7
 49#define ETP_SMBUS_REPORT_OFFSET		2
 50#define ETP_SMBUS_HELLOPACKET_LEN	5
 51#define ETP_SMBUS_IAP_PASSWORD		0x1234
 52#define ETP_SMBUS_IAP_MODE_ON		(1 << 6)
 53
 54static int elan_smbus_initialize(struct i2c_client *client)
 55{
 56	u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 };
 57	u8 values[I2C_SMBUS_BLOCK_MAX] = {0};
 58	int len, error;
 59
 60	/* Get hello packet */
 61	len = i2c_smbus_read_block_data(client,
 62					ETP_SMBUS_HELLOPACKET_CMD, values);
 63	if (len != ETP_SMBUS_HELLOPACKET_LEN) {
 64		dev_err(&client->dev, "hello packet length fail: %d\n", len);
 65		error = len < 0 ? len : -EIO;
 66		return error;
 67	}
 68
 69	/* compare hello packet */
 70	if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) {
 71		dev_err(&client->dev, "hello packet fail [%*ph]\n",
 72			ETP_SMBUS_HELLOPACKET_LEN, values);
 73		return -ENXIO;
 74	}
 75
 76	/* enable tp */
 77	error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP);
 78	if (error) {
 79		dev_err(&client->dev, "failed to enable touchpad: %d\n", error);
 80		return error;
 81	}
 82
 83	return 0;
 84}
 85
 86static int elan_smbus_set_mode(struct i2c_client *client, u8 mode)
 87{
 88	u8 cmd[4] = { 0x00, 0x07, 0x00, mode };
 89
 90	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
 91					  sizeof(cmd), cmd);
 92}
 93
 94static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep)
 95{
 96	if (sleep)
 97		return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD);
 98	else
 99		return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */
100}
101
102static int elan_smbus_power_control(struct i2c_client *client, bool enable)
103{
104	return 0; /* A no-op */
105}
106
107static int elan_smbus_calibrate(struct i2c_client *client)
108{
109	u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 };
110
111	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
112					  sizeof(cmd), cmd);
113}
114
115static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val)
116{
117	int error;
118	u8 buf[I2C_SMBUS_BLOCK_MAX] = {0};
119
120	BUILD_BUG_ON(ETP_CALIBRATE_MAX_LEN > sizeof(buf));
121
122	error = i2c_smbus_read_block_data(client,
123					  ETP_SMBUS_CALIBRATE_QUERY, buf);
124	if (error < 0)
125		return error;
126
127	memcpy(val, buf, ETP_CALIBRATE_MAX_LEN);
128	return 0;
129}
130
131static int elan_smbus_get_baseline_data(struct i2c_client *client,
132					bool max_baseline, u8 *value)
133{
134	int error;
135	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
136
137	error = i2c_smbus_read_block_data(client,
138					  max_baseline ?
139						ETP_SMBUS_MAX_BASELINE_CMD :
140						ETP_SMBUS_MIN_BASELINE_CMD,
141					  val);
142	if (error < 0)
143		return error;
144
145	*value = be16_to_cpup((__be16 *)val);
146
147	return 0;
148}
149
150static int elan_smbus_get_version(struct i2c_client *client,
151				  u8 pattern, bool iap, u8 *version)
152{
153	int error;
154	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
155
156	error = i2c_smbus_read_block_data(client,
157					  iap ? ETP_SMBUS_IAP_VERSION_CMD :
158						ETP_SMBUS_FW_VERSION_CMD,
159					  val);
160	if (error < 0) {
161		dev_err(&client->dev, "failed to get %s version: %d\n",
162			iap ? "IAP" : "FW", error);
163		return error;
164	}
165
166	*version = val[2];
167	return 0;
168}
169
170static int elan_smbus_get_sm_version(struct i2c_client *client, u8 pattern,
171				     u16 *ic_type, u8 *version, u8 *clickpad)
172{
173	int error;
174	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
175
176	error = i2c_smbus_read_block_data(client,
177					  ETP_SMBUS_SM_VERSION_CMD, val);
178	if (error < 0) {
179		dev_err(&client->dev, "failed to get SM version: %d\n", error);
180		return error;
181	}
182
183	*version = val[0];
184	*ic_type = val[1];
185	*clickpad = val[0] & 0x10;
186	return 0;
187}
188
189static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
190{
191	int error;
192	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
193
194	error = i2c_smbus_read_block_data(client,
195					  ETP_SMBUS_UNIQUEID_CMD, val);
196	if (error < 0) {
197		dev_err(&client->dev, "failed to get product ID: %d\n", error);
198		return error;
199	}
200
201	*id = be16_to_cpup((__be16 *)val);
202	return 0;
203}
204
205static int elan_smbus_get_checksum(struct i2c_client *client,
206				   bool iap, u16 *csum)
207{
208	int error;
209	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
210
211	error = i2c_smbus_read_block_data(client,
212					  iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
213						ETP_SMBUS_IAP_CHECKSUM_CMD,
214					  val);
215	if (error < 0) {
216		dev_err(&client->dev, "failed to get %s checksum: %d\n",
217			iap ? "IAP" : "FW", error);
218		return error;
219	}
220
221	*csum = be16_to_cpup((__be16 *)val);
222	return 0;
223}
224
225static int elan_smbus_get_max(struct i2c_client *client,
226			      unsigned int *max_x, unsigned int *max_y)
227{
228	int ret;
229	int error;
230	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
231
232	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
233	if (ret != 3) {
234		error = ret < 0 ? ret : -EIO;
235		dev_err(&client->dev, "failed to get dimensions: %d\n", error);
236		return error;
237	}
238
239	*max_x = (0x0f & val[0]) << 8 | val[1];
240	*max_y = (0xf0 & val[0]) << 4 | val[2];
241
242	return 0;
243}
244
245static int elan_smbus_get_resolution(struct i2c_client *client,
246				     u8 *hw_res_x, u8 *hw_res_y)
247{
248	int ret;
249	int error;
250	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
251
252	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val);
253	if (ret != 3) {
254		error = ret < 0 ? ret : -EIO;
255		dev_err(&client->dev, "failed to get resolution: %d\n", error);
256		return error;
257	}
258
259	*hw_res_x = val[1] & 0x0F;
260	*hw_res_y = (val[1] & 0xF0) >> 4;
261
262	return 0;
263}
264
265static int elan_smbus_get_num_traces(struct i2c_client *client,
266				     unsigned int *x_traces,
267				     unsigned int *y_traces)
268{
269	int ret;
270	int error;
271	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
272
273	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val);
274	if (ret != 3) {
275		error = ret < 0 ? ret : -EIO;
276		dev_err(&client->dev, "failed to get trace info: %d\n", error);
277		return error;
278	}
279
280	*x_traces = val[1];
281	*y_traces = val[2];
282
283	return 0;
284}
285
286static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
287					      int *adjustment)
288{
289	*adjustment = ETP_PRESSURE_OFFSET;
290	return 0;
291}
292
293static int elan_smbus_iap_get_mode(struct i2c_client *client,
294				   enum tp_mode *mode)
295{
296	int error;
297	u16 constant;
298	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
299
300	error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
301	if (error < 0) {
302		dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
303			error);
304		return error;
305	}
306
307	constant = be16_to_cpup((__be16 *)val);
308	dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
309
310	*mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
311
312	return 0;
313}
314
315static int elan_smbus_iap_reset(struct i2c_client *client)
316{
317	int error;
318
319	error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
320	if (error) {
321		dev_err(&client->dev, "cannot reset IC: %d\n", error);
322		return error;
323	}
324
325	return 0;
326}
327
328static int elan_smbus_set_flash_key(struct i2c_client *client)
329{
330	int error;
331	u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
332
333	error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
334					   sizeof(cmd), cmd);
335	if (error) {
336		dev_err(&client->dev, "cannot set flash key: %d\n", error);
337		return error;
338	}
339
340	return 0;
341}
342
343static int elan_smbus_prepare_fw_update(struct i2c_client *client, u16 ic_type,
344					u8 iap_version, u16 fw_page_size)
345{
346	struct device *dev = &client->dev;
347	int len;
348	int error;
349	enum tp_mode mode;
350	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
351	u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
352	u16 password;
353
354	/* Get FW in which mode	(IAP_MODE/MAIN_MODE)  */
355	error = elan_smbus_iap_get_mode(client, &mode);
356	if (error)
357		return error;
358
359	if (mode == MAIN_MODE) {
360
361		/* set flash key */
362		error = elan_smbus_set_flash_key(client);
363		if (error)
364			return error;
365
366		/* write iap password */
367		if (i2c_smbus_write_byte(client,
368					 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
369			dev_err(dev, "cannot write iap password\n");
370			return -EIO;
371		}
372
373		error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
374						   sizeof(cmd), cmd);
375		if (error) {
376			dev_err(dev, "failed to write iap password: %d\n",
377				error);
378			return error;
379		}
380
381		/*
382		 * Read back password to make sure we enabled flash
383		 * successfully.
384		 */
385		len = i2c_smbus_read_block_data(client,
386						ETP_SMBUS_IAP_PASSWORD_READ,
387						val);
388		if (len < (int)sizeof(u16)) {
389			error = len < 0 ? len : -EIO;
390			dev_err(dev, "failed to read iap password: %d\n",
391				error);
392			return error;
393		}
394
395		password = be16_to_cpup((__be16 *)val);
396		if (password != ETP_SMBUS_IAP_PASSWORD) {
397			dev_err(dev, "wrong iap password = 0x%X\n", password);
398			return -EIO;
399		}
400
401		/* Wait 30ms for MAIN_MODE change to IAP_MODE */
402		msleep(30);
403	}
404
405	error = elan_smbus_set_flash_key(client);
406	if (error)
407		return error;
408
409	/* Reset IC */
410	error = elan_smbus_iap_reset(client);
411	if (error)
412		return error;
413
414	return 0;
415}
416
417
418static int elan_smbus_write_fw_block(struct i2c_client *client, u16 fw_page_size,
419				     const u8 *page, u16 checksum, int idx)
420{
421	struct device *dev = &client->dev;
422	int error;
423	u16 result;
424	u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
425
426	/*
427	 * Due to the limitation of smbus protocol limiting
428	 * transfer to 32 bytes at a time, we must split block
429	 * in 2 transfers.
430	 */
431	error = i2c_smbus_write_block_data(client,
432					   ETP_SMBUS_WRITE_FW_BLOCK,
433					   fw_page_size / 2,
434					   page);
435	if (error) {
436		dev_err(dev, "Failed to write page %d (part %d): %d\n",
437			idx, 1, error);
438		return error;
439	}
440
441	error = i2c_smbus_write_block_data(client,
442					   ETP_SMBUS_WRITE_FW_BLOCK,
443					   fw_page_size / 2,
444					   page + fw_page_size / 2);
445	if (error) {
446		dev_err(dev, "Failed to write page %d (part %d): %d\n",
447			idx, 2, error);
448		return error;
449	}
450
451
452	/* Wait for F/W to update one page ROM data. */
453	usleep_range(8000, 10000);
454
455	error = i2c_smbus_read_block_data(client,
456					  ETP_SMBUS_IAP_CTRL_CMD, val);
457	if (error < 0) {
458		dev_err(dev, "Failed to read IAP write result: %d\n",
459			error);
460		return error;
461	}
462
463	result = be16_to_cpup((__be16 *)val);
464	if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
465		dev_err(dev, "IAP reports failed write: %04hx\n",
466			result);
467		return -EIO;
468	}
469
470	return 0;
471}
472
473static int elan_smbus_get_report_features(struct i2c_client *client, u8 pattern,
474					  unsigned int *features,
475					  unsigned int *report_len)
476{
477	/*
478	 * SMBus controllers with pattern 2 lack area info, as newer
479	 * high-precision packets use that space for coordinates.
480	 */
481	*features = pattern <= 0x01 ? ETP_FEATURE_REPORT_MK : 0;
482	*report_len = ETP_SMBUS_REPORT_LEN;
483	return 0;
484}
485
486static int elan_smbus_get_report(struct i2c_client *client,
487				 u8 *report, unsigned int report_len)
488{
489	int len;
490
491	BUILD_BUG_ON(I2C_SMBUS_BLOCK_MAX > ETP_SMBUS_REPORT_LEN);
492
493	len = i2c_smbus_read_block_data(client,
494					ETP_SMBUS_PACKET_QUERY,
495					&report[ETP_SMBUS_REPORT_OFFSET]);
496	if (len < 0) {
497		dev_err(&client->dev, "failed to read report data: %d\n", len);
498		return len;
499	}
500
501	if (report[ETP_REPORT_ID_OFFSET] == ETP_TP_REPORT_ID2)
502		report_len = ETP_SMBUS_REPORT_LEN2;
503
504	if (len != report_len) {
505		dev_err(&client->dev,
506			"wrong report length (%d vs %d expected)\n",
507			len, report_len);
508		return -EIO;
509	}
510
511	return 0;
512}
513
514static int elan_smbus_finish_fw_update(struct i2c_client *client,
515				       struct completion *fw_completion)
516{
517	/* No special handling unlike I2C transport */
518	return 0;
519}
520
521static int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern)
522{
523	*pattern = 0;
524	return 0;
525}
526
527const struct elan_transport_ops elan_smbus_ops = {
528	.initialize		= elan_smbus_initialize,
529	.sleep_control		= elan_smbus_sleep_control,
530	.power_control		= elan_smbus_power_control,
531	.set_mode		= elan_smbus_set_mode,
532
533	.calibrate		= elan_smbus_calibrate,
534	.calibrate_result	= elan_smbus_calibrate_result,
535
536	.get_baseline_data	= elan_smbus_get_baseline_data,
537
538	.get_version		= elan_smbus_get_version,
539	.get_sm_version		= elan_smbus_get_sm_version,
540	.get_product_id		= elan_smbus_get_product_id,
541	.get_checksum		= elan_smbus_get_checksum,
542	.get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
543
544	.get_max		= elan_smbus_get_max,
545	.get_resolution		= elan_smbus_get_resolution,
546	.get_num_traces		= elan_smbus_get_num_traces,
547
548	.iap_get_mode		= elan_smbus_iap_get_mode,
549	.iap_reset		= elan_smbus_iap_reset,
550
551	.prepare_fw_update	= elan_smbus_prepare_fw_update,
552	.write_fw_block		= elan_smbus_write_fw_block,
553	.finish_fw_update	= elan_smbus_finish_fw_update,
554
555	.get_report_features	= elan_smbus_get_report_features,
556	.get_report		= elan_smbus_get_report,
557	.get_pattern		= elan_smbus_get_pattern,
558};
v4.10.11
 
  1/*
  2 * Elan I2C/SMBus Touchpad driver - SMBus interface
  3 *
  4 * Copyright (c) 2013 ELAN Microelectronics Corp.
  5 *
  6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
  7 *
  8 * Based on cyapa driver:
  9 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
 10 * copyright (c) 2011-2012 Google, Inc.
 11 *
 12 * This program is free software; you can redistribute it and/or modify it
 13 * under the terms of the GNU General Public License version 2 as published
 14 * by the Free Software Foundation.
 15 *
 16 * Trademarks are the property of their respective owners.
 17 */
 18
 19#include <linux/delay.h>
 20#include <linux/i2c.h>
 21#include <linux/init.h>
 22#include <linux/kernel.h>
 23
 24#include "elan_i2c.h"
 25
 26/* Elan SMbus commands */
 27#define ETP_SMBUS_IAP_CMD		0x00
 28#define ETP_SMBUS_ENABLE_TP		0x20
 29#define ETP_SMBUS_SLEEP_CMD		0x21
 30#define ETP_SMBUS_IAP_PASSWORD_WRITE	0x29
 31#define ETP_SMBUS_IAP_PASSWORD_READ	0x80
 32#define ETP_SMBUS_WRITE_FW_BLOCK	0x2A
 33#define ETP_SMBUS_IAP_RESET_CMD		0x2B
 34#define ETP_SMBUS_RANGE_CMD		0xA0
 35#define ETP_SMBUS_FW_VERSION_CMD	0xA1
 36#define ETP_SMBUS_XY_TRACENUM_CMD	0xA2
 37#define ETP_SMBUS_SM_VERSION_CMD	0xA3
 38#define ETP_SMBUS_UNIQUEID_CMD		0xA3
 39#define ETP_SMBUS_RESOLUTION_CMD	0xA4
 40#define ETP_SMBUS_HELLOPACKET_CMD	0xA7
 41#define ETP_SMBUS_PACKET_QUERY		0xA8
 42#define ETP_SMBUS_IAP_VERSION_CMD	0xAC
 43#define ETP_SMBUS_IAP_CTRL_CMD		0xAD
 44#define ETP_SMBUS_IAP_CHECKSUM_CMD	0xAE
 45#define ETP_SMBUS_FW_CHECKSUM_CMD	0xAF
 46#define ETP_SMBUS_MAX_BASELINE_CMD	0xC3
 47#define ETP_SMBUS_MIN_BASELINE_CMD	0xC4
 48#define ETP_SMBUS_CALIBRATE_QUERY	0xC5
 49
 50#define ETP_SMBUS_REPORT_LEN		32
 
 51#define ETP_SMBUS_REPORT_OFFSET		2
 52#define ETP_SMBUS_HELLOPACKET_LEN	5
 53#define ETP_SMBUS_IAP_PASSWORD		0x1234
 54#define ETP_SMBUS_IAP_MODE_ON		(1 << 6)
 55
 56static int elan_smbus_initialize(struct i2c_client *client)
 57{
 58	u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 };
 59	u8 values[ETP_SMBUS_HELLOPACKET_LEN] = { 0, 0, 0, 0, 0 };
 60	int len, error;
 61
 62	/* Get hello packet */
 63	len = i2c_smbus_read_block_data(client,
 64					ETP_SMBUS_HELLOPACKET_CMD, values);
 65	if (len != ETP_SMBUS_HELLOPACKET_LEN) {
 66		dev_err(&client->dev, "hello packet length fail: %d\n", len);
 67		error = len < 0 ? len : -EIO;
 68		return error;
 69	}
 70
 71	/* compare hello packet */
 72	if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) {
 73		dev_err(&client->dev, "hello packet fail [%*ph]\n",
 74			ETP_SMBUS_HELLOPACKET_LEN, values);
 75		return -ENXIO;
 76	}
 77
 78	/* enable tp */
 79	error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP);
 80	if (error) {
 81		dev_err(&client->dev, "failed to enable touchpad: %d\n", error);
 82		return error;
 83	}
 84
 85	return 0;
 86}
 87
 88static int elan_smbus_set_mode(struct i2c_client *client, u8 mode)
 89{
 90	u8 cmd[4] = { 0x00, 0x07, 0x00, mode };
 91
 92	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
 93					  sizeof(cmd), cmd);
 94}
 95
 96static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep)
 97{
 98	if (sleep)
 99		return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD);
100	else
101		return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */
102}
103
104static int elan_smbus_power_control(struct i2c_client *client, bool enable)
105{
106	return 0; /* A no-op */
107}
108
109static int elan_smbus_calibrate(struct i2c_client *client)
110{
111	u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 };
112
113	return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
114					  sizeof(cmd), cmd);
115}
116
117static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val)
118{
119	int error;
 
 
 
120
121	error = i2c_smbus_read_block_data(client,
122					  ETP_SMBUS_CALIBRATE_QUERY, val);
123	if (error < 0)
124		return error;
125
 
126	return 0;
127}
128
129static int elan_smbus_get_baseline_data(struct i2c_client *client,
130					bool max_baseline, u8 *value)
131{
132	int error;
133	u8 val[3];
134
135	error = i2c_smbus_read_block_data(client,
136					  max_baseline ?
137						ETP_SMBUS_MAX_BASELINE_CMD :
138						ETP_SMBUS_MIN_BASELINE_CMD,
139					  val);
140	if (error < 0)
141		return error;
142
143	*value = be16_to_cpup((__be16 *)val);
144
145	return 0;
146}
147
148static int elan_smbus_get_version(struct i2c_client *client,
149				  bool iap, u8 *version)
150{
151	int error;
152	u8 val[3];
153
154	error = i2c_smbus_read_block_data(client,
155					  iap ? ETP_SMBUS_IAP_VERSION_CMD :
156						ETP_SMBUS_FW_VERSION_CMD,
157					  val);
158	if (error < 0) {
159		dev_err(&client->dev, "failed to get %s version: %d\n",
160			iap ? "IAP" : "FW", error);
161		return error;
162	}
163
164	*version = val[2];
165	return 0;
166}
167
168static int elan_smbus_get_sm_version(struct i2c_client *client,
169				     u8 *ic_type, u8 *version)
170{
171	int error;
172	u8 val[3];
173
174	error = i2c_smbus_read_block_data(client,
175					  ETP_SMBUS_SM_VERSION_CMD, val);
176	if (error < 0) {
177		dev_err(&client->dev, "failed to get SM version: %d\n", error);
178		return error;
179	}
180
181	*version = val[0];
182	*ic_type = val[1];
 
183	return 0;
184}
185
186static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
187{
188	int error;
189	u8 val[3];
190
191	error = i2c_smbus_read_block_data(client,
192					  ETP_SMBUS_UNIQUEID_CMD, val);
193	if (error < 0) {
194		dev_err(&client->dev, "failed to get product ID: %d\n", error);
195		return error;
196	}
197
198	*id = be16_to_cpup((__be16 *)val);
199	return 0;
200}
201
202static int elan_smbus_get_checksum(struct i2c_client *client,
203				   bool iap, u16 *csum)
204{
205	int error;
206	u8 val[3];
207
208	error = i2c_smbus_read_block_data(client,
209					  iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
210						ETP_SMBUS_IAP_CHECKSUM_CMD,
211					  val);
212	if (error < 0) {
213		dev_err(&client->dev, "failed to get %s checksum: %d\n",
214			iap ? "IAP" : "FW", error);
215		return error;
216	}
217
218	*csum = be16_to_cpup((__be16 *)val);
219	return 0;
220}
221
222static int elan_smbus_get_max(struct i2c_client *client,
223			      unsigned int *max_x, unsigned int *max_y)
224{
225	int ret;
226	int error;
227	u8 val[3];
228
229	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
230	if (ret != 3) {
231		error = ret < 0 ? ret : -EIO;
232		dev_err(&client->dev, "failed to get dimensions: %d\n", error);
233		return error;
234	}
235
236	*max_x = (0x0f & val[0]) << 8 | val[1];
237	*max_y = (0xf0 & val[0]) << 4 | val[2];
238
239	return 0;
240}
241
242static int elan_smbus_get_resolution(struct i2c_client *client,
243				     u8 *hw_res_x, u8 *hw_res_y)
244{
245	int ret;
246	int error;
247	u8 val[3];
248
249	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val);
250	if (ret != 3) {
251		error = ret < 0 ? ret : -EIO;
252		dev_err(&client->dev, "failed to get resolution: %d\n", error);
253		return error;
254	}
255
256	*hw_res_x = val[1] & 0x0F;
257	*hw_res_y = (val[1] & 0xF0) >> 4;
258
259	return 0;
260}
261
262static int elan_smbus_get_num_traces(struct i2c_client *client,
263				     unsigned int *x_traces,
264				     unsigned int *y_traces)
265{
266	int ret;
267	int error;
268	u8 val[3];
269
270	ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val);
271	if (ret != 3) {
272		error = ret < 0 ? ret : -EIO;
273		dev_err(&client->dev, "failed to get trace info: %d\n", error);
274		return error;
275	}
276
277	*x_traces = val[1];
278	*y_traces = val[2];
279
280	return 0;
281}
282
283static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
284					      int *adjustment)
285{
286	*adjustment = ETP_PRESSURE_OFFSET;
287	return 0;
288}
289
290static int elan_smbus_iap_get_mode(struct i2c_client *client,
291				   enum tp_mode *mode)
292{
293	int error;
294	u16 constant;
295	u8 val[3];
296
297	error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
298	if (error < 0) {
299		dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
300			error);
301		return error;
302	}
303
304	constant = be16_to_cpup((__be16 *)val);
305	dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
306
307	*mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
308
309	return 0;
310}
311
312static int elan_smbus_iap_reset(struct i2c_client *client)
313{
314	int error;
315
316	error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
317	if (error) {
318		dev_err(&client->dev, "cannot reset IC: %d\n", error);
319		return error;
320	}
321
322	return 0;
323}
324
325static int elan_smbus_set_flash_key(struct i2c_client *client)
326{
327	int error;
328	u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
329
330	error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
331					   sizeof(cmd), cmd);
332	if (error) {
333		dev_err(&client->dev, "cannot set flash key: %d\n", error);
334		return error;
335	}
336
337	return 0;
338}
339
340static int elan_smbus_prepare_fw_update(struct i2c_client *client)
 
341{
342	struct device *dev = &client->dev;
343	int len;
344	int error;
345	enum tp_mode mode;
346	u8 val[3];
347	u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
348	u16 password;
349
350	/* Get FW in which mode	(IAP_MODE/MAIN_MODE)  */
351	error = elan_smbus_iap_get_mode(client, &mode);
352	if (error)
353		return error;
354
355	if (mode == MAIN_MODE) {
356
357		/* set flash key */
358		error = elan_smbus_set_flash_key(client);
359		if (error)
360			return error;
361
362		/* write iap password */
363		if (i2c_smbus_write_byte(client,
364					 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
365			dev_err(dev, "cannot write iap password\n");
366			return -EIO;
367		}
368
369		error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
370						   sizeof(cmd), cmd);
371		if (error) {
372			dev_err(dev, "failed to write iap password: %d\n",
373				error);
374			return error;
375		}
376
377		/*
378		 * Read back password to make sure we enabled flash
379		 * successfully.
380		 */
381		len = i2c_smbus_read_block_data(client,
382						ETP_SMBUS_IAP_PASSWORD_READ,
383						val);
384		if (len < sizeof(u16)) {
385			error = len < 0 ? len : -EIO;
386			dev_err(dev, "failed to read iap password: %d\n",
387				error);
388			return error;
389		}
390
391		password = be16_to_cpup((__be16 *)val);
392		if (password != ETP_SMBUS_IAP_PASSWORD) {
393			dev_err(dev, "wrong iap password = 0x%X\n", password);
394			return -EIO;
395		}
396
397		/* Wait 30ms for MAIN_MODE change to IAP_MODE */
398		msleep(30);
399	}
400
401	error = elan_smbus_set_flash_key(client);
402	if (error)
403		return error;
404
405	/* Reset IC */
406	error = elan_smbus_iap_reset(client);
407	if (error)
408		return error;
409
410	return 0;
411}
412
413
414static int elan_smbus_write_fw_block(struct i2c_client *client,
415				     const u8 *page, u16 checksum, int idx)
416{
417	struct device *dev = &client->dev;
418	int error;
419	u16 result;
420	u8 val[3];
421
422	/*
423	 * Due to the limitation of smbus protocol limiting
424	 * transfer to 32 bytes at a time, we must split block
425	 * in 2 transfers.
426	 */
427	error = i2c_smbus_write_block_data(client,
428					   ETP_SMBUS_WRITE_FW_BLOCK,
429					   ETP_FW_PAGE_SIZE / 2,
430					   page);
431	if (error) {
432		dev_err(dev, "Failed to write page %d (part %d): %d\n",
433			idx, 1, error);
434		return error;
435	}
436
437	error = i2c_smbus_write_block_data(client,
438					   ETP_SMBUS_WRITE_FW_BLOCK,
439					   ETP_FW_PAGE_SIZE / 2,
440					   page + ETP_FW_PAGE_SIZE / 2);
441	if (error) {
442		dev_err(dev, "Failed to write page %d (part %d): %d\n",
443			idx, 2, error);
444		return error;
445	}
446
447
448	/* Wait for F/W to update one page ROM data. */
449	usleep_range(8000, 10000);
450
451	error = i2c_smbus_read_block_data(client,
452					  ETP_SMBUS_IAP_CTRL_CMD, val);
453	if (error < 0) {
454		dev_err(dev, "Failed to read IAP write result: %d\n",
455			error);
456		return error;
457	}
458
459	result = be16_to_cpup((__be16 *)val);
460	if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
461		dev_err(dev, "IAP reports failed write: %04hx\n",
462			result);
463		return -EIO;
464	}
465
466	return 0;
467}
468
469static int elan_smbus_get_report(struct i2c_client *client, u8 *report)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470{
471	int len;
472
 
 
473	len = i2c_smbus_read_block_data(client,
474					ETP_SMBUS_PACKET_QUERY,
475					&report[ETP_SMBUS_REPORT_OFFSET]);
476	if (len < 0) {
477		dev_err(&client->dev, "failed to read report data: %d\n", len);
478		return len;
479	}
480
481	if (len != ETP_SMBUS_REPORT_LEN) {
 
 
 
482		dev_err(&client->dev,
483			"wrong report length (%d vs %d expected)\n",
484			len, ETP_SMBUS_REPORT_LEN);
485		return -EIO;
486	}
487
488	return 0;
489}
490
491static int elan_smbus_finish_fw_update(struct i2c_client *client,
492				       struct completion *fw_completion)
493{
494	/* No special handling unlike I2C transport */
495	return 0;
496}
497
 
 
 
 
 
 
498const struct elan_transport_ops elan_smbus_ops = {
499	.initialize		= elan_smbus_initialize,
500	.sleep_control		= elan_smbus_sleep_control,
501	.power_control		= elan_smbus_power_control,
502	.set_mode		= elan_smbus_set_mode,
503
504	.calibrate		= elan_smbus_calibrate,
505	.calibrate_result	= elan_smbus_calibrate_result,
506
507	.get_baseline_data	= elan_smbus_get_baseline_data,
508
509	.get_version		= elan_smbus_get_version,
510	.get_sm_version		= elan_smbus_get_sm_version,
511	.get_product_id		= elan_smbus_get_product_id,
512	.get_checksum		= elan_smbus_get_checksum,
513	.get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
514
515	.get_max		= elan_smbus_get_max,
516	.get_resolution		= elan_smbus_get_resolution,
517	.get_num_traces		= elan_smbus_get_num_traces,
518
519	.iap_get_mode		= elan_smbus_iap_get_mode,
520	.iap_reset		= elan_smbus_iap_reset,
521
522	.prepare_fw_update	= elan_smbus_prepare_fw_update,
523	.write_fw_block		= elan_smbus_write_fw_block,
524	.finish_fw_update	= elan_smbus_finish_fw_update,
525
 
526	.get_report		= elan_smbus_get_report,
 
527};