Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / input / mouse / elan_i2c_smbus.c
CommitLineData
6696777c
DL
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>
6696777c
DL
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)) {
98a4a59e 73 dev_err(&client->dev, "hello packet fail [%*ph]\n",
6696777c
DL
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, u8 *version)
169{
170 int error;
171 u8 val[3];
172
173 error = i2c_smbus_read_block_data(client,
174 ETP_SMBUS_SM_VERSION_CMD, val);
175 if (error < 0) {
176 dev_err(&client->dev, "failed to get SM version: %d\n", error);
177 return error;
178 }
179
180 *version = val[0]; /* XXX Why 0 and not 2 as in IAP/FW versions? */
181 return 0;
182}
183
184static int elan_smbus_get_product_id(struct i2c_client *client, u8 *id)
185{
186 int error;
187 u8 val[3];
188
189 error = i2c_smbus_read_block_data(client,
190 ETP_SMBUS_UNIQUEID_CMD, val);
191 if (error < 0) {
192 dev_err(&client->dev, "failed to get product ID: %d\n", error);
193 return error;
194 }
195
196 *id = val[1];
197 return 0;
198}
199
200static int elan_smbus_get_checksum(struct i2c_client *client,
201 bool iap, u16 *csum)
202{
203 int error;
204 u8 val[3];
205
206 error = i2c_smbus_read_block_data(client,
207 iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
208 ETP_SMBUS_IAP_CHECKSUM_CMD,
209 val);
210 if (error < 0) {
211 dev_err(&client->dev, "failed to get %s checksum: %d\n",
212 iap ? "IAP" : "FW", error);
213 return error;
214 }
215
216 *csum = be16_to_cpup((__be16 *)val);
217 return 0;
218}
219
220static int elan_smbus_get_max(struct i2c_client *client,
221 unsigned int *max_x, unsigned int *max_y)
222{
223 int error;
224 u8 val[3];
225
226 error = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
227 if (error) {
228 dev_err(&client->dev, "failed to get dimensions: %d\n", error);
229 return error;
230 }
231
232 *max_x = (0x0f & val[0]) << 8 | val[1];
233 *max_y = (0xf0 & val[0]) << 4 | val[2];
234
235 return 0;
236}
237
238static int elan_smbus_get_resolution(struct i2c_client *client,
239 u8 *hw_res_x, u8 *hw_res_y)
240{
241 int error;
242 u8 val[3];
243
244 error = i2c_smbus_read_block_data(client,
245 ETP_SMBUS_RESOLUTION_CMD, val);
246 if (error) {
247 dev_err(&client->dev, "failed to get resolution: %d\n", error);
248 return error;
249 }
250
251 *hw_res_x = val[1] & 0x0F;
252 *hw_res_y = (val[1] & 0xF0) >> 4;
253
254 return 0;
255}
256
257static int elan_smbus_get_num_traces(struct i2c_client *client,
258 unsigned int *x_traces,
259 unsigned int *y_traces)
260{
261 int error;
262 u8 val[3];
263
264 error = i2c_smbus_read_block_data(client,
265 ETP_SMBUS_XY_TRACENUM_CMD, val);
266 if (error) {
267 dev_err(&client->dev, "failed to get trace info: %d\n", error);
268 return error;
269 }
270
9f423801
DL
271 *x_traces = val[1];
272 *y_traces = val[2];
6696777c
DL
273
274 return 0;
275}
276
b9bced0e 277static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
278 int *adjustment)
279{
280 *adjustment = ETP_PRESSURE_OFFSET;
281 return 0;
282}
283
6696777c
DL
284static int elan_smbus_iap_get_mode(struct i2c_client *client,
285 enum tp_mode *mode)
286{
287 int error;
288 u16 constant;
289 u8 val[3];
290
291 error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
292 if (error < 0) {
293 dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
294 error);
295 return error;
296 }
297
298 constant = be16_to_cpup((__be16 *)val);
299 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
300
301 *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
302
303 return 0;
304}
305
306static int elan_smbus_iap_reset(struct i2c_client *client)
307{
308 int error;
309
310 error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
311 if (error) {
312 dev_err(&client->dev, "cannot reset IC: %d\n", error);
313 return error;
314 }
315
316 return 0;
317}
318
319static int elan_smbus_set_flash_key(struct i2c_client *client)
320{
321 int error;
322 u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
323
324 error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
325 sizeof(cmd), cmd);
326 if (error) {
327 dev_err(&client->dev, "cannot set flash key: %d\n", error);
328 return error;
329 }
330
331 return 0;
332}
333
334static int elan_smbus_prepare_fw_update(struct i2c_client *client)
335{
336 struct device *dev = &client->dev;
337 int len;
338 int error;
339 enum tp_mode mode;
340 u8 val[3];
341 u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
342 u16 password;
343
344 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
345 error = elan_smbus_iap_get_mode(client, &mode);
346 if (error)
347 return error;
348
349 if (mode == MAIN_MODE) {
350
351 /* set flash key */
352 error = elan_smbus_set_flash_key(client);
353 if (error)
354 return error;
355
356 /* write iap password */
357 if (i2c_smbus_write_byte(client,
358 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
359 dev_err(dev, "cannot write iap password\n");
360 return -EIO;
361 }
362
363 error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
364 sizeof(cmd), cmd);
365 if (error) {
366 dev_err(dev, "failed to write iap password: %d\n",
367 error);
368 return error;
369 }
370
371 /*
372 * Read back password to make sure we enabled flash
373 * successfully.
374 */
375 len = i2c_smbus_read_block_data(client,
376 ETP_SMBUS_IAP_PASSWORD_READ,
377 val);
378 if (len < sizeof(u16)) {
379 error = len < 0 ? len : -EIO;
380 dev_err(dev, "failed to read iap password: %d\n",
381 error);
382 return error;
383 }
384
385 password = be16_to_cpup((__be16 *)val);
386 if (password != ETP_SMBUS_IAP_PASSWORD) {
387 dev_err(dev, "wrong iap password = 0x%X\n", password);
388 return -EIO;
389 }
390
391 /* Wait 30ms for MAIN_MODE change to IAP_MODE */
392 msleep(30);
393 }
394
395 error = elan_smbus_set_flash_key(client);
396 if (error)
397 return error;
398
399 /* Reset IC */
400 error = elan_smbus_iap_reset(client);
401 if (error)
402 return error;
403
404 return 0;
405}
406
407
408static int elan_smbus_write_fw_block(struct i2c_client *client,
409 const u8 *page, u16 checksum, int idx)
410{
411 struct device *dev = &client->dev;
412 int error;
413 u16 result;
414 u8 val[3];
415
416 /*
417 * Due to the limitation of smbus protocol limiting
418 * transfer to 32 bytes at a time, we must split block
419 * in 2 transfers.
420 */
421 error = i2c_smbus_write_block_data(client,
422 ETP_SMBUS_WRITE_FW_BLOCK,
423 ETP_FW_PAGE_SIZE / 2,
424 page);
425 if (error) {
426 dev_err(dev, "Failed to write page %d (part %d): %d\n",
427 idx, 1, error);
428 return error;
429 }
430
431 error = i2c_smbus_write_block_data(client,
432 ETP_SMBUS_WRITE_FW_BLOCK,
433 ETP_FW_PAGE_SIZE / 2,
434 page + ETP_FW_PAGE_SIZE / 2);
435 if (error) {
436 dev_err(dev, "Failed to write page %d (part %d): %d\n",
437 idx, 2, error);
438 return error;
439 }
440
441
442 /* Wait for F/W to update one page ROM data. */
443 usleep_range(8000, 10000);
444
445 error = i2c_smbus_read_block_data(client,
446 ETP_SMBUS_IAP_CTRL_CMD, val);
447 if (error < 0) {
448 dev_err(dev, "Failed to read IAP write result: %d\n",
449 error);
450 return error;
451 }
452
453 result = be16_to_cpup((__be16 *)val);
454 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
455 dev_err(dev, "IAP reports failed write: %04hx\n",
456 result);
457 return -EIO;
458 }
459
460 return 0;
461}
462
463static int elan_smbus_get_report(struct i2c_client *client, u8 *report)
464{
465 int len;
466
467 len = i2c_smbus_read_block_data(client,
468 ETP_SMBUS_PACKET_QUERY,
469 &report[ETP_SMBUS_REPORT_OFFSET]);
470 if (len < 0) {
471 dev_err(&client->dev, "failed to read report data: %d\n", len);
472 return len;
473 }
474
475 if (len != ETP_SMBUS_REPORT_LEN) {
476 dev_err(&client->dev,
477 "wrong report length (%d vs %d expected)\n",
478 len, ETP_SMBUS_REPORT_LEN);
479 return -EIO;
480 }
481
482 return 0;
483}
484
485static int elan_smbus_finish_fw_update(struct i2c_client *client,
486 struct completion *fw_completion)
487{
488 /* No special handling unlike I2C transport */
489 return 0;
490}
491
492const struct elan_transport_ops elan_smbus_ops = {
493 .initialize = elan_smbus_initialize,
494 .sleep_control = elan_smbus_sleep_control,
495 .power_control = elan_smbus_power_control,
496 .set_mode = elan_smbus_set_mode,
497
498 .calibrate = elan_smbus_calibrate,
499 .calibrate_result = elan_smbus_calibrate_result,
500
501 .get_baseline_data = elan_smbus_get_baseline_data,
502
503 .get_version = elan_smbus_get_version,
504 .get_sm_version = elan_smbus_get_sm_version,
505 .get_product_id = elan_smbus_get_product_id,
506 .get_checksum = elan_smbus_get_checksum,
b9bced0e 507 .get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
6696777c
DL
508
509 .get_max = elan_smbus_get_max,
510 .get_resolution = elan_smbus_get_resolution,
511 .get_num_traces = elan_smbus_get_num_traces,
512
513 .iap_get_mode = elan_smbus_iap_get_mode,
514 .iap_reset = elan_smbus_iap_reset,
515
516 .prepare_fw_update = elan_smbus_prepare_fw_update,
517 .write_fw_block = elan_smbus_write_fw_block,
518 .finish_fw_update = elan_smbus_finish_fw_update,
519
520 .get_report = elan_smbus_get_report,
521};
This page took 0.0542 seconds and 5 git commands to generate.