8a0b6bd859bea9c65ef06f373cce843efad5f0ee
[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(abov_tk_fw_mode_check(info) != 1)
1764 {
1765 pr_err("%s: err, flash mode is not: %d\n", __func__, ret);
1766 return 0;
1767 }
1768
1769 ret = abov_tk_flash_erase(info);
1770 msleep(1300);
1771 tk_debug_info(true, &info->client->dev, "%s fw_write start\n", __func__);
1772
1773 for (ii = 1; ii < count; ii++) {
1774 /* first 32byte is header */
1775 addrH = (unsigned char)((address >> 8) & 0xFF);
1776 addrL = (unsigned char)(address & 0xFF);
1777 if (cmd == BUILT_IN)
1778 memcpy(data, &info->firm_data_bin->data[ii * 32], 32);
1779 else if (cmd == SDCARD)
1780 memcpy(data, &info->firm_data_ums[ii * 32], 32);
1781
1782 ret = abov_tk_fw_write(info, &addrH, &addrL, data);
1783 if (ret < 0) {
1784 tk_debug_err(true, &info->client->dev,
1785 "%s: err, no device : %d\n", __func__, ret);
1786 return ret;
1787 }
1788
1789 address += 0x20;
1790
1791 memset(data, 0, 32);
1792 }
1793 if (is_ft1604_chip)
1794 ret = abov_tk_i2c_read_checksum_ft1604(info);
1795 else
1796 ret = abov_tk_i2c_read_checksum(info);
1797 tk_debug_dbg(true, &info->client->dev, "%s checksum readed\n", __func__);
1798
1799 ret = abov_tk_fw_mode_exit(info);
1800 tk_debug_info(true, &info->client->dev, "%s fw_write end\n", __func__);
1801
1802 return ret;
1803 }
1804
1805 static void abov_release_fw(struct abov_tk_info *info, u8 cmd)
1806 {
1807 switch(cmd) {
1808 case BUILT_IN:
1809 release_firmware(info->firm_data_bin);
1810 break;
1811
1812 case SDCARD:
1813 kfree(info->firm_data_ums);
1814 break;
1815
1816 default:
1817 break;
1818 }
1819 }
1820
1821 static int abov_flash_fw(struct abov_tk_info *info, bool probe, u8 cmd)
1822 {
1823 struct i2c_client *client = info->client;
1824 int retry = 2;
1825 int ret;
1826 int block_count;
1827 const u8 *fw_data;
1828
1829 switch(cmd) {
1830 case BUILT_IN:
1831 fw_data = info->firm_data_bin->data;
1832 break;
1833
1834 case SDCARD:
1835 fw_data = info->firm_data_ums;
1836 break;
1837
1838 default:
1839 return -1;
1840 break;
1841 }
1842
1843 block_count = (int)(info->firm_size / 32);
1844
1845 while (retry--) {
1846 ret = abov_tk_fw_update(info, cmd);
1847 if (ret < 0)
1848 break;
1849 #if ABOV_ISP_FIRMUP_ROUTINE
1850 abov_tk_reset_for_bootmode(info);
1851 abov_fw_update(info, fw_data, block_count,
1852 info->pdata->gpio_scl, info->pdata->gpio_sda);
1853 #endif
1854
1855 if ((info->checksum_h != info->checksum_h_bin) ||
1856 (info->checksum_l != info->checksum_l_bin)) {
1857 tk_debug_err(true, &client->dev,
1858 "%s checksum fail.(0x%x,0x%x),(0x%x,0x%x) retry:%d\n",
1859 __func__, info->checksum_h, info->checksum_l,
1860 info->checksum_h_bin, info->checksum_l_bin, retry);
1861 ret = -1;
1862 continue;
1863 }else
1864 tk_debug_info(true, &client->dev,"%s checksum successed.\n",__func__);
1865
1866 abov_tk_reset_for_bootmode(info);
1867 msleep(ABOV_RESET_DELAY);
1868 ret = get_tk_fw_version(info, true);
1869 if (ret) {
1870 tk_debug_err(true, &client->dev, "%s fw version read fail\n", __func__);
1871 ret = -1;
1872 continue;
1873 }
1874
1875 if (info->fw_ver == 0) {
1876 tk_debug_err(true, &client->dev, "%s fw version fail (0x%x)\n",
1877 __func__, info->fw_ver);
1878 ret = -1;
1879 continue;
1880 }
1881
1882 if ((cmd == BUILT_IN) && (info->fw_ver != info->fw_ver_bin)){
1883 tk_debug_err(true, &client->dev, "%s fw version fail 0x%x, 0x%x\n",
1884 __func__, info->fw_ver, info->fw_ver_bin);
1885 ret = -1;
1886 continue;
1887 }
1888 ret = 0;
1889 break;
1890 }
1891
1892 return ret;
1893 }
1894
1895 static ssize_t touchkey_fw_update(struct device *dev,
1896 struct device_attribute *attr, const char *buf, size_t count)
1897 {
1898 struct abov_tk_info *info = dev_get_drvdata(dev);
1899 struct i2c_client *client = info->client;
1900 int ret;
1901 u8 cmd;
1902
1903 switch(*buf) {
1904 case 's':
1905 case 'S':
1906 cmd = BUILT_IN;
1907 break;
1908 case 'i':
1909 case 'I':
1910 cmd = SDCARD;
1911 break;
1912 default:
1913 info->fw_update_state = 2;
1914 goto touchkey_fw_update_out;
1915 }
1916
1917 ret = abov_load_fw(info, cmd);
1918 if (ret) {
1919 tk_debug_err(true, &client->dev,
1920 "%s fw load fail\n", __func__);
1921 info->fw_update_state = 2;
1922 goto touchkey_fw_update_out;
1923 }
1924
1925 info->fw_update_state = 1;
1926 disable_irq(info->irq);
1927 info->enabled = false;
1928 ret = abov_flash_fw(info, false, cmd);
1929 if (info->flip_mode){
1930 abov_mode_enable(client, ABOV_FLIP, CMD_FLIP_ON);
1931 } else {
1932 if (info->glovemode)
1933 abov_mode_enable(client, ABOV_GLOVE, CMD_GLOVE_ON);
1934 }
1935 if (info->keyboard_mode)
1936 abov_mode_enable(client, ABOV_KEYBOARD, CMD_MOBILE_KBD_ON);
1937
1938 info->enabled = true;
1939 enable_irq(info->irq);
1940 if (ret) {
1941 tk_debug_err(true, &client->dev, "%s fail\n", __func__);
1942 // info->fw_update_state = 2;
1943 info->fw_update_state = 0;
1944
1945 } else {
1946 tk_debug_info(true, &client->dev, "%s success\n", __func__);
1947 info->fw_update_state = 0;
1948 }
1949
1950 abov_release_fw(info, cmd);
1951
1952 touchkey_fw_update_out:
1953 tk_debug_dbg(true, &client->dev, "%s : %d\n", __func__, info->fw_update_state);
1954
1955 return count;
1956 }
1957
1958 static ssize_t touchkey_fw_update_status(struct device *dev,
1959 struct device_attribute *attr, char *buf)
1960 {
1961 struct abov_tk_info *info = dev_get_drvdata(dev);
1962 struct i2c_client *client = info->client;
1963 int count = 0;
1964
1965 tk_debug_info(true, &client->dev, "%s : %d\n", __func__, info->fw_update_state);
1966
1967 if (info->fw_update_state == 0)
1968 count = sprintf(buf, "PASS\n");
1969 else if (info->fw_update_state == 1)
1970 count = sprintf(buf, "Downloading\n");
1971 else if (info->fw_update_state == 2)
1972 count = sprintf(buf, "Fail\n");
1973
1974 return count;
1975 }
1976
1977 static ssize_t abov_glove_mode(struct device *dev,
1978 struct device_attribute *attr, const char *buf, size_t count)
1979 {
1980 struct abov_tk_info *info = dev_get_drvdata(dev);
1981 struct i2c_client *client = info->client;
1982 int scan_buffer;
1983 int ret;
1984 u8 cmd;
1985
1986 ret = sscanf(buf, "%d", &scan_buffer);
1987 tk_debug_info(true, &client->dev, "%s : %d\n", __func__, scan_buffer);
1988
1989
1990 if (ret != 1) {
1991 tk_debug_err(true, &client->dev, "%s: cmd read err\n", __func__);
1992 return count;
1993 }
1994
1995 if (!(scan_buffer == 0 || scan_buffer == 1)) {
1996 tk_debug_err(true, &client->dev, "%s: wrong command(%d)\n",
1997 __func__, scan_buffer);
1998 return count;
1999 }
2000
2001 if (!info->enabled)
2002 return count;
2003
2004 if (info->glovemode == scan_buffer) {
2005 tk_debug_dbg(true, &client->dev, "%s same command(%d)\n",
2006 __func__, scan_buffer);
2007 return count;
2008 }
2009
2010 if (scan_buffer == 1) {
2011 cmd = CMD_GLOVE_ON;
2012 } else {
2013 cmd = CMD_GLOVE_OFF;
2014 }
2015
2016 ret = abov_mode_enable(client, ABOV_GLOVE, cmd);
2017 if (ret < 0) {
2018 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2019 return count;
2020 }
2021
2022 info->glovemode = scan_buffer;
2023
2024 return count;
2025 }
2026
2027 static ssize_t abov_glove_mode_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029 {
2030 struct abov_tk_info *info = dev_get_drvdata(dev);
2031
2032 return sprintf(buf, "%d\n", info->glovemode);
2033 }
2034
2035 static ssize_t keyboard_cover_mode_enable(struct device *dev,
2036 struct device_attribute *attr,
2037 const char *buf, size_t count)
2038 {
2039 struct abov_tk_info *info = dev_get_drvdata(dev);
2040 struct i2c_client *client = info->client;
2041 int keyboard_mode_on;
2042 int ret;
2043 u8 cmd;
2044
2045 tk_debug_dbg(true, &client->dev, "%s : Mobile KBD sysfs node called\n",__func__);
2046
2047 sscanf(buf, "%d", &keyboard_mode_on);
2048 tk_debug_info(true, &client->dev, "%s : %d\n",
2049 __func__, keyboard_mode_on);
2050
2051 if (!(keyboard_mode_on == 0 || keyboard_mode_on == 1)) {
2052 tk_debug_err(true, &client->dev, "%s: wrong command(%d)\n",
2053 __func__, keyboard_mode_on);
2054 return count;
2055 }
2056
2057 if (!info->enabled)
2058 goto out;
2059
2060 if (info->keyboard_mode == keyboard_mode_on) {
2061 tk_debug_dbg(true, &client->dev, "%s same command(%d)\n",
2062 __func__, keyboard_mode_on);
2063 goto out;
2064 }
2065
2066 if (keyboard_mode_on == 1) {
2067 cmd = CMD_MOBILE_KBD_ON;
2068 } else {
2069 cmd = CMD_MOBILE_KBD_OFF;
2070 }
2071
2072 /* mobile keyboard use same register with glove mode */
2073 ret = abov_mode_enable(client, ABOV_KEYBOARD, cmd);
2074 if (ret < 0) {
2075 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2076 goto out;
2077 }
2078
2079 out:
2080 info->keyboard_mode = keyboard_mode_on;
2081 return count;
2082 }
2083
2084 static ssize_t flip_cover_mode_enable(struct device *dev,
2085 struct device_attribute *attr,
2086 const char *buf, size_t count)
2087 {
2088 struct abov_tk_info *info = dev_get_drvdata(dev);
2089 struct i2c_client *client = info->client;
2090 int flip_mode_on;
2091 int ret;
2092 u8 cmd;
2093
2094 sscanf(buf, "%d\n", &flip_mode_on);
2095 tk_debug_info(true, &client->dev, "%s : %d\n", __func__, flip_mode_on);
2096
2097 if (!info->enabled)
2098 goto out;
2099
2100 #ifdef CONFIG_TOUCHKEY_GRIP
2101 if (flip_mode_on) {
2102 cmd = 0x10;
2103 ret = abov_tk_i2c_write(info->client, ABOV_SW_RESET, &cmd, 1);
2104 if (ret < 0) {
2105 tk_debug_err(true, &client->dev, "%s sw_reset fail(%d)\n", __func__, ret);
2106 }
2107 abov_sar_olny_mode(info, 1);
2108 } else {
2109 abov_sar_olny_mode(info, 0);
2110 }
2111 #else
2112 /* glove mode control */
2113 if (flip_mode_on) {
2114 cmd = CMD_FLIP_ON;
2115 } else {
2116 if (info->glovemode)
2117 cmd = CMD_GLOVE_ON;
2118 cmd = CMD_FLIP_OFF;
2119 }
2120
2121 if (info->glovemode){
2122 ret = abov_mode_enable(client, ABOV_GLOVE, cmd);
2123 if (ret < 0) {
2124 tk_debug_err(true, &client->dev, "%s glove mode fail(%d)\n", __func__, ret);
2125 goto out;
2126 }
2127 } else{
2128 ret = abov_mode_enable(client, ABOV_FLIP, cmd);
2129 if (ret < 0) {
2130 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2131 goto out;
2132 }
2133 }
2134 #endif
2135
2136 out:
2137 info->flip_mode = flip_mode_on;
2138 return count;
2139 }
2140
2141 static DEVICE_ATTR(touchkey_threshold, S_IRUGO, touchkey_threshold_show, NULL);
2142 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2143 touchkey_led_control);
2144 #ifdef CONFIG_TOUCHKEY_GRIP
2145 static DEVICE_ATTR(touchkey_grip_threshold, S_IRUGO, touchkey_grip_threshold_show, NULL);
2146 static DEVICE_ATTR(touchkey_total_cap, S_IRUGO, touchkey_total_cap_show, NULL);
2147 static DEVICE_ATTR(sar_enable, S_IRUGO | S_IWUSR | S_IWGRP, NULL, touchkey_sar_enable);
2148 static DEVICE_ATTR(sw_reset, S_IRUGO | S_IWUSR | S_IWGRP, NULL, touchkey_grip_sw_reset);
2149 static DEVICE_ATTR(touchkey_earjack, S_IRUGO | S_IWUSR | S_IWGRP, NULL, touchkey_sensing_change);
2150 static DEVICE_ATTR(touchkey_grip, S_IRUGO, touchkey_grip_show, NULL);
2151 static DEVICE_ATTR(touchkey_grip_baseline, S_IRUGO, touchkey_grip_baseline_show, NULL);
2152 static DEVICE_ATTR(touchkey_grip_raw, S_IRUGO, touchkey_grip_raw_show, NULL);
2153 static DEVICE_ATTR(touchkey_grip_gain, S_IRUGO, touchkey_grip_gain_show, NULL);
2154 static DEVICE_ATTR(touchkey_grip_check, S_IRUGO, touchkey_grip_check_show, NULL);
2155 #ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
2156 static DEVICE_ATTR(touchkey_sar_only_mode, S_IRUGO | S_IWUSR | S_IWGRP | S_IWOTH,
2157 NULL, touchkey_mode_change);
2158 static DEVICE_ATTR(touchkey_sar_press_threshold, S_IRUGO | S_IWUSR | S_IWGRP | S_IWOTH,
2159 NULL, touchkey_sar_press_threshold_store);
2160 static DEVICE_ATTR(touchkey_sar_release_threshold, S_IRUGO | S_IWUSR | S_IWGRP | S_IWOTH,
2161 NULL, touchkey_sar_release_threshold_store);
2162 #endif
2163 #endif
2164 static DEVICE_ATTR(touchkey_recent, S_IRUGO, touchkey_menu_show, NULL);
2165 static DEVICE_ATTR(touchkey_back, S_IRUGO, touchkey_back_show, NULL);
2166 static DEVICE_ATTR(touchkey_recent_raw, S_IRUGO, touchkey_menu_raw_show, NULL);
2167 static DEVICE_ATTR(touchkey_back_raw, S_IRUGO, touchkey_back_raw_show, NULL);
2168 static DEVICE_ATTR(touchkey_chip_name, S_IRUGO, touchkey_chip_name, NULL);
2169 static DEVICE_ATTR(touchkey_firm_version_phone, S_IRUGO, bin_fw_ver, NULL);
2170 static DEVICE_ATTR(touchkey_firm_version_panel, S_IRUGO, read_fw_ver, NULL);
2171 static DEVICE_ATTR(touchkey_firm_update, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2172 touchkey_fw_update);
2173 static DEVICE_ATTR(touchkey_firm_update_status, S_IRUGO | S_IWUSR | S_IWGRP,
2174 touchkey_fw_update_status, NULL);
2175 static DEVICE_ATTR(glove_mode, S_IRUGO | S_IWUSR | S_IWGRP,
2176 abov_glove_mode_show, abov_glove_mode);
2177 static DEVICE_ATTR(keyboard_mode, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2178 keyboard_cover_mode_enable);
2179 static DEVICE_ATTR(flip_mode, S_IRUGO | S_IWUSR | S_IWGRP, NULL,
2180 flip_cover_mode_enable);
2181
2182 static struct attribute *sec_touchkey_attributes[] = {
2183 &dev_attr_touchkey_threshold.attr,
2184 &dev_attr_brightness.attr,
2185 #ifdef CONFIG_TOUCHKEY_GRIP
2186 &dev_attr_touchkey_grip_threshold.attr,
2187 &dev_attr_touchkey_total_cap.attr,
2188 &dev_attr_sar_enable.attr,
2189 &dev_attr_sw_reset.attr,
2190 &dev_attr_touchkey_earjack.attr,
2191 &dev_attr_touchkey_grip.attr,
2192 &dev_attr_touchkey_grip_baseline.attr,
2193 &dev_attr_touchkey_grip_raw.attr,
2194 &dev_attr_touchkey_grip_gain.attr,
2195 &dev_attr_touchkey_grip_check.attr,
2196 #ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
2197 &dev_attr_touchkey_sar_only_mode.attr,
2198 &dev_attr_touchkey_sar_press_threshold.attr,
2199 &dev_attr_touchkey_sar_release_threshold.attr,
2200 #endif
2201 #endif
2202 &dev_attr_touchkey_recent.attr,
2203 &dev_attr_touchkey_back.attr,
2204 &dev_attr_touchkey_recent_raw.attr,
2205 &dev_attr_touchkey_back_raw.attr,
2206 &dev_attr_touchkey_chip_name.attr,
2207 &dev_attr_touchkey_firm_version_phone.attr,
2208 &dev_attr_touchkey_firm_version_panel.attr,
2209 &dev_attr_touchkey_firm_update.attr,
2210 &dev_attr_touchkey_firm_update_status.attr,
2211 &dev_attr_glove_mode.attr,
2212 &dev_attr_keyboard_mode.attr,
2213 &dev_attr_flip_mode.attr,
2214 NULL,
2215 };
2216
2217 static struct attribute_group sec_touchkey_attr_group = {
2218 .attrs = sec_touchkey_attributes,
2219 };
2220
2221 extern int get_samsung_lcd_attached(void);
2222
2223 static int abov_tk_fw_check(struct abov_tk_info *info)
2224 {
2225 struct i2c_client *client = info->client;
2226 int ret, fw_update = 0;
2227 u8 buf;
2228
2229 ret = abov_load_fw(info, BUILT_IN);
2230 if (ret) {
2231 tk_debug_err(true, &client->dev,
2232 "%s fw load fail\n", __func__);
2233 return ret;
2234 }
2235
2236 ret = get_tk_fw_version(info, true);
2237
2238 #ifdef LED_TWINKLE_BOOTING
2239 if(ret)
2240 tk_debug_err(true, &client->dev,
2241 "%s: i2c fail...[%d], addr[%d]\n",
2242 __func__, ret, info->client->addr);
2243 tk_debug_err(true, &client->dev,
2244 "%s: touchkey driver unload\n", __func__);
2245
2246 if (get_samsung_lcd_attached() == 0) {
2247 tk_debug_err(true, &client->dev, "%s : get_samsung_lcd_attached()=0 \n", __func__);
2248 abov_release_fw(info, BUILT_IN);
2249 return ret;
2250 }
2251 #endif
2252
2253 //Check Model No.
2254 ret = abov_tk_i2c_read(client, ABOV_MODEL_NUMBER, &buf, 1);
2255 if (ret < 0) {
2256 tk_debug_err(true, &client->dev, "%s fail(%d)\n", __func__, ret);
2257 }
2258 if(info->fw_model_number != buf){
2259 tk_debug_info(true, &client->dev, "fw model number = %x ic model number = %x \n", info->fw_model_number, buf);
2260 fw_update = 1;
2261 goto flash_fw;
2262 }
2263
2264 if ((info->fw_ver == 0) || info->fw_ver < info->fw_ver_bin){
2265 tk_debug_dbg(true, &client->dev, "excute tk firmware update (0x%x -> 0x%x\n",
2266 info->fw_ver, info->fw_ver_bin);
2267 fw_update = 1;
2268 }
2269
2270 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
2271 if(info->fw_ver >= 0xd0){ //test firmware
2272 tk_debug_dbg(true, &client->dev, "excute tk firmware update (0x%x -> 0x%x\n",
2273 info->fw_ver, info->fw_ver_bin);
2274 fw_update = 1;
2275 }
2276 #endif
2277
2278 flash_fw:
2279 if(fw_update){
2280 ret = abov_flash_fw(info, true, BUILT_IN);
2281 if (ret) {
2282 tk_debug_err(true, &client->dev,
2283 "failed to abov_flash_fw (%d)\n", ret);
2284 } else {
2285 tk_debug_info(true, &client->dev,
2286 "fw update success\n");
2287 }
2288 }
2289
2290 abov_release_fw(info, BUILT_IN);
2291
2292 return ret;
2293 }
2294
2295 static int abov_power(void *data, bool on)
2296 {
2297 struct abov_tk_info *info = (struct abov_tk_info *)data;
2298 struct i2c_client *client = info->client;
2299
2300 int ret = 0;
2301
2302 info->pdata->avdd_vreg = regulator_get(NULL, "vtouch_3.3v");
2303 if (IS_ERR(info->pdata->avdd_vreg)) {
2304 info->pdata->avdd_vreg = NULL;
2305 tk_debug_err(true, &client->dev, "%s : avdd_vreg get error, ignoring\n", __func__);
2306 }
2307 info->pdata->dvdd_vreg = regulator_get(NULL, "vtouch_2.8v");
2308 if (IS_ERR(info->pdata->dvdd_vreg)) {
2309 info->pdata->dvdd_vreg = NULL;
2310 tk_debug_err(true, &client->dev, "%s : dvdd_vreg get error, ignoring\n",__func__);
2311 }
2312
2313 if (on) {
2314 if (info->pdata->avdd_vreg) {
2315 ret = regulator_enable(info->pdata->avdd_vreg);
2316 if(ret){
2317 tk_debug_err(true, &client->dev, "%s : avdd reg enable fail\n", __func__);
2318 }
2319 }
2320 if (info->pdata->dvdd_vreg) {
2321 ret = regulator_enable(info->pdata->dvdd_vreg);
2322 if(ret){
2323 tk_debug_err(true, &client->dev, "%s : dvdd reg enable fail\n", __func__);
2324 }
2325 }
2326 } else {
2327 if (info->pdata->avdd_vreg) {
2328 ret = regulator_disable(info->pdata->avdd_vreg);
2329 if(ret){
2330 tk_debug_err(true, &client->dev, "%s : avdd reg disable fail\n", __func__);
2331 }
2332 }
2333 if (info->pdata->dvdd_vreg) {
2334 ret = regulator_disable(info->pdata->dvdd_vreg);
2335 if(ret){
2336 tk_debug_err(true, &client->dev, "%s : dvdd reg disable fail\n", __func__);
2337 }
2338 }
2339 }
2340 regulator_put(info->pdata->avdd_vreg);
2341 regulator_put(info->pdata->dvdd_vreg);
2342
2343 tk_debug_info(true, &client->dev, "%s %s\n", __func__, on ? "on" : "off");
2344
2345 return ret;
2346 }
2347
2348 #if 1
2349 static int abov_pinctrl_configure(struct abov_tk_info *info,
2350 bool active)
2351 {
2352 struct pinctrl_state *set_state;
2353 int retval;
2354
2355 if (active) {
2356 set_state =
2357 pinctrl_lookup_state(info->pinctrl,
2358 "on_irq");
2359 if (IS_ERR(set_state)) {
2360 tk_debug_err(true, &info->client->dev,
2361 "%s: cannot get ts pinctrl active state\n", __func__);
2362 return PTR_ERR(set_state);
2363 }
2364 } else {
2365 set_state =
2366 pinctrl_lookup_state(info->pinctrl,
2367 "off_irq");
2368 if (IS_ERR(set_state)) {
2369 tk_debug_err(true, &info->client->dev,
2370 "%s: cannot get gpiokey pinctrl sleep state\n", __func__);
2371 return PTR_ERR(set_state);
2372 }
2373 }
2374 retval = pinctrl_select_state(info->pinctrl, set_state);
2375 if (retval) {
2376 tk_debug_err(true, &info->client->dev,
2377 "%s: cannot set ts pinctrl active state\n", __func__);
2378 return retval;
2379 }
2380
2381 return 0;
2382 }
2383 #endif
2384 static int abov_gpio_reg_init(struct device *dev,
2385 struct abov_touchkey_platform_data *pdata)
2386 {
2387 int ret = 0;
2388
2389 ret = gpio_request(pdata->gpio_int, "tkey_gpio_int");
2390 if(ret < 0){
2391 tk_debug_err(true, dev,
2392 "unable to request gpio_int\n");
2393 return ret;
2394 }
2395
2396 pdata->power = abov_power;
2397
2398 return ret;
2399 }
2400
2401 #ifdef CONFIG_OF
2402 static int abov_parse_dt(struct device *dev,
2403 struct abov_touchkey_platform_data *pdata)
2404 {
2405 struct device_node *np = dev->of_node;
2406 int ret;
2407 //u32 tmp[2] = {0, };
2408
2409 pdata->gpio_int = of_get_named_gpio(np, "abov,irq-gpio", 0);
2410 if(pdata->gpio_int < 0){
2411 tk_debug_err(true, dev, "unable to get gpio_int\n");
2412 return pdata->gpio_int;
2413 }
2414
2415 pdata->gpio_scl = of_get_named_gpio(np, "abov,scl-gpio", 0);
2416 if(pdata->gpio_scl < 0){
2417 tk_debug_err(true, dev, "unable to get gpio_scl\n");
2418 return pdata->gpio_scl;
2419 }
2420
2421 pdata->gpio_sda = of_get_named_gpio(np, "abov,sda-gpio", 0);
2422 if(pdata->gpio_sda < 0){
2423 tk_debug_err(true, dev, "unable to get gpio_sda\n");
2424 return pdata->gpio_sda;
2425 }
2426
2427 pdata->sub_det = of_get_named_gpio(np, "abov,sub-det",0);
2428 if(pdata->sub_det < 0){
2429 tk_debug_info(true, dev, "unable to get sub_det\n");
2430 }else{
2431 tk_debug_info(true, dev, "%s: sub_det:%d\n",__func__,pdata->sub_det);
2432 }
2433
2434 if (is_ft1604_chip) {
2435 pdata->fw_path = fw_path;
2436 }
2437 else {
2438 ret = of_property_read_string(np, "abov,fw_path", (const char **)&pdata->fw_path);
2439 if (ret) {
2440 tk_debug_err(true, dev, "touchkey:failed to read fw_path %d\n", ret);
2441 pdata->fw_path = TK_FW_PATH_BIN;
2442 }
2443 }
2444 tk_debug_info(true, dev, "%s: fw path %s\n", __func__, pdata->fw_path);
2445
2446 pdata->boot_on_ldo = of_property_read_bool(np, "abov,boot-on-ldo");
2447
2448 tk_debug_info(true, dev, "%s: gpio_int:%d, gpio_scl:%d, gpio_sda:%d\n",
2449 __func__, pdata->gpio_int, pdata->gpio_scl,
2450 pdata->gpio_sda);
2451
2452 if (is_ft1604_chip) { // only for compatibility
2453 //temp A5x ltn
2454 pdata->ds_det = of_get_named_gpio(np, "abov,ds-det",0);
2455 if(pdata->ds_det < 0) {
2456 tk_debug_info(true, dev, "don't check ds_det\n");
2457 }
2458 else {
2459 tk_debug_info(true, dev, "%s: A5 LTN temp ds_det:%d\n",__func__,pdata->ds_det);
2460 }
2461 }
2462
2463 return 0;
2464 }
2465 #else
2466 static int abov_parse_dt(struct device *dev,
2467 struct abov_touchkey_platform_data *pdata)
2468 {
2469 return -ENODEV;
2470 }
2471 #endif
2472
2473 static int abov_tk_probe(struct i2c_client *client,
2474 const struct i2c_device_id *id)
2475 {
2476 struct abov_tk_info *info;
2477 struct input_dev *input_dev;
2478 #if 0
2479 struct device *touchkey_dev;
2480 int i;
2481 #endif
2482 int ret = 0;
2483
2484 #ifdef LED_TWINKLE_BOOTING
2485 if (get_samsung_lcd_attached() == 0) {
2486 tk_debug_err(true, &client->dev, "%s : get_samsung_lcd_attached()=0 \n", __func__);
2487 return -EIO;
2488 }
2489 #endif
2490 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
2491 tk_debug_err(true, &client->dev,
2492 "i2c_check_functionality fail\n");
2493 return -EIO;
2494 }
2495
2496 info = kzalloc(sizeof(struct abov_tk_info), GFP_KERNEL);
2497 if (!info) {
2498 tk_debug_err(true, &client->dev, "Failed to allocate memory\n");
2499 ret = -ENOMEM;
2500 goto err_alloc;
2501 }
2502
2503 input_dev = input_allocate_device();
2504 if (!input_dev) {
2505 tk_debug_err(true, &client->dev,
2506 "Failed to allocate memory for input device\n");
2507 ret = -ENOMEM;
2508 goto err_input_alloc;
2509 }
2510
2511 info->client = client;
2512 info->input_dev = input_dev;
2513
2514 #ifdef CONFIG_TOUCHKEY_GRIP
2515 wake_lock_init(&info->touckey_wake_lock, WAKE_LOCK_SUSPEND, "touchkey wake lock");
2516 #endif
2517
2518 if (client->dev.of_node) {
2519 struct abov_touchkey_platform_data *pdata;
2520 pdata = devm_kzalloc(&client->dev,
2521 sizeof(struct abov_touchkey_platform_data), GFP_KERNEL);
2522 if (!pdata) {
2523 tk_debug_err(true, &client->dev, "Failed to allocate memory\n");
2524 ret = -ENOMEM;
2525 goto err_config;
2526 }
2527
2528 ret = abov_parse_dt(&client->dev, pdata);
2529 if (ret){
2530 tk_debug_err(true, &client->dev, "failed to abov_parse_dt\n");
2531 ret = -ENOMEM;
2532 goto err_config;
2533 }
2534
2535 info->pdata = pdata;
2536 } else
2537 info->pdata = client->dev.platform_data;
2538
2539 if (is_ft1604_chip) { // only for compatibility
2540 //temp A5x ltn
2541 if (gpio_is_valid(info->pdata->ds_det)) {
2542 ret = gpio_get_value(info->pdata->ds_det);
2543 if (ret == 0) {
2544 tk_debug_err(true, &client->dev, "%s : 1804 DS_DET is low\n",__func__);
2545 }
2546 else if (ret == 1) {
2547 tk_debug_err(true, &client->dev, "%s : 1804 DS_DET is high\n",__func__);
2548 ret = -1;
2549 goto err_config;
2550 }
2551 }
2552 }
2553
2554 if (info->pdata == NULL) {
2555 tk_debug_err(true, &client->dev, "failed to get platform data\n");
2556 goto err_config;
2557 }
2558 #if 1
2559 /* Get pinctrl if target uses pinctrl */
2560 info->pinctrl = devm_pinctrl_get(&client->dev);
2561 if (IS_ERR(info->pinctrl)) {
2562 if (PTR_ERR(info->pinctrl) == -EPROBE_DEFER)
2563 goto err_config;
2564
2565 tk_debug_err(true, &client->dev, "%s: Target does not use pinctrl\n", __func__);
2566 info->pinctrl = NULL;
2567 }
2568
2569 if (info->pinctrl) {
2570 ret = abov_pinctrl_configure(info, true);
2571 if (ret)
2572 tk_debug_err(true, &client->dev,
2573 "%s: cannot set ts pinctrl active state\n", __func__);
2574 }
2575
2576 /* sub-det pinctrl */
2577 if (gpio_is_valid(info->pdata->sub_det)) {
2578 info->pinctrl_det = devm_pinctrl_get(&client->dev);
2579 if (IS_ERR(info->pinctrl_det)) {
2580 tk_debug_err(true, &client->dev, "%s: Failed to get pinctrl\n", __func__);
2581 goto err_config;
2582 }
2583
2584 info->pins_default = pinctrl_lookup_state(info->pinctrl_det, "sub_det");
2585 if (IS_ERR(info->pins_default)) {
2586 tk_debug_err(true, &client->dev, "%s: Failed to get pinctrl state\n", __func__);
2587 devm_pinctrl_put(info->pinctrl_det);
2588 goto err_config;
2589 }
2590
2591 ret = pinctrl_select_state(info->pinctrl_det, info->pins_default);
2592 if (ret < 0)
2593 tk_debug_err(true, &client->dev, "%s: Failed to configure sub_det pin\n", __func__);
2594 }
2595 #endif
2596 ret = abov_gpio_reg_init(&client->dev, info->pdata);
2597 if(ret){
2598 tk_debug_err(true, &client->dev, "failed to init reg\n");
2599 goto pwr_config;
2600 }
2601 if (info->pdata->power)
2602 info->pdata->power(info, true);
2603
2604 if(!info->pdata->boot_on_ldo)
2605 msleep(ABOV_RESET_DELAY);
2606
2607 if (gpio_is_valid(info->pdata->sub_det)) {
2608 ret = gpio_get_value(info->pdata->sub_det);
2609 if (ret) {
2610 tk_debug_err(true, &client->dev, "Device wasn't connected to board \n");
2611 ret = -ENODEV;
2612 goto err_i2c_check;
2613 }
2614 }
2615
2616 info->enabled = true;
2617 info->irq = -1;
2618 client->irq = gpio_to_irq(info->pdata->gpio_int);
2619
2620 mutex_init(&info->lock);
2621
2622 if (is_ft1604_chip) {
2623 //temp A5x ltn
2624 client->addr = 0x20;
2625 }
2626
2627 info->input_event = info->pdata->input_event;
2628 info->touchkey_count = sizeof(touchkey_keycode) / sizeof(int);
2629 i2c_set_clientdata(client, info);
2630
2631 ret = abov_tk_fw_check(info);
2632 if (ret) {
2633 tk_debug_err(true, &client->dev,
2634 "failed to firmware check (%d)\n", ret);
2635 goto err_reg_input_dev;
2636 }
2637
2638 ret = get_tk_fw_version(info, false);
2639 if (ret < 0) {
2640 tk_debug_err(true, &client->dev, "%s read fail\n", __func__);
2641 goto err_reg_input_dev;
2642 }
2643
2644 snprintf(info->phys, sizeof(info->phys),
2645 "%s/input0", dev_name(&client->dev));
2646 input_dev->name = "sec_touchkey";
2647 input_dev->phys = info->phys;
2648 input_dev->id.bustype = BUS_HOST;
2649 input_dev->dev.parent = &client->dev;
2650 #if 1 //def CONFIG_INPUT_ENABLED
2651 input_dev->open = abov_tk_input_open;
2652 input_dev->close = abov_tk_input_close;
2653 #endif
2654 set_bit(EV_KEY, input_dev->evbit);
2655 set_bit(KEY_RECENT, input_dev->keybit);
2656 set_bit(KEY_BACK, input_dev->keybit);
2657 set_bit(KEY_CP_GRIP, input_dev->keybit);
2658 set_bit(EV_LED, input_dev->evbit);
2659 set_bit(LED_MISC, input_dev->ledbit);
2660 input_set_drvdata(input_dev, info);
2661
2662 ret = input_register_device(input_dev);
2663 if (ret) {
2664 tk_debug_err(true, &client->dev, "failed to register input dev (%d)\n",
2665 ret);
2666 goto err_reg_input_dev;
2667 }
2668
2669 if (!info->pdata->irq_flag) {
2670 tk_debug_err(true, &client->dev, "no irq_flag\n");
2671 ret = request_threaded_irq(client->irq, NULL, abov_tk_interrupt,
2672 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, ABOV_TK_NAME, info);
2673 } else {
2674 ret = request_threaded_irq(client->irq, NULL, abov_tk_interrupt,
2675 info->pdata->irq_flag, ABOV_TK_NAME, info);
2676 }
2677 if (ret < 0) {
2678 tk_debug_err(true, &client->dev, "Failed to register interrupt\n");
2679 goto err_req_irq;
2680 }
2681 info->irq = client->irq;
2682
2683 #ifdef CONFIG_HAS_EARLYSUSPEND
2684 info->early_suspend.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING;
2685 info->early_suspend.suspend = abov_tk_early_suspend;
2686 info->early_suspend.resume = abov_tk_late_resume;
2687 register_early_suspend(&info->early_suspend);
2688 #endif
2689
2690 info->dev = sec_device_create(info, "sec_touchkey");
2691 if (IS_ERR(info->dev))
2692 tk_debug_err(true, &client->dev,
2693 "Failed to create device for the touchkey sysfs\n");
2694
2695 ret = sysfs_create_group(&info->dev ->kobj,
2696 &sec_touchkey_attr_group);
2697 if (ret)
2698 tk_debug_err(true, &client->dev, "Failed to create sysfs group\n");
2699
2700 ret = sysfs_create_link(&info->dev ->kobj,
2701 &info->input_dev->dev.kobj, "input");
2702 if (ret < 0) {
2703 tk_debug_err(true, &client->dev,
2704 "%s: Failed to create input symbolic link\n",
2705 __func__);
2706 }
2707
2708
2709 #ifdef LED_TWINKLE_BOOTING
2710 if (get_samsung_lcd_attached() == 0) {
2711 tk_debug_err(true, &client->dev,
2712 "%s : get_samsung_lcd_attached()=0, so start LED twinkle \n", __func__);
2713
2714 INIT_DELAYED_WORK(&info->led_twinkle_work, led_twinkle_work);
2715 info->led_twinkle_check = 1;
2716
2717 schedule_delayed_work(&info->led_twinkle_work, msecs_to_jiffies(400));
2718 }
2719 #endif
2720 #ifdef CONFIG_TOUCHKEY_GRIP
2721 info->sar_sensing = 1;
2722 device_init_wakeup(&client->dev, true);
2723 #endif
2724
2725 tk_debug_err(true, &client->dev, "%s done\n", __func__);
2726
2727 #ifdef CONFIG_TOUCHKEY_GRIP
2728 if (lpcharge == 1) {
2729 disable_irq(info->irq);
2730 tk_debug_err(true, &client->dev, "%s disable_irq\n", __func__);
2731 abov_sar_olny_mode(info, 1);
2732 }
2733 #endif
2734 return 0;
2735
2736 err_req_irq:
2737 input_unregister_device(input_dev);
2738 err_reg_input_dev:
2739 mutex_destroy(&info->lock);
2740 gpio_free(info->pdata->gpio_int);
2741 err_i2c_check:
2742 if (info->pdata->power)
2743 info->pdata->power(info, false);
2744 pwr_config:
2745 err_config:
2746 #ifdef CONFIG_TOUCHKEY_GRIP
2747 wake_lock_destroy(&info->touckey_wake_lock);
2748 #endif
2749 input_free_device(input_dev);
2750 err_input_alloc:
2751 kfree(info);
2752 err_alloc:
2753 tk_debug_err(true, &client->dev, "%s fail\n",__func__);
2754 return ret;
2755
2756 }
2757
2758
2759 #ifdef LED_TWINKLE_BOOTING
2760 static void led_twinkle_work(struct work_struct *work)
2761 {
2762 struct abov_tk_info *info = container_of(work, struct abov_tk_info,
2763 led_twinkle_work.work);
2764 static bool led_on = 1;
2765 static int count = 0;
2766 tk_debug_info(true, &info->client->dev, "%s, on=%d, c=%d\n",__func__, led_on, count++ );
2767
2768 if(info->led_twinkle_check == 1){
2769
2770 touchkey_led_set(info,led_on);
2771 if(led_on) led_on = 0;
2772 else led_on = 1;
2773
2774 schedule_delayed_work(&info->led_twinkle_work, msecs_to_jiffies(400));
2775 }else{
2776
2777 if(led_on == 0)
2778 touchkey_led_set(info, 0);
2779 }
2780
2781 }
2782 #endif
2783
2784 static int abov_tk_remove(struct i2c_client *client)
2785 {
2786 struct abov_tk_info *info = i2c_get_clientdata(client);
2787
2788 /* if (info->enabled)
2789 info->pdata->power(0);
2790 */
2791 info->enabled = false;
2792 #ifdef CONFIG_TOUCHKEY_GRIP
2793 device_init_wakeup(&client->dev, false);
2794 wake_lock_destroy(&info->touckey_wake_lock);
2795 #endif
2796 #ifdef CONFIG_HAS_EARLYSUSPEND
2797 unregister_early_suspend(&info->early_suspend);
2798 #endif
2799 if (info->irq >= 0)
2800 free_irq(info->irq, info);
2801 input_unregister_device(info->input_dev);
2802 input_free_device(info->input_dev);
2803 kfree(info);
2804
2805 return 0;
2806 }
2807
2808 static void abov_tk_shutdown(struct i2c_client *client)
2809 {
2810 struct abov_tk_info *info = i2c_get_clientdata(client);
2811 u8 cmd = CMD_LED_OFF;
2812 tk_debug_info(true, &client->dev, "Inside abov_tk_shutdown \n");
2813
2814 if (info->enabled){
2815 disable_irq(info->irq);
2816 abov_tk_i2c_write(client, ABOV_BTNSTATUS, &cmd, 1);
2817 info->pdata->power(info, false);
2818 }
2819 info->enabled = false;
2820
2821 // just power off.
2822 // if (info->irq >= 0)
2823 // free_irq(info->irq, info);
2824 // kfree(info);
2825 }
2826
2827 #if defined(CONFIG_PM) && !defined(CONFIG_TOUCHKEY_GRIP)
2828 static int abov_tk_suspend(struct device *dev)
2829 {
2830 struct i2c_client *client = to_i2c_client(dev);
2831 struct abov_tk_info *info = i2c_get_clientdata(client);
2832
2833 if (!info->enabled) {
2834 tk_debug_info(true, &client->dev, "%s: already power off\n", __func__);
2835 return 0;
2836 }
2837
2838 tk_debug_info(true, &client->dev, "%s: users=%d\n", __func__,
2839 info->input_dev->users);
2840
2841 disable_irq(info->irq);
2842 info->enabled = false;
2843 release_all_fingers(info);
2844
2845 if (info->pdata->power)
2846 info->pdata->power(info, false);
2847 return 0;
2848 }
2849
2850 static int abov_tk_resume(struct device *dev)
2851 {
2852 struct i2c_client *client = to_i2c_client(dev);
2853 struct abov_tk_info *info = i2c_get_clientdata(client);
2854 u8 led_data;
2855
2856 if (info->enabled) {
2857 tk_debug_info(true, &client->dev, "%s: already power on\n", __func__);
2858 return 0;
2859 }
2860
2861 tk_debug_info(true, &info->client->dev, "%s: users=%d\n", __func__,
2862 info->input_dev->users);
2863
2864 if (info->pdata->power) {
2865 info->pdata->power(info, true);
2866 msleep(ABOV_RESET_DELAY);
2867 } else /* touchkey on by i2c */
2868 get_tk_fw_version(info, true);
2869
2870 info->enabled = true;
2871
2872 if (abov_touchled_cmd_reserved && \
2873 abov_touchkey_led_status == CMD_LED_ON) {
2874 abov_touchled_cmd_reserved = 0;
2875 led_data=abov_touchkey_led_status;
2876
2877 abov_tk_i2c_write(client, ABOV_BTNSTATUS, &led_data, 1);
2878
2879 tk_debug_dbg(true, &info->client->dev, "%s: LED reserved on\n", __func__);
2880 }
2881 enable_irq(info->irq);
2882
2883 return 0;
2884 }
2885 #endif
2886
2887 #ifdef CONFIG_HAS_EARLYSUSPEND
2888 static void abov_tk_early_suspend(struct early_suspend *h)
2889 {
2890 struct abov_tk_info *info;
2891 info = container_of(h, struct abov_tk_info, early_suspend);
2892 abov_tk_suspend(&info->client->dev);
2893
2894 }
2895
2896 static void abov_tk_late_resume(struct early_suspend *h)
2897 {
2898 struct abov_tk_info *info;
2899 info = container_of(h, struct abov_tk_info, early_suspend);
2900 abov_tk_resume(&info->client->dev);
2901 }
2902 #endif
2903
2904 #if 1//def CONFIG_INPUT_ENABLED
2905 static int abov_tk_input_open(struct input_dev *dev)
2906 {
2907 struct abov_tk_info *info = input_get_drvdata(dev);
2908
2909 tk_debug_dbg(true, &info->client->dev, "%s %d \n",__func__,__LINE__);
2910 tk_debug_info(true, &info->client->dev, "%s: users=%d, v:0x%02x, g(%d), f(%d), k(%d)\n", __func__,
2911 info->input_dev->users, info->fw_ver, info->flip_mode, info->glovemode, info->keyboard_mode);
2912 #ifdef CONFIG_TOUCHKEY_GRIP
2913 if (lpcharge == 1) {
2914 tk_debug_info(true, &info->client->dev, "%s(lpcharge): sar_enable(%d)\n", __func__, info->sar_enable);
2915 return 0;
2916 }
2917
2918 tk_debug_info(true, &info->client->dev, "%s: sar_enable(%d)\n", __func__, info->sar_enable);
2919
2920 if (info->flip_mode)
2921 abov_sar_olny_mode(info, 1);
2922 else
2923 abov_sar_olny_mode(info, 0);
2924
2925 if (device_may_wakeup(&info->client->dev))
2926 disable_irq_wake(info->irq );
2927 #else
2928 abov_tk_resume(&info->client->dev);
2929 if (info->pinctrl)
2930 abov_pinctrl_configure(info, true);
2931
2932 if (info->flip_mode){
2933 abov_mode_enable(info->client, ABOV_FLIP, CMD_FLIP_ON);
2934 } else {
2935 if (info->glovemode)
2936 abov_mode_enable(info->client, ABOV_GLOVE, CMD_GLOVE_ON);
2937 }
2938 if (info->keyboard_mode)
2939 abov_mode_enable(info->client, ABOV_KEYBOARD, CMD_MOBILE_KBD_ON);
2940 #endif
2941 return 0;
2942 }
2943 static void abov_tk_input_close(struct input_dev *dev)
2944 {
2945 struct abov_tk_info *info = input_get_drvdata(dev);
2946
2947 tk_debug_dbg(true, &info->client->dev, "%s %d \n",__func__,__LINE__);
2948 tk_debug_info(true, &info->client->dev, "%s: users=%d\n", __func__,
2949 info->input_dev->users);
2950 #ifdef CONFIG_TOUCHKEY_GRIP
2951 tk_debug_info(true, &info->client->dev, "%s: sar_enable(%d)\n", __func__, info->sar_enable);
2952 abov_sar_olny_mode(info, 1);
2953
2954 if (device_may_wakeup(&info->client->dev))
2955 enable_irq_wake(info->irq );
2956 #else
2957 abov_tk_suspend(&info->client->dev);
2958 if (info->pinctrl)
2959 abov_pinctrl_configure(info, false);
2960 #endif
2961
2962 #ifdef LED_TWINKLE_BOOTING
2963 info->led_twinkle_check = 0;
2964 #endif
2965
2966 }
2967 #endif
2968
2969 #if 0//defined(CONFIG_PM) && !defined(CONFIG_HAS_EARLYSUSPEND) &&!defined(CONFIG_INPUT_ENABLED)
2970 static const struct dev_pm_ops abov_tk_pm_ops = {
2971 .suspend = abov_tk_suspend,
2972 .resume = abov_tk_resume,
2973 };
2974 #endif
2975
2976 static const struct i2c_device_id abov_tk_id[] = {
2977 { ABOV_TK_NAME, 0},
2978 {}
2979 };
2980 MODULE_DEVICE_TABLE(i2c, abov_tk_id);
2981
2982 #ifdef CONFIG_OF
2983 static struct of_device_id abov_match_table[] = {
2984 { .compatible = "abov,mc96ft18xx",},
2985 { .compatible = "abov,mc96ft16xx",}, // for abov_parse_dt
2986 { },
2987 };
2988 #else
2989 #define abov_match_table NULL
2990 #endif
2991
2992 static struct i2c_driver abov_tk_driver = {
2993 .probe = abov_tk_probe,
2994 .remove = abov_tk_remove,
2995 .shutdown = abov_tk_shutdown,
2996 .driver = {
2997 .name = ABOV_TK_NAME,
2998 .of_match_table = abov_match_table,
2999 #if 0//defined(CONFIG_PM) && !defined(CONFIG_HAS_EARLYSUSPEND) &&!defined(CONFIG_INPUT_ENABLED)
3000 .pm = &abov_tk_pm_ops,
3001 #endif
3002 },
3003 .id_table = abov_tk_id,
3004 };
3005
3006 static int __init touchkey_init(void)
3007 {
3008 pr_err("%s: abov,%s\n", __func__, (is_ft1604_chip ? "mc96ft16xx" : "mc96ft18xx"));
3009
3010 #if defined(CONFIG_BATTERY_SAMSUNG) && !defined(CONFIG_TOUCHKEY_GRIP)
3011 if (lpcharge == 1) {
3012 pr_notice("%s : Do not load driver due to : lpm %d\n",
3013 __func__, lpcharge);
3014 return 0;
3015 }
3016 #endif
3017
3018 return i2c_add_driver(&abov_tk_driver);
3019 }
3020
3021 static void __exit touchkey_exit(void)
3022 {
3023 i2c_del_driver(&abov_tk_driver);
3024 }
3025
3026 module_init(touchkey_init);
3027 module_exit(touchkey_exit);
3028
3029 static int __init get_bootloader(char *bootloader)
3030 {
3031 is_ft1604_chip = false;
3032
3033 if (strstr(bootloader, "A510M") || strstr(bootloader, "A510Y")) {
3034 is_ft1604_chip = true;
3035 fw_path = "abov/abov_ft1604_a5_ltn.fw";
3036 }
3037 else if (strstr(bootloader, "A310N0") || strstr(bootloader, "A310M")) {
3038 is_ft1604_chip = true;
3039 fw_path = "abov/abov_ft1604_a3.fw";
3040 }
3041
3042 return 0;
3043 }
3044 early_param("androidboot.bootloader", get_bootloader);
3045
3046 /* Module information */
3047 MODULE_AUTHOR("Samsung Electronics");
3048 MODULE_DESCRIPTION("Touchkey driver for Abov MF16xx and MF18xx chip");
3049 MODULE_LICENSE("GPL");