Merge branches 'acpi-dock', 'acpi-ec' and 'acpi-scan'
[platform/kernel/linux-rpi.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         if (ret != 1) {
121                 if (ret >= 0)
122                         ret = -EIO;
123                 dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n",
124                         reg, ret);
125                 return ret;
126         }
127
128         return 0;
129 }
130
131 static int elan_i2c_initialize(struct i2c_client *client)
132 {
133         struct device *dev = &client->dev;
134         int error;
135         u8 val[256];
136
137         error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
138         if (error) {
139                 dev_err(dev, "device reset failed: %d\n", error);
140                 return error;
141         }
142
143         /* Wait for the device to reset */
144         msleep(100);
145
146         /* get reset acknowledgement 0000 */
147         error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
148         if (error < 0) {
149                 dev_err(dev, "failed to read reset response: %d\n", error);
150                 return error;
151         }
152
153         error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
154                                     val, ETP_I2C_DESC_LENGTH);
155         if (error) {
156                 dev_err(dev, "cannot get device descriptor: %d\n", error);
157                 return error;
158         }
159
160         error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
161                                     val, ETP_I2C_REPORT_DESC_LENGTH);
162         if (error) {
163                 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
164                 return error;
165         }
166
167         return 0;
168 }
169
170 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
171 {
172         return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
173                                   sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
174 }
175
176 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
177 {
178         u8 val[2];
179         u16 reg;
180         int error;
181
182         error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
183         if (error) {
184                 dev_err(&client->dev,
185                         "failed to read current power state: %d\n",
186                         error);
187                 return error;
188         }
189
190         reg = le16_to_cpup((__le16 *)val);
191         if (enable)
192                 reg &= ~ETP_DISABLE_POWER;
193         else
194                 reg |= ETP_DISABLE_POWER;
195
196         error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
197         if (error) {
198                 dev_err(&client->dev,
199                         "failed to write current power state: %d\n",
200                         error);
201                 return error;
202         }
203
204         return 0;
205 }
206
207 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
208 {
209         return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
210 }
211
212
213 static int elan_i2c_calibrate(struct i2c_client *client)
214 {
215         return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
216 }
217
218 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
219 {
220         return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
221 }
222
223 static int elan_i2c_get_baseline_data(struct i2c_client *client,
224                                       bool max_baseline, u8 *value)
225 {
226         int error;
227         u8 val[3];
228
229         error = elan_i2c_read_cmd(client,
230                                   max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
231                                                  ETP_I2C_MIN_BASELINE_CMD,
232                                   val);
233         if (error)
234                 return error;
235
236         *value = le16_to_cpup((__le16 *)val);
237
238         return 0;
239 }
240
241 static int elan_i2c_get_version(struct i2c_client *client,
242                                 bool iap, u8 *version)
243 {
244         int error;
245         u8 val[3];
246
247         error = elan_i2c_read_cmd(client,
248                                   iap ? ETP_I2C_IAP_VERSION_CMD :
249                                         ETP_I2C_FW_VERSION_CMD,
250                                   val);
251         if (error) {
252                 dev_err(&client->dev, "failed to get %s version: %d\n",
253                         iap ? "IAP" : "FW", error);
254                 return error;
255         }
256
257         *version = val[0];
258         return 0;
259 }
260
261 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version)
262 {
263         int error;
264         u8 val[3];
265
266         error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
267         if (error) {
268                 dev_err(&client->dev, "failed to get SM version: %d\n", error);
269                 return error;
270         }
271
272         *version = val[0];
273         return 0;
274 }
275
276 static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id)
277 {
278         int error;
279         u8 val[3];
280
281         error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
282         if (error) {
283                 dev_err(&client->dev, "failed to get product ID: %d\n", error);
284                 return error;
285         }
286
287         *id = val[0];
288         return 0;
289 }
290
291 static int elan_i2c_get_checksum(struct i2c_client *client,
292                                  bool iap, u16 *csum)
293 {
294         int error;
295         u8 val[3];
296
297         error = elan_i2c_read_cmd(client,
298                                   iap ? ETP_I2C_IAP_CHECKSUM_CMD :
299                                         ETP_I2C_FW_CHECKSUM_CMD,
300                                   val);
301         if (error) {
302                 dev_err(&client->dev, "failed to get %s checksum: %d\n",
303                         iap ? "IAP" : "FW", error);
304                 return error;
305         }
306
307         *csum = le16_to_cpup((__le16 *)val);
308         return 0;
309 }
310
311 static int elan_i2c_get_max(struct i2c_client *client,
312                             unsigned int *max_x, unsigned int *max_y)
313 {
314         int error;
315         u8 val[3];
316
317         error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
318         if (error) {
319                 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
320                 return error;
321         }
322
323         *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
324
325         error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
326         if (error) {
327                 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
328                 return error;
329         }
330
331         *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
332
333         return 0;
334 }
335
336 static int elan_i2c_get_resolution(struct i2c_client *client,
337                                    u8 *hw_res_x, u8 *hw_res_y)
338 {
339         int error;
340         u8 val[3];
341
342         error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
343         if (error) {
344                 dev_err(&client->dev, "failed to get resolution: %d\n", error);
345                 return error;
346         }
347
348         *hw_res_x = val[0];
349         *hw_res_y = val[1];
350
351         return 0;
352 }
353
354 static int elan_i2c_get_num_traces(struct i2c_client *client,
355                                    unsigned int *x_traces,
356                                    unsigned int *y_traces)
357 {
358         int error;
359         u8 val[3];
360
361         error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
362         if (error) {
363                 dev_err(&client->dev, "failed to get trace info: %d\n", error);
364                 return error;
365         }
366
367         *x_traces = val[0] - 1;
368         *y_traces = val[1] - 1;
369
370         return 0;
371 }
372
373 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
374 {
375         int error;
376         u16 constant;
377         u8 val[3];
378
379         error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
380         if (error) {
381                 dev_err(&client->dev,
382                         "failed to read iap control register: %d\n",
383                         error);
384                 return error;
385         }
386
387         constant = le16_to_cpup((__le16 *)val);
388         dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
389
390         *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
391
392         return 0;
393 }
394
395 static int elan_i2c_iap_reset(struct i2c_client *client)
396 {
397         int error;
398
399         error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
400                                    ETP_I2C_IAP_RESET);
401         if (error) {
402                 dev_err(&client->dev, "cannot reset IC: %d\n", error);
403                 return error;
404         }
405
406         return 0;
407 }
408
409 static int elan_i2c_set_flash_key(struct i2c_client *client)
410 {
411         int error;
412
413         error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
414                                    ETP_I2C_IAP_PASSWORD);
415         if (error) {
416                 dev_err(&client->dev, "cannot set flash key: %d\n", error);
417                 return error;
418         }
419
420         return 0;
421 }
422
423 static int elan_i2c_prepare_fw_update(struct i2c_client *client)
424 {
425         struct device *dev = &client->dev;
426         int error;
427         enum tp_mode mode;
428         u8 val[3];
429         u16 password;
430
431         /* Get FW in which mode (IAP_MODE/MAIN_MODE)  */
432         error = elan_i2c_iap_get_mode(client, &mode);
433         if (error)
434                 return error;
435
436         if (mode == IAP_MODE) {
437                 /* Reset IC */
438                 error = elan_i2c_iap_reset(client);
439                 if (error)
440                         return error;
441
442                 msleep(30);
443         }
444
445         /* Set flash key*/
446         error = elan_i2c_set_flash_key(client);
447         if (error)
448                 return error;
449
450         /* Wait for F/W IAP initialization */
451         msleep(mode == MAIN_MODE ? 100 : 30);
452
453         /* Check if we are in IAP mode or not */
454         error = elan_i2c_iap_get_mode(client, &mode);
455         if (error)
456                 return error;
457
458         if (mode == MAIN_MODE) {
459                 dev_err(dev, "wrong mode: %d\n", mode);
460                 return -EIO;
461         }
462
463         /* Set flash key again */
464         error = elan_i2c_set_flash_key(client);
465         if (error)
466                 return error;
467
468         /* Wait for F/W IAP initialization */
469         msleep(30);
470
471         /* read back to check we actually enabled successfully. */
472         error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
473         if (error) {
474                 dev_err(dev, "cannot read iap password: %d\n",
475                         error);
476                 return error;
477         }
478
479         password = le16_to_cpup((__le16 *)val);
480         if (password != ETP_I2C_IAP_PASSWORD) {
481                 dev_err(dev, "wrong iap password: 0x%X\n", password);
482                 return -EIO;
483         }
484
485         return 0;
486 }
487
488 static int elan_i2c_write_fw_block(struct i2c_client *client,
489                                    const u8 *page, u16 checksum, int idx)
490 {
491         struct device *dev = &client->dev;
492         u8 page_store[ETP_FW_PAGE_SIZE + 4];
493         u8 val[3];
494         u16 result;
495         int ret, error;
496
497         page_store[0] = ETP_I2C_IAP_REG_L;
498         page_store[1] = ETP_I2C_IAP_REG_H;
499         memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
500         /* recode checksum at last two bytes */
501         put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
502
503         ret = i2c_master_send(client, page_store, sizeof(page_store));
504         if (ret != sizeof(page_store)) {
505                 error = ret < 0 ? ret : -EIO;
506                 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
507                 return error;
508         }
509
510         /* Wait for F/W to update one page ROM data. */
511         msleep(20);
512
513         error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
514         if (error) {
515                 dev_err(dev, "Failed to read IAP write result: %d\n", error);
516                 return error;
517         }
518
519         result = le16_to_cpup((__le16 *)val);
520         if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
521                 dev_err(dev, "IAP reports failed write: %04hx\n",
522                         result);
523                 return -EIO;
524         }
525
526         return 0;
527 }
528
529 static int elan_i2c_finish_fw_update(struct i2c_client *client,
530                                      struct completion *completion)
531 {
532         struct device *dev = &client->dev;
533         long ret;
534         int error;
535         int len;
536         u8 buffer[ETP_I2C_INF_LENGTH];
537
538         reinit_completion(completion);
539         enable_irq(client->irq);
540
541         error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
542         if (!error)
543                 ret = wait_for_completion_interruptible_timeout(completion,
544                                                         msecs_to_jiffies(300));
545         disable_irq(client->irq);
546
547         if (error) {
548                 dev_err(dev, "device reset failed: %d\n", error);
549                 return error;
550         } else if (ret == 0) {
551                 dev_err(dev, "timeout waiting for device reset\n");
552                 return -ETIMEDOUT;
553         } else if (ret < 0) {
554                 error = ret;
555                 dev_err(dev, "error waiting for device reset: %d\n", error);
556                 return error;
557         }
558
559         len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
560         if (len != ETP_I2C_INF_LENGTH) {
561                 error = len < 0 ? len : -EIO;
562                 dev_err(dev, "failed to read INT signal: %d (%d)\n",
563                         error, len);
564                 return error;
565         }
566
567         return 0;
568 }
569
570 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
571 {
572         int len;
573
574         len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
575         if (len < 0) {
576                 dev_err(&client->dev, "failed to read report data: %d\n", len);
577                 return len;
578         }
579
580         if (len != ETP_I2C_REPORT_LEN) {
581                 dev_err(&client->dev,
582                         "wrong report length (%d vs %d expected)\n",
583                         len, ETP_I2C_REPORT_LEN);
584                 return -EIO;
585         }
586
587         return 0;
588 }
589
590 const struct elan_transport_ops elan_i2c_ops = {
591         .initialize             = elan_i2c_initialize,
592         .sleep_control          = elan_i2c_sleep_control,
593         .power_control          = elan_i2c_power_control,
594         .set_mode               = elan_i2c_set_mode,
595
596         .calibrate              = elan_i2c_calibrate,
597         .calibrate_result       = elan_i2c_calibrate_result,
598
599         .get_baseline_data      = elan_i2c_get_baseline_data,
600
601         .get_version            = elan_i2c_get_version,
602         .get_sm_version         = elan_i2c_get_sm_version,
603         .get_product_id         = elan_i2c_get_product_id,
604         .get_checksum           = elan_i2c_get_checksum,
605
606         .get_max                = elan_i2c_get_max,
607         .get_resolution         = elan_i2c_get_resolution,
608         .get_num_traces         = elan_i2c_get_num_traces,
609
610         .iap_get_mode           = elan_i2c_iap_get_mode,
611         .iap_reset              = elan_i2c_iap_reset,
612
613         .prepare_fw_update      = elan_i2c_prepare_fw_update,
614         .write_fw_block         = elan_i2c_write_fw_block,
615         .finish_fw_update       = elan_i2c_finish_fw_update,
616
617         .get_report             = elan_i2c_get_report,
618 };