OSDN Git Service

Merge tag 'irqchip-fixes-4.0-2' of git://git.infradead.org/users/jcooper/linux
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / input / mouse / elan_i2c_i2c.c
1 /*
2  * Elan I2C/SMBus Touchpad driver - I2C 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/completion.h>
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <asm/unaligned.h>
27
28 #include "elan_i2c.h"
29
30 /* Elan i2c commands */
31 #define ETP_I2C_RESET                   0x0100
32 #define ETP_I2C_WAKE_UP                 0x0800
33 #define ETP_I2C_SLEEP                   0x0801
34 #define ETP_I2C_DESC_CMD                0x0001
35 #define ETP_I2C_REPORT_DESC_CMD         0x0002
36 #define ETP_I2C_STAND_CMD               0x0005
37 #define ETP_I2C_UNIQUEID_CMD            0x0101
38 #define ETP_I2C_FW_VERSION_CMD          0x0102
39 #define ETP_I2C_SM_VERSION_CMD          0x0103
40 #define ETP_I2C_XY_TRACENUM_CMD         0x0105
41 #define ETP_I2C_MAX_X_AXIS_CMD          0x0106
42 #define ETP_I2C_MAX_Y_AXIS_CMD          0x0107
43 #define ETP_I2C_RESOLUTION_CMD          0x0108
44 #define ETP_I2C_IAP_VERSION_CMD         0x0110
45 #define ETP_I2C_SET_CMD                 0x0300
46 #define ETP_I2C_POWER_CMD               0x0307
47 #define ETP_I2C_FW_CHECKSUM_CMD         0x030F
48 #define ETP_I2C_IAP_CTRL_CMD            0x0310
49 #define ETP_I2C_IAP_CMD                 0x0311
50 #define ETP_I2C_IAP_RESET_CMD           0x0314
51 #define ETP_I2C_IAP_CHECKSUM_CMD        0x0315
52 #define ETP_I2C_CALIBRATE_CMD           0x0316
53 #define ETP_I2C_MAX_BASELINE_CMD        0x0317
54 #define ETP_I2C_MIN_BASELINE_CMD        0x0318
55
56 #define ETP_I2C_REPORT_LEN              34
57 #define ETP_I2C_DESC_LENGTH             30
58 #define ETP_I2C_REPORT_DESC_LENGTH      158
59 #define ETP_I2C_INF_LENGTH              2
60 #define ETP_I2C_IAP_PASSWORD            0x1EA5
61 #define ETP_I2C_IAP_RESET               0xF0F0
62 #define ETP_I2C_MAIN_MODE_ON            (1 << 9)
63 #define ETP_I2C_IAP_REG_L               0x01
64 #define ETP_I2C_IAP_REG_H               0x06
65
66 static int elan_i2c_read_block(struct i2c_client *client,
67                                u16 reg, u8 *val, u16 len)
68 {
69         __le16 buf[] = {
70                 cpu_to_le16(reg),
71         };
72         struct i2c_msg msgs[] = {
73                 {
74                         .addr = client->addr,
75                         .flags = client->flags & I2C_M_TEN,
76                         .len = sizeof(buf),
77                         .buf = (u8 *)buf,
78                 },
79                 {
80                         .addr = client->addr,
81                         .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
82                         .len = len,
83                         .buf = val,
84                 }
85         };
86         int ret;
87
88         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
89         return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
90 }
91
92 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
93 {
94         int retval;
95
96         retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
97         if (retval < 0) {
98                 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
99                 return retval;
100         }
101
102         return 0;
103 }
104
105 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
106 {
107         __le16 buf[] = {
108                 cpu_to_le16(reg),
109                 cpu_to_le16(cmd),
110         };
111         struct i2c_msg msg = {
112                 .addr = client->addr,
113                 .flags = client->flags & I2C_M_TEN,
114                 .len = sizeof(buf),
115                 .buf = (u8 *)buf,
116         };
117         int ret;
118
119         ret = i2c_transfer(client->adapter, &msg, 1);
120         return ret == 1 ? 0 : (ret < 0 ? ret : -EIO);
121 }
122
123 static int elan_i2c_initialize(struct i2c_client *client)
124 {
125         struct device *dev = &client->dev;
126         int error;
127         u8 val[256];
128
129         error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
130         if (error) {
131                 dev_err(dev, "device reset failed: %d\n", error);
132                 return error;
133         }
134
135         /* Wait for the device to reset */
136         msleep(100);
137
138         /* get reset acknowledgement 0000 */
139         error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
140         if (error < 0) {
141                 dev_err(dev, "failed to read reset response: %d\n", error);
142                 return error;
143         }
144
145         error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
146                                     val, ETP_I2C_DESC_LENGTH);
147         if (error) {
148                 dev_err(dev, "cannot get device descriptor: %d\n", error);
149                 return error;
150         }
151
152         error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
153                                     val, ETP_I2C_REPORT_DESC_LENGTH);
154         if (error) {
155                 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
156                 return error;
157         }
158
159         return 0;
160 }
161
162 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
163 {
164         return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
165                                   sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
166 }
167
168 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
169 {
170         u8 val[2];
171         u16 reg;
172         int error;
173
174         error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
175         if (error) {
176                 dev_err(&client->dev,
177                         "failed to read current power state: %d\n",
178                         error);
179                 return error;
180         }
181
182         reg = le16_to_cpup((__le16 *)val);
183         if (enable)
184                 reg &= ~ETP_DISABLE_POWER;
185         else
186                 reg |= ETP_DISABLE_POWER;
187
188         error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
189         if (error) {
190                 dev_err(&client->dev,
191                         "failed to write current power state: %d\n",
192                         error);
193                 return error;
194         }
195
196         return 0;
197 }
198
199 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
200 {
201         return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
202 }
203
204
205 static int elan_i2c_calibrate(struct i2c_client *client)
206 {
207         return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
208 }
209
210 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
211 {
212         return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
213 }
214
215 static int elan_i2c_get_baseline_data(struct i2c_client *client,
216                                       bool max_baseline, u8 *value)
217 {
218         int error;
219         u8 val[3];
220
221         error = elan_i2c_read_cmd(client,
222                                   max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
223                                                  ETP_I2C_MIN_BASELINE_CMD,
224                                   val);
225         if (error)
226                 return error;
227
228         *value = le16_to_cpup((__le16 *)val);
229
230         return 0;
231 }
232
233 static int elan_i2c_get_version(struct i2c_client *client,
234                                 bool iap, u8 *version)
235 {
236         int error;
237         u8 val[3];
238
239         error = elan_i2c_read_cmd(client,
240                                   iap ? ETP_I2C_IAP_VERSION_CMD :
241                                         ETP_I2C_FW_VERSION_CMD,
242                                   val);
243         if (error) {
244                 dev_err(&client->dev, "failed to get %s version: %d\n",
245                         iap ? "IAP" : "FW", error);
246                 return error;
247         }
248
249         *version = val[0];
250         return 0;
251 }
252
253 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version)
254 {
255         int error;
256         u8 val[3];
257
258         error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
259         if (error) {
260                 dev_err(&client->dev, "failed to get SM version: %d\n", error);
261                 return error;
262         }
263
264         *version = val[0];
265         return 0;
266 }
267
268 static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id)
269 {
270         int error;
271         u8 val[3];
272
273         error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
274         if (error) {
275                 dev_err(&client->dev, "failed to get product ID: %d\n", error);
276                 return error;
277         }
278
279         *id = val[0];
280         return 0;
281 }
282
283 static int elan_i2c_get_checksum(struct i2c_client *client,
284                                  bool iap, u16 *csum)
285 {
286         int error;
287         u8 val[3];
288
289         error = elan_i2c_read_cmd(client,
290                                   iap ? ETP_I2C_IAP_CHECKSUM_CMD :
291                                         ETP_I2C_FW_CHECKSUM_CMD,
292                                   val);
293         if (error) {
294                 dev_err(&client->dev, "failed to get %s checksum: %d\n",
295                         iap ? "IAP" : "FW", error);
296                 return error;
297         }
298
299         *csum = le16_to_cpup((__le16 *)val);
300         return 0;
301 }
302
303 static int elan_i2c_get_max(struct i2c_client *client,
304                             unsigned int *max_x, unsigned int *max_y)
305 {
306         int error;
307         u8 val[3];
308
309         error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
310         if (error) {
311                 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
312                 return error;
313         }
314
315         *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
316
317         error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
318         if (error) {
319                 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
320                 return error;
321         }
322
323         *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
324
325         return 0;
326 }
327
328 static int elan_i2c_get_resolution(struct i2c_client *client,
329                                    u8 *hw_res_x, u8 *hw_res_y)
330 {
331         int error;
332         u8 val[3];
333
334         error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
335         if (error) {
336                 dev_err(&client->dev, "failed to get resolution: %d\n", error);
337                 return error;
338         }
339
340         *hw_res_x = val[0];
341         *hw_res_y = val[1];
342
343         return 0;
344 }
345
346 static int elan_i2c_get_num_traces(struct i2c_client *client,
347                                    unsigned int *x_traces,
348                                    unsigned int *y_traces)
349 {
350         int error;
351         u8 val[3];
352
353         error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
354         if (error) {
355                 dev_err(&client->dev, "failed to get trace info: %d\n", error);
356                 return error;
357         }
358
359         *x_traces = val[0] - 1;
360         *y_traces = val[1] - 1;
361
362         return 0;
363 }
364
365 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
366 {
367         int error;
368         u16 constant;
369         u8 val[3];
370
371         error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
372         if (error) {
373                 dev_err(&client->dev,
374                         "failed to read iap control register: %d\n",
375                         error);
376                 return error;
377         }
378
379         constant = le16_to_cpup((__le16 *)val);
380         dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
381
382         *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
383
384         return 0;
385 }
386
387 static int elan_i2c_iap_reset(struct i2c_client *client)
388 {
389         int error;
390
391         error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
392                                    ETP_I2C_IAP_RESET);
393         if (error) {
394                 dev_err(&client->dev, "cannot reset IC: %d\n", error);
395                 return error;
396         }
397
398         return 0;
399 }
400
401 static int elan_i2c_set_flash_key(struct i2c_client *client)
402 {
403         int error;
404
405         error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
406                                    ETP_I2C_IAP_PASSWORD);
407         if (error) {
408                 dev_err(&client->dev, "cannot set flash key: %d\n", error);
409                 return error;
410         }
411
412         return 0;
413 }
414
415 static int elan_i2c_prepare_fw_update(struct i2c_client *client)
416 {
417         struct device *dev = &client->dev;
418         int error;
419         enum tp_mode mode;
420         u8 val[3];
421         u16 password;
422
423         /* Get FW in which mode (IAP_MODE/MAIN_MODE)  */
424         error = elan_i2c_iap_get_mode(client, &mode);
425         if (error)
426                 return error;
427
428         if (mode == IAP_MODE) {
429                 /* Reset IC */
430                 error = elan_i2c_iap_reset(client);
431                 if (error)
432                         return error;
433
434                 msleep(30);
435         }
436
437         /* Set flash key*/
438         error = elan_i2c_set_flash_key(client);
439         if (error)
440                 return error;
441
442         /* Wait for F/W IAP initialization */
443         msleep(mode == MAIN_MODE ? 100 : 30);
444
445         /* Check if we are in IAP mode or not */
446         error = elan_i2c_iap_get_mode(client, &mode);
447         if (error)
448                 return error;
449
450         if (mode == MAIN_MODE) {
451                 dev_err(dev, "wrong mode: %d\n", mode);
452                 return -EIO;
453         }
454
455         /* Set flash key again */
456         error = elan_i2c_set_flash_key(client);
457         if (error)
458                 return error;
459
460         /* Wait for F/W IAP initialization */
461         msleep(30);
462
463         /* read back to check we actually enabled successfully. */
464         error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
465         if (error) {
466                 dev_err(dev, "cannot read iap password: %d\n",
467                         error);
468                 return error;
469         }
470
471         password = le16_to_cpup((__le16 *)val);
472         if (password != ETP_I2C_IAP_PASSWORD) {
473                 dev_err(dev, "wrong iap password: 0x%X\n", password);
474                 return -EIO;
475         }
476
477         return 0;
478 }
479
480 static int elan_i2c_write_fw_block(struct i2c_client *client,
481                                    const u8 *page, u16 checksum, int idx)
482 {
483         struct device *dev = &client->dev;
484         u8 page_store[ETP_FW_PAGE_SIZE + 4];
485         u8 val[3];
486         u16 result;
487         int ret, error;
488
489         page_store[0] = ETP_I2C_IAP_REG_L;
490         page_store[1] = ETP_I2C_IAP_REG_H;
491         memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
492         /* recode checksum at last two bytes */
493         put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
494
495         ret = i2c_master_send(client, page_store, sizeof(page_store));
496         if (ret != sizeof(page_store)) {
497                 error = ret < 0 ? ret : -EIO;
498                 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
499                 return error;
500         }
501
502         /* Wait for F/W to update one page ROM data. */
503         msleep(20);
504
505         error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
506         if (error) {
507                 dev_err(dev, "Failed to read IAP write result: %d\n", error);
508                 return error;
509         }
510
511         result = le16_to_cpup((__le16 *)val);
512         if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
513                 dev_err(dev, "IAP reports failed write: %04hx\n",
514                         result);
515                 return -EIO;
516         }
517
518         return 0;
519 }
520
521 static int elan_i2c_finish_fw_update(struct i2c_client *client,
522                                      struct completion *completion)
523 {
524         struct device *dev = &client->dev;
525         long ret;
526         int error;
527         int len;
528         u8 buffer[ETP_I2C_INF_LENGTH];
529
530         reinit_completion(completion);
531         enable_irq(client->irq);
532
533         error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
534         if (!error)
535                 ret = wait_for_completion_interruptible_timeout(completion,
536                                                         msecs_to_jiffies(300));
537         disable_irq(client->irq);
538
539         if (error) {
540                 dev_err(dev, "device reset failed: %d\n", error);
541                 return error;
542         } else if (ret == 0) {
543                 dev_err(dev, "timeout waiting for device reset\n");
544                 return -ETIMEDOUT;
545         } else if (ret < 0) {
546                 error = ret;
547                 dev_err(dev, "error waiting for device reset: %d\n", error);
548                 return error;
549         }
550
551         len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
552         if (len != ETP_I2C_INF_LENGTH) {
553                 error = len < 0 ? len : -EIO;
554                 dev_err(dev, "failed to read INT signal: %d (%d)\n",
555                         error, len);
556                 return error;
557         }
558
559         return 0;
560 }
561
562 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
563 {
564         int len;
565
566         len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
567         if (len < 0) {
568                 dev_err(&client->dev, "failed to read report data: %d\n", len);
569                 return len;
570         }
571
572         if (len != ETP_I2C_REPORT_LEN) {
573                 dev_err(&client->dev,
574                         "wrong report length (%d vs %d expected)\n",
575                         len, ETP_I2C_REPORT_LEN);
576                 return -EIO;
577         }
578
579         return 0;
580 }
581
582 const struct elan_transport_ops elan_i2c_ops = {
583         .initialize             = elan_i2c_initialize,
584         .sleep_control          = elan_i2c_sleep_control,
585         .power_control          = elan_i2c_power_control,
586         .set_mode               = elan_i2c_set_mode,
587
588         .calibrate              = elan_i2c_calibrate,
589         .calibrate_result       = elan_i2c_calibrate_result,
590
591         .get_baseline_data      = elan_i2c_get_baseline_data,
592
593         .get_version            = elan_i2c_get_version,
594         .get_sm_version         = elan_i2c_get_sm_version,
595         .get_product_id         = elan_i2c_get_product_id,
596         .get_checksum           = elan_i2c_get_checksum,
597
598         .get_max                = elan_i2c_get_max,
599         .get_resolution         = elan_i2c_get_resolution,
600         .get_num_traces         = elan_i2c_get_num_traces,
601
602         .iap_get_mode           = elan_i2c_iap_get_mode,
603         .iap_reset              = elan_i2c_iap_reset,
604
605         .prepare_fw_update      = elan_i2c_prepare_fw_update,
606         .write_fw_block         = elan_i2c_write_fw_block,
607         .finish_fw_update       = elan_i2c_finish_fw_update,
608
609         .get_report             = elan_i2c_get_report,
610 };