universal7580: abov_touchkey: Fix firmware loading of ft1604 devices
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / drivers / input / keyboard / abov_touchkey_ft1804.c
1 /* abov_touchkey.c -- Linux driver for abov chip as touchkey
2 *
3 * Copyright (C) 2013 Samsung Electronics Co.Ltd
4 * Author: Junkyeong Kim <jk0430.kim@samsung.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 */
17
18 #include <linux/delay.h>
19 #include <linux/firmware.h>
20 #include <linux/gpio.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/input.h>
24 #include <linux/input/mt.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/i2c/abov_touchkey_ft1804.h>
32 #include <linux/io.h>
33 #include <asm/unaligned.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/sec_sysfs.h>
36
37 #include <linux/pinctrl/consumer.h>
38
39 #ifdef CONFIG_OF
40 #include <linux/of_gpio.h>
41 #endif
42
43 #ifdef CONFIG_HAS_EARLYSUSPEND
44 #include <linux/earlysuspend.h>
45 #endif
46
47 #ifdef CONFIG_BATTERY_SAMSUNG
48 #include <linux/sec_batt.h>
49 #endif
50
51 #ifdef CONFIG_SEC_DEBUG_TSP_LOG
52 #define SEC_DEBUG_TK_LOG
53 #endif
54
55 #ifdef SEC_DEBUG_TK_LOG
56 #include <linux/sec_debug.h>
57 #endif
58
59 #ifdef SEC_DEBUG_TK_LOG
60 #define tk_debug_dbg(mode, dev, fmt, ...) \
61 ({ \
62 if (mode) { \
63 dev_dbg(dev, fmt, ## __VA_ARGS__); \
64 sec_debug_tsp_log(fmt, ## __VA_ARGS__); \
65 } \
66 else \
67 dev_dbg(dev, fmt, ## __VA_ARGS__); \
68 })
69
70 #define tk_debug_info(mode, dev, fmt, ...) \
71 ({ \
72 if (mode) { \
73 dev_info(dev, fmt, ## __VA_ARGS__); \
74 sec_debug_tsp_log(fmt, ## __VA_ARGS__); \
75 } \
76 else \
77 dev_info(dev, fmt, ## __VA_ARGS__); \
78 })
79
80 #define tk_debug_err(mode, dev, fmt, ...) \
81 ({ \
82 if (mode) { \
83 dev_err(dev, fmt, ## __VA_ARGS__); \
84 sec_debug_tsp_log(fmt, ## __VA_ARGS__); \
85 } \
86 else \
87 dev_err(dev, fmt, ## __VA_ARGS__); \
88 })
89 #else
90 #define tk_debug_dbg(mode, dev, fmt, ...) dev_dbg(dev, fmt, ## __VA_ARGS__)
91 #define tk_debug_info(mode, dev, fmt, ...) dev_info(dev, fmt, ## __VA_ARGS__)
92 #define tk_debug_err(mode, dev, fmt, ...) dev_err(dev, fmt, ## __VA_ARGS__)
93 #endif
94
95 /* registers */
96 #define ABOV_BTNSTATUS 0x00
97 #define ABOV_FW_VER 0x01
98 //#define ABOV_PCB_VER 0x02
99 //#define ABOV_COMMAND 0x03
100 #define ABOV_THRESHOLD 0x02
101 #define ABOV_THRESHOLD_FT16XX 0x04
102 //#define ABOV_SENS 0x05
103 //#define ABOV_SETIDAC 0x06
104 #define ABOV_BTNSTATUS_NEW 0x07
105 #define ABOV_DIFFDATA 0x0A
106 #define ABOV_RAWDATA 0x0E
107 #define ABOV_VENDORID 0x12
108 #define ABOV_GLOVE 0x13
109 #define ABOV_KEYBOARD 0x13
110 #define ABOV_MODEL_NUMBER 0x14 //Model No.
111 #define ABOV_FLIP 0x15
112 #define ABOV_SW_RESET 0x1A
113
114 /* command */
115 #define CMD_LED_ON 0x10
116 #define CMD_LED_OFF 0x20
117
118 #define CMD_SAR_TOTALCAP 0x16
119 #define CMD_SAR_MODE 0x17
120 #define CMD_SAR_TOTALCAP_READ 0x18
121 #define CMD_SAR_ENABLE 0x24
122 #define CMD_SAR_SENSING 0x25
123 #define CMD_SAR_NOISE_THRESHOLD 0x26
124 #define CMD_SAR_BASELINE 0x28
125 #define CMD_SAR_DIFFDATA 0x2A
126 #define CMD_SAR_RAWDATA 0x2E
127 #define CMD_SAR_THRESHOLD 0x32
128
129 #define CMD_DATA_UPDATE 0x40
130 #define CMD_MODE_CHECK 0x41
131 #define CMD_LED_CTRL_ON 0x60
132 #define CMD_LED_CTRL_OFF 0x70
133 #define CMD_STOP_MODE 0x80
134 #define CMD_GLOVE_OFF 0x10
135 #define CMD_GLOVE_ON 0x20
136 #define CMD_MOBILE_KBD_OFF 0x10
137 #define CMD_MOBILE_KBD_ON 0x20
138 #define CMD_FLIP_OFF 0x10
139 #define CMD_FLIP_ON 0x20
140 #define CMD_OFF 0x10
141 #define CMD_ON 0x20
142
143 #define ABOV_BOOT_DELAY 45
144 #define ABOV_RESET_DELAY 150
145 #define ABOV_FLASH_MODE 0x18
146
147 //static struct device *sec_touchkey;
148
149 #ifdef LED_TWINKLE_BOOTING
150 static void led_twinkle_work(struct work_struct *work);
151 #endif
152
153 #define TK_FW_PATH_BIN "abov/abov_noble.fw"
154 #define TK_FW_PATH_SDCARD "/sdcard/abov_fw.bin"
155
156 #define I2C_M_WR 0 /* for i2c */
157
158 enum {
159 BUILT_IN = 0,
160 SDCARD,
161 };
162
163 #define ABOV_ISP_FIRMUP_ROUTINE 0
164
165 extern unsigned int system_rev;
166 extern struct class *sec_class;
167 static int touchkey_keycode[] = { 0,
168 KEY_RECENT, KEY_BACK
169 #ifdef CONFIG_TOUCHKEY_GRIP
170 , KEY_CP_GRIP
171 #endif
172 };
173
174 struct abov_tk_info {
175 struct i2c_client *client;
176 struct input_dev *input_dev;
177 struct device *dev;
178 struct abov_touchkey_platform_data *pdata;
179 #ifdef CONFIG_HAS_EARLYSUSPEND
180 struct early_suspend early_suspend;
181 #endif
182 struct mutex lock;
183 struct pinctrl *pinctrl;
184 struct pinctrl *pinctrl_det;
185 struct pinctrl_state *pins_default;
186
187 const struct firmware *firm_data_bin;
188 const u8 *firm_data_ums;
189 char phys[32];
190 long firm_size;
191 int irq;
192 u16 menu_s;
193 u16 back_s;
194 u16 menu_raw;
195 u16 back_raw;
196 #ifdef CONFIG_TOUCHKEY_GRIP
197 struct wake_lock touckey_wake_lock;
198 u16 grip_p_thd;
199 u16 grip_r_thd;
200 u16 grip_n_thd;
201 u16 grip_s1;
202 u16 grip_s2;
203 u16 grip_baseline;
204 u16 grip_raw1;
205 u16 grip_raw2;
206 u16 grip_event;
207 bool sar_mode;
208 bool sar_enable;
209 bool sar_enable_off;
210 bool sar_sensing;
211 #endif
212 int (*power) (bool on);
213 void (*input_event)(void *data);
214 int touchkey_count;
215 u8 fw_update_state;
216 u8 fw_ver;
217 u8 fw_ver_bin;
218 u8 fw_model_number;
219 u8 checksum_h;
220 u8 checksum_h_bin;
221 u8 checksum_l;
222 u8 checksum_l_bin;
223 bool enabled;
224 bool glovemode;
225 bool keyboard_mode;
226 bool flip_mode;
227 #ifdef LED_TWINKLE_BOOTING
228 struct delayed_work led_twinkle_work;
229 bool led_twinkle_check;
230 #endif
231 };
232
233
234 #ifdef CONFIG_HAS_EARLYSUSPEND
235 static void abov_tk_early_suspend(struct early_suspend *h);
236 static void abov_tk_late_resume(struct early_suspend *h);
237 #endif
238
239 #if 1//def CONFIG_INPUT_ENABLED
240 static int abov_tk_input_open(struct input_dev *dev);
241 static void abov_tk_input_close(struct input_dev *dev);
242 #endif
243
244 static int abov_tk_i2c_read_checksum(struct abov_tk_info *info);
245 static int abov_tk_i2c_read_checksum_ft1604(struct abov_tk_info *info);
246 static void abov_tk_reset(struct abov_tk_info *info);
247
248 static int abov_touchkey_led_status;
249 static int abov_touchled_cmd_reserved;
250
251 bool is_ft1604_chip = false;
252 char *fw_path;
253
254 static int abov_mode_enable(struct i2c_client *client,u8 cmd_reg, u8 cmd)
255 {
256 return i2c_smbus_write_byte_data(client, cmd_reg, cmd);
257 }
258
259 #if ABOV_ISP_FIRMUP_ROUTINE
260 static void abov_config_gpio_i2c(struct abov_tk_info *info, int onoff)
261 {
262 struct device *i2c_dev = info->client->dev.parent->parent;
263 struct pinctrl *pinctrl_i2c;
264
265 if (onoff) {
266 pinctrl_i2c = devm_pinctrl_get_select(i2c_dev, "on_i2c");
267 if (IS_ERR(pinctrl_i2c))
268 tk_debug_err(true, &info->client->dev, "%s: Failed to configure i2c pin\n", __func__);
269 } else {
270 pinctrl_i2c = devm_pinctrl_get_select(i2c_dev, "off_i2c");
271 if (IS_ERR(pinctrl_i2c))
272 tk_debug_err(true, &info->client->dev, "%s: Failed to configure i2c pin\n", __func__);
273 }
274 }
275 #endif
276
277 static int abov_tk_i2c_read(struct i2c_client *client,
278 u8 reg, u8 *val, unsigned int len)
279 {
280 struct abov_tk_info *info = i2c_get_clientdata(client);
281 struct i2c_msg msg;
282 int ret;
283 int retry = 3;
284
285 mutex_lock(&info->lock);
286 msg.addr = client->addr;
287 msg.flags = I2C_M_WR;
288 msg.len = 1;
289 msg.buf = &reg;
290 while (retry--) {
291 ret = i2c_transfer(client->adapter, &msg, 1);
292 if (ret >= 0)
293 break;
294
295 tk_debug_err(true, &client->dev, "%s fail(address set)(%d)\n",
296 __func__, retry);
297 msleep(10);
298 }
299 if (ret < 0) {
300 mutex_unlock(&info->lock);
301 return ret;
302 }
303 retry = 3;
304 msg.flags = 1;/*I2C_M_RD*/
305 msg.len = len;
306 msg.buf = val;
307 while (retry--) {
308 ret = i2c_transfer(client->adapter, &msg, 1);
309 if (ret >= 0) {
310 mutex_unlock(&info->lock);
311 return 0;
312 }
313 tk_debug_err(true, &client->dev, "%s fail(data read)(%d)\n",
314 __func__, retry);
315 msleep(10);
316 }
317 mutex_unlock(&info->lock);
318 return ret;
319 }
320
321 static int abov_tk_i2c_read_data(struct i2c_client *client, u8 *val, unsigned int len)
322 {
323 struct abov_tk_info *info = i2c_get_clientdata(client);
324 struct i2c_msg msg;
325 int ret;
326 int retry = 3;
327
328 mutex_lock(&info->lock);
329 msg.addr = client->addr;
330 msg.flags = 1;/*I2C_M_RD*/
331 msg.len = len;
332 msg.buf = val;
333 while (retry--) {
334 ret = i2c_transfer(client->adapter, &msg, 1);
335 if (ret >= 0) {
336 mutex_unlock(&info->lock);
337 return 0;
338 }
339 dev_err(&client->dev, "%s fail(data read)(%d)\n",
340 __func__, retry);
341 msleep(10);
342 }
343 mutex_unlock(&info->lock);
344 return ret;
345 }
346
347 static int abov_tk_i2c_write(struct i2c_client *client,
348 u8 reg, u8 *val, unsigned int len)
349 {
350 struct abov_tk_info *info = i2c_get_clientdata(client);
351 struct i2c_msg msg[1];
352 unsigned char data[2];
353 int ret;
354 int retry = 3;
355
356 mutex_lock(&info->lock);
357 data[0] = reg;
358 data[1] = *val;
359 msg->addr = client->addr;
360 msg->flags = I2C_M_WR;
361 msg->len = 2;
362 msg->buf = data;
363
364 while (retry--) {
365 ret = i2c_transfer(client->adapter, msg, 1);
366 if (ret >= 0) {
367 mutex_unlock(&info->lock);
368 return 0;
369 }
370 tk_debug_err(true, &client->dev, "%s fail(%d)\n",
371 __func__, retry);
372 msleep(10);
373 }
374 mutex_unlock(&info->lock);
375 return ret;
376 }
377
378 #ifdef CONFIG_TOUCHKEY_GRIP
379 static void abov_sar_olny_mode(struct abov_tk_info *info, int on)
380 {
381 struct i2c_client *client = info->client;
382 int retry =3;
383 int ret;
384 u8 cmd;
385 u8 r_buf;
386 int mode_retry = 5;
387
388 if(info->sar_mode == on){
389 tk_debug_info(true, &client->dev, "[TK] %s : skip already %s\n", __func__, (on==1)? "sar only mode":"normal mode");
390 return;
391 }
392
393 if(on == 1) cmd = 0x20;
394 else cmd = 0x10;
395
396 tk_debug_info(true, &client->dev, "[TK] %s : %s, cmd=%x\n", __func__, (on==1)? "sar only mode":"normal mode", cmd);
397 sar_mode:
398 while(retry>0){
399 ret = abov_tk_i2c_write(info->client, CMD_SAR_MODE, &cmd, 1);
400 if (ret < 0) {
401 tk_debug_err(true, &info->client->dev, "%s fail(%d), retry %d\n", __func__, ret, retry);
402 retry--;
403 msleep(20);
404 continue;
405 }
406 break;
407 }
408
409 msleep(40);
410
411 ret = abov_tk_i2c_read(info->client, CMD_SAR_MODE, &r_buf, 1);
412 if (ret < 0) {
413 tk_debug_err(true, &info->client->dev, "%s fail(%d)\n", __func__, ret);
414 }
415 tk_debug_info(true, &client->dev, "%s read reg = %x\n", __func__,r_buf);
416
417 if((r_buf != cmd) && (mode_retry > 0)){
418 tk_debug_info(true, &info->client->dev, "%s change fail retry\n", __func__);
419 mode_retry--;
420
421 if(mode_retry == 0){
422 abov_tk_reset(info);
423 }
424 goto sar_mode;
425 }
426
427 if(r_buf == 0x20)
428 info->sar_mode = 1;
429 else if(r_buf == 0x10)
430 info->sar_mode = 0;
431 }
432
433 static void touchkey_sar_sensing(struct abov_tk_info *info, int on)
434 {
435 struct i2c_client *client = info->client;
436 int ret;
437 u8 cmd;
438
439 if(on==1) cmd = CMD_ON;
440 else cmd = CMD_OFF;
441
442 tk_debug_info(true, &client->dev, "[TK] %s : %s\n", __func__, (on)? "on":"off");
443
444 ret = abov_tk_i2c_write(info->client, CMD_SAR_SENSING, &cmd, 1);
445 if (ret < 0) {
446 tk_debug_err(true, &info->client->dev, "%s fail(%d)\n", __func__, ret);
447 }
448 info->sar_sensing = on;
449 }
450 #endif
451
452 static void release_all_fingers(struct abov_tk_info *info)
453 {
454 struct i2c_client *client = info->client;
455 int i;
456
457 tk_debug_info(true, &client->dev, "%s called (touchkey_count=%d)\n", __func__,info->touchkey_count);
458
459 for (i = 1; i < info->touchkey_count; i++) {
460 input_report_key(info->input_dev,
461 touchkey_keycode[i], 0);
462 }
463 input_sync(info->input_dev);
464 }
465
466 static int abov_tk_reset_for_bootmode(struct abov_tk_info *info)
467 {
468 int ret=0;
469
470 info->pdata->power(info, false);
471 msleep(50);
472 info->pdata->power(info, true);
473
474 return ret;
475
476 }
477
478 static void abov_tk_reset(struct abov_tk_info *info)
479 {
480 struct i2c_client *client = info->client;
481
482 if (info->enabled == false)
483 return;
484
485 tk_debug_info(true,&client->dev, "%s start\n", __func__);
486 disable_irq_nosync(info->irq);
487
488 info->enabled = false;
489
490 release_all_fingers(info);
491
492 abov_tk_reset_for_bootmode(info);
493 msleep(ABOV_RESET_DELAY);
494
495 #ifdef CONFIG_TOUCHKEY_GRIP
496 if(info->sar_enable)
497 abov_mode_enable(client, CMD_SAR_ENABLE, CMD_ON);
498
499 if(info->sar_sensing != 1)
500 touchkey_sar_sensing(info, 0);
501 #else
502 if (info->flip_mode){
503 abov_mode_enable(client, ABOV_FLIP, CMD_FLIP_ON);
504 } else {
505 if (info->glovemode)
506 abov_mode_enable(client, ABOV_GLOVE, CMD_GLOVE_ON);
507 }
508 if (info->keyboard_mode)
509 abov_mode_enable(client, ABOV_KEYBOARD, CMD_MOBILE_KBD_ON);
510 #endif
511 info->enabled = true;
512
513 enable_irq(info->irq);
514 tk_debug_info(true,&client->dev, "%s end\n", __func__);
515 }
516
517 static irqreturn_t abov_tk_interrupt(int irq, void *dev_id)
518 {
519 struct abov_tk_info *info = dev_id;
520 struct i2c_client *client = info->client;
521 int ret, retry;
522 u8 buf;
523 #ifdef CONFIG_TOUCHKEY_GRIP
524 int grip_data;
525 u8 grip_press = 0;
526
527 wake_lock(&info->touckey_wake_lock);
528 #endif
529
530 ret = abov_tk_i2c_read(client, (is_ft1604_chip ? ABOV_BTNSTATUS : ABOV_BTNSTATUS_NEW), &buf, 1);
531 if (ret < 0) {
532 retry = 3;
533 while (retry--) {
534 tk_debug_err(true, &client->dev, "%s read fail(%d)\n",
535 __func__, retry);
536 ret = abov_tk_i2c_read(client, (is_ft1604_chip ? ABOV_BTNSTATUS : ABOV_BTNSTATUS_NEW), &buf, 1);
537 if (ret == 0)
538 break;
539 else
540 msleep(10);
541 }
542 if (retry == 0) {
543 abov_tk_reset(info);
544 #ifdef CONFIG_TOUCHKEY_GRIP
545 wake_unlock(&info->touckey_wake_lock);
546 #endif
547 return IRQ_HANDLED;
548 }
549 }
550
551 tk_debug_info(true, &client->dev, " %s buf = 0x%02x\n",__func__, buf);
552
553 {
554 int menu_data = buf & 0x03;
555 int back_data = (buf >> 2) & 0x03;
556 u8 menu_press = !(menu_data % 2);
557 u8 back_press = !(back_data % 2);
558 #ifdef CONFIG_TOUCHKEY_GRIP
559 grip_data = (buf >> 4) & 0x03;
560 grip_press = !(grip_data % 2);
561 #endif
562 if (menu_data)
563 input_report_key(info->input_dev,
564 touchkey_keycode[1], menu_press);
565 if (back_data)
566 input_report_key(info->input_dev,
567 touchkey_keycode[2], back_press);
568 #ifdef CONFIG_TOUCHKEY_GRIP
569 if (grip_data){
570 input_report_key(info->input_dev,
571 touchkey_keycode[3], grip_press);
572 info->grip_event = grip_press;
573 }
574 #endif
575 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
576 tk_debug_info(true, &client->dev,
577 "keycode : %s%s ver 0x%02x\n",
578 menu_data ? (menu_press ? "P" : "R") : "",
579 back_data ? (back_press ? "P" : "R") : "",
580 info->fw_ver);
581 #else
582 tk_debug_info(true, &client->dev,
583 "keycode : %s%s%x ver 0x%02x\n",
584 menu_data ? (menu_press ? "menu P " : "menu R ") : "",
585 back_data ? (back_press ? "back P " : "back R ") : "",
586 buf, info->fw_ver);
587 #endif
588 #ifdef CONFIG_TOUCHKEY_GRIP
589 if (grip_data){
590 tk_debug_info(true, &client->dev, "%s%x \n",
591 grip_press ? "grip P " : "grip R ", buf);
592 }
593 #endif
594 }
595
596 input_sync(info->input_dev);
597
598 #ifdef CONFIG_TOUCHKEY_GRIP
599 wake_unlock(&info->touckey_wake_lock);
600 #endif
601 return IRQ_HANDLED;
602
603 }
604 static int touchkey_led_set(struct abov_tk_info *info, int data)
605 {
606 u8 cmd;
607 int ret;
608
609 if (data == 1)
610 cmd = CMD_LED_ON;
611 else
612 cmd = CMD_LED_OFF;
613
614 if (!info->enabled) {
615 abov_touchled_cmd_reserved = 1;
616 return 1;
617 }
618
619 ret = abov_tk_i2c_write(info->client, ABOV_BTNSTATUS, &cmd, 1);
620 if (ret < 0) {
621 tk_debug_err(true, &info->client->dev, "%s fail(%d)\n", __func__, ret);
622 abov_touchled_cmd_reserved = 1;
623 return 1;
624 }
625
626 return 0;
627 }
628
629 static ssize_t touchkey_led_control(struct device *dev,
630 struct device_attribute *attr, const char *buf,
631 size_t count)
632 {
633 struct abov_tk_info *info = dev_get_drvdata(dev);
634 int data;
635 u8 cmd;
636 int ret;
637
638 ret = sscanf(buf, "%d", &data);
639 if (ret != 1) {
640 tk_debug_err(true, &info->client->dev, "%s: cmd read err\n", __func__);
641 return count;
642 }
643
644 if (!(data == 0 || data == 1)) {
645 tk_debug_err(true, &info->client->dev, "%s: wrong command(%d)\n",
646 __func__, data);
647 return count;
648 }
649
650 if (data == 1)
651 cmd = CMD_LED_ON;
652 else
653 cmd = CMD_LED_OFF;
654
655 #ifdef LED_TWINKLE_BOOTING
656 if(info->led_twinkle_check == 1){
657 info->led_twinkle_check = 0;
658 cancel_delayed_work(&info->led_twinkle_work);
659 }
660 #endif
661
662 if(touchkey_led_set(info, data))
663 goto out;
664
665 msleep(20);
666
667 abov_touchled_cmd_reserved = 0;
668 tk_debug_info(true, &info->client->dev, "%s data(%d)\n",__func__,data);
669
670 out:
671 abov_touchkey_led_status = cmd;
672
673 return count;
674 }
675
676 #ifdef CONFIG_TOUCHKEY_GRIP
677 static ssize_t touchkey_sar_enable(struct device *dev,
678 struct device_attribute *attr, const char *buf,
679 size_t count)
680 {
681 struct abov_tk_info *info = dev_get_drvdata(dev);
682 struct i2c_client *client = info->client;
683 int data;
684 int ret;
685 u8 cmd;
686
687 ret = sscanf(buf, "%d", &data);
688 if (ret != 1) {
689 dev_err(&client->dev, "%s: cmd read err\n", __func__);
690 return count;
691 }
692
693 if (!(data >= 0 && data <= 3)) {
694 dev_err(&client->dev, "%s: wrong command(%d)\n",
695 __func__, data);
696 return count;
697 }
698
699 /*data 0:0ff, 1:on, 2:force off, 3:force off->on */
700
701 if(data == 3){
702 info->sar_enable_off = 0;
703 tk_debug_info(true, &info->client->dev, "%s : Power back off _ force off -> on (%d)\n", __func__, info->sar_enable);
704 if(info->sar_enable)
705 data = 1;
706 else
707 return count;
708 }
709
710 if(info->sar_enable_off){
711 if(data == 1)
712 info->sar_enable = true;
713 else
714 info->sar_enable = false;
715 tk_debug_info(true, &info->client->dev, "%s skip, Power back off _ force off mode (%d)\n", __func__, info->sar_enable);
716 return count;
717 }
718
719 if(data == 1)
720 cmd = 0x20;
721 else if(data == 2){ //test app : Power back off _ force off
722 cmd = 0x10;
723 info->sar_enable_off = 1;
724 }
725 else cmd = 0x10;
726
727 tk_debug_info(true, &info->client->dev, "%s data(%d)\n",__func__,data);
728
729 ret = abov_mode_enable(client, CMD_SAR_ENABLE, cmd);
730 if (ret < 0) {
731 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
732 return count;
733 }
734
735 if(data == 1)
736 info->sar_enable = true;
737 else{
738 input_report_key(info->input_dev, touchkey_keycode[3], 0);
739 info->grip_event = 0;
740 info->sar_enable = false;
741 }
742
743 dev_notice(&client->dev, "%s data(%d)\n",__func__,data);
744
745 return count;
746 }
747 #endif
748
749 static ssize_t touchkey_threshold_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
751 {
752 struct abov_tk_info *info = dev_get_drvdata(dev);
753 struct i2c_client *client = info->client;
754 u8 r_buf;
755 int ret;
756
757 ret = abov_tk_i2c_read(client, (is_ft1604_chip ? ABOV_THRESHOLD_FT16XX : ABOV_THRESHOLD), &r_buf, 1);
758 if (ret < 0) {
759 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
760 r_buf = 0;
761 }
762 return sprintf(buf, "%d\n", r_buf);
763 }
764
765 static void get_diff_data(struct abov_tk_info *info)
766 {
767 struct i2c_client *client = info->client;
768 u8 r_buf[4];
769 int ret;
770
771 ret = abov_tk_i2c_read(client, ABOV_DIFFDATA, r_buf, 4);
772 if (ret < 0) {
773 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
774 info->menu_s = 0;
775 info->back_s = 0;
776 return;
777 }
778
779 info->menu_s = (r_buf[0] << 8) | r_buf[1];
780 info->back_s = (r_buf[2] << 8) | r_buf[3];
781 }
782
783 static ssize_t touchkey_menu_show(struct device *dev,
784 struct device_attribute *attr, char *buf)
785 {
786 struct abov_tk_info *info = dev_get_drvdata(dev);
787
788 get_diff_data(info);
789
790 return sprintf(buf, "%d\n", info->menu_s);
791 }
792
793 static ssize_t touchkey_back_show(struct device *dev,
794 struct device_attribute *attr, char *buf)
795 {
796 struct abov_tk_info *info = dev_get_drvdata(dev);
797
798 get_diff_data(info);
799
800 return sprintf(buf, "%d\n", info->back_s);
801 }
802
803 static void get_raw_data(struct abov_tk_info *info)
804 {
805 struct i2c_client *client = info->client;
806 u8 r_buf[4];
807 int ret;
808
809 ret = abov_tk_i2c_read(client, ABOV_RAWDATA, r_buf, 4);
810 if (ret < 0) {
811 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
812 info->menu_raw = 0;
813 info->back_raw = 0;
814 return;
815 }
816
817 info->menu_raw = (r_buf[0] << 8) | r_buf[1];
818 info->back_raw = (r_buf[2] << 8) | r_buf[3];
819 }
820
821 static ssize_t touchkey_menu_raw_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
823 {
824 struct abov_tk_info *info = dev_get_drvdata(dev);
825
826 get_raw_data(info);
827
828 return sprintf(buf, "%d\n", info->menu_raw);
829 }
830
831 static ssize_t touchkey_back_raw_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
833 {
834 struct abov_tk_info *info = dev_get_drvdata(dev);
835
836 get_raw_data(info);
837
838 return sprintf(buf, "%d\n", info->back_raw);
839 }
840
841 #ifdef CONFIG_TOUCHKEY_GRIP
842 static ssize_t touchkey_grip_threshold_show(struct device *dev,
843 struct device_attribute *attr, char *buf)
844 {
845 struct abov_tk_info *info = dev_get_drvdata(dev);
846
847 u8 r_buf[4];
848 int ret;
849
850 ret = abov_tk_i2c_read(info->client, CMD_SAR_THRESHOLD, r_buf, 4);
851 if (ret < 0) {
852 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
853 info->grip_p_thd = 0;
854 info->grip_r_thd = 0;
855 return sprintf(buf, "%d\n", 0);
856 }
857 info->grip_p_thd = (r_buf[0] << 8) | r_buf[1];
858 info->grip_r_thd = (r_buf[2] << 8) | r_buf[3];
859
860 ret = abov_tk_i2c_read(info->client, CMD_SAR_NOISE_THRESHOLD, r_buf, 2);
861 if (ret < 0) {
862 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
863 info->grip_n_thd = 0;
864 return sprintf(buf, "%d\n", 0);
865 }
866 info->grip_n_thd = (r_buf[0] << 8) | r_buf[1];
867
868 return sprintf(buf, "%d,%d,%d\n", info->grip_p_thd, info->grip_r_thd, info->grip_n_thd );
869 }
870 static ssize_t touchkey_total_cap_show(struct device *dev,
871 struct device_attribute *attr, char *buf)
872 {
873 struct abov_tk_info *info = dev_get_drvdata(dev);
874 u8 r_buf[2];
875 u8 cmd;
876 int ret;
877 int value;
878
879 cmd = 0x20;
880 ret = abov_tk_i2c_write(info->client, CMD_SAR_TOTALCAP, &cmd, 1);
881 if (ret < 0) {
882 tk_debug_err(true, &info->client->dev, "%s write fail(%d)\n", __func__, ret);
883 }
884
885 usleep_range(10, 10);
886
887 ret = abov_tk_i2c_read(info->client, CMD_SAR_TOTALCAP_READ, r_buf, 2);
888 if (ret < 0) {
889 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
890 return sprintf(buf, "%d\n", 0);
891 }
892 value = (r_buf[0] << 8) | r_buf[1];
893
894 return sprintf(buf, "%d\n", value/100);
895 }
896 static ssize_t touchkey_grip_show(struct device *dev,
897 struct device_attribute *attr, char *buf)
898 {
899 struct abov_tk_info *info = dev_get_drvdata(dev);
900
901 u8 r_buf[4];
902 int ret;
903
904 ret = abov_tk_i2c_read(info->client, CMD_SAR_DIFFDATA, r_buf, 4);
905 if (ret < 0) {
906 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
907 info->grip_s1 = 0;
908 info->grip_s2 = 0;
909 return sprintf(buf, "%d\n", 0);
910 }
911 info->grip_s1 = (r_buf[0] << 8) | r_buf[1];
912 info->grip_s2 = (r_buf[2] << 8) | r_buf[3];
913
914
915 return sprintf(buf, "%d,%d\n", info->grip_s1, info->grip_s2);
916 }
917
918 static ssize_t touchkey_grip_baseline_show(struct device *dev,
919 struct device_attribute *attr, char *buf)
920 {
921 struct abov_tk_info *info = dev_get_drvdata(dev);
922
923 u8 r_buf[2];
924 int ret;
925
926 ret = abov_tk_i2c_read(info->client, CMD_SAR_BASELINE, r_buf, 2);
927 if (ret < 0) {
928 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
929 info->grip_baseline = 0;
930 return sprintf(buf, "%d\n", 0);
931 }
932 info->grip_baseline = (r_buf[0] << 8) | r_buf[1];
933
934 return sprintf(buf, "%d\n", info->grip_baseline);
935 }
936
937 static ssize_t touchkey_grip_raw_show(struct device *dev,
938 struct device_attribute *attr, char *buf)
939 {
940 struct abov_tk_info *info = dev_get_drvdata(dev);
941
942 u8 r_buf[4];
943 int ret;
944
945 ret = abov_tk_i2c_read(info->client, CMD_SAR_RAWDATA, r_buf, 4);
946 if (ret < 0) {
947 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
948 info->grip_raw1 = 0;
949 info->grip_raw2 = 0;
950 return sprintf(buf, "%d\n", 0);
951 }
952 info->grip_raw1 = (r_buf[0] << 8) | r_buf[1];
953 info->grip_raw2 = 0; //(r_buf[2] << 8) | r_buf[3]; NA
954
955 return sprintf(buf, "%d,%d\n", info->grip_raw1, info->grip_raw2);
956 }
957
958 static ssize_t touchkey_grip_gain_show(struct device *dev,
959 struct device_attribute *attr, char *buf)
960 {
961 return sprintf(buf, "%d,%d,%d,%d\n", 0, 0, 0, 0);
962 }
963
964 static ssize_t touchkey_grip_check_show(struct device *dev,
965 struct device_attribute *attr, char *buf)
966 {
967 struct abov_tk_info *info = dev_get_drvdata(dev);
968
969 //dev_info(&info->client->dev, "%s event:%d\n", __func__, info->grip_event);
970
971 return sprintf(buf, "%d\n", info->grip_event);
972 }
973
974 static ssize_t touchkey_grip_sw_reset(struct device *dev,
975 struct device_attribute *attr, const char *buf,
976 size_t count)
977 {
978 struct abov_tk_info *info = dev_get_drvdata(dev);
979 struct i2c_client *client = info->client;
980 int data;
981 int ret;
982 u8 cmd = 0x10;
983
984 ret = sscanf(buf, "%d", &data);
985 if (ret != 1) {
986 dev_err(&client->dev, "%s: cmd read err\n", __func__);
987 return count;
988 }
989
990 if (!(data == 1)) {
991 dev_err(&client->dev, "%s: wrong command(%d)\n",
992 __func__, data);
993 return count;
994 }
995
996 ret = abov_tk_i2c_write(info->client, ABOV_SW_RESET, &cmd, 1);
997 if (ret < 0) {
998 dev_err(&info->client->dev, "%s fail(%d)\n", __func__, ret);
999 return count;
1000 }
1001
1002 tk_debug_info(true, &info->client->dev, "%s data(%d)\n",__func__,data);
1003
1004 return count;
1005 }
1006
1007 static ssize_t touchkey_sensing_change(struct device *dev,
1008 struct device_attribute *attr, const char *buf,
1009 size_t count)
1010 {
1011 struct abov_tk_info *info = dev_get_drvdata(dev);
1012 struct i2c_client *client = info->client;
1013 int ret, data;
1014
1015 ret = sscanf(buf, "%d", &data);
1016 if (ret != 1) {
1017 dev_err(&client->dev, "%s: cmd read err\n", __func__);
1018 return count;
1019 }
1020
1021 if (!(data == 0 || data == 1)) {
1022 dev_err(&client->dev, "%s: wrong command(%d)\n",
1023 __func__, data);
1024 return count;
1025 }
1026
1027 if(data == 1) //EarJack inserted
1028 touchkey_sar_sensing(info, 0);
1029 else
1030 touchkey_sar_sensing(info, 1);
1031
1032 tk_debug_info(true, &info->client->dev, "%s earjack (%d)\n",__func__,data);
1033
1034 return count;
1035 }
1036
1037 #ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
1038 static ssize_t touchkey_sar_press_threshold_store(struct device *dev,
1039 struct device_attribute *attr, const char *buf,
1040 size_t count)
1041 {
1042 struct abov_tk_info *info = dev_get_drvdata(dev);
1043
1044 int ret;
1045 int threshold;
1046 u8 cmd[2];
1047
1048 ret = sscanf(buf, "%d", &threshold);
1049 if (ret != 1) {
1050 tk_debug_err(true, &info->client->dev, "%s: failed to read thresold, buf is %s\n", __func__,buf);
1051 return count;
1052 }
1053
1054 if(threshold > 0xff) {
1055 cmd[0] = (threshold >> 8) & 0xff;
1056 cmd[1] = 0xff & threshold;
1057 }else if(threshold < 0) {
1058 cmd[0] = 0x0;
1059 cmd[1] = 0x0;
1060 }else{
1061 cmd[0] = 0x0;
1062 cmd[1] = (u8)threshold;
1063 }
1064
1065 tk_debug_info(true,&info->client->dev, "%s buf : %d, threshold : %d\n",
1066 __func__, threshold,(cmd[0]<<8 )| cmd[1]);
1067
1068 ret = abov_tk_i2c_write(info->client, CMD_SAR_THRESHOLD, &cmd[0], 1);
1069 if (ret != 0) {
1070 tk_debug_info(true,&info->client->dev, "%s failed to write press_threhold data1", __func__);
1071 goto press_threshold_out;
1072 }
1073 ret = abov_tk_i2c_write(info->client, CMD_SAR_THRESHOLD + 0x01, &cmd[1], 1);
1074 if (ret != 0) {
1075 tk_debug_info(true,&info->client->dev, "%s failed to write press_threhold data2", __func__);
1076 goto press_threshold_out;
1077 }
1078 press_threshold_out:
1079 return count;
1080 }
1081
1082 static ssize_t touchkey_sar_release_threshold_store(struct device *dev,
1083 struct device_attribute *attr, const char *buf,
1084 size_t count)
1085 {
1086 struct abov_tk_info *info = dev_get_drvdata(dev);
1087
1088 int ret;
1089 int threshold;
1090 u8 cmd[2];
1091
1092 ret = sscanf(buf, "%d", &threshold);
1093 if (ret != 1) {
1094 tk_debug_err(true, &info->client->dev, "%s: failed to read thresold, buf is %s\n", __func__,buf);
1095 return count;
1096 }
1097
1098 if(threshold > 0xff) {
1099 cmd[0] = (threshold >> 8) & 0xff;
1100 cmd[1] = 0xff & threshold;
1101 }else if(threshold < 0) {
1102 cmd[0] = 0x0;
1103 cmd[1] = 0x0;
1104 }else{
1105 cmd[0] = 0x0;
1106 cmd[1] = (u8)threshold;
1107 }
1108
1109 tk_debug_info(true,&info->client->dev, "%s buf : %d, threshold : %d\n",
1110 __func__, threshold,(cmd[0] << 8) | cmd[1]);
1111
1112 ret = abov_tk_i2c_write(info->client, CMD_SAR_THRESHOLD+0x02, &cmd[0], 1);
1113 tk_debug_info(true,&info->client->dev, "%s ret : %d\n", __func__,ret);
1114
1115 if (ret != 0) {
1116 tk_debug_info(true,&info->client->dev, "%s failed to write release_threshold_data1", __func__);
1117 goto release_threshold_out;
1118 }
1119 ret = abov_tk_i2c_write(info->client, CMD_SAR_THRESHOLD+0x03, &cmd[1], 1);
1120 tk_debug_info(true,&info->client->dev, "%s ret : %d\n", __func__,ret);
1121 if (ret != 0) {
1122 tk_debug_info(true,&info->client->dev, "%s failed to write release_threshold_data2", __func__);
1123 goto release_threshold_out;
1124 }
1125 release_threshold_out:
1126 return count;
1127 }
1128
1129 static ssize_t touchkey_mode_change(struct device *dev,
1130 struct device_attribute *attr, const char *buf,
1131 size_t count)
1132 {
1133 struct abov_tk_info *info = dev_get_drvdata(dev);
1134 struct i2c_client *client = info->client;
1135 int ret, data;
1136
1137 ret = sscanf(buf, "%d", &data);
1138 if (ret != 1) {
1139 dev_err(&client->dev, "%s: cmd read err\n", __func__);
1140 return count;
1141 }
1142
1143 if (!(data == 0 || data == 1)) {
1144 dev_err(&client->dev, "%s: wrong command(%d)\n",
1145 __func__, data);
1146 return count;
1147 }
1148
1149 tk_debug_info(true, &info->client->dev, "%s data(%d)\n",__func__,data);
1150
1151 abov_sar_olny_mode(info, data);
1152
1153 return count;
1154 }
1155 #endif
1156
1157 #endif
1158
1159 static ssize_t touchkey_chip_name(struct device *dev,
1160 struct device_attribute *attr, char *buf)
1161 {
1162 struct abov_tk_info *info = dev_get_drvdata(dev);
1163 struct i2c_client *client = info->client;
1164
1165 tk_debug_dbg(true, &client->dev, "%s\n", __func__);
1166
1167 return sprintf(buf, (is_ft1604_chip ? "FT1604\n" : "FT1804\n"));
1168 }
1169
1170 static ssize_t bin_fw_ver(struct device *dev,
1171 struct device_attribute *attr, char *buf)
1172 {
1173 struct abov_tk_info *info = dev_get_drvdata(dev);
1174 struct i2c_client *client = info->client;
1175
1176 tk_debug_dbg(true, &client->dev, "fw version bin : 0x%x\n", info->fw_ver_bin);
1177
1178 return sprintf(buf, "0x%02x\n", info->fw_ver_bin);
1179 }
1180
1181 static int get_tk_fw_version(struct abov_tk_info *info, bool bootmode)
1182 {
1183 struct i2c_client *client = info->client;
1184 u8 buf;
1185 int ret;
1186 int retry = 3;
1187
1188 ret = abov_tk_i2c_read(client, ABOV_FW_VER, &buf, 1);
1189 if (ret < 0) {
1190 while (retry--) {
1191 tk_debug_err(true, &client->dev, "%s read fail(%d)\n",
1192 __func__, retry);
1193 if (!bootmode)
1194 abov_tk_reset(info);
1195 else
1196 return -1;
1197 ret = abov_tk_i2c_read(client, ABOV_FW_VER, &buf, 1);
1198 if (ret == 0)
1199 break;
1200 }
1201 if (retry <= 0)
1202 return -1;
1203 }
1204
1205 info->fw_ver = buf;
1206 tk_debug_info(true, &client->dev, "%s : 0x%x\n", __func__, buf);
1207 return 0;
1208 }
1209
1210 static ssize_t read_fw_ver(struct device *dev,
1211 struct device_attribute *attr, char *buf)
1212 {
1213 struct abov_tk_info *info = dev_get_drvdata(dev);
1214 struct i2c_client *client = info->client;
1215 int ret;
1216
1217 ret = get_tk_fw_version(info, false);
1218 if (ret < 0) {
1219 tk_debug_err(true, &client->dev, "%s read fail\n", __func__);
1220 info->fw_ver = 0;
1221 }
1222
1223 return sprintf(buf, "0x%02x\n", info->fw_ver);
1224 }
1225
1226 static int abov_load_fw(struct abov_tk_info *info, u8 cmd)
1227 {
1228 struct i2c_client *client = info->client;
1229 struct file *fp;
1230 mm_segment_t old_fs;
1231 long fsize, nread;
1232 int ret = 0;
1233
1234 switch(cmd) {
1235 case BUILT_IN:
1236 ret = request_firmware(&info->firm_data_bin,
1237 info->pdata->fw_path, &client->dev);
1238 if (ret) {
1239 tk_debug_err(true, &client->dev,
1240 "%s request_firmware fail(%d)\n", __func__, cmd);
1241 return ret;
1242 }
1243 /* Header info
1244 * 0x00 0x91 : model info,
1245 * 0x00 0x00 : module info (Rev 0.0),
1246 * 0x00 0xF3 : F/W
1247 * 0x00 0x00 0x17 0x10 : checksum
1248 * ~ 22byte 0x00 */
1249 info->fw_model_number = info->firm_data_bin->data[1];
1250 info->fw_ver_bin = info->firm_data_bin->data[5];
1251 info->checksum_h_bin = info->firm_data_bin->data[8];
1252 info->checksum_l_bin = info->firm_data_bin->data[9];
1253 info->firm_size = info->firm_data_bin->size;
1254
1255 if (is_ft1604_chip && strncmp(info->pdata->fw_path, "abov/abov_ft1604_a3.fw", 22) == 0) {
1256 info->fw_model_number = 0x30; //A310 fw 0x05
1257 }
1258
1259 tk_debug_info(true, &client->dev, "%s, bin version:%2X,%2X,%2X crc:%2X,%2X\n", __func__, \
1260 info->firm_data_bin->data[1], info->firm_data_bin->data[3], info->fw_ver_bin, \
1261 info->checksum_h_bin, info->checksum_l_bin);
1262 break;
1263
1264 case SDCARD:
1265 old_fs = get_fs();
1266 set_fs(get_ds());
1267 fp = filp_open(TK_FW_PATH_SDCARD, O_RDONLY, S_IRUSR);
1268 if (IS_ERR(fp)) {
1269 tk_debug_err(true, &client->dev,
1270 "%s %s open error\n", __func__, TK_FW_PATH_SDCARD);
1271 ret = -ENOENT;
1272 goto fail_sdcard_open;
1273 }
1274
1275 fsize = fp->f_path.dentry->d_inode->i_size;
1276 info->firm_data_ums = kzalloc((size_t)fsize, GFP_KERNEL);
1277 if (!info->firm_data_ums) {
1278 tk_debug_err(true, &client->dev,
1279 "%s fail to kzalloc for fw\n", __func__);
1280 ret = -ENOMEM;
1281 goto fail_sdcard_kzalloc;
1282 }
1283
1284 nread = vfs_read(fp,
1285 (char __user *)info->firm_data_ums, fsize, &fp->f_pos);
1286 if (nread != fsize) {
1287 tk_debug_err(true, &client->dev,
1288 "%s fail to vfs_read file\n", __func__);
1289 ret = -EINVAL;
1290 goto fail_sdcard_size;
1291 }
1292 filp_close(fp, current->files);
1293 set_fs(old_fs);
1294
1295 info->firm_size = nread;
1296 info->checksum_h_bin = info->firm_data_ums[8];
1297 info->checksum_l_bin = info->firm_data_ums[9];
1298
1299 tk_debug_info(true, &client->dev,"%s, bin version:%2X,%2X,%2X crc:%2X,%2X\n", __func__, \
1300 info->firm_data_ums[1], info->firm_data_ums[3], info->firm_data_ums[5], \
1301 info->checksum_h_bin, info->checksum_l_bin);
1302 break;
1303
1304 default:
1305 ret = -1;
1306 break;
1307 }
1308 tk_debug_info(true, &client->dev, "fw_size : %lu\n", info->firm_size);
1309 tk_debug_info(true, &client->dev, "%s success\n", __func__);
1310 return ret;
1311
1312 fail_sdcard_size:
1313 kfree(&info->firm_data_ums);
1314 fail_sdcard_kzalloc:
1315 filp_close(fp, current->files);
1316 fail_sdcard_open:
1317 set_fs(old_fs);
1318 return ret;
1319 }
1320
1321 #if ABOV_ISP_FIRMUP_ROUTINE
1322 void abov_i2c_start(int scl, int sda)
1323 {
1324 gpio_direction_output(sda, 1);
1325 gpio_direction_output(scl, 1);
1326 usleep_range(15, 17);
1327 gpio_direction_output(sda, 0);
1328 usleep_range(10, 12);
1329 gpio_direction_output(scl, 0);
1330 usleep_range(10, 12);
1331 }
1332
1333 void abov_i2c_stop(int scl, int sda)
1334 {
1335 gpio_direction_output(scl, 0);
1336 usleep_range(10, 12);
1337 gpio_direction_output(sda, 0);
1338 usleep_range(10, 12);
1339 gpio_direction_output(scl, 1);
1340 usleep_range(10, 12);
1341 gpio_direction_output(sda, 1);
1342 }
1343
1344 void abov_testdelay(void)
1345 {
1346 u8 i;
1347 u8 delay;
1348
1349 /* 120nms */
1350 for (i = 0; i < 15; i++)
1351 delay = 0;
1352 }
1353
1354
1355 void abov_byte_send(u8 data, int scl, int sda)
1356 {
1357 u8 i;
1358 for (i = 0x80; i != 0; i >>= 1) {
1359 gpio_direction_output(scl, 0);
1360 usleep_range(1,1);
1361
1362 if (data & i)
1363 gpio_direction_output(sda, 1);
1364 else
1365 gpio_direction_output(sda, 0);
1366
1367 usleep_range(1,1);
1368 gpio_direction_output(scl, 1);
1369 usleep_range(1,1);
1370 }
1371 usleep_range(1,1);
1372
1373 gpio_direction_output(scl, 0);
1374 gpio_direction_input(sda);
1375 usleep_range(1,1);
1376
1377 gpio_direction_output(scl, 1);
1378 usleep_range(1,1);
1379
1380 gpio_get_value(sda);
1381 abov_testdelay();
1382
1383 gpio_direction_output(scl, 0);
1384 gpio_direction_output(sda, 0);
1385 usleep_range(20,20);
1386 }
1387
1388 u8 abov_byte_read(bool type, int scl, int sda)
1389 {
1390 u8 i;
1391 u8 data = 0;
1392 u8 index = 0x7;
1393
1394 gpio_direction_output(scl, 0);
1395 gpio_direction_input(sda);
1396 usleep_range(1,1);
1397
1398 for (i = 0; i < 8; i++) {
1399 gpio_direction_output(scl, 0);
1400 usleep_range(1,1);
1401 gpio_direction_output(scl, 1);
1402 usleep_range(1,1);
1403
1404 data = data | (u8)(gpio_get_value(sda) << index);
1405 index -= 1;
1406 }
1407 usleep_range(1,1);
1408 gpio_direction_output(scl, 0);
1409
1410 gpio_direction_output(sda, 0);
1411 usleep_range(1,1);
1412
1413 if (type) { /*ACK */
1414 gpio_direction_output(sda, 0);
1415 usleep_range(1,1);
1416 gpio_direction_output(scl, 1);
1417 usleep_range(1,1);
1418 gpio_direction_output(scl, 0);
1419 usleep_range(1,1);
1420 } else { /* NAK */
1421 gpio_direction_output(sda, 1);
1422 usleep_range(1,1);
1423 gpio_direction_output(scl, 1);
1424 usleep_range(1,1);
1425 gpio_direction_output(scl, 0);
1426 usleep_range(1,1);
1427 gpio_direction_output(sda, 0);
1428 usleep_range(1,1);
1429 }
1430 usleep_range(20,20);
1431
1432 return data;
1433 }
1434
1435 void abov_enter_mode(int scl, int sda)
1436 {
1437 abov_i2c_start(scl, sda);
1438 abov_testdelay();
1439 abov_byte_send(ABOV_ID, scl, sda);
1440 abov_byte_send(0xAC, scl, sda);
1441 abov_byte_send(0x5B, scl, sda);
1442 abov_byte_send(0x2D, scl, sda);
1443 abov_i2c_stop(scl, sda);
1444 }
1445
1446 void abov_firm_write(const u8 *fw_data, int block, int scl, int sda)
1447 {
1448 int i, j;
1449 u16 pos = 0x20;
1450 u8 addr[2];
1451 u8 data[32] = {0, };
1452
1453 addr[0] = 0x10;
1454 addr[1] = 0x00;
1455 for (i = 0; i < (block - 0x20); i++) {
1456 if (i % 8 == 0) {
1457 addr[0] = 0x10 + i/8;
1458 addr[1] = 0;
1459 } else
1460 addr[1] = addr[1] + 0x20;
1461 memcpy(data, fw_data + pos, 32);
1462 abov_i2c_start(scl, sda);
1463 abov_testdelay();
1464 abov_byte_send(ABOV_ID, scl, sda);
1465 abov_byte_send(0xAC, scl, sda);
1466 abov_byte_send(0x7A, scl, sda);
1467 abov_byte_send(addr[0], scl, sda);
1468 abov_byte_send(addr[1], scl, sda);
1469 for (j = 0; j < 32; j++)
1470 abov_byte_send(data[j], scl, sda);
1471 abov_i2c_stop(scl, sda);
1472
1473 pos += 0x20;
1474
1475 usleep_range(3000,3000);; //usleep(2000); //msleep(2);
1476 }
1477 }
1478
1479 void abov_read_address_set(int scl, int sda)
1480 {
1481 abov_i2c_start(scl, sda);
1482 abov_testdelay();
1483 abov_byte_send(ABOV_ID, scl, sda);
1484 abov_byte_send(0xAC, scl, sda);
1485 abov_byte_send(0x9E, scl, sda);
1486 abov_byte_send(0x10, scl, sda); /* start addr H */
1487 abov_byte_send(0x00, scl, sda); /* start addr L */
1488 abov_byte_send(0x3F, scl, sda); /* end addr H */
1489 abov_byte_send(0xFF, scl, sda); /* end addr L */
1490 abov_i2c_stop(scl, sda);
1491 }
1492
1493 void abov_checksum(struct abov_tk_info *info, int scl, int sda)
1494 {
1495 struct i2c_client *client = info->client;
1496
1497 u8 status;
1498 u8 bootver;
1499 u8 firmver;
1500 u8 checksumh;
1501 u8 checksuml;
1502
1503 abov_read_address_set(scl, sda);
1504 msleep(5);
1505
1506 abov_i2c_start(scl, sda);
1507 abov_testdelay();
1508 abov_byte_send(ABOV_ID, scl, sda);
1509 abov_byte_send(0x00, scl, sda);
1510
1511 abov_i2c_start(scl, sda); /* restart */
1512 abov_testdelay();
1513 abov_byte_send(ABOV_ID + 1, scl, sda);
1514 status = abov_byte_read(true, scl, sda);
1515 bootver = abov_byte_read(true, scl, sda);
1516 firmver = abov_byte_read(true, scl, sda);
1517 checksumh = abov_byte_read(true, scl, sda);
1518 checksuml = abov_byte_read(false, scl, sda);
1519 abov_i2c_stop(scl, sda);
1520 msleep(3);
1521
1522 info->checksum_h = checksumh;
1523 info->checksum_l = checksuml;
1524
1525 tk_debug_dbg(true, &client->dev,
1526 "%s status(0x%x), boot(0x%x), firm(0x%x), cs_h(0x%x), cs_l(0x%x)\n",
1527 __func__, status, bootver, firmver, checksumh, checksuml);
1528 }
1529
1530 void abov_exit_mode(int scl, int sda)
1531 {
1532 abov_i2c_start(scl, sda);
1533 abov_testdelay();
1534 abov_byte_send(ABOV_ID, scl, sda);
1535 abov_byte_send(0xAC, scl, sda);
1536 abov_byte_send(0x5B, scl, sda);
1537 abov_byte_send(0xE1, scl, sda);
1538 abov_i2c_stop(scl, sda);
1539 }
1540
1541 static int abov_fw_update(struct abov_tk_info *info,
1542 const u8 *fw_data, int block, int scl, int sda)
1543 {
1544 tk_debug_dbg(true, &info->client->dev, "%s start (%d)\n",__func__,__LINE__);
1545 abov_config_gpio_i2c(info, 0);
1546 msleep(ABOV_BOOT_DELAY);
1547 abov_enter_mode(scl, sda);
1548 msleep(1100); //msleep(600);
1549 abov_firm_write(fw_data, block, scl, sda);
1550 abov_checksum(info, scl, sda);
1551 abov_config_gpio_i2c(info, 1);
1552 tk_debug_dbg(true, &info->client->dev, "%s end (%d)\n",__func__,__LINE__);
1553 return 0;
1554 }
1555 #endif
1556
1557 static int abov_tk_check_busy(struct abov_tk_info *info)
1558 {
1559 int ret, count = 0;
1560 unsigned char val = 0x00;
1561
1562 do {
1563 ret = i2c_master_recv(info->client, &val, sizeof(val));
1564
1565 if (val & 0x01) {
1566 count++;
1567 if (count > 1000)
1568 tk_debug_err(true, &info->client->dev,
1569 "%s: busy %d\n", __func__, count);
1570 return ret;
1571 } else {
1572 break;
1573 }
1574
1575 } while(1);
1576
1577 return ret;
1578 }
1579
1580 static int abov_tk_i2c_read_checksum(struct abov_tk_info *info)
1581 {
1582 unsigned char data[6] = {0xAC, 0x9E, 0x10, 0x00, 0x3F, 0xFF};
1583 unsigned char data2[1] = {0x00};
1584 unsigned char checksum[6] = {0, };
1585 int ret;
1586
1587 i2c_master_send(info->client, data, 6);
1588
1589 usleep_range(5000, 5000);
1590
1591 i2c_master_send(info->client, data2, 1);
1592 usleep_range(5000, 5000);
1593
1594 ret = abov_tk_i2c_read_data(info->client, checksum, 6);
1595
1596 tk_debug_info(true, &info->client->dev, "%s: ret:%d [%X][%X][%X][%X][%X]\n",
1597 __func__, ret, checksum[0], checksum[1], checksum[2]
1598 , checksum[4], checksum[5]);
1599 info->checksum_h = checksum[4];
1600 info->checksum_l = checksum[5];
1601 return 0;
1602 }
1603
1604 static int abov_tk_i2c_read_checksum_ft1604(struct abov_tk_info *info)
1605 {
1606 unsigned char data[6] = {0xAC, 0x9E, 0x10, 0x00, 0x3F, 0xFF};
1607 unsigned char checksum[5] = {0, };
1608 int ret;
1609 unsigned char reg = 0x00;
1610
1611 i2c_master_send(info->client, data, 6);
1612
1613 usleep_range(5000, 5000);
1614
1615 abov_tk_check_busy(info);
1616
1617 ret = abov_tk_i2c_read(info->client, reg, checksum, 5);
1618
1619 tk_debug_info(true, &info->client->dev, "%s: ret:%d [%X][%X][%X][%X][%X]\n",
1620 __func__, ret, checksum[0], checksum[1], checksum[2]
1621 , checksum[3], checksum[4]);
1622 info->checksum_h = checksum[3];
1623 info->checksum_l = checksum[4];
1624 return 0;
1625 }
1626
1627 static int abov_tk_fw_write(struct abov_tk_info *info, unsigned char *addrH,
1628 unsigned char *addrL, unsigned char *val)
1629 {
1630 int length = 36, ret = 0;
1631 unsigned char data[36];
1632
1633 data[0] = 0xAC;
1634 data[1] = 0x7A;
1635 memcpy(&data[2], addrH, 1);
1636 memcpy(&data[3], addrL, 1);
1637 memcpy(&data[4], val, 32);
1638
1639 ret = i2c_master_send(info->client, data, length);
1640 if (ret != length) {
1641 tk_debug_err(true, &info->client->dev,
1642 "%s: write fail[%x%x], %d\n", __func__, *addrH, *addrL, ret);
1643 return ret;
1644 }
1645
1646 usleep_range(3000, 3000);
1647
1648 abov_tk_check_busy(info);
1649
1650 return 0;
1651 }
1652
1653 static int abov_tk_fw_mode_enter(struct abov_tk_info *info)
1654 {
1655 unsigned char data[2] = {0xAC, 0x5B};
1656 int ret = 0;
1657
1658 ret = i2c_master_send(info->client, data, 2);
1659 if (ret != 2) {
1660 pr_err("%s: write fail\n", __func__);
1661 return -1;
1662 }
1663
1664 return 0;
1665 }
1666
1667 static int abov_tk_fw_mode_enter_ft1604(struct abov_tk_info *info)
1668 {
1669 unsigned char data[3] = {0xAC, 0x5B, 0x2D};
1670 int ret = 0;
1671
1672 ret = i2c_master_send(info->client, data, 3);
1673 if (ret != 3) {
1674 tk_debug_err(true, &info->client->dev,
1675 "%s: write fail\n", __func__);
1676 return -1;
1677 }
1678
1679 return 0;
1680 }
1681
1682 static int abov_tk_fw_mode_check(struct abov_tk_info *info)
1683 {
1684 unsigned char buf[1] = {0};
1685 int ret;
1686
1687 ret = abov_tk_i2c_read_data(info->client, buf, 1);
1688 if(ret<0){
1689 pr_err("%s: write fail\n", __func__);
1690 return 0;
1691 }
1692
1693 dev_info(&info->client->dev, "%s: ret:%02X\n",__func__, buf[0]);
1694
1695 if(buf[0]==ABOV_FLASH_MODE)
1696 return 1;
1697
1698 pr_err("%s: value is same same, %X, %X \n", __func__, ABOV_FLASH_MODE, buf[0] );
1699
1700 return 0;
1701 }
1702
1703 static int abov_tk_flash_erase(struct abov_tk_info *info)
1704 {
1705 unsigned char data[2] = {0xAC, 0x2D};
1706 int ret = 0;
1707
1708 ret = i2c_master_send(info->client, data, 2);
1709 if (ret != 2) {
1710 pr_err("%s: write fail\n", __func__);
1711 return -1;
1712 }
1713
1714 return 0;
1715
1716 }
1717
1718 static int abov_tk_fw_mode_exit(struct abov_tk_info *info)
1719 {
1720 unsigned char data[2] = {0xAC, 0xE1};
1721 int ret = 0;
1722
1723 ret = i2c_master_send(info->client, data, 2);
1724 if (ret != 2) {
1725 pr_err("%s: write fail\n", __func__);
1726 return -1;
1727 }
1728
1729 return 0;
1730
1731 }
1732
1733 static int abov_tk_fw_update(struct abov_tk_info *info, u8 cmd)
1734 {
1735 int ret, ii = 0;
1736 int count;
1737 unsigned short address;
1738 unsigned char addrH, addrL;
1739 unsigned char data[32] = {0, };
1740
1741
1742 tk_debug_info(true, &info->client->dev, "%s start\n", __func__);
1743
1744 count = info->firm_size / 32;
1745
1746 address = (is_ft1604_chip ? 0x1000 : 0x800);
1747
1748 tk_debug_info(true, &info->client->dev, "%s reset\n", __func__);
1749 abov_tk_reset_for_bootmode(info);
1750 msleep(ABOV_BOOT_DELAY);
1751 if (is_ft1604_chip)
1752 ret = abov_tk_fw_mode_enter_ft1604(info);
1753 else
1754 ret = abov_tk_fw_mode_enter(info);
1755 if(ret<0){
1756 tk_debug_err(true, &info->client->dev,
1757 "%s:abov_tk_fw_mode_enter fail\n", __func__);
1758 return ret;
1759 }
1760 usleep_range(5 * 1000, 5 * 1000);
1761 tk_debug_info(true, &info->client->dev, "%s fw_mode_cmd sended\n", __func__);
1762
1763 if (is_ft1604_chip) {
1764 tk_debug_info(true, &info->client->dev, "%s ft1604: skipping abov_tk_fw_mode_check() and abov_tk_flash_erase()\n", __func__);
1765 } else {
1766 if(abov_tk_fw_mode_check(info) != 1) {
1767 pr_err("%s: err, flash mode is not: %d\n", __func__, ret);
1768 return 0;
1769 }
1770 ret = abov_tk_flash_erase(info);
1771 }
1772
1773 msleep(1300);
1774 tk_debug_info(true, &info->client->dev, "%s fw_write start\n", __func__);
1775
1776 for (ii = 1; ii < count; ii++) {
1777 /* first 32byte is header */
1778 addrH = (unsigned char)((address >> 8) & 0xFF);
1779 addrL = (unsigned char)(address & 0xFF);
1780 if (cmd == BUILT_IN)
1781 memcpy(data, &info->firm_data_bin->data[ii * 32], 32);
1782 else if (cmd == SDCARD)
1783 memcpy(data, &info->firm_data_ums[ii * 32], 32);
1784
1785 ret = abov_tk_fw_write(info, &addrH, &addrL, data);
1786 if (ret < 0) {
1787 tk_debug_err(true, &info->client->dev,
1788 "%s: err, no device : %d\n", __func__, ret);
1789 return ret;
1790 }
1791
1792 address += 0x20;
1793
1794 memset(data, 0, 32);
1795 }
1796 if (is_ft1604_chip)
1797 ret = abov_tk_i2c_read_checksum_ft1604(info);
1798 else
1799 ret = abov_tk_i2c_read_checksum(info);
1800 tk_debug_dbg(true, &info->client->dev, "%s checksum readed\n", __func__);
1801
1802 ret = abov_tk_fw_mode_exit(info);
1803 tk_debug_info(true, &info->client->dev, "%s fw_write end\n", __func__);
1804
1805 return ret;
1806 }
1807
1808 static void abov_release_fw(struct abov_tk_info *info, u8 cmd)
1809 {
1810 switch(cmd) {
1811 case BUILT_IN:
1812 release_firmware(info->firm_data_bin);
1813 break;
1814
1815 case SDCARD:
1816 kfree(info->firm_data_ums);
1817 break;
1818
1819 default:
1820 break;
1821 }
1822 }
1823
1824 static int abov_flash_fw(struct abov_tk_info *info, bool probe, u8 cmd)
1825 {
1826 struct i2c_client *client = info->client;
1827 int retry = 2;
1828 int ret;
1829 int block_count;
1830 const u8 *fw_data;
1831
1832 switch(cmd) {
1833 case BUILT_IN:
1834 fw_data = info->firm_data_bin->data;
1835 break;
1836
1837 case SDCARD:
1838 fw_data = info->firm_data_ums;
1839 break;
1840
1841 default:
1842 return -1;
1843 break;
1844 }
1845
1846 block_count = (int)(info->firm_size / 32);
1847
1848 while (retry--) {
1849 ret = abov_tk_fw_update(info, cmd);
1850 if (ret < 0)
1851 break;
1852 #if ABOV_ISP_FIRMUP_ROUTINE
1853 abov_tk_reset_for_bootmode(info);
1854 abov_fw_update(info, fw_data, block_count,
1855 info->pdata->gpio_scl, info->pdata->gpio_sda);
1856 #endif
1857
1858 if ((info->checksum_h != info->checksum_h_bin) ||
1859 (info->checksum_l != info->checksum_l_bin)) {
1860 tk_debug_err(true, &client->dev,
1861 "%s checksum fail.(0x%x,0x%x),(0x%x,0x%x) retry:%d\n",
1862 __func__, info->checksum_h, info->checksum_l,
1863 info->checksum_h_bin, info->checksum_l_bin, retry);
1864 ret = -1;
1865 continue;
1866 }else
1867 tk_debug_info(true, &client->dev,"%s checksum successed.\n",__func__);
1868
1869 abov_tk_reset_for_bootmode(info);
1870 msleep(ABOV_RESET_DELAY);
1871 ret = get_tk_fw_version(info, true);
1872 if (ret) {
1873 tk_debug_err(true, &client->dev, "%s fw version read fail\n", __func__);
1874 ret = -1;
1875 continue;
1876 }
1877
1878 if (info->fw_ver == 0) {
1879 tk_debug_err(true, &client->dev, "%s fw version fail (0x%x)\n",
1880 __func__, info->fw_ver);
1881 ret = -1;
1882 continue;
1883 }
1884
1885 if ((cmd == BUILT_IN) && (info->fw_ver != info->fw_ver_bin)){
1886 tk_debug_err(true, &client->dev, "%s fw version fail 0x%x, 0x%x\n",
1887 __func__, info->fw_ver, info->fw_ver_bin);
1888 ret = -1;
1889 continue;
1890 }
1891 ret = 0;
1892 break;
1893 }
1894
1895 return ret;
1896 }
1897
1898 static ssize_t touchkey_fw_update(struct device *dev,
1899 struct device_attribute *attr, const char *buf, size_t count)
1900 {
1901 struct abov_tk_info *info = dev_get_drvdata(dev);
1902 struct i2c_client *client = info->client;
1903 int ret;
1904 u8 cmd;
1905
1906 switch(*buf) {
1907 case 's':
1908 case 'S':
1909 cmd = BUILT_IN;
1910 break;
1911 case 'i':
1912 case 'I':
1913 cmd = SDCARD;
1914 break;
1915 default:
1916 info->fw_update_state = 2;
1917 goto touchkey_fw_update_out;
1918 }
1919
1920 ret = abov_load_fw(info, cmd);
1921 if (ret) {
1922 tk_debug_err(true, &client->dev,
1923 "%s fw load fail\n", __func__);
1924 info->fw_update_state = 2;
1925 goto touchkey_fw_update_out;
1926 }
1927
1928 info->fw_update_state = 1;
1929 disable_irq(info->irq);
1930 info->enabled = false;
1931 ret = abov_flash_fw(info, false, cmd);
1932 if (info->flip_mode){
1933 abov_mode_enable(client, ABOV_FLIP, CMD_FLIP_ON);
1934 } else {
1935 if (info->glovemode)
1936 abov_mode_enable(client, ABOV_GLOVE, CMD_GLOVE_ON);
1937 }
1938 if (info->keyboard_mode)
1939 abov_mode_enable(client, ABOV_KEYBOARD, CMD_MOBILE_KBD_ON);
1940
1941 info->enabled = true;
1942 enable_irq(info->irq);
1943 if (ret) {
1944 tk_debug_err(true, &client->dev, "%s fail\n", __func__);
1945 // info->fw_update_state = 2;
1946 info->fw_update_state = 0;
1947
1948 } else {
1949 tk_debug_info(true, &client->dev, "%s success\n", __func__);
1950 info->fw_update_state = 0;
1951 }
1952
1953 abov_release_fw(info, cmd);
1954
1955 touchkey_fw_update_out:
1956 tk_debug_dbg(true, &client->dev, "%s : %d\n", __func__, info->fw_update_state);
1957
1958 return count;
1959 }
1960
1961 static ssize_t touchkey_fw_update_status(struct device *dev,
1962 struct device_attribute *attr, char *buf)
1963 {
1964 struct abov_tk_info *info = dev_get_drvdata(dev);
1965 struct i2c_client *client = info->client;
1966 int count = 0;
1967
1968 tk_debug_info(true, &client->dev, "%s : %d\n", __func__, info->fw_update_state);
1969
1970 if (info->fw_update_state == 0)
1971 count = sprintf(buf, "PASS\n");
1972 else if (info->fw_update_state == 1)
1973 count = sprintf(buf, "Downloading\n");
1974 else if (info->fw_update_state == 2)
1975 count = sprintf(buf, "Fail\n");
1976
1977 return count;
1978 }
1979
1980 static ssize_t abov_glove_mode(struct device *dev,
1981 struct device_attribute *attr, const char *buf, size_t count)
1982 {
1983 struct abov_tk_info *info = dev_get_drvdata(dev);
1984 struct i2c_client *client = info->client;
1985 int scan_buffer;
1986 int ret;
1987 u8 cmd;
1988
1989 ret = sscanf(buf, "%d", &scan_buffer);
1990 tk_debug_info(true, &client->dev, "%s : %d\n", __func__, scan_buffer);
1991
1992
1993 if (ret != 1) {
1994 tk_debug_err(true, &client->dev, "%s: cmd read err\n", __func__);
1995 return count;
1996 }
1997
1998 if (!(scan_buffer == 0 || scan_buffer == 1)) {
1999 tk_debug_err(true, &client->dev, "%s: wrong command(%d)\n",
2000 __func__, scan_buffer);
2001 return count;
2002 }
2003
2004 if (!info->enabled)
2005 return count;
2006
2007 if (info->glovemode == scan_buffer) {
2008 tk_debug_dbg(true, &client->dev, "%s same command(%d)\n",
2009 __func__, scan_buffer);
2010 return count;
2011 }
2012
2013 if (scan_buffer == 1) {
2014 cmd = CMD_GLOVE_ON;
2015 } else {
2016 cmd = CMD_GLOVE_OFF;
2017 }
2018
2019 ret = abov_mode_enable(client, ABOV_GLOVE, cmd);
2020 if (ret < 0) {
2021 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2022 return count;
2023 }
2024
2025 info->glovemode = scan_buffer;
2026
2027 return count;
2028 }
2029
2030 static ssize_t abov_glove_mode_show(struct device *dev,
2031 struct device_attribute *attr, char *buf)
2032 {
2033 struct abov_tk_info *info = dev_get_drvdata(dev);
2034
2035 return sprintf(buf, "%d\n", info->glovemode);
2036 }
2037
2038 static ssize_t keyboard_cover_mode_enable(struct device *dev,
2039 struct device_attribute *attr,
2040 const char *buf, size_t count)
2041 {
2042 struct abov_tk_info *info = dev_get_drvdata(dev);
2043 struct i2c_client *client = info->client;
2044 int keyboard_mode_on;
2045 int ret;
2046 u8 cmd;
2047
2048 tk_debug_dbg(true, &client->dev, "%s : Mobile KBD sysfs node called\n",__func__);
2049
2050 sscanf(buf, "%d", &keyboard_mode_on);
2051 tk_debug_info(true, &client->dev, "%s : %d\n",
2052 __func__, keyboard_mode_on);
2053
2054 if (!(keyboard_mode_on == 0 || keyboard_mode_on == 1)) {
2055 tk_debug_err(true, &client->dev, "%s: wrong command(%d)\n",
2056 __func__, keyboard_mode_on);
2057 return count;
2058 }
2059
2060 if (!info->enabled)
2061 goto out;
2062
2063 if (info->keyboard_mode == keyboard_mode_on) {
2064 tk_debug_dbg(true, &client->dev, "%s same command(%d)\n",
2065 __func__, keyboard_mode_on);
2066 goto out;
2067 }
2068
2069 if (keyboard_mode_on == 1) {
2070 cmd = CMD_MOBILE_KBD_ON;
2071 } else {
2072 cmd = CMD_MOBILE_KBD_OFF;
2073 }
2074
2075 /* mobile keyboard use same register with glove mode */
2076 ret = abov_mode_enable(client, ABOV_KEYBOARD, cmd);
2077 if (ret < 0) {
2078 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2079 goto out;
2080 }
2081
2082 out:
2083 info->keyboard_mode = keyboard_mode_on;
2084 return count;
2085 }
2086
2087 static ssize_t flip_cover_mode_enable(struct device *dev,
2088 struct device_attribute *attr,
2089 const char *buf, size_t count)
2090 {
2091 struct abov_tk_info *info = dev_get_drvdata(dev);
2092 struct i2c_client *client = info->client;
2093 int flip_mode_on;
2094 int ret;
2095 u8 cmd;
2096
2097 sscanf(buf, "%d\n", &flip_mode_on);
2098 tk_debug_info(true, &client->dev, "%s : %d\n", __func__, flip_mode_on);
2099
2100 if (!info->enabled)
2101 goto out;
2102
2103 #ifdef CONFIG_TOUCHKEY_GRIP
2104 if (flip_mode_on) {
2105 cmd = 0x10;
2106 ret = abov_tk_i2c_write(info->client, ABOV_SW_RESET, &cmd, 1);
2107 if (ret < 0) {
2108 tk_debug_err(true, &client->dev, "%s sw_reset fail(%d)\n", __func__, ret);
2109 }
2110 abov_sar_olny_mode(info, 1);
2111 } else {
2112 abov_sar_olny_mode(info, 0);
2113 }
2114 #else
2115 /* glove mode control */
2116 if (flip_mode_on) {
2117 cmd = CMD_FLIP_ON;
2118 } else {
2119 if (info->glovemode)
2120 cmd = CMD_GLOVE_ON;
2121 cmd = CMD_FLIP_OFF;
2122 }
2123
2124 if (info->glovemode){
2125 ret = abov_mode_enable(client, ABOV_GLOVE, cmd);
2126 if (ret < 0) {
2127 tk_debug_err(true, &client->dev, "%s glove mode fail(%d)\n", __func__, ret);
2128 goto out;
2129 }
2130 } else{
2131 ret = abov_mode_enable(client, ABOV_FLIP, cmd);
2132 if (ret < 0) {
2133 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2134 goto out;
2135 }
2136 }
2137 #endif
2138
2139 out:
2140 info->flip_mode = flip_mode_on;
2141 return count;
2142 }
2143
2144 static DEVICE_ATTR(touchkey_threshold, S_IRUGO, touchkey_threshold_show, NULL);
2145 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2146 touchkey_led_control);
2147 #ifdef CONFIG_TOUCHKEY_GRIP
2148 static DEVICE_ATTR(touchkey_grip_threshold, S_IRUGO, touchkey_grip_threshold_show, NULL);
2149 static DEVICE_ATTR(touchkey_total_cap, S_IRUGO, touchkey_total_cap_show, NULL);
2150 static DEVICE_ATTR(sar_enable, S_IRUGO | S_IWUSR | S_IWGRP, NULL, touchkey_sar_enable);
2151 static DEVICE_ATTR(sw_reset, S_IRUGO | S_IWUSR | S_IWGRP, NULL, touchkey_grip_sw_reset);
2152 static DEVICE_ATTR(touchkey_earjack, S_IRUGO | S_IWUSR | S_IWGRP, NULL, touchkey_sensing_change);
2153 static DEVICE_ATTR(touchkey_grip, S_IRUGO, touchkey_grip_show, NULL);
2154 static DEVICE_ATTR(touchkey_grip_baseline, S_IRUGO, touchkey_grip_baseline_show, NULL);
2155 static DEVICE_ATTR(touchkey_grip_raw, S_IRUGO, touchkey_grip_raw_show, NULL);
2156 static DEVICE_ATTR(touchkey_grip_gain, S_IRUGO, touchkey_grip_gain_show, NULL);
2157 static DEVICE_ATTR(touchkey_grip_check, S_IRUGO, touchkey_grip_check_show, NULL);
2158 #ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
2159 static DEVICE_ATTR(touchkey_sar_only_mode, S_IRUGO | S_IWUSR | S_IWGRP | S_IWOTH,
2160 NULL, touchkey_mode_change);
2161 static DEVICE_ATTR(touchkey_sar_press_threshold, S_IRUGO | S_IWUSR | S_IWGRP | S_IWOTH,
2162 NULL, touchkey_sar_press_threshold_store);
2163 static DEVICE_ATTR(touchkey_sar_release_threshold, S_IRUGO | S_IWUSR | S_IWGRP | S_IWOTH,
2164 NULL, touchkey_sar_release_threshold_store);
2165 #endif
2166 #endif
2167 static DEVICE_ATTR(touchkey_recent, S_IRUGO, touchkey_menu_show, NULL);
2168 static DEVICE_ATTR(touchkey_back, S_IRUGO, touchkey_back_show, NULL);
2169 static DEVICE_ATTR(touchkey_recent_raw, S_IRUGO, touchkey_menu_raw_show, NULL);
2170 static DEVICE_ATTR(touchkey_back_raw, S_IRUGO, touchkey_back_raw_show, NULL);
2171 static DEVICE_ATTR(touchkey_chip_name, S_IRUGO, touchkey_chip_name, NULL);
2172 static DEVICE_ATTR(touchkey_firm_version_phone, S_IRUGO, bin_fw_ver, NULL);
2173 static DEVICE_ATTR(touchkey_firm_version_panel, S_IRUGO, read_fw_ver, NULL);
2174 static DEVICE_ATTR(touchkey_firm_update, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2175 touchkey_fw_update);
2176 static DEVICE_ATTR(touchkey_firm_update_status, S_IRUGO | S_IWUSR | S_IWGRP,
2177 touchkey_fw_update_status, NULL);
2178 static DEVICE_ATTR(glove_mode, S_IRUGO | S_IWUSR | S_IWGRP,
2179 abov_glove_mode_show, abov_glove_mode);
2180 static DEVICE_ATTR(keyboard_mode, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2181 keyboard_cover_mode_enable);
2182 static DEVICE_ATTR(flip_mode, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2183 flip_cover_mode_enable);
2184
2185 static struct attribute *sec_touchkey_attributes[] = {
2186 &dev_attr_touchkey_threshold.attr,
2187 &dev_attr_brightness.attr,
2188 #ifdef CONFIG_TOUCHKEY_GRIP
2189 &dev_attr_touchkey_grip_threshold.attr,
2190 &dev_attr_touchkey_total_cap.attr,
2191 &dev_attr_sar_enable.attr,
2192 &dev_attr_sw_reset.attr,
2193 &dev_attr_touchkey_earjack.attr,
2194 &dev_attr_touchkey_grip.attr,
2195 &dev_attr_touchkey_grip_baseline.attr,
2196 &dev_attr_touchkey_grip_raw.attr,
2197 &dev_attr_touchkey_grip_gain.attr,
2198 &dev_attr_touchkey_grip_check.attr,
2199 #ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
2200 &dev_attr_touchkey_sar_only_mode.attr,
2201 &dev_attr_touchkey_sar_press_threshold.attr,
2202 &dev_attr_touchkey_sar_release_threshold.attr,
2203 #endif
2204 #endif
2205 &dev_attr_touchkey_recent.attr,
2206 &dev_attr_touchkey_back.attr,
2207 &dev_attr_touchkey_recent_raw.attr,
2208 &dev_attr_touchkey_back_raw.attr,
2209 &dev_attr_touchkey_chip_name.attr,
2210 &dev_attr_touchkey_firm_version_phone.attr,
2211 &dev_attr_touchkey_firm_version_panel.attr,
2212 &dev_attr_touchkey_firm_update.attr,
2213 &dev_attr_touchkey_firm_update_status.attr,
2214 &dev_attr_glove_mode.attr,
2215 &dev_attr_keyboard_mode.attr,
2216 &dev_attr_flip_mode.attr,
2217 NULL,
2218 };
2219
2220 static struct attribute_group sec_touchkey_attr_group = {
2221 .attrs = sec_touchkey_attributes,
2222 };
2223
2224 extern int get_samsung_lcd_attached(void);
2225
2226 static int abov_tk_fw_check(struct abov_tk_info *info)
2227 {
2228 struct i2c_client *client = info->client;
2229 int ret, fw_update = 0;
2230 u8 buf;
2231
2232 ret = abov_load_fw(info, BUILT_IN);
2233 if (ret) {
2234 tk_debug_err(true, &client->dev,
2235 "%s fw load fail\n", __func__);
2236 return ret;
2237 }
2238
2239 ret = get_tk_fw_version(info, true);
2240
2241 #ifdef LED_TWINKLE_BOOTING
2242 if(ret)
2243 tk_debug_err(true, &client->dev,
2244 "%s: i2c fail...[%d], addr[%d]\n",
2245 __func__, ret, info->client->addr);
2246 tk_debug_err(true, &client->dev,
2247 "%s: touchkey driver unload\n", __func__);
2248
2249 if (get_samsung_lcd_attached() == 0) {
2250 tk_debug_err(true, &client->dev, "%s : get_samsung_lcd_attached()=0 \n", __func__);
2251 abov_release_fw(info, BUILT_IN);
2252 return ret;
2253 }
2254 #endif
2255
2256 //Check Model No.
2257 ret = abov_tk_i2c_read(client, ABOV_MODEL_NUMBER, &buf, 1);
2258 if (ret < 0) {
2259 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2260 }
2261 if(info->fw_model_number != buf){
2262 tk_debug_info(true, &client->dev, "fw model number = %x ic model number = %x \n", info->fw_model_number, buf);
2263 fw_update = 1;
2264 goto flash_fw;
2265 }
2266
2267 if ((info->fw_ver == 0) || info->fw_ver < info->fw_ver_bin){
2268 tk_debug_dbg(true, &client->dev, "excute tk firmware update (0x%x -> 0x%x\n",
2269 info->fw_ver, info->fw_ver_bin);
2270 fw_update = 1;
2271 }
2272
2273 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
2274 if(info->fw_ver >= 0xd0){ //test firmware
2275 tk_debug_dbg(true, &client->dev, "excute tk firmware update (0x%x -> 0x%x\n",
2276 info->fw_ver, info->fw_ver_bin);
2277 fw_update = 1;
2278 }
2279 #endif
2280
2281 flash_fw:
2282 if(fw_update){
2283 ret = abov_flash_fw(info, true, BUILT_IN);
2284 if (ret) {
2285 tk_debug_err(true, &client->dev,
2286 "failed to abov_flash_fw (%d)\n", ret);
2287 } else {
2288 tk_debug_info(true, &client->dev,
2289 "fw update success\n");
2290 }
2291 }
2292
2293 abov_release_fw(info, BUILT_IN);
2294
2295 return ret;
2296 }
2297
2298 static int abov_power(void *data, bool on)
2299 {
2300 struct abov_tk_info *info = (struct abov_tk_info *)data;
2301 struct i2c_client *client = info->client;
2302
2303 int ret = 0;
2304
2305 info->pdata->avdd_vreg = regulator_get(NULL, "vtouch_3.3v");
2306 if (IS_ERR(info->pdata->avdd_vreg)) {
2307 info->pdata->avdd_vreg = NULL;
2308 tk_debug_err(true, &client->dev, "%s : avdd_vreg get error, ignoring\n", __func__);
2309 }
2310 info->pdata->dvdd_vreg = regulator_get(NULL, "vtouch_2.8v");
2311 if (IS_ERR(info->pdata->dvdd_vreg)) {
2312 info->pdata->dvdd_vreg = NULL;
2313 tk_debug_err(true, &client->dev, "%s : dvdd_vreg get error, ignoring\n",__func__);
2314 }
2315
2316 if (on) {
2317 if (info->pdata->avdd_vreg) {
2318 ret = regulator_enable(info->pdata->avdd_vreg);
2319 if(ret){
2320 tk_debug_err(true, &client->dev, "%s : avdd reg enable fail\n", __func__);
2321 }
2322 }
2323 if (info->pdata->dvdd_vreg) {
2324 ret = regulator_enable(info->pdata->dvdd_vreg);
2325 if(ret){
2326 tk_debug_err(true, &client->dev, "%s : dvdd reg enable fail\n", __func__);
2327 }
2328 }
2329 } else {
2330 if (info->pdata->avdd_vreg) {
2331 ret = regulator_disable(info->pdata->avdd_vreg);
2332 if(ret){
2333 tk_debug_err(true, &client->dev, "%s : avdd reg disable fail\n", __func__);
2334 }
2335 }
2336 if (info->pdata->dvdd_vreg) {
2337 ret = regulator_disable(info->pdata->dvdd_vreg);
2338 if(ret){
2339 tk_debug_err(true, &client->dev, "%s : dvdd reg disable fail\n", __func__);
2340 }
2341 }
2342 }
2343 regulator_put(info->pdata->avdd_vreg);
2344 regulator_put(info->pdata->dvdd_vreg);
2345
2346 tk_debug_info(true, &client->dev, "%s %s\n", __func__, on ? "on" : "off");
2347
2348 return ret;
2349 }
2350
2351 #if 1
2352 static int abov_pinctrl_configure(struct abov_tk_info *info,
2353 bool active)
2354 {
2355 struct pinctrl_state *set_state;
2356 int retval;
2357
2358 if (active) {
2359 set_state =
2360 pinctrl_lookup_state(info->pinctrl,
2361 "on_irq");
2362 if (IS_ERR(set_state)) {
2363 tk_debug_err(true, &info->client->dev,
2364 "%s: cannot get ts pinctrl active state\n", __func__);
2365 return PTR_ERR(set_state);
2366 }
2367 } else {
2368 set_state =
2369 pinctrl_lookup_state(info->pinctrl,
2370 "off_irq");
2371 if (IS_ERR(set_state)) {
2372 tk_debug_err(true, &info->client->dev,
2373 "%s: cannot get gpiokey pinctrl sleep state\n", __func__);
2374 return PTR_ERR(set_state);
2375 }
2376 }
2377 retval = pinctrl_select_state(info->pinctrl, set_state);
2378 if (retval) {
2379 tk_debug_err(true, &info->client->dev,
2380 "%s: cannot set ts pinctrl active state\n", __func__);
2381 return retval;
2382 }
2383
2384 return 0;
2385 }
2386 #endif
2387 static int abov_gpio_reg_init(struct device *dev,
2388 struct abov_touchkey_platform_data *pdata)
2389 {
2390 int ret = 0;
2391
2392 ret = gpio_request(pdata->gpio_int, "tkey_gpio_int");
2393 if(ret < 0){
2394 tk_debug_err(true, dev,
2395 "unable to request gpio_int\n");
2396 return ret;
2397 }
2398
2399 pdata->power = abov_power;
2400
2401 return ret;
2402 }
2403
2404 #ifdef CONFIG_OF
2405 static int abov_parse_dt(struct device *dev,
2406 struct abov_touchkey_platform_data *pdata)
2407 {
2408 struct device_node *np = dev->of_node;
2409 int ret;
2410 //u32 tmp[2] = {0, };
2411
2412 pdata->gpio_int = of_get_named_gpio(np, "abov,irq-gpio", 0);
2413 if(pdata->gpio_int < 0){
2414 tk_debug_err(true, dev, "unable to get gpio_int\n");
2415 return pdata->gpio_int;
2416 }
2417
2418 pdata->gpio_scl = of_get_named_gpio(np, "abov,scl-gpio", 0);
2419 if(pdata->gpio_scl < 0){
2420 tk_debug_err(true, dev, "unable to get gpio_scl\n");
2421 return pdata->gpio_scl;
2422 }
2423
2424 pdata->gpio_sda = of_get_named_gpio(np, "abov,sda-gpio", 0);
2425 if(pdata->gpio_sda < 0){
2426 tk_debug_err(true, dev, "unable to get gpio_sda\n");
2427 return pdata->gpio_sda;
2428 }
2429
2430 pdata->sub_det = of_get_named_gpio(np, "abov,sub-det",0);
2431 if(pdata->sub_det < 0){
2432 tk_debug_info(true, dev, "unable to get sub_det\n");
2433 }else{
2434 tk_debug_info(true, dev, "%s: sub_det:%d\n",__func__,pdata->sub_det);
2435 }
2436
2437 if (is_ft1604_chip) {
2438 pdata->fw_path = fw_path;
2439 }
2440 else {
2441 ret = of_property_read_string(np, "abov,fw_path", (const char **)&pdata->fw_path);
2442 if (ret) {
2443 tk_debug_err(true, dev, "touchkey:failed to read fw_path %d\n", ret);
2444 pdata->fw_path = TK_FW_PATH_BIN;
2445 }
2446 }
2447 tk_debug_info(true, dev, "%s: fw path %s\n", __func__, pdata->fw_path);
2448
2449 pdata->boot_on_ldo = of_property_read_bool(np, "abov,boot-on-ldo");
2450
2451 tk_debug_info(true, dev, "%s: gpio_int:%d, gpio_scl:%d, gpio_sda:%d\n",
2452 __func__, pdata->gpio_int, pdata->gpio_scl,
2453 pdata->gpio_sda);
2454
2455 if (is_ft1604_chip) { // only for compatibility
2456 //temp A5x ltn
2457 pdata->ds_det = of_get_named_gpio(np, "abov,ds-det",0);
2458 if(pdata->ds_det < 0) {
2459 tk_debug_info(true, dev, "don't check ds_det\n");
2460 }
2461 else {
2462 tk_debug_info(true, dev, "%s: A5 LTN temp ds_det:%d\n",__func__,pdata->ds_det);
2463 }
2464 }
2465
2466 return 0;
2467 }
2468 #else
2469 static int abov_parse_dt(struct device *dev,
2470 struct abov_touchkey_platform_data *pdata)
2471 {
2472 return -ENODEV;
2473 }
2474 #endif
2475
2476 static int abov_tk_probe(struct i2c_client *client,
2477 const struct i2c_device_id *id)
2478 {
2479 struct abov_tk_info *info;
2480 struct input_dev *input_dev;
2481 #if 0
2482 struct device *touchkey_dev;
2483 int i;
2484 #endif
2485 int ret = 0;
2486
2487 #ifdef LED_TWINKLE_BOOTING
2488 if (get_samsung_lcd_attached() == 0) {
2489 tk_debug_err(true, &client->dev, "%s : get_samsung_lcd_attached()=0 \n", __func__);
2490 return -EIO;
2491 }
2492 #endif
2493 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
2494 tk_debug_err(true, &client->dev,
2495 "i2c_check_functionality fail\n");
2496 return -EIO;
2497 }
2498
2499 info = kzalloc(sizeof(struct abov_tk_info), GFP_KERNEL);
2500 if (!info) {
2501 tk_debug_err(true, &client->dev, "Failed to allocate memory\n");
2502 ret = -ENOMEM;
2503 goto err_alloc;
2504 }
2505
2506 input_dev = input_allocate_device();
2507 if (!input_dev) {
2508 tk_debug_err(true, &client->dev,
2509 "Failed to allocate memory for input device\n");
2510 ret = -ENOMEM;
2511 goto err_input_alloc;
2512 }
2513
2514 info->client = client;
2515 info->input_dev = input_dev;
2516
2517 #ifdef CONFIG_TOUCHKEY_GRIP
2518 wake_lock_init(&info->touckey_wake_lock, WAKE_LOCK_SUSPEND, "touchkey wake lock");
2519 #endif
2520
2521 if (client->dev.of_node) {
2522 struct abov_touchkey_platform_data *pdata;
2523 pdata = devm_kzalloc(&client->dev,
2524 sizeof(struct abov_touchkey_platform_data), GFP_KERNEL);
2525 if (!pdata) {
2526 tk_debug_err(true, &client->dev, "Failed to allocate memory\n");
2527 ret = -ENOMEM;
2528 goto err_config;
2529 }
2530
2531 ret = abov_parse_dt(&client->dev, pdata);
2532 if (ret){
2533 tk_debug_err(true, &client->dev, "failed to abov_parse_dt\n");
2534 ret = -ENOMEM;
2535 goto err_config;
2536 }
2537
2538 info->pdata = pdata;
2539 } else
2540 info->pdata = client->dev.platform_data;
2541
2542 if (is_ft1604_chip) { // only for compatibility
2543 //temp A5x ltn
2544 if (gpio_is_valid(info->pdata->ds_det)) {
2545 ret = gpio_get_value(info->pdata->ds_det);
2546 if (ret == 0) {
2547 tk_debug_err(true, &client->dev, "%s : 1804 DS_DET is low\n",__func__);
2548 }
2549 else if (ret == 1) {
2550 tk_debug_err(true, &client->dev, "%s : 1804 DS_DET is high\n",__func__);
2551 ret = -1;
2552 goto err_config;
2553 }
2554 }
2555 }
2556
2557 if (info->pdata == NULL) {
2558 tk_debug_err(true, &client->dev, "failed to get platform data\n");
2559 goto err_config;
2560 }
2561 #if 1
2562 /* Get pinctrl if target uses pinctrl */
2563 info->pinctrl = devm_pinctrl_get(&client->dev);
2564 if (IS_ERR(info->pinctrl)) {
2565 if (PTR_ERR(info->pinctrl) == -EPROBE_DEFER)
2566 goto err_config;
2567
2568 tk_debug_err(true, &client->dev, "%s: Target does not use pinctrl\n", __func__);
2569 info->pinctrl = NULL;
2570 }
2571
2572 if (info->pinctrl) {
2573 ret = abov_pinctrl_configure(info, true);
2574 if (ret)
2575 tk_debug_err(true, &client->dev,
2576 "%s: cannot set ts pinctrl active state\n", __func__);
2577 }
2578
2579 /* sub-det pinctrl */
2580 if (gpio_is_valid(info->pdata->sub_det)) {
2581 info->pinctrl_det = devm_pinctrl_get(&client->dev);
2582 if (IS_ERR(info->pinctrl_det)) {
2583 tk_debug_err(true, &client->dev, "%s: Failed to get pinctrl\n", __func__);
2584 goto err_config;
2585 }
2586
2587 info->pins_default = pinctrl_lookup_state(info->pinctrl_det, "sub_det");
2588 if (IS_ERR(info->pins_default)) {
2589 tk_debug_err(true, &client->dev, "%s: Failed to get pinctrl state\n", __func__);
2590 devm_pinctrl_put(info->pinctrl_det);
2591 goto err_config;
2592 }
2593
2594 ret = pinctrl_select_state(info->pinctrl_det, info->pins_default);
2595 if (ret < 0)
2596 tk_debug_err(true, &client->dev, "%s: Failed to configure sub_det pin\n", __func__);
2597 }
2598 #endif
2599 ret = abov_gpio_reg_init(&client->dev, info->pdata);
2600 if(ret){
2601 tk_debug_err(true, &client->dev, "failed to init reg\n");
2602 goto pwr_config;
2603 }
2604 if (info->pdata->power)
2605 info->pdata->power(info, true);
2606
2607 if(!info->pdata->boot_on_ldo)
2608 msleep(ABOV_RESET_DELAY);
2609
2610 if (gpio_is_valid(info->pdata->sub_det)) {
2611 ret = gpio_get_value(info->pdata->sub_det);
2612 if (ret) {
2613 tk_debug_err(true, &client->dev, "Device wasn't connected to board \n");
2614 ret = -ENODEV;
2615 goto err_i2c_check;
2616 }
2617 }
2618
2619 info->enabled = true;
2620 info->irq = -1;
2621 client->irq = gpio_to_irq(info->pdata->gpio_int);
2622
2623 mutex_init(&info->lock);
2624
2625 if (is_ft1604_chip) {
2626 //temp A5x ltn
2627 client->addr = 0x20;
2628 }
2629
2630 info->input_event = info->pdata->input_event;
2631 info->touchkey_count = sizeof(touchkey_keycode) / sizeof(int);
2632 i2c_set_clientdata(client, info);
2633
2634 ret = abov_tk_fw_check(info);
2635 if (ret) {
2636 tk_debug_err(true, &client->dev,
2637 "failed to firmware check (%d)\n", ret);
2638 goto err_reg_input_dev;
2639 }
2640
2641 ret = get_tk_fw_version(info, false);
2642 if (ret < 0) {
2643 tk_debug_err(true, &client->dev, "%s read fail\n", __func__);
2644 goto err_reg_input_dev;
2645 }
2646
2647 snprintf(info->phys, sizeof(info->phys),
2648 "%s/input0", dev_name(&client->dev));
2649 input_dev->name = "sec_touchkey";
2650 input_dev->phys = info->phys;
2651 input_dev->id.bustype = BUS_HOST;
2652 input_dev->dev.parent = &client->dev;
2653 #if 1 //def CONFIG_INPUT_ENABLED
2654 input_dev->open = abov_tk_input_open;
2655 input_dev->close = abov_tk_input_close;
2656 #endif
2657 set_bit(EV_KEY, input_dev->evbit);
2658 set_bit(KEY_RECENT, input_dev->keybit);
2659 set_bit(KEY_BACK, input_dev->keybit);
2660 set_bit(KEY_CP_GRIP, input_dev->keybit);
2661 set_bit(EV_LED, input_dev->evbit);
2662 set_bit(LED_MISC, input_dev->ledbit);
2663 input_set_drvdata(input_dev, info);
2664
2665 ret = input_register_device(input_dev);
2666 if (ret) {
2667 tk_debug_err(true, &client->dev, "failed to register input dev (%d)\n",
2668 ret);
2669 goto err_reg_input_dev;
2670 }
2671
2672 if (!info->pdata->irq_flag) {
2673 tk_debug_err(true, &client->dev, "no irq_flag\n");
2674 ret = request_threaded_irq(client->irq, NULL, abov_tk_interrupt,
2675 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, ABOV_TK_NAME, info);
2676 } else {
2677 ret = request_threaded_irq(client->irq, NULL, abov_tk_interrupt,
2678 info->pdata->irq_flag, ABOV_TK_NAME, info);
2679 }
2680 if (ret < 0) {
2681 tk_debug_err(true, &client->dev, "Failed to register interrupt\n");
2682 goto err_req_irq;
2683 }
2684 info->irq = client->irq;
2685
2686 #ifdef CONFIG_HAS_EARLYSUSPEND
2687 info->early_suspend.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING;
2688 info->early_suspend.suspend = abov_tk_early_suspend;
2689 info->early_suspend.resume = abov_tk_late_resume;
2690 register_early_suspend(&info->early_suspend);
2691 #endif
2692
2693 info->dev = sec_device_create(info, "sec_touchkey");
2694 if (IS_ERR(info->dev))
2695 tk_debug_err(true, &client->dev,
2696 "Failed to create device for the touchkey sysfs\n");
2697
2698 ret = sysfs_create_group(&info->dev ->kobj,
2699 &sec_touchkey_attr_group);
2700 if (ret)
2701 tk_debug_err(true, &client->dev, "Failed to create sysfs group\n");
2702
2703 ret = sysfs_create_link(&info->dev ->kobj,
2704 &info->input_dev->dev.kobj, "input");
2705 if (ret < 0) {
2706 tk_debug_err(true, &client->dev,
2707 "%s: Failed to create input symbolic link\n",
2708 __func__);
2709 }
2710
2711
2712 #ifdef LED_TWINKLE_BOOTING
2713 if (get_samsung_lcd_attached() == 0) {
2714 tk_debug_err(true, &client->dev,
2715 "%s : get_samsung_lcd_attached()=0, so start LED twinkle \n", __func__);
2716
2717 INIT_DELAYED_WORK(&info->led_twinkle_work, led_twinkle_work);
2718 info->led_twinkle_check = 1;
2719
2720 schedule_delayed_work(&info->led_twinkle_work, msecs_to_jiffies(400));
2721 }
2722 #endif
2723 #ifdef CONFIG_TOUCHKEY_GRIP
2724 info->sar_sensing = 1;
2725 device_init_wakeup(&client->dev, true);
2726 #endif
2727
2728 tk_debug_err(true, &client->dev, "%s done\n", __func__);
2729
2730 #ifdef CONFIG_TOUCHKEY_GRIP
2731 if (lpcharge == 1) {
2732 disable_irq(info->irq);
2733 tk_debug_err(true, &client->dev, "%s disable_irq\n", __func__);
2734 abov_sar_olny_mode(info, 1);
2735 }
2736 #endif
2737 return 0;
2738
2739 err_req_irq:
2740 input_unregister_device(input_dev);
2741 err_reg_input_dev:
2742 mutex_destroy(&info->lock);
2743 gpio_free(info->pdata->gpio_int);
2744 err_i2c_check:
2745 if (info->pdata->power)
2746 info->pdata->power(info, false);
2747 pwr_config:
2748 err_config:
2749 #ifdef CONFIG_TOUCHKEY_GRIP
2750 wake_lock_destroy(&info->touckey_wake_lock);
2751 #endif
2752 input_free_device(input_dev);
2753 err_input_alloc:
2754 kfree(info);
2755 err_alloc:
2756 tk_debug_err(true, &client->dev, "%s fail\n",__func__);
2757 return ret;
2758
2759 }
2760
2761
2762 #ifdef LED_TWINKLE_BOOTING
2763 static void led_twinkle_work(struct work_struct *work)
2764 {
2765 struct abov_tk_info *info = container_of(work, struct abov_tk_info,
2766 led_twinkle_work.work);
2767 static bool led_on = 1;
2768 static int count = 0;
2769 tk_debug_info(true, &info->client->dev, "%s, on=%d, c=%d\n",__func__, led_on, count++ );
2770
2771 if(info->led_twinkle_check == 1){
2772
2773 touchkey_led_set(info,led_on);
2774 if(led_on) led_on = 0;
2775 else led_on = 1;
2776
2777 schedule_delayed_work(&info->led_twinkle_work, msecs_to_jiffies(400));
2778 }else{
2779
2780 if(led_on == 0)
2781 touchkey_led_set(info, 0);
2782 }
2783
2784 }
2785 #endif
2786
2787 static int abov_tk_remove(struct i2c_client *client)
2788 {
2789 struct abov_tk_info *info = i2c_get_clientdata(client);
2790
2791 /* if (info->enabled)
2792 info->pdata->power(0);
2793 */
2794 info->enabled = false;
2795 #ifdef CONFIG_TOUCHKEY_GRIP
2796 device_init_wakeup(&client->dev, false);
2797 wake_lock_destroy(&info->touckey_wake_lock);
2798 #endif
2799 #ifdef CONFIG_HAS_EARLYSUSPEND
2800 unregister_early_suspend(&info->early_suspend);
2801 #endif
2802 if (info->irq >= 0)
2803 free_irq(info->irq, info);
2804 input_unregister_device(info->input_dev);
2805 input_free_device(info->input_dev);
2806 kfree(info);
2807
2808 return 0;
2809 }
2810
2811 static void abov_tk_shutdown(struct i2c_client *client)
2812 {
2813 struct abov_tk_info *info = i2c_get_clientdata(client);
2814 u8 cmd = CMD_LED_OFF;
2815 tk_debug_info(true, &client->dev, "Inside abov_tk_shutdown \n");
2816
2817 if (info->enabled){
2818 disable_irq(info->irq);
2819 abov_tk_i2c_write(client, ABOV_BTNSTATUS, &cmd, 1);
2820 info->pdata->power(info, false);
2821 }
2822 info->enabled = false;
2823
2824 // just power off.
2825 // if (info->irq >= 0)
2826 // free_irq(info->irq, info);
2827 // kfree(info);
2828 }
2829
2830 #if defined(CONFIG_PM) && !defined(CONFIG_TOUCHKEY_GRIP)
2831 static int abov_tk_suspend(struct device *dev)
2832 {
2833 struct i2c_client *client = to_i2c_client(dev);
2834 struct abov_tk_info *info = i2c_get_clientdata(client);
2835
2836 if (!info->enabled) {
2837 tk_debug_info(true, &client->dev, "%s: already power off\n", __func__);
2838 return 0;
2839 }
2840
2841 tk_debug_info(true, &client->dev, "%s: users=%d\n", __func__,
2842 info->input_dev->users);
2843
2844 disable_irq(info->irq);
2845 info->enabled = false;
2846 release_all_fingers(info);
2847
2848 if (info->pdata->power)
2849 info->pdata->power(info, false);
2850 return 0;
2851 }
2852
2853 static int abov_tk_resume(struct device *dev)
2854 {
2855 struct i2c_client *client = to_i2c_client(dev);
2856 struct abov_tk_info *info = i2c_get_clientdata(client);
2857 u8 led_data;
2858
2859 if (info->enabled) {
2860 tk_debug_info(true, &client->dev, "%s: already power on\n", __func__);
2861 return 0;
2862 }
2863
2864 tk_debug_info(true, &info->client->dev, "%s: users=%d\n", __func__,
2865 info->input_dev->users);
2866
2867 if (info->pdata->power) {
2868 info->pdata->power(info, true);
2869 msleep(ABOV_RESET_DELAY);
2870 } else /* touchkey on by i2c */
2871 get_tk_fw_version(info, true);
2872
2873 info->enabled = true;
2874
2875 if (abov_touchled_cmd_reserved && \
2876 abov_touchkey_led_status == CMD_LED_ON) {
2877 abov_touchled_cmd_reserved = 0;
2878 led_data=abov_touchkey_led_status;
2879
2880 abov_tk_i2c_write(client, ABOV_BTNSTATUS, &led_data, 1);
2881
2882 tk_debug_dbg(true, &info->client->dev, "%s: LED reserved on\n", __func__);
2883 }
2884 enable_irq(info->irq);
2885
2886 return 0;
2887 }
2888 #endif
2889
2890 #ifdef CONFIG_HAS_EARLYSUSPEND
2891 static void abov_tk_early_suspend(struct early_suspend *h)
2892 {
2893 struct abov_tk_info *info;
2894 info = container_of(h, struct abov_tk_info, early_suspend);
2895 abov_tk_suspend(&info->client->dev);
2896
2897 }
2898
2899 static void abov_tk_late_resume(struct early_suspend *h)
2900 {
2901 struct abov_tk_info *info;
2902 info = container_of(h, struct abov_tk_info, early_suspend);
2903 abov_tk_resume(&info->client->dev);
2904 }
2905 #endif
2906
2907 #if 1//def CONFIG_INPUT_ENABLED
2908 static int abov_tk_input_open(struct input_dev *dev)
2909 {
2910 struct abov_tk_info *info = input_get_drvdata(dev);
2911
2912 tk_debug_dbg(true, &info->client->dev, "%s %d \n",__func__,__LINE__);
2913 tk_debug_info(true, &info->client->dev, "%s: users=%d, v:0x%02x, g(%d), f(%d), k(%d)\n", __func__,
2914 info->input_dev->users, info->fw_ver, info->flip_mode, info->glovemode, info->keyboard_mode);
2915 #ifdef CONFIG_TOUCHKEY_GRIP
2916 if (lpcharge == 1) {
2917 tk_debug_info(true, &info->client->dev, "%s(lpcharge): sar_enable(%d)\n", __func__, info->sar_enable);
2918 return 0;
2919 }
2920
2921 tk_debug_info(true, &info->client->dev, "%s: sar_enable(%d)\n", __func__, info->sar_enable);
2922
2923 if (info->flip_mode)
2924 abov_sar_olny_mode(info, 1);
2925 else
2926 abov_sar_olny_mode(info, 0);
2927
2928 if (device_may_wakeup(&info->client->dev))
2929 disable_irq_wake(info->irq );
2930 #else
2931 abov_tk_resume(&info->client->dev);
2932 if (info->pinctrl)
2933 abov_pinctrl_configure(info, true);
2934
2935 if (info->flip_mode){
2936 abov_mode_enable(info->client, ABOV_FLIP, CMD_FLIP_ON);
2937 } else {
2938 if (info->glovemode)
2939 abov_mode_enable(info->client, ABOV_GLOVE, CMD_GLOVE_ON);
2940 }
2941 if (info->keyboard_mode)
2942 abov_mode_enable(info->client, ABOV_KEYBOARD, CMD_MOBILE_KBD_ON);
2943 #endif
2944 return 0;
2945 }
2946 static void abov_tk_input_close(struct input_dev *dev)
2947 {
2948 struct abov_tk_info *info = input_get_drvdata(dev);
2949
2950 tk_debug_dbg(true, &info->client->dev, "%s %d \n",__func__,__LINE__);
2951 tk_debug_info(true, &info->client->dev, "%s: users=%d\n", __func__,
2952 info->input_dev->users);
2953 #ifdef CONFIG_TOUCHKEY_GRIP
2954 tk_debug_info(true, &info->client->dev, "%s: sar_enable(%d)\n", __func__, info->sar_enable);
2955 abov_sar_olny_mode(info, 1);
2956
2957 if (device_may_wakeup(&info->client->dev))
2958 enable_irq_wake(info->irq );
2959 #else
2960 abov_tk_suspend(&info->client->dev);
2961 if (info->pinctrl)
2962 abov_pinctrl_configure(info, false);
2963 #endif
2964
2965 #ifdef LED_TWINKLE_BOOTING
2966 info->led_twinkle_check = 0;
2967 #endif
2968
2969 }
2970 #endif
2971
2972 #if 0//defined(CONFIG_PM) && !defined(CONFIG_HAS_EARLYSUSPEND) &&!defined(CONFIG_INPUT_ENABLED)
2973 static const struct dev_pm_ops abov_tk_pm_ops = {
2974 .suspend = abov_tk_suspend,
2975 .resume = abov_tk_resume,
2976 };
2977 #endif
2978
2979 static const struct i2c_device_id abov_tk_id[] = {
2980 { ABOV_TK_NAME, 0},
2981 {}
2982 };
2983 MODULE_DEVICE_TABLE(i2c, abov_tk_id);
2984
2985 #ifdef CONFIG_OF
2986 static struct of_device_id abov_match_table[] = {
2987 { .compatible = "abov,mc96ft18xx",},
2988 { .compatible = "abov,mc96ft16xx",}, // for abov_parse_dt
2989 { },
2990 };
2991 #else
2992 #define abov_match_table NULL
2993 #endif
2994
2995 static struct i2c_driver abov_tk_driver = {
2996 .probe = abov_tk_probe,
2997 .remove = abov_tk_remove,
2998 .shutdown = abov_tk_shutdown,
2999 .driver = {
3000 .name = ABOV_TK_NAME,
3001 .of_match_table = abov_match_table,
3002 #if 0//defined(CONFIG_PM) && !defined(CONFIG_HAS_EARLYSUSPEND) &&!defined(CONFIG_INPUT_ENABLED)
3003 .pm = &abov_tk_pm_ops,
3004 #endif
3005 },
3006 .id_table = abov_tk_id,
3007 };
3008
3009 static int __init touchkey_init(void)
3010 {
3011 pr_err("%s: abov,%s\n", __func__, (is_ft1604_chip ? "mc96ft16xx" : "mc96ft18xx"));
3012
3013 #if defined(CONFIG_BATTERY_SAMSUNG) && !defined(CONFIG_TOUCHKEY_GRIP)
3014 if (lpcharge == 1) {
3015 pr_notice("%s : Do not load driver due to : lpm %d\n",
3016 __func__, lpcharge);
3017 return 0;
3018 }
3019 #endif
3020
3021 return i2c_add_driver(&abov_tk_driver);
3022 }
3023
3024 static void __exit touchkey_exit(void)
3025 {
3026 i2c_del_driver(&abov_tk_driver);
3027 }
3028
3029 module_init(touchkey_init);
3030 module_exit(touchkey_exit);
3031
3032 static int __init get_bootloader(char *bootloader)
3033 {
3034 is_ft1604_chip = false;
3035
3036 if (strstr(bootloader, "A510M")) {
3037 is_ft1604_chip = true;
3038 fw_path = "abov/abov_ft1604_a5_ltn.fw";
3039 }
3040 else if (strstr(bootloader, "A510Y") || strstr(bootloader, "A510K") ||
3041 strstr(bootloader, "A510L") || strstr(bootloader, "A510S") ||
3042 strstr(bootloader, "A5108")) {
3043 is_ft1604_chip = true;
3044 fw_path = "abov/abov_ft1604_a5.fw";
3045 }
3046 else if (strstr(bootloader, "A310N0") || strstr(bootloader, "A310M")) {
3047 is_ft1604_chip = true;
3048 fw_path = "abov/abov_ft1604_a3.fw";
3049 }
3050
3051 return 0;
3052 }
3053 early_param("androidboot.bootloader", get_bootloader);
3054
3055 /* Module information */
3056 MODULE_AUTHOR("Samsung Electronics");
3057 MODULE_DESCRIPTION("Touchkey driver for Abov MF16xx and MF18xx chip");
3058 MODULE_LICENSE("GPL");