mortaromarcello

ft5x_ts.c

Jun 3rd, 2012
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 53.76 KB | None | 0 0
  1. /*
  2.  * drivers/input/touchscreen/ft5x0x_ts.c
  3.  *
  4.  * FocalTech ft5x TouchScreen driver.
  5.  *
  6.  * Copyright (c) 2010  Focal tech Ltd.
  7.  *
  8.  * This software is licensed under the terms of the GNU General Public
  9.  * License version 2, as published by the Free Software Foundation, and
  10.  * may be copied, distributed, and modified under those terms.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  *
  18.  *  note: only support mulititouch  Wenfs 2010-10-01
  19.  *  for this touchscreen to work, it's slave addr must be set to 0x7e | 0x70
  20.  */
  21.  
  22. #include <linux/i2c.h>
  23. #include <linux/input.h>
  24. #include "ft5x_ts.h"
  25. #ifdef CONFIG_HAS_EARLYSUSPEND
  26.     #include <linux/pm.h>
  27.     #include <linux/earlysuspend.h>
  28. #endif
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/errno.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/slab.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/async.h>
  38. #include <linux/hrtimer.h>
  39. #include <linux/init.h>
  40. #include <linux/ioport.h>
  41. #include <asm/irq.h>
  42. #include <asm/io.h>
  43. #include <asm/uaccess.h>
  44.  
  45. #include <mach/irqs.h>
  46. #include <mach/system.h>
  47. #include <mach/hardware.h>
  48. #include <mach/sys_config.h>
  49. #include "ctp_platform_ops.h"
  50.  
  51. #define FOR_TSLIB_TEST
  52. //#define PRINT_INT_INFO
  53. #define PRINT_POINT_INFO
  54. //#define DEBUG
  55. #define TOUCH_KEY_SUPPORT
  56. #ifdef TOUCH_KEY_SUPPORT
  57. #define TOUCH_KEY_LIGHT_SUPPORT
  58. //#define TOUCH_KEY_FOR_EVB13
  59. //#define TOUCH_KEY_FOR_ANGDA
  60. #define TOUCH_KEY_FOR_711
  61. #ifdef TOUCH_KEY_FOR_ANGDA
  62.     #define TOUCH_KEY_X_LIMIT   (60000)
  63.     #define TOUCH_KEY_NUMBER    (4)
  64. #endif
  65. #ifdef TOUCH_KEY_FOR_EVB13
  66.     #define TOUCH_KEY_LOWER_X_LIMIT (848)
  67.     #define TOUCH_KEY_HIGHER_X_LIMIT    (852)
  68.     #define TOUCH_KEY_NUMBER    (5)
  69. #endif
  70. #ifdef TOUCH_KEY_FOR_711
  71.     #define TOUCH_KEY_X_LIMIT (862)
  72.     #define KEY1_Y (145)
  73.     #define KEY2_Y (271)
  74.     #define KEY3_Y (334)
  75.     #define TOUCH_KEY_NUMBER    (4)
  76. #endif
  77. #endif
  78.  
  79. //#define CONFIG_SUPPORT_FTS_CTP_UPG
  80.  
  81. struct i2c_dev{
  82. struct list_head list;
  83. struct i2c_adapter *adap;
  84. struct device *dev;
  85. };
  86.  
  87. static struct class *i2c_dev_class;
  88. static LIST_HEAD (i2c_dev_list);
  89. static DEFINE_SPINLOCK(i2c_dev_list_lock);
  90.  
  91. #define FT5X_NAME   "ft5x_ts"//"synaptics_i2c_rmi"//"synaptics-rmi-ts"//
  92.  
  93. static struct i2c_client *this_client;
  94. #ifdef TOUCH_KEY_LIGHT_SUPPORT
  95. static int gpio_light_hdle = 0;
  96. #endif
  97. #ifdef TOUCH_KEY_SUPPORT
  98. static int key_tp  = 0;
  99. static int key_val = 0;
  100. #endif
  101.  
  102. #ifdef PRINT_POINT_INFO
  103. #define print_point_info(fmt, args...)   \
  104.         do{                              \
  105.                 pr_info(fmt, ##args);     \
  106.         }while(0)
  107. #else
  108. #define print_point_info(fmt, args...)   //
  109. #endif
  110.  
  111. #ifdef PRINT_INT_INFO
  112. #define print_int_info(fmt, args...)     \
  113.         do{                              \
  114.                 pr_info(fmt, ##args);     \
  115.         }while(0)
  116. #else
  117. #define print_int_info(fmt, args...)   //
  118. #endif
  119. ///////////////////////////////////////////////
  120. //specific tp related macro: need be configured for specific tp
  121. #ifdef CONFIG_ARCH_SUN4I
  122. #define CTP_IRQ_NO          (IRQ_EINT21)
  123. #elif defined(CONFIG_ARCH_SUN5I)
  124. #define CTP_IRQ_NO          (IRQ_EINT9)
  125. #endif
  126.  
  127. #define CTP_IRQ_MODE            (NEGATIVE_EDGE)
  128. #define CTP_NAME            FT5X_NAME
  129. #define TS_RESET_LOW_PERIOD     (1)
  130. #define TS_INITIAL_HIGH_PERIOD      (30)
  131. #define TS_WAKEUP_LOW_PERIOD    (20)
  132. #define TS_WAKEUP_HIGH_PERIOD   (20)
  133. #define TS_POLL_DELAY           (10)    /* ms delay between samples */
  134. #define TS_POLL_PERIOD          (10)    /* ms delay between samples */
  135. #define SCREEN_MAX_X            (screen_max_x)
  136. #define SCREEN_MAX_Y            (screen_max_y)
  137. #define PRESS_MAX           (255)
  138.  
  139.  
  140. static void* __iomem gpio_addr = NULL;
  141. static int gpio_int_hdle = 0;
  142. static int gpio_wakeup_hdle = 0;
  143. static int gpio_reset_hdle = 0;
  144. static int gpio_wakeup_enable = 1;
  145. static int gpio_reset_enable = 1;
  146.  
  147. static int screen_max_x = 0;
  148. static int screen_max_y = 0;
  149. static int revert_x_flag = 0;
  150. static int revert_y_flag = 0;
  151. static int exchange_x_y_flag = 0;
  152. static int  int_cfg_addr[]={PIO_INT_CFG0_OFFSET,PIO_INT_CFG1_OFFSET,
  153.             PIO_INT_CFG2_OFFSET, PIO_INT_CFG3_OFFSET};
  154. /* Addresses to scan */
  155. static union{
  156.     unsigned short dirty_addr_buf[2];
  157.     const unsigned short normal_i2c[2];
  158. }u_i2c_addr = {{0x00},};
  159. static __u32 twi_id = 0;
  160.  
  161. /*
  162.  * ctp_get_pendown_state  : get the int_line data state,
  163.  *
  164.  * return value:
  165.  *             return PRESS_DOWN: if down
  166.  *             return FREE_UP: if up,
  167.  *             return 0: do not need process, equal free up.
  168.  */
  169. static int ctp_get_pendown_state(void)
  170. {
  171.     unsigned int reg_val;
  172.     static int state = FREE_UP;
  173.  
  174.     //get the input port state
  175.     reg_val = readl(gpio_addr + PIOH_DATA);
  176.     //pr_info("reg_val = %x\n",reg_val);
  177.     if(!(reg_val & (1<<CTP_IRQ_NO))){
  178.         state = PRESS_DOWN;
  179.         print_int_info("pen down. \n");
  180.     }else{ //touch panel is free up
  181.         state = FREE_UP;
  182.         print_int_info("free up. \n");
  183.     }
  184.     return state;
  185. }
  186.  
  187. /**
  188.  * ctp_clear_penirq - clear int pending
  189.  *
  190.  */
  191. static void ctp_clear_penirq(void)
  192. {
  193.     int reg_val;
  194.     //clear the IRQ_EINT29 interrupt pending
  195.     //pr_info("clear pend irq pending\n");
  196.     reg_val = readl(gpio_addr + PIO_INT_STAT_OFFSET);
  197.     //writel(reg_val,gpio_addr + PIO_INT_STAT_OFFSET);
  198.     //writel(reg_val&(1<<(IRQ_EINT21)),gpio_addr + PIO_INT_STAT_OFFSET);
  199.     if((reg_val = (reg_val&(1<<(CTP_IRQ_NO))))){
  200.         print_int_info("==CTP_IRQ_NO=\n");
  201.         writel(reg_val,gpio_addr + PIO_INT_STAT_OFFSET);
  202.     }
  203.     return;
  204. }
  205.  
  206. /**
  207.  * ctp_set_irq_mode - according sysconfig's subkey "ctp_int_port" to config int port.
  208.  *
  209.  * return value:
  210.  *              0:      success;
  211.  *              others: fail;
  212.  */
  213. static int ctp_set_irq_mode(char *major_key , char *subkey, int ext_int_num, ext_int_mode int_mode)
  214. {
  215.     int ret = 0;
  216.     __u32 reg_num = 0;
  217.     __u32 reg_addr = 0;
  218.     __u32 reg_val = 0;
  219.     //config gpio to int mode
  220.     pr_info("%s: config gpio to int mode. \n", __func__);
  221. #ifndef SYSCONFIG_GPIO_ENABLE
  222. #else
  223.     if(gpio_int_hdle){
  224.         gpio_release(gpio_int_hdle, 2);
  225.     }
  226.     gpio_int_hdle = gpio_request_ex(major_key, subkey);
  227.     if(!gpio_int_hdle){
  228.         pr_info("request tp_int_port failed. \n");
  229.         ret = -1;
  230.         goto request_tp_int_port_failed;
  231.     }
  232. #endif
  233.  
  234. #ifdef AW_GPIO_INT_API_ENABLE
  235. #else
  236.     pr_info(" INTERRUPT CONFIG\n");
  237.     reg_num = ext_int_num%8;
  238.     reg_addr = ext_int_num/8;
  239.     reg_val = readl(gpio_addr + int_cfg_addr[reg_addr]);
  240.     reg_val &= (~(7 << (reg_num * 4)));
  241.     reg_val |= (int_mode << (reg_num * 4));
  242.     writel(reg_val,gpio_addr+int_cfg_addr[reg_addr]);
  243.  
  244.     ctp_clear_penirq();
  245.  
  246.     reg_val = readl(gpio_addr+PIO_INT_CTRL_OFFSET);
  247.     reg_val |= (1 << ext_int_num);
  248.     writel(reg_val,gpio_addr+PIO_INT_CTRL_OFFSET);
  249.  
  250.     udelay(1);
  251. #endif
  252.  
  253. request_tp_int_port_failed:
  254.     return ret;
  255. }
  256.  
  257. /**
  258.  * ctp_set_gpio_mode - according sysconfig's subkey "ctp_io_port" to config io port.
  259.  *
  260.  * return value:
  261.  *              0:      success;
  262.  *              others: fail;
  263.  */
  264. static int ctp_set_gpio_mode(void)
  265. {
  266.     //int reg_val;
  267.     int ret = 0;
  268.     //config gpio to io mode
  269.     pr_info("%s: config gpio to io mode. \n", __func__);
  270. #ifndef SYSCONFIG_GPIO_ENABLE
  271. #else
  272.     if(gpio_int_hdle){
  273.         gpio_release(gpio_int_hdle, 2);
  274.     }
  275.     gpio_int_hdle = gpio_request_ex("ctp_para", "ctp_io_port");
  276.     if(!gpio_int_hdle){
  277.         pr_info("request ctp_io_port failed. \n");
  278.         ret = -1;
  279.         goto request_tp_io_port_failed;
  280.     }
  281. #endif
  282.     return ret;
  283.  
  284. request_tp_io_port_failed:
  285.     return ret;
  286. }
  287.  
  288. /**
  289.  * ctp_judge_int_occur - whether interrupt occur.
  290.  *
  291.  * return value:
  292.  *              0:      int occur;
  293.  *              others: no int occur;
  294.  */
  295. static int ctp_judge_int_occur(void)
  296. {
  297.     //int reg_val[3];
  298.     int reg_val;
  299.     int ret = -1;
  300.  
  301.     reg_val = readl(gpio_addr + PIO_INT_STAT_OFFSET);
  302.     if(reg_val&(1<<(CTP_IRQ_NO))){
  303.         ret = 0;
  304.     }
  305.     return ret;
  306. }
  307.  
  308. /**
  309.  * ctp_free_platform_resource - corresponding with ctp_init_platform_resource
  310.  *
  311.  */
  312. static void ctp_free_platform_resource(void)
  313. {
  314.     if(gpio_addr){
  315.         iounmap(gpio_addr);
  316.     }
  317.  
  318.     if(gpio_int_hdle){
  319.         gpio_release(gpio_int_hdle, 2);
  320.     }
  321.  
  322.     if(gpio_wakeup_hdle){
  323.         gpio_release(gpio_wakeup_hdle, 2);
  324.     }
  325.  
  326.     if(gpio_reset_hdle){
  327.         gpio_release(gpio_reset_hdle, 2);
  328.     }
  329.  
  330.     return;
  331. }
  332.  
  333.  
  334. /**
  335.  * ctp_init_platform_resource - initialize platform related resource
  336.  * return value: 0 : success
  337.  *               -EIO :  i/o err.
  338.  *
  339.  */
  340. static int ctp_init_platform_resource(void)
  341. {
  342.     int ret = 0;
  343.  
  344.     gpio_addr = ioremap(PIO_BASE_ADDRESS, PIO_RANGE_SIZE);
  345.     //pr_info("%s, gpio_addr = 0x%x. \n", __func__, gpio_addr);
  346.     if(!gpio_addr) {
  347.         ret = -EIO;
  348.         goto exit_ioremap_failed;
  349.     }
  350.     //    gpio_wakeup_enable = 1;
  351.     gpio_wakeup_hdle = gpio_request_ex("ctp_para", "ctp_wakeup");
  352.     if(!gpio_wakeup_hdle) {
  353.         pr_warning("%s: tp_wakeup request gpio fail!\n", __func__);
  354.         gpio_wakeup_enable = 0;
  355.     }
  356.  
  357.     gpio_reset_hdle = gpio_request_ex("ctp_para", "ctp_reset");
  358.     if(!gpio_reset_hdle) {
  359.         pr_warning("%s: tp_reset request gpio fail!\n", __func__);
  360.         gpio_reset_enable = 0;
  361.     }
  362.  
  363.     return ret;
  364.  
  365. exit_ioremap_failed:
  366.     ctp_free_platform_resource();
  367.     return ret;
  368. }
  369.  
  370.  
  371. /**
  372.  * ctp_fetch_sysconfig_para - get config info from sysconfig.fex file.
  373.  * return value:
  374.  *                    = 0; success;
  375.  *                    < 0; err
  376.  */
  377. static int ctp_fetch_sysconfig_para(void)
  378. {
  379.     int ret = -1;
  380.     int ctp_used = -1;
  381.     char name[I2C_NAME_SIZE];
  382.     __u32 twi_addr = 0;
  383.     //__u32 twi_id = 0;
  384.     script_parser_value_type_t type = SCIRPT_PARSER_VALUE_TYPE_STRING;
  385.  
  386.     pr_info("%s. \n", __func__);
  387.  
  388.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_used", &ctp_used, 1)){
  389.         pr_err("%s: script_parser_fetch err. \n", __func__);
  390.         goto script_parser_fetch_err;
  391.     }
  392.     if(1 != ctp_used){
  393.         pr_err("%s: ctp_unused. \n",  __func__);
  394.         //ret = 1;
  395.         return ret;
  396.     }
  397.  
  398.     if(SCRIPT_PARSER_OK != script_parser_fetch_ex("ctp_para", "ctp_name", (int *)(&name), &type, sizeof(name)/sizeof(int))){
  399.         pr_err("%s: script_parser_fetch err. \n", __func__);
  400.         goto script_parser_fetch_err;
  401.     }
  402.     if(strcmp(CTP_NAME, name)){
  403.         pr_err("%s: name %s does not match CTP_NAME. \n", __func__, name);
  404.         pr_err(CTP_NAME);
  405.         //ret = 1;
  406.         return ret;
  407.     }
  408.  
  409.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_twi_addr", &twi_addr, sizeof(twi_addr)/sizeof(__u32))){
  410.         pr_err("%s: script_parser_fetch err. \n", name);
  411.         goto script_parser_fetch_err;
  412.     }
  413.     //big-endian or small-endian?
  414.     //pr_info("%s: before: ctp_twi_addr is 0x%x, dirty_addr_buf: 0x%hx. dirty_addr_buf[1]: 0x%hx \n", __func__, twi_addr, u_i2c_addr.dirty_addr_buf[0], u_i2c_addr.dirty_addr_buf[1]);
  415.     u_i2c_addr.dirty_addr_buf[0] = twi_addr;
  416.     u_i2c_addr.dirty_addr_buf[1] = I2C_CLIENT_END;
  417.     pr_info("%s: after: ctp_twi_addr is 0x%x, dirty_addr_buf: 0x%hx. dirty_addr_buf[1]: 0x%hx \n", __func__, twi_addr, u_i2c_addr.dirty_addr_buf[0], u_i2c_addr.dirty_addr_buf[1]);
  418.     //pr_info("%s: after: ctp_twi_addr is 0x%x, u32_dirty_addr_buf: 0x%hx. u32_dirty_addr_buf[1]: 0x%hx \n", __func__, twi_addr, u32_dirty_addr_buf[0],u32_dirty_addr_buf[1]);
  419.  
  420.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_twi_id", &twi_id, sizeof(twi_id)/sizeof(__u32))){
  421.         pr_err("%s: script_parser_fetch err. \n", name);
  422.         goto script_parser_fetch_err;
  423.     }
  424.     pr_info("%s: ctp_twi_id is %d. \n", __func__, twi_id);
  425.  
  426.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_screen_max_x", &screen_max_x, 1)){
  427.         pr_err("%s: script_parser_fetch err. \n", __func__);
  428.         goto script_parser_fetch_err;
  429.     }
  430.     pr_info("%s: screen_max_x = %d. \n", __func__, screen_max_x);
  431.  
  432.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_screen_max_y", &screen_max_y, 1)){
  433.         pr_err("%s: script_parser_fetch err. \n", __func__);
  434.         goto script_parser_fetch_err;
  435.     }
  436.     pr_info("%s: screen_max_y = %d. \n", __func__, screen_max_y);
  437.  
  438.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_revert_x_flag", &revert_x_flag, 1)){
  439.         pr_err("%s: script_parser_fetch err. \n", __func__);
  440.         goto script_parser_fetch_err;
  441.     }
  442.     pr_info("%s: revert_x_flag = %d. \n", __func__, revert_x_flag);
  443.  
  444.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_revert_y_flag", &revert_y_flag, 1)){
  445.         pr_err("%s: script_parser_fetch err. \n", __func__);
  446.         goto script_parser_fetch_err;
  447.     }
  448.     pr_info("%s: revert_y_flag = %d. \n", __func__, revert_y_flag);
  449.  
  450.     if(SCRIPT_PARSER_OK != script_parser_fetch("ctp_para", "ctp_exchange_x_y_flag", &exchange_x_y_flag, 1)){
  451.         pr_err("ft5x_ts: script_parser_fetch err. \n");
  452.         goto script_parser_fetch_err;
  453.     }
  454.     pr_info("%s: exchange_x_y_flag = %d. \n", __func__, exchange_x_y_flag);
  455.  
  456.     return 0;
  457.  
  458. script_parser_fetch_err:
  459.     pr_notice("=========script_parser_fetch_err============\n");
  460.     return ret;
  461. }
  462.  
  463. /**
  464.  * ctp_reset - function
  465.  *
  466.  */
  467. static void ctp_reset(void)
  468. {
  469.     if(gpio_reset_enable){
  470.         pr_info("%s. \n", __func__);
  471.         if(EGPIO_SUCCESS != gpio_write_one_pin_value(gpio_reset_hdle, 0, "ctp_reset")){
  472.             pr_info("%s: err when operate gpio. \n", __func__);
  473.         }
  474.         mdelay(TS_RESET_LOW_PERIOD);
  475.         if(EGPIO_SUCCESS != gpio_write_one_pin_value(gpio_reset_hdle, 1, "ctp_reset")){
  476.             pr_info("%s: err when operate gpio. \n", __func__);
  477.         }
  478.         mdelay(TS_INITIAL_HIGH_PERIOD);
  479.     }
  480. }
  481.  
  482. /**
  483.  * ctp_wakeup - function
  484.  *
  485.  */
  486. static void ctp_wakeup(void)
  487. {
  488.     if(1 == gpio_wakeup_enable){
  489.         pr_info("%s. \n", __func__);
  490.         if(EGPIO_SUCCESS != gpio_write_one_pin_value(gpio_wakeup_hdle, 0, "ctp_wakeup")){
  491.             pr_info("%s: err when operate gpio. \n", __func__);
  492.         }
  493.         mdelay(TS_WAKEUP_LOW_PERIOD);
  494.         if(EGPIO_SUCCESS != gpio_write_one_pin_value(gpio_wakeup_hdle, 1, "ctp_wakeup")){
  495.             pr_info("%s: err when operate gpio. \n", __func__);
  496.         }
  497.         mdelay(TS_WAKEUP_HIGH_PERIOD);
  498.  
  499.     }
  500.     return;
  501. }
  502. /**
  503.  * ctp_detect - Device detection callback for automatic device creation
  504.  * return value:
  505.  *                    = 0; success;
  506.  *                    < 0; err
  507.  */
  508. static int ctp_detect(struct i2c_client *client, struct i2c_board_info *info)
  509. {
  510.     struct i2c_adapter *adapter = client->adapter;
  511.  
  512.     if(twi_id == adapter->nr)
  513.     {
  514.         pr_info("%s: Detected chip %s at adapter %d, address 0x%02x\n",
  515.              __func__, CTP_NAME, i2c_adapter_id(adapter), client->addr);
  516.  
  517.         strlcpy(info->type, CTP_NAME, I2C_NAME_SIZE);
  518.         return 0;
  519.     }else{
  520.         return -ENODEV;
  521.     }
  522. }
  523. ////////////////////////////////////////////////////////////////
  524.  
  525. static struct ctp_platform_ops ctp_ops = {
  526.     .get_pendown_state = ctp_get_pendown_state,
  527.     .clear_penirq      = ctp_clear_penirq,
  528.     .set_irq_mode      = ctp_set_irq_mode,
  529.     .set_gpio_mode     = ctp_set_gpio_mode,
  530.     .judge_int_occur   = ctp_judge_int_occur,
  531.     .init_platform_resource = ctp_init_platform_resource,
  532.     .free_platform_resource = ctp_free_platform_resource,
  533.     .fetch_sysconfig_para = ctp_fetch_sysconfig_para,
  534.     .ts_reset =          ctp_reset,
  535.     .ts_wakeup =         ctp_wakeup,
  536.     .ts_detect = ctp_detect,
  537. };
  538.  
  539. int fts_ctpm_fw_upgrade_with_i_file(void);
  540.  
  541. static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
  542. {
  543.     struct i2c_dev *i2c_dev;
  544.     spin_lock(&i2c_dev_list_lock);
  545.  
  546.     list_for_each_entry(i2c_dev,&i2c_dev_list,list){
  547.         pr_info("--line = %d ,i2c_dev->adapt->nr = %d,index = %d.\n",__LINE__,i2c_dev->adap->nr,index);
  548.         if(i2c_dev->adap->nr == index){
  549.              goto found;
  550.         }
  551.     }
  552.     i2c_dev = NULL;
  553.  
  554. found:
  555.     spin_unlock(&i2c_dev_list_lock);
  556.  
  557.     return i2c_dev ;
  558. }
  559.  
  560. static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
  561. {
  562.     struct i2c_dev *i2c_dev;
  563.  
  564.     if (adap->nr >= I2C_MINORS){
  565.         pr_info("i2c-dev:out of device minors (%d) \n",adap->nr);
  566.         return ERR_PTR (-ENODEV);
  567.     }
  568.  
  569.     i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
  570.     if (!i2c_dev){
  571.         return ERR_PTR(-ENOMEM);
  572.     }
  573.     i2c_dev->adap = adap;
  574.  
  575.     spin_lock(&i2c_dev_list_lock);
  576.     list_add_tail(&i2c_dev->list, &i2c_dev_list);
  577.     spin_unlock(&i2c_dev_list_lock);
  578.  
  579.     return i2c_dev;
  580. }
  581.  
  582.  
  583. static int ft5x_i2c_rxdata(char *rxdata, int length);
  584.  
  585. struct ts_event {
  586.     u16 x1;
  587.     u16 y1;
  588.     u16 x2;
  589.     u16 y2;
  590.     u16 x3;
  591.     u16 y3;
  592.     u16 x4;
  593.     u16 y4;
  594.     u16 x5;
  595.     u16 y5;
  596.     u16 pressure;
  597.     s16 touch_ID1;
  598.     s16 touch_ID2;
  599.     s16 touch_ID3;
  600.     s16 touch_ID4;
  601.     s16 touch_ID5;
  602.     u8  touch_point;
  603. };
  604.  
  605. struct ft5x_ts_data {
  606.     struct input_dev    *input_dev;
  607.     struct ts_event     event;
  608.     struct work_struct  pen_event_work;
  609.     struct workqueue_struct *ts_workqueue;
  610. #ifdef CONFIG_HAS_EARLYSUSPEND
  611.     struct early_suspend    early_suspend;
  612. #endif
  613. };
  614.  
  615.  
  616. /* ---------------------------------------------------------------------
  617. *
  618. *   Focal Touch panel upgrade related driver
  619. *
  620. *
  621. ----------------------------------------------------------------------*/
  622.  
  623. typedef enum
  624. {
  625.     ERR_OK,
  626.     ERR_MODE,
  627.     ERR_READID,
  628.     ERR_ERASE,
  629.     ERR_STATUS,
  630.     ERR_ECC,
  631.     ERR_DL_ERASE_FAIL,
  632.     ERR_DL_PROGRAM_FAIL,
  633.     ERR_DL_VERIFY_FAIL
  634. }E_UPGRADE_ERR_TYPE;
  635.  
  636. typedef unsigned char         FTS_BYTE;     //8 bit
  637. typedef unsigned short        FTS_WORD;    //16 bit
  638. typedef unsigned int          FTS_DWRD;    //16 bit
  639. typedef unsigned char         FTS_BOOL;    //8 bit
  640.  
  641. #define FTS_NULL                0x0
  642. #define FTS_TRUE                0x01
  643. #define FTS_FALSE               0x0
  644.  
  645. #define I2C_CTPM_ADDRESS        (0x70>>1)
  646.  
  647. void delay_ms(FTS_WORD  w_ms)
  648. {
  649.     //platform related, please implement this function
  650.     msleep( w_ms );
  651. }
  652.  
  653.  
  654. /*
  655. [function]:
  656.     callback: read data from ctpm by i2c interface,implemented by special user;
  657. [parameters]:
  658.     bt_ctpm_addr[in]    :the address of the ctpm;
  659.     pbt_buf[out]        :data buffer;
  660.     dw_lenth[in]        :the length of the data buffer;
  661. [return]:
  662.     FTS_TRUE     :success;
  663.     FTS_FALSE    :fail;
  664. */
  665. int i2c_read_interface(u8 bt_ctpm_addr, u8* pbt_buf, u16 dw_lenth)
  666. {
  667.     int ret;
  668.  
  669.     ret=i2c_master_recv(this_client, pbt_buf, dw_lenth);
  670.  
  671.     if(ret != dw_lenth){
  672.         pr_info("ret = %d. \n", ret);
  673.         pr_info("i2c_read_interface error\n");
  674.         return FTS_FALSE;
  675.     }
  676.  
  677.     return FTS_TRUE;
  678. }
  679.  
  680. /*
  681. [function]:
  682.     callback: write data to ctpm by i2c interface,implemented by special user;
  683. [parameters]:
  684.     bt_ctpm_addr[in]    :the address of the ctpm;
  685.     pbt_buf[in]        :data buffer;
  686.     dw_lenth[in]        :the length of the data buffer;
  687. [return]:
  688.     FTS_TRUE     :success;
  689.     FTS_FALSE    :fail;
  690. */
  691. int i2c_write_interface(u8 bt_ctpm_addr, u8* pbt_buf, u16 dw_lenth)
  692. {
  693.     int ret;
  694.     ret=i2c_master_send(this_client, pbt_buf, dw_lenth);
  695.     if(ret != dw_lenth){
  696.         pr_info("i2c_write_interface error\n");
  697.         return FTS_FALSE;
  698.     }
  699.  
  700.     return FTS_TRUE;
  701. }
  702.  
  703.  
  704. /***************************************************************************************/
  705.  
  706. /*
  707. [function]:
  708.     read out the register value.
  709. [parameters]:
  710.     e_reg_name[in]    :register name;
  711.     pbt_buf[out]    :the returned register value;
  712.     bt_len[in]        :length of pbt_buf, should be set to 2;
  713. [return]:
  714.     FTS_TRUE    :success;
  715.     FTS_FALSE    :io fail;
  716. */
  717. u8 fts_register_read(u8 e_reg_name, u8* pbt_buf, u8 bt_len)
  718. {
  719.     u8 read_cmd[3]= {0};
  720.     u8 cmd_len     = 0;
  721.  
  722.     read_cmd[0] = e_reg_name;
  723.     cmd_len = 1;
  724.  
  725.     /*call the write callback function*/
  726.     //    if(!i2c_write_interface(I2C_CTPM_ADDRESS, &read_cmd, cmd_len))
  727.     //    {
  728.     //        return FTS_FALSE;
  729.     //    }
  730.  
  731.  
  732.     if(!i2c_write_interface(I2C_CTPM_ADDRESS, read_cmd, cmd_len))   {//change by zhengdixu
  733.         return FTS_FALSE;
  734.     }
  735.  
  736.     /*call the read callback function to get the register value*/
  737.     if(!i2c_read_interface(I2C_CTPM_ADDRESS, pbt_buf, bt_len)){
  738.         return FTS_FALSE;
  739.     }
  740.     return FTS_TRUE;
  741. }
  742.  
  743. /*
  744. [function]:
  745.     write a value to register.
  746. [parameters]:
  747.     e_reg_name[in]    :register name;
  748.     pbt_buf[in]        :the returned register value;
  749. [return]:
  750.     FTS_TRUE    :success;
  751.     FTS_FALSE    :io fail;
  752. */
  753. int fts_register_write(u8 e_reg_name, u8 bt_value)
  754. {
  755.     FTS_BYTE write_cmd[2] = {0};
  756.  
  757.     write_cmd[0] = e_reg_name;
  758.     write_cmd[1] = bt_value;
  759.  
  760.     /*call the write callback function*/
  761.     //return i2c_write_interface(I2C_CTPM_ADDRESS, &write_cmd, 2);
  762.     return i2c_write_interface(I2C_CTPM_ADDRESS, write_cmd, 2); //change by zhengdixu
  763. }
  764.  
  765. /*
  766. [function]:
  767.     send a command to ctpm.
  768. [parameters]:
  769.     btcmd[in]        :command code;
  770.     btPara1[in]    :parameter 1;
  771.     btPara2[in]    :parameter 2;
  772.     btPara3[in]    :parameter 3;
  773.     num[in]        :the valid input parameter numbers, if only command code needed and no parameters followed,then the num is 1;
  774. [return]:
  775.     FTS_TRUE    :success;
  776.     FTS_FALSE    :io fail;
  777. */
  778. int cmd_write(u8 btcmd,u8 btPara1,u8 btPara2,u8 btPara3,u8 num)
  779. {
  780.     FTS_BYTE write_cmd[4] = {0};
  781.  
  782.     write_cmd[0] = btcmd;
  783.     write_cmd[1] = btPara1;
  784.     write_cmd[2] = btPara2;
  785.     write_cmd[3] = btPara3;
  786.     //return i2c_write_interface(I2C_CTPM_ADDRESS, &write_cmd, num);
  787.     return i2c_write_interface(I2C_CTPM_ADDRESS, write_cmd, num);//change by zhengdixu
  788. }
  789.  
  790. /*
  791. [function]:
  792.     write data to ctpm , the destination address is 0.
  793. [parameters]:
  794.     pbt_buf[in]    :point to data buffer;
  795.     bt_len[in]        :the data numbers;
  796. [return]:
  797.     FTS_TRUE    :success;
  798.     FTS_FALSE    :io fail;
  799. */
  800. int byte_write(u8* pbt_buf, u16 dw_len)
  801. {
  802.     return i2c_write_interface(I2C_CTPM_ADDRESS, pbt_buf, dw_len);
  803. }
  804.  
  805. /*
  806. [function]:
  807.     read out data from ctpm,the destination address is 0.
  808. [parameters]:
  809.     pbt_buf[out]    :point to data buffer;
  810.     bt_len[in]        :the data numbers;
  811. [return]:
  812.     FTS_TRUE    :success;
  813.     FTS_FALSE    :io fail;
  814. */
  815. int byte_read(u8* pbt_buf, u8 bt_len)
  816. {
  817.     return i2c_read_interface(I2C_CTPM_ADDRESS, pbt_buf, bt_len);
  818.     //ft5x_i2c_rxdata
  819. }
  820.  
  821.  
  822. /*
  823. [function]:
  824.     burn the FW to ctpm.
  825. [parameters]:(ref. SPEC)
  826.     pbt_buf[in]    :point to Head+FW ;
  827.     dw_lenth[in]:the length of the FW + 6(the Head length);
  828.     bt_ecc[in]    :the ECC of the FW
  829. [return]:
  830.     ERR_OK        :no error;
  831.     ERR_MODE    :fail to switch to UPDATE mode;
  832.     ERR_READID    :read id fail;
  833.     ERR_ERASE    :erase chip fail;
  834.     ERR_STATUS    :status error;
  835.     ERR_ECC        :ecc error.
  836. */
  837.  
  838.  
  839. #define    FTS_PACKET_LENGTH       128 //2//4//8//16//32//64//128//256
  840.  
  841. static unsigned char CTPM_FW[]=
  842. {
  843. #include "ft_app.i"
  844. };
  845. unsigned char fts_ctpm_get_i_file_ver(void)
  846. {
  847.     unsigned int ui_sz;
  848.     ui_sz = sizeof(CTPM_FW);
  849.     if (ui_sz > 2)
  850.     {
  851.         return CTPM_FW[ui_sz - 2];
  852.     }
  853.     else
  854.     {
  855.         //TBD, error handling?
  856.         return 0xff; //default value
  857.     }
  858. }
  859. E_UPGRADE_ERR_TYPE  fts_ctpm_fw_upgrade(u8* pbt_buf, u16 dw_lenth)
  860. {
  861.     u8 reg_val[2] = {0};
  862.     FTS_BOOL i_ret = 0;
  863.     u16 i = 0;
  864.  
  865.  
  866.     u16  packet_number;
  867.     u16  j;
  868.     u16  temp;
  869.     u16  lenght;
  870.     u8  packet_buf[FTS_PACKET_LENGTH + 6];
  871.     u8  auc_i2c_write_buf[10];
  872.     u8 bt_ecc;
  873.  
  874.     /*********Step 1:Reset  CTPM *****/
  875.     /*write 0xaa to register 0xfc*/
  876.     delay_ms(100);
  877.     fts_register_write(0xfc,0xaa);
  878.     delay_ms(50);
  879.      /*write 0x55 to register 0xfc*/
  880.     fts_register_write(0xfc,0x55);
  881.     pr_info("Step 1: Reset CTPM test\n");
  882.  
  883.     delay_ms(30);
  884.  
  885.     /*********Step 2:Enter upgrade mode *****/
  886.      auc_i2c_write_buf[0] = 0x55;
  887.      auc_i2c_write_buf[1] = 0xaa;
  888.      i = 0;
  889.      do{
  890.         i++;
  891.         i_ret = i2c_write_interface(I2C_CTPM_ADDRESS, auc_i2c_write_buf, 2);
  892.         pr_info("Step 2: Enter update mode. \n");
  893.         delay_ms(5);
  894.      }while((FTS_FALSE == i_ret) && i<5);
  895.  
  896.     /*********Step 3:check READ-ID***********************/
  897.     /*send the opration head*/
  898.     i = 0;
  899.     do{
  900.         if(i > 3)
  901.         {
  902.           cmd_write(0x07,0x00,0x00,0x00,1);
  903.           return ERR_READID;
  904.         }
  905.         /*read out the CTPM ID*/
  906.         pr_info("====Step 3:check READ-ID====");
  907.         cmd_write(0x90,0x00,0x00,0x00,4);
  908.         byte_read(reg_val,2);
  909.         i++;
  910.         delay_ms(5);
  911.         pr_info("Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0],reg_val[1]);
  912.     }while(reg_val[1] != 0x03);//while(reg_val[0] != 0x79 || reg_val[1] != 0x03);
  913.  
  914.      /*********Step 4:erase app*******************************/
  915.     cmd_write(0x61,0x00,0x00,0x00,1);
  916.     delay_ms(1500);
  917.     pr_info("Step 4: erase. \n");
  918.  
  919.     /*********Step 5:write firmware(FW) to ctpm flash*********/
  920.     bt_ecc = 0;
  921.     pr_info("Step 5: start upgrade. \n");
  922.     dw_lenth = dw_lenth - 8;
  923.     packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
  924.     packet_buf[0] = 0xbf;
  925.     packet_buf[1] = 0x00;
  926.     for (j=0;j<packet_number;j++)
  927.     {
  928.         temp = j * FTS_PACKET_LENGTH;
  929.         packet_buf[2] = (FTS_BYTE)(temp>>8);
  930.         packet_buf[3] = (FTS_BYTE)temp;
  931.         lenght = FTS_PACKET_LENGTH;
  932.         packet_buf[4] = (FTS_BYTE)(lenght>>8);
  933.         packet_buf[5] = (FTS_BYTE)lenght;
  934.  
  935.         for (i=0;i<FTS_PACKET_LENGTH;i++)
  936.         {
  937.             packet_buf[6+i] = pbt_buf[j*FTS_PACKET_LENGTH + i];
  938.             bt_ecc ^= packet_buf[6+i];
  939.         }
  940.  
  941.         byte_write(&packet_buf[0],FTS_PACKET_LENGTH + 6);
  942.         delay_ms(FTS_PACKET_LENGTH/6 + 1);
  943.         if ((j * FTS_PACKET_LENGTH % 1024) == 0)
  944.         {
  945.               pr_info("upgrade the 0x%x th byte.\n", ((unsigned int)j) * FTS_PACKET_LENGTH);
  946.         }
  947.     }
  948.  
  949.     if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
  950.     {
  951.         temp = packet_number * FTS_PACKET_LENGTH;
  952.         packet_buf[2] = (FTS_BYTE)(temp>>8);
  953.         packet_buf[3] = (FTS_BYTE)temp;
  954.  
  955.         temp = (dw_lenth) % FTS_PACKET_LENGTH;
  956.         packet_buf[4] = (FTS_BYTE)(temp>>8);
  957.         packet_buf[5] = (FTS_BYTE)temp;
  958.  
  959.         for (i=0;i<temp;i++)
  960.         {
  961.             packet_buf[6+i] = pbt_buf[ packet_number*FTS_PACKET_LENGTH + i];
  962.             bt_ecc ^= packet_buf[6+i];
  963.         }
  964.  
  965.         byte_write(&packet_buf[0],temp+6);
  966.         delay_ms(20);
  967.     }
  968.  
  969.     //send the last six byte
  970.     for (i = 0; i<6; i++)
  971.     {
  972.         temp = 0x6ffa + i;
  973.         packet_buf[2] = (FTS_BYTE)(temp>>8);
  974.         packet_buf[3] = (FTS_BYTE)temp;
  975.         temp =1;
  976.         packet_buf[4] = (FTS_BYTE)(temp>>8);
  977.         packet_buf[5] = (FTS_BYTE)temp;
  978.         packet_buf[6] = pbt_buf[ dw_lenth + i];
  979.         bt_ecc ^= packet_buf[6];
  980.  
  981.         byte_write(&packet_buf[0],7);
  982.         delay_ms(20);
  983.     }
  984.  
  985.     /*********Step 6: read out checksum***********************/
  986.     /*send the opration head*/
  987.     //cmd_write(0xcc,0x00,0x00,0x00,1);//把0xcc当作寄存器地址,去读出一个字节
  988.    // byte_read(reg_val,1);//change by zhengdixu
  989.  
  990.     fts_register_read(0xcc, reg_val,1);
  991.  
  992.     pr_info("Step 6:  ecc read 0x%x, new firmware 0x%x. \n", reg_val[0], bt_ecc);
  993.     if(reg_val[0] != bt_ecc)
  994.     {
  995.        cmd_write(0x07,0x00,0x00,0x00,1);
  996.         return ERR_ECC;
  997.     }
  998.  
  999.     /*********Step 7: reset the new FW***********************/
  1000.     cmd_write(0x07,0x00,0x00,0x00,1);
  1001.     msleep(30);
  1002.     return ERR_OK;
  1003. }
  1004.  
  1005. int fts_ctpm_auto_clb(void)
  1006. {
  1007.     unsigned char uc_temp;
  1008.     unsigned char i ;
  1009.  
  1010.     pr_info("[FTS] start auto CLB.\n");
  1011.     msleep(200);
  1012.     fts_register_write(0, 0x40);
  1013.     delay_ms(100);                       //make sure already enter factory mode
  1014.     fts_register_write(2, 0x4);               //write command to start calibration
  1015.     delay_ms(300);
  1016.     for(i=0;i<100;i++)
  1017.     {
  1018.         fts_register_read(0,&uc_temp,1);
  1019.         if ( ((uc_temp&0x70)>>4) == 0x0)    //return to normal mode, calibration finish
  1020.         {
  1021.             break;
  1022.         }
  1023.         delay_ms(200);
  1024.         pr_info("[FTS] waiting calibration %d\n",i);
  1025.     }
  1026.  
  1027.     pr_info("[FTS] calibration OK.\n");
  1028.  
  1029.     msleep(300);
  1030.     fts_register_write(0, 0x40);              //goto factory mode
  1031.     delay_ms(100);                       //make sure already enter factory mode
  1032.    fts_register_write(2, 0x5);               //store CLB result
  1033.     delay_ms(300);
  1034.     fts_register_write(0, 0x0);               //return to normal mode
  1035.     msleep(300);
  1036.     pr_info("[FTS] store CLB result OK.\n");
  1037.     return 0;
  1038. }
  1039. void getVerNo(u8* buf, int len)
  1040. {
  1041.     u8 start_reg=0x0;
  1042.     int ret = -1;
  1043.     //int status = 0;
  1044.     int i = 0;
  1045.     start_reg = 0xa6;
  1046.  
  1047. #if 0
  1048.     pr_info("read 0xa6 one time. \n");
  1049.     if(FTS_FALSE == fts_register_read(0xa6, buf, len)){
  1050.         return ;
  1051.     }
  1052.  
  1053.     for (i=0; i< len; i++)
  1054.     {
  1055.         pr_info("=========buf[%d] = 0x%x \n", i, buf[i]);
  1056.     }
  1057.  
  1058.     pr_info("read 0xa8. \n");
  1059.     if(FTS_FALSE == fts_register_read(0xa8, buf, len)){
  1060.         return ;
  1061.     }
  1062.     for (i=0; i< len; i++)
  1063.     {
  1064.         pr_info("=========buf[%d] = 0x%x \n", i, buf[i]);
  1065.     }
  1066.  
  1067.     ft5x_i2c_rxdata(buf, len);
  1068.  
  1069.     for (i=0; i< len; i++)
  1070.         {
  1071.             pr_info("=========buf[%d] = 0x%x \n", i, buf[i]);
  1072.         }
  1073.  
  1074.     byte_read(buf, len);
  1075.     for (i=0; i< len; i++)
  1076.     {
  1077.         pr_info("=========buf[%d] = 0x%x \n", i, buf[i]);
  1078.     }
  1079.  
  1080. #endif
  1081.  
  1082.     ret =fts_register_read(0xa6, buf, len);
  1083.     //et = ft5406_read_regs(ft5x0x_ts_data_test->client,start_reg, buf, 2);
  1084.     if (ret < 0)
  1085.     {
  1086.         pr_info("%s read_data i2c_rxdata failed: %d\n", __func__, ret);
  1087.         return;
  1088.     }
  1089.     for (i=0; i<2; i++)
  1090.     {
  1091.         pr_info("=========buf[%d] = 0x%x \n", i, buf[i]);
  1092.     }
  1093.  
  1094.  
  1095.     return;
  1096. }
  1097.  
  1098. int fts_ctpm_fw_upgrade_with_i_file(void)
  1099. {
  1100.     FTS_BYTE*     pbt_buf = FTS_NULL;
  1101.     int i_ret = 0;
  1102.     unsigned char a;
  1103.     unsigned char b;
  1104. #define BUFFER_LEN (2)            //len == 2
  1105.     unsigned char buf[BUFFER_LEN] = {0};
  1106.  
  1107.     //=========FW upgrade========================*/
  1108.     pr_info("%s. \n", __func__);
  1109.  
  1110.     pbt_buf = CTPM_FW;
  1111.     //msleep(200);
  1112.    // cmd_write(0x07,0x00,0x00,0x00,1);
  1113.     msleep(100);
  1114.     getVerNo(buf, BUFFER_LEN);
  1115.     a = buf[0];
  1116.     b = fts_ctpm_get_i_file_ver();
  1117.     pr_info("a == %hu,  b== %hu \n",a, b);
  1118.  
  1119.     /*
  1120.       * when the firmware in touch panel maybe corrupted,
  1121.       * or the firmware in host flash is new, need upgrade
  1122.       */
  1123.     if ( 0xa6 == a ||a < b ){
  1124.         /*call the upgrade function*/
  1125.         i_ret =  fts_ctpm_fw_upgrade(&pbt_buf[0],sizeof(CTPM_FW));
  1126.         if (i_ret != 0){
  1127.             pr_info("[FTS] upgrade failed i_ret = %d.\n", i_ret);
  1128.         } else {
  1129.             pr_info("[FTS] upgrade successfully.\n");
  1130.             fts_ctpm_auto_clb();  //start auto CLB
  1131.         }
  1132.  
  1133.     }
  1134.  
  1135.     return i_ret;
  1136.  
  1137. }
  1138.  
  1139. unsigned char fts_ctpm_get_upg_ver(void)
  1140. {
  1141.     unsigned int ui_sz;
  1142.     ui_sz = sizeof(CTPM_FW);
  1143.     if (ui_sz > 2){
  1144.         return CTPM_FW[0];
  1145.     }
  1146.     else{
  1147.         //TBD, error handling?
  1148.         return 0xff; //default value
  1149.     }
  1150. }
  1151.  
  1152. static int ft5x_i2c_rxdata(char *rxdata, int length)
  1153. {
  1154.     int ret;
  1155.  
  1156.     struct i2c_msg msgs[] = {
  1157.         {
  1158.             .addr   = this_client->addr,
  1159.             .flags  = 0,
  1160.             .len    = 1,
  1161.             .buf    = rxdata,
  1162.         },
  1163.         {
  1164.             .addr   = this_client->addr,
  1165.             .flags  = I2C_M_RD,
  1166.             .len    = length,
  1167.             .buf    = rxdata,
  1168.         },
  1169.     };
  1170.  
  1171.         //pr_info("IIC add = %x\n",this_client->addr);
  1172.     ret = i2c_transfer(this_client->adapter, msgs, 2);
  1173.     if (ret < 0)
  1174.         pr_info("msg %s i2c read error: %d\n", __func__, ret);
  1175.  
  1176.     return ret;
  1177. }
  1178.  
  1179. static int ft5x_i2c_txdata(char *txdata, int length)
  1180. {
  1181.     int ret;
  1182.  
  1183.     struct i2c_msg msg[] = {
  1184.         {
  1185.             .addr   = this_client->addr,
  1186.             .flags  = 0,
  1187.             .len    = length,
  1188.             .buf    = txdata,
  1189.         },
  1190.     };
  1191.  
  1192.     //msleep(1);
  1193.     ret = i2c_transfer(this_client->adapter, msg, 1);
  1194.     if (ret < 0)
  1195.         pr_err("%s i2c write error: %d\n", __func__, ret);
  1196.  
  1197.     return ret;
  1198. }
  1199.  
  1200. static int ft5x_set_reg(u8 addr, u8 para)
  1201. {
  1202.     u8 buf[3];
  1203.     int ret = -1;
  1204.  
  1205.     buf[0] = addr;
  1206.     buf[1] = para;
  1207.     ret = ft5x_i2c_txdata(buf, 2);
  1208.     if (ret < 0) {
  1209.         pr_err("write reg failed! %#x ret: %d", buf[0], ret);
  1210.         return -1;
  1211.     }
  1212.  
  1213.     return 0;
  1214. }
  1215.  
  1216. static void ft5x_ts_release(void)
  1217. {
  1218.     struct ft5x_ts_data *data = i2c_get_clientdata(this_client);
  1219. #ifdef CONFIG_FT5X0X_MULTITOUCH
  1220. #ifdef TOUCH_KEY_SUPPORT
  1221.     if(1 == key_tp){
  1222.         input_report_key(data->input_dev, key_val, 0);
  1223.         print_point_info("Release Key = %d\n",key_val);
  1224.     } else{
  1225.         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
  1226.     }
  1227. #else
  1228.     input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
  1229. #endif
  1230.  
  1231. #else
  1232.     input_report_abs(data->input_dev, ABS_PRESSURE, 0);
  1233.     input_report_key(data->input_dev, BTN_TOUCH, 0);
  1234. #endif
  1235.  
  1236.     input_sync(data->input_dev);
  1237.     return;
  1238.  
  1239. }
  1240.  
  1241. static int ft5x_read_data(void)
  1242. {
  1243.     struct ft5x_ts_data *data = i2c_get_clientdata(this_client);
  1244.     struct ts_event *event = &data->event;
  1245.     unsigned char buf[32]={0};
  1246.     int ret = -1;
  1247.  
  1248. #ifdef CONFIG_FT5X0X_MULTITOUCH
  1249.     ret = ft5x_i2c_rxdata(buf, 31);
  1250. #else
  1251.     ret = ft5x_i2c_rxdata(buf, 31);
  1252. #endif
  1253.     if (ret < 0) {
  1254.         pr_info("%s read_data i2c_rxdata failed: %d\n", __func__, ret);
  1255.         return ret;
  1256.     }
  1257.  
  1258.     memset(event, 0, sizeof(struct ts_event));
  1259.  
  1260.     event->touch_point = buf[2] & 0x07;// 000 0111
  1261.     print_point_info("touch point = %d\n",event->touch_point);
  1262.  
  1263.     if (event->touch_point == 0) {
  1264.         ft5x_ts_release();
  1265.         return 1;
  1266.     }
  1267.  
  1268. #ifdef CONFIG_FT5X0X_MULTITOUCH
  1269.     switch (event->touch_point) {
  1270.     case 5:
  1271.         event->x5 = (s16)(buf[0x1b] & 0x0F)<<8 | (s16)buf[0x1c];
  1272.         event->y5 = (s16)(buf[0x1d] & 0x0F)<<8 | (s16)buf[0x1e];
  1273.         if(1 == revert_x_flag){
  1274.             event->x5 = SCREEN_MAX_X - event->x5;
  1275.         }
  1276.         if(1 == revert_y_flag){
  1277.             event->y5 = SCREEN_MAX_Y - event->y5;
  1278.         }
  1279.         //pr_info("before swap: event->x5 = %d, event->y5 = %d. \n", event->x5, event->y5);
  1280.         if(1 == exchange_x_y_flag){
  1281.             swap(event->x5, event->y5);
  1282.         }
  1283.         //pr_info("after swap: event->x5 = %d, event->y5 = %d. \n", event->x5, event->y5);
  1284.         event->touch_ID5=(s16)(buf[0x1D] & 0xF0)>>4;
  1285.     case 4:
  1286.         event->x4 = (s16)(buf[0x15] & 0x0F)<<8 | (s16)buf[0x16];
  1287.         event->y4 = (s16)(buf[0x17] & 0x0F)<<8 | (s16)buf[0x18];
  1288.         if(1 == revert_x_flag){
  1289.             event->x4 = SCREEN_MAX_X - event->x4;
  1290.         }
  1291.         if(1 == revert_y_flag){
  1292.             event->y4 = SCREEN_MAX_Y - event->y4;
  1293.         }
  1294.         //pr_info("before swap: event->x4 = %d, event->y4 = %d. \n", event->x4, event->y4);
  1295.         if(1 == exchange_x_y_flag){
  1296.             swap(event->x4, event->y4);
  1297.         }
  1298.         //pr_info("after swap: event->x4 = %d, event->y4 = %d. \n", event->x4, event->y4);
  1299.         event->touch_ID4=(s16)(buf[0x17] & 0xF0)>>4;
  1300.     case 3:
  1301.         event->x3 = (s16)(buf[0x0f] & 0x0F)<<8 | (s16)buf[0x10];
  1302.         event->y3 = (s16)(buf[0x11] & 0x0F)<<8 | (s16)buf[0x12];
  1303.         if(1 == revert_x_flag){
  1304.             event->x3 = SCREEN_MAX_X - event->x3;
  1305.         }
  1306.         if(1 == revert_y_flag){
  1307.             event->y3 = SCREEN_MAX_Y - event->y3;
  1308.         }
  1309.         //pr_info("before swap: event->x3 = %d, event->y3 = %d. \n", event->x3, event->y3);
  1310.         if(1 == exchange_x_y_flag){
  1311.             swap(event->x3, event->y3);
  1312.         }
  1313.         //pr_info("after swap: event->x3 = %d, event->y3 = %d. \n", event->x3, event->y3);
  1314.         event->touch_ID3=(s16)(buf[0x11] & 0xF0)>>4;
  1315.     case 2:
  1316.         event->x2 = (s16)(buf[9] & 0x0F)<<8 | (s16)buf[10];
  1317.         event->y2 = (s16)(buf[11] & 0x0F)<<8 | (s16)buf[12];
  1318.         if(1 == revert_x_flag){
  1319.             event->x2 = SCREEN_MAX_X - event->x2;
  1320.         }
  1321.         if(1 == revert_y_flag){
  1322.             event->y2 = SCREEN_MAX_Y - event->y2;
  1323.         }
  1324.         //pr_info("before swap: event->x2 = %d, event->y2 = %d. \n", event->x2, event->y2);
  1325.         if(1 == exchange_x_y_flag){
  1326.             swap(event->x2, event->y2);
  1327.         }
  1328.         //pr_info("after swap: event->x2 = %d, event->y2 = %d. \n", event->x2, event->y2);
  1329.         event->touch_ID2=(s16)(buf[0x0b] & 0xF0)>>4;
  1330.     case 1:
  1331.         event->x1 = (s16)(buf[3] & 0x0F)<<8 | (s16)buf[4];
  1332.         event->y1 = (s16)(buf[5] & 0x0F)<<8 | (s16)buf[6];
  1333. #ifdef TOUCH_KEY_FOR_ANGDA
  1334.         if(event->x1 < TOUCH_KEY_X_LIMIT)
  1335.         {
  1336.             if(1 == revert_x_flag){
  1337.                 event->x1 = SCREEN_MAX_X - event->x1;
  1338.             }
  1339.             if(1 == revert_y_flag){
  1340.                 event->y1 = SCREEN_MAX_Y - event->y1;
  1341.             }
  1342.             //pr_info("before swap: event->x1 = %d, event->y1 = %d. \n", event->x1, event->y1);
  1343.             if(1 == exchange_x_y_flag){
  1344.                 swap(event->x1, event->y1);
  1345.             }
  1346.         }
  1347. #elif defined(TOUCH_KEY_FOR_EVB13)
  1348.         if((event->x1 > TOUCH_KEY_LOWER_X_LIMIT)&&(event->x1<TOUCH_KEY_HIGHER_X_LIMIT))
  1349.         {
  1350.             if(1 == revert_x_flag){
  1351.                 event->x1 = SCREEN_MAX_X - event->x1;
  1352.             }
  1353.             if(1 == revert_y_flag){
  1354.                 event->y1 = SCREEN_MAX_Y - event->y1;
  1355.             }
  1356.             //pr_info("before swap: event->x1 = %d, event->y1 = %d. \n", event->x1, event->y1);
  1357.             if(1 == exchange_x_y_flag){
  1358.                 swap(event->x1, event->y1);
  1359.             }
  1360.         }
  1361. #elif defined(TOUCH_KEY_FOR_711)
  1362.     //if((event->x1 > TOUCH_KEY_LOWER_X_LIMIT)&&(event->x1<TOUCH_KEY_HIGHER_X_LIMIT)) // originale
  1363.         if(event->x1 == TOUCH_KEY_X_LIMIT) // modificato
  1364.         {
  1365.             if(1 == revert_x_flag){
  1366.                 event->x1 = SCREEN_MAX_X - event->x1;
  1367.             }
  1368.             if(1 == revert_y_flag){
  1369.                 event->y1 = SCREEN_MAX_Y - event->y1;
  1370.             }
  1371.             //pr_info("before swap: event->x1 = %d, event->y1 = %d. \n", event->x1, event->y1);
  1372.             if(1 == exchange_x_y_flag){
  1373.                 swap(event->x1, event->y1);
  1374.             }
  1375.         }
  1376. #else
  1377.         if(1 == revert_x_flag){
  1378.             event->x1 = SCREEN_MAX_X - event->x1;
  1379.         }
  1380.         if(1 == revert_y_flag){
  1381.             event->y1 = SCREEN_MAX_Y - event->y1;
  1382.         }
  1383.         //pr_info("before swap: event->x1 = %d, event->y1 = %d. \n", event->x1, event->y1);
  1384.         if(1 == exchange_x_y_flag){
  1385.             swap(event->x1, event->y1);
  1386.         }
  1387. #endif
  1388.  
  1389.         //pr_info("after swap: event->x1 = %d, event->y1 = %d. \n", event->x1, event->y1);
  1390.         event->touch_ID1=(s16)(buf[0x05] & 0xF0)>>4;
  1391.         break;
  1392.     default:
  1393.         return -1;
  1394.     }
  1395. #else
  1396.     if (event->touch_point == 1) {
  1397.         event->x1 = (s16)(buf[3] & 0x0F)<<8 | (s16)buf[4];
  1398.         event->y1 = (s16)(buf[5] & 0x0F)<<8 | (s16)buf[6];
  1399.     }
  1400. #endif
  1401.     event->pressure = 200;
  1402.  
  1403.     dev_dbg(&this_client->dev, "%s: 1:%d %d 2:%d %d \n", __func__,
  1404.     event->x1, event->y1, event->x2, event->y2);
  1405.  
  1406.  
  1407.     return 0;
  1408. }
  1409.  
  1410. #ifdef TOUCH_KEY_LIGHT_SUPPORT
  1411. static void ft5x_lighting(void)
  1412. {
  1413.     if(EGPIO_SUCCESS != gpio_write_one_pin_value(gpio_light_hdle, 1, "ctp_light")){
  1414.         pr_info("ft5x_ts_light: err when operate gpio. \n");
  1415.     }
  1416. //  msleep(15);
  1417.     msleep(30);
  1418.     if(EGPIO_SUCCESS != gpio_write_one_pin_value(gpio_light_hdle, 0, "ctp_light")){
  1419.         pr_info("ft5x_ts_light: err when operate gpio. \n");
  1420.     }
  1421.  
  1422.     return;
  1423. }
  1424. #endif
  1425.  
  1426. static void ft5x_report_multitouch(void)
  1427. {
  1428.     struct ft5x_ts_data *data = i2c_get_clientdata(this_client);
  1429.     struct ts_event *event = &data->event;
  1430.  
  1431. #ifdef TOUCH_KEY_SUPPORT
  1432.     if(1 == key_tp){
  1433.         return;
  1434.     }
  1435. #endif
  1436.  
  1437.     switch(event->touch_point) {
  1438.     case 5:
  1439.         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, event->touch_ID5);
  1440.         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, event->pressure);
  1441.         input_report_abs(data->input_dev, ABS_MT_POSITION_X, event->x5);
  1442.         input_report_abs(data->input_dev, ABS_MT_POSITION_Y, event->y5);
  1443.         input_report_abs(data->input_dev, ABS_MT_WIDTH_MAJOR, 1);
  1444.         input_mt_sync(data->input_dev);
  1445.         print_point_info("===x5 = %d,y5 = %d ====\n",event->x2,event->y2);
  1446.     case 4:
  1447.         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, event->touch_ID4);
  1448.         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, event->pressure);
  1449.         input_report_abs(data->input_dev, ABS_MT_POSITION_X, event->x4);
  1450.         input_report_abs(data->input_dev, ABS_MT_POSITION_Y, event->y4);
  1451.         input_report_abs(data->input_dev, ABS_MT_WIDTH_MAJOR, 1);
  1452.         input_mt_sync(data->input_dev);
  1453.         print_point_info("===x4 = %d,y4 = %d ====\n",event->x2,event->y2);
  1454.     case 3:
  1455.         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, event->touch_ID3);
  1456.         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, event->pressure);
  1457.         input_report_abs(data->input_dev, ABS_MT_POSITION_X, event->x3);
  1458.         input_report_abs(data->input_dev, ABS_MT_POSITION_Y, event->y3);
  1459.         input_report_abs(data->input_dev, ABS_MT_WIDTH_MAJOR, 1);
  1460.         input_mt_sync(data->input_dev);
  1461.         print_point_info("===x3 = %d,y3 = %d ====\n",event->x2,event->y2);
  1462.     case 2:
  1463.         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, event->touch_ID2);
  1464.         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, event->pressure);
  1465.         input_report_abs(data->input_dev, ABS_MT_POSITION_X, event->x2);
  1466.         input_report_abs(data->input_dev, ABS_MT_POSITION_Y, event->y2);
  1467.         input_report_abs(data->input_dev, ABS_MT_WIDTH_MAJOR, 1);
  1468.         input_mt_sync(data->input_dev);
  1469.         print_point_info("===x2 = %d,y2 = %d ====\n",event->x2,event->y2);
  1470.     case 1:
  1471.         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, event->touch_ID1);
  1472.         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, event->pressure);
  1473.         input_report_abs(data->input_dev, ABS_MT_POSITION_X, event->x1);
  1474.         input_report_abs(data->input_dev, ABS_MT_POSITION_Y, event->y1);
  1475.         input_report_abs(data->input_dev, ABS_MT_WIDTH_MAJOR, 1);
  1476.         input_mt_sync(data->input_dev);
  1477.         print_point_info("===x1 = %d,y1 = %d ====\n",event->x1,event->y1);
  1478.         break;
  1479.     default:
  1480.         print_point_info("==touch_point default =\n");
  1481.         break;
  1482.     }
  1483.  
  1484.     input_sync(data->input_dev);
  1485.     dev_dbg(&this_client->dev, "%s: 1:%d %d 2:%d %d \n", __func__,
  1486.         event->x1, event->y1, event->x2, event->y2);
  1487.     return;
  1488. }
  1489.  
  1490. #ifndef CONFIG_FT5X0X_MULTITOUCH
  1491. static void ft5x_report_singletouch(void)
  1492. {
  1493.     struct ft5x_ts_data *data = i2c_get_clientdata(this_client);
  1494.     struct ts_event *event = &data->event;
  1495.  
  1496.     if (event->touch_point == 1) {
  1497.         input_report_abs(data->input_dev, ABS_X, event->x1);
  1498.         input_report_abs(data->input_dev, ABS_Y, event->y1);
  1499.         input_report_abs(data->input_dev, ABS_PRESSURE, event->pressure);
  1500.     }
  1501.     input_report_key(data->input_dev, BTN_TOUCH, 1);
  1502.     input_sync(data->input_dev);
  1503.     dev_dbg(&this_client->dev, "%s: 1:%d %d 2:%d %d \n", __func__,
  1504.         event->x1, event->y1, event->x2, event->y2);
  1505.  
  1506.     return;
  1507. }
  1508. #endif
  1509.  
  1510. #ifdef TOUCH_KEY_SUPPORT
  1511. static void ft5x_report_touchkey(void)
  1512. {
  1513.     struct ft5x_ts_data *data = i2c_get_clientdata(this_client);
  1514.     struct ts_event *event = &data->event;
  1515.     //print_point_info("x=%d===Y=%d\n",event->x1,event->y1);
  1516.  
  1517. #ifdef TOUCH_KEY_FOR_ANGDA
  1518.     if((1==event->touch_point)&&(event->x1 > TOUCH_KEY_X_LIMIT)){
  1519.         key_tp = 1;
  1520.         if(event->y1 < 40){
  1521.             key_val = 1;
  1522.             input_report_key(data->input_dev, key_val, 1);
  1523.             input_sync(data->input_dev);
  1524.             print_point_info("===KEY 1====\n");
  1525.         }else if(event->y1 < 90){
  1526.             key_val = 2;
  1527.             input_report_key(data->input_dev, key_val, 1);
  1528.             input_sync(data->input_dev);
  1529.             print_point_info("===KEY 2 ====\n");
  1530.         }else{
  1531.             key_val = 3;
  1532.             input_report_key(data->input_dev, key_val, 1);
  1533.             input_sync(data->input_dev);
  1534.             print_point_info("===KEY 3====\n");
  1535.         }
  1536.     } else{
  1537.         key_tp = 0;
  1538.     }
  1539. #endif
  1540. #ifdef TOUCH_KEY_FOR_EVB13
  1541.     if((1==event->touch_point)&&((event->x1 > TOUCH_KEY_LOWER_X_LIMIT)&&(event->x1<TOUCH_KEY_HIGHER_X_LIMIT))){
  1542.         key_tp = 1;
  1543.         if(event->y1 < 5){
  1544.             key_val = 1;
  1545.             input_report_key(data->input_dev, key_val, 1);
  1546.             input_sync(data->input_dev);
  1547.             print_point_info("===KEY 1====\n");
  1548.         }else if((event->y1 < 45)&&(event->y1>35)){
  1549.             key_val = 2;
  1550.             input_report_key(data->input_dev, key_val, 1);
  1551.             input_sync(data->input_dev);
  1552.             print_point_info("===KEY 2 ====\n");
  1553.         }else if((event->y1 < 75)&&(event->y1>65)){
  1554.             key_val = 3;
  1555.             input_report_key(data->input_dev, key_val, 1);
  1556.             input_sync(data->input_dev);
  1557.             print_point_info("===KEY 3====\n");
  1558.         }else if ((event->y1 < 105)&&(event->y1>95))    {
  1559.             key_val = 4;
  1560.             input_report_key(data->input_dev, key_val, 1);
  1561.             input_sync(data->input_dev);
  1562.             print_point_info("===KEY 4====\n");
  1563.         }
  1564.     }else{
  1565.         key_tp = 0;
  1566.     }
  1567. #endif
  1568. #ifdef TOUCH_KEY_FOR_711
  1569.     if((1==event->touch_point)&&(event->x1 == TOUCH_KEY_X_LIMIT)){
  1570.         key_tp = 1;
  1571.         if(event->y1 == KEY1_Y) {
  1572.             key_val = 1;
  1573.             input_report_key(data->input_dev, key_val, 1);
  1574.             input_sync(data->input_dev);
  1575.             print_point_info("===KEY 1====\n");
  1576.         }else if(event->y1 == KEY2_Y){
  1577.             key_val = 2;
  1578.             input_report_key(data->input_dev, key_val, 1);
  1579.             input_sync(data->input_dev);
  1580.             print_point_info("===KEY 2 ====\n");
  1581.         }else if(event->y1 == KEY3_Y) {
  1582.             key_val = 3;
  1583.             input_report_key(data->input_dev, key_val, 1);
  1584.             input_sync(data->input_dev);
  1585.             print_point_info("===KEY 3====\n");
  1586.         }
  1587. #ifdef TOUCH_KEY_LIGHT_SUPPORT
  1588.     ft5x_lighting();
  1589. #endif
  1590. }else{
  1591.         key_tp = 0;
  1592.     }
  1593. #endif
  1594.     return;
  1595. }
  1596.  
  1597. #endif
  1598.  
  1599. static void ft5x_report_value(void)
  1600. {
  1601.  
  1602.     //pr_info("==ft5x_report_value =\n");
  1603. #ifdef TOUCH_KEY_SUPPORT
  1604.     ft5x_report_touchkey();
  1605. #endif
  1606.  
  1607. #ifdef CONFIG_FT5X0X_MULTITOUCH
  1608.     ft5x_report_multitouch();
  1609. #else   /* CONFIG_FT5X0X_MULTITOUCH*/
  1610.     ft5x_report_singletouch();
  1611. #endif  /* CONFIG_FT5X0X_MULTITOUCH*/
  1612.     return;
  1613. }   /*end ft5x_report_value*/
  1614.  
  1615. static void ft5x_ts_pen_irq_work(struct work_struct *work)
  1616. {
  1617.     int ret = -1;
  1618.     //pr_info("==work 1=\n");
  1619.     ret = ft5x_read_data();
  1620.     if (ret == 0) {
  1621.         ft5x_report_value();
  1622.     }
  1623.     //enable_irq(SW_INT_IRQNO_PIO);
  1624.  
  1625. }
  1626.  
  1627. static irqreturn_t ft5x_ts_interrupt(int irq, void *dev_id)
  1628. {
  1629.     struct ft5x_ts_data *ft5x_ts = dev_id;
  1630.  
  1631.     print_int_info("==========------ft5x_ts TS Interrupt-----============\n");
  1632.     if(!ctp_ops.judge_int_occur()){
  1633.         print_int_info("==IRQ_EINT21=\n");
  1634.         ctp_ops.clear_penirq();
  1635.         if (!work_pending(&ft5x_ts->pen_event_work))
  1636.         {
  1637.             print_int_info("Enter work\n");
  1638.             queue_work(ft5x_ts->ts_workqueue, &ft5x_ts->pen_event_work);
  1639.         }
  1640.     }else{
  1641.         print_int_info("Other Interrupt\n");
  1642.         return IRQ_NONE;
  1643.     }
  1644.  
  1645.     return IRQ_HANDLED;
  1646. }
  1647.  
  1648. #ifdef CONFIG_HAS_EARLYSUSPEND
  1649.  
  1650. static void ft5x_ts_suspend(struct early_suspend *handler)
  1651. {
  1652. //  struct ft5x_ts_data *ts;
  1653. //  ts =  container_of(handler, struct ft5x_ts_data, early_suspend);
  1654.  
  1655.     pr_info("==ft5x_ts_suspend=\n");
  1656. //  disable_irq(this_client->irq);
  1657. //  disable_irq(IRQ_EINT(6));
  1658. //  cancel_work_sync(&ts->pen_event_work);
  1659. //  flush_workqueue(ts->ts_workqueue);
  1660.     // ==set mode ==,
  1661. //      ft5x_set_reg(FT5X0X_REG_PMODE, PMODE_HIBERNATE);
  1662.  
  1663.    /*    //gpio i28 output low
  1664.     pr_info("==ft5x_ts_suspend=\n");
  1665.     //enter HIBERNATE mode
  1666.     ft5x_set_reg(0x3a,PMODE_HIBERNATE);
  1667.     */
  1668.     //suspend
  1669.     //gpio_write_one_pin_value(gpio_wakeup_hdle, 0, "ctp_wakeup");
  1670.     // ==set mode ==,
  1671.     pr_info("ft5x_ts_suspend: write FT5X0X_REG_PMODE .\n");
  1672.     ft5x_set_reg(FT5X0X_REG_PMODE, PMODE_HIBERNATE);
  1673. }
  1674.  
  1675. static void ft5x_ts_resume(struct early_suspend *handler)
  1676. {
  1677.     pr_info("==ft5x_ts_resume== \n");
  1678.     //reset
  1679.     ctp_ops.ts_reset();
  1680.     //wakeup
  1681.     ctp_ops.ts_wakeup();
  1682. }
  1683. #endif  //CONFIG_HAS_EARLYSUSPEND
  1684.  
  1685. static int
  1686. ft5x_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1687. {
  1688.     struct ft5x_ts_data *ft5x_ts;
  1689.     struct input_dev *input_dev;
  1690.     struct device *dev;
  1691.     struct i2c_dev *i2c_dev;
  1692.     int err = 0;
  1693.  
  1694. #ifdef TOUCH_KEY_SUPPORT
  1695.     int i = 0;
  1696. #endif
  1697.  
  1698.     pr_info("====%s begin=====.  \n", __func__);
  1699.     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1700.         err = -ENODEV;
  1701.         goto exit_check_functionality_failed;
  1702.     }
  1703.  
  1704.  
  1705.     ft5x_ts = kzalloc(sizeof(*ft5x_ts), GFP_KERNEL);
  1706.     if (!ft5x_ts)   {
  1707.         err = -ENOMEM;
  1708.         goto exit_alloc_data_failed;
  1709.     }
  1710.  
  1711.     //pr_info("touch panel gpio addr: = 0x%x", gpio_addr);
  1712.     this_client = client;
  1713.  
  1714.     //pr_info("ft5x_ts_probe : client->addr = %d. \n", client->addr);
  1715.     this_client->addr = client->addr;
  1716.     //pr_info("ft5x_ts_probe : client->addr = %d. \n", client->addr);
  1717.     i2c_set_clientdata(client, ft5x_ts);
  1718.  
  1719. #ifdef TOUCH_KEY_LIGHT_SUPPORT
  1720.     gpio_light_hdle = gpio_request_ex("ctp_para", "ctp_light");
  1721. #endif
  1722.  
  1723. #ifdef CONFIG_SUPPORT_FTS_CTP_UPG
  1724.     fts_ctpm_fw_upgrade_with_i_file();
  1725. #endif
  1726.  
  1727.  
  1728. //  pr_info("==INIT_WORK=\n");
  1729.     INIT_WORK(&ft5x_ts->pen_event_work, ft5x_ts_pen_irq_work);
  1730.     ft5x_ts->ts_workqueue = create_singlethread_workqueue(dev_name(&client->dev));
  1731.     if (!ft5x_ts->ts_workqueue) {
  1732.         err = -ESRCH;
  1733.         goto exit_create_singlethread;
  1734.     }
  1735.  
  1736.     input_dev = input_allocate_device();
  1737.     if (!input_dev) {
  1738.         err = -ENOMEM;
  1739.         dev_err(&client->dev, "failed to allocate input device\n");
  1740.         goto exit_input_dev_alloc_failed;
  1741.     }
  1742.  
  1743.     ft5x_ts->input_dev = input_dev;
  1744.  
  1745. #ifdef CONFIG_FT5X0X_MULTITOUCH
  1746.     set_bit(ABS_MT_TOUCH_MAJOR, input_dev->absbit);
  1747.     set_bit(ABS_MT_POSITION_X, input_dev->absbit);
  1748.     set_bit(ABS_MT_POSITION_Y, input_dev->absbit);
  1749.     set_bit(ABS_MT_WIDTH_MAJOR, input_dev->absbit);
  1750. #ifdef FOR_TSLIB_TEST
  1751.     set_bit(BTN_TOUCH, input_dev->keybit);
  1752. #endif
  1753.     input_set_abs_params(input_dev,
  1754.                  ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
  1755.     input_set_abs_params(input_dev,
  1756.                  ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
  1757.     input_set_abs_params(input_dev,
  1758.                  ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
  1759.     input_set_abs_params(input_dev,
  1760.                  ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
  1761.     input_set_abs_params(input_dev,
  1762.                  ABS_MT_TRACKING_ID, 0, 4, 0, 0);
  1763. #ifdef TOUCH_KEY_SUPPORT
  1764.     key_tp = 0;
  1765.     input_dev->evbit[0] = BIT_MASK(EV_KEY);
  1766.     for (i = 1; i < TOUCH_KEY_NUMBER; i++)
  1767.         set_bit(i, input_dev->keybit);
  1768. #endif
  1769. #else
  1770.     set_bit(ABS_X, input_dev->absbit);
  1771.     set_bit(ABS_Y, input_dev->absbit);
  1772.     set_bit(ABS_PRESSURE, input_dev->absbit);
  1773.     set_bit(BTN_TOUCH, input_dev->keybit);
  1774.     input_set_abs_params(input_dev, ABS_X, 0, SCREEN_MAX_X, 0, 0);
  1775.     input_set_abs_params(input_dev, ABS_Y, 0, SCREEN_MAX_Y, 0, 0);
  1776.     input_set_abs_params(input_dev,
  1777.                  ABS_PRESSURE, 0, PRESS_MAX, 0 , 0);
  1778. #endif
  1779.  
  1780.     set_bit(EV_ABS, input_dev->evbit);
  1781.     set_bit(EV_KEY, input_dev->evbit);
  1782.  
  1783.     input_dev->name     = CTP_NAME;     //dev_name(&client->dev)
  1784.     err = input_register_device(input_dev);
  1785.     if (err) {
  1786.         dev_err(&client->dev,
  1787.         "ft5x_ts_probe: failed to register input device: %s\n",
  1788.         dev_name(&client->dev));
  1789.         goto exit_input_register_device_failed;
  1790.     }
  1791.  
  1792. #ifdef CONFIG_HAS_EARLYSUSPEND
  1793.     pr_info("==register_early_suspend =\n");
  1794.     ft5x_ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  1795.     ft5x_ts->early_suspend.suspend = ft5x_ts_suspend;
  1796.     ft5x_ts->early_suspend.resume   = ft5x_ts_resume;
  1797.     register_early_suspend(&ft5x_ts->early_suspend);
  1798. #endif
  1799.  
  1800. #ifdef CONFIG_FT5X0X_MULTITOUCH
  1801.     pr_info("CONFIG_FT5X0X_MULTITOUCH is defined. \n");
  1802. #endif
  1803.  
  1804.     err = ctp_ops.set_irq_mode("ctp_para", "ctp_int_port", CTP_IRQ_NO, CTP_IRQ_MODE);
  1805.     if(0 != err){
  1806.         pr_info("%s:ctp_ops.set_irq_mode err. \n", __func__);
  1807.         goto exit_set_irq_mode;
  1808.     }
  1809.     err = request_irq(SW_INT_IRQNO_PIO, ft5x_ts_interrupt, IRQF_TRIGGER_FALLING | IRQF_SHARED, "ft5x_ts", ft5x_ts);
  1810.  
  1811.     if (err < 0) {
  1812.         dev_err(&client->dev, "ft5x_ts_probe: request irq failed\n");
  1813.         goto exit_irq_request_failed;
  1814.     }
  1815.  
  1816.         i2c_dev = get_free_i2c_dev(client->adapter);
  1817.     if (IS_ERR(i2c_dev)){
  1818.         err = PTR_ERR(i2c_dev);
  1819.         return err;
  1820.     }
  1821.     dev = device_create(i2c_dev_class, &client->adapter->dev, MKDEV(I2C_MAJOR,client->adapter->nr), NULL, "aw_i2c_ts%d", client->adapter->nr);
  1822.     if (IS_ERR(dev))    {
  1823.             err = PTR_ERR(dev);
  1824.             return err;
  1825.     }
  1826.  
  1827.     pr_info("==%s over =\n", __func__);
  1828.  
  1829.     return 0;
  1830.  
  1831. exit_irq_request_failed:
  1832. exit_set_irq_mode:
  1833.     cancel_work_sync(&ft5x_ts->pen_event_work);
  1834.     destroy_workqueue(ft5x_ts->ts_workqueue);
  1835.     enable_irq(SW_INT_IRQNO_PIO);
  1836. exit_input_register_device_failed:
  1837.     input_free_device(input_dev);
  1838. exit_input_dev_alloc_failed:
  1839.     free_irq(SW_INT_IRQNO_PIO, ft5x_ts);
  1840. exit_create_singlethread:
  1841.     pr_info("==singlethread error =\n");
  1842.     i2c_set_clientdata(client, NULL);
  1843.     kfree(ft5x_ts);
  1844. exit_alloc_data_failed:
  1845. exit_check_functionality_failed:
  1846.     return err;
  1847. }
  1848.  
  1849. static int __devexit ft5x_ts_remove(struct i2c_client *client)
  1850. {
  1851.  
  1852.     struct ft5x_ts_data *ft5x_ts = i2c_get_clientdata(client);
  1853.     ft5x_set_reg(FT5X0X_REG_PMODE, PMODE_HIBERNATE);
  1854.  
  1855.     pr_info("==ft5x_ts_remove=\n");
  1856.     free_irq(SW_INT_IRQNO_PIO, ft5x_ts);
  1857. #ifdef CONFIG_HAS_EARLYSUSPEND
  1858.     unregister_early_suspend(&ft5x_ts->early_suspend);
  1859. #endif
  1860.     input_unregister_device(ft5x_ts->input_dev);
  1861.     input_free_device(ft5x_ts->input_dev);
  1862.     cancel_work_sync(&ft5x_ts->pen_event_work);
  1863.     destroy_workqueue(ft5x_ts->ts_workqueue);
  1864.     kfree(ft5x_ts);
  1865.  
  1866.     i2c_set_clientdata(client, NULL);
  1867.     ctp_ops.free_platform_resource();
  1868.  
  1869.     return 0;
  1870.  
  1871. }
  1872.  
  1873. static const struct i2c_device_id ft5x_ts_id[] = {
  1874.     { CTP_NAME, 0 },
  1875.     {}
  1876. };
  1877. MODULE_DEVICE_TABLE(i2c, ft5x_ts_id);
  1878.  
  1879. static struct i2c_driver ft5x_ts_driver = {
  1880.     .class = I2C_CLASS_HWMON,
  1881.     .probe      = ft5x_ts_probe,
  1882.     .remove     = __devexit_p(ft5x_ts_remove),
  1883.     .id_table   = ft5x_ts_id,
  1884.     .driver = {
  1885.         .name   = CTP_NAME,
  1886.         .owner  = THIS_MODULE,
  1887.     },
  1888.     .address_list   = u_i2c_addr.normal_i2c,
  1889. };
  1890.  
  1891. static int aw_open(struct inode *inode, struct file *file)
  1892. {
  1893.     int subminor;
  1894.     int ret = 0;
  1895.     struct i2c_client *client;
  1896.     struct i2c_adapter *adapter;
  1897.     struct i2c_dev *i2c_dev;
  1898.  
  1899.     pr_info("====%s======.\n", __func__);
  1900.  
  1901.     #ifdef AW_DEBUG
  1902.             pr_info("enter aw_open function\n");
  1903.     #endif
  1904.  
  1905.     subminor = iminor(inode);
  1906.     #ifdef AW_DEBUG
  1907.           pr_info("subminor=%d\n",subminor);
  1908.     #endif
  1909.  
  1910.     //lock_kernel();
  1911.     i2c_dev = i2c_dev_get_by_minor(2);
  1912.     if (!i2c_dev)   {
  1913.         pr_info("error i2c_dev\n");
  1914.         return -ENODEV;
  1915.     }
  1916.  
  1917.     adapter = i2c_get_adapter(i2c_dev->adap->nr);
  1918.     if (!adapter)   {
  1919.         return -ENODEV;
  1920.     }
  1921.  
  1922.     client = kzalloc(sizeof(*client), GFP_KERNEL);
  1923.  
  1924.     if (!client)    {
  1925.         i2c_put_adapter(adapter);
  1926.         ret = -ENOMEM;
  1927.     }
  1928.     snprintf(client->name, I2C_NAME_SIZE, "pctp_i2c_ts%d", adapter->nr);
  1929.     client->driver = &ft5x_ts_driver;
  1930.     client->adapter = adapter;
  1931.     file->private_data = client;
  1932.  
  1933.     return 0;
  1934. }
  1935.  
  1936. static long aw_ioctl(struct file *file, unsigned int cmd,unsigned long arg )
  1937. {
  1938.     //struct i2c_client *client = (struct i2c_client *) file->private_data;
  1939.  
  1940.     pr_info("====%s====.\n",__func__);
  1941.  
  1942.     #ifdef AW_DEBUG
  1943.            pr_info("line :%d,cmd = %d,arg = %d.\n",__LINE__,cmd,arg);
  1944.     #endif
  1945.  
  1946.     switch (cmd) {
  1947.         case UPGRADE:
  1948.         pr_info("==UPGRADE_WORK=\n");
  1949.         fts_ctpm_fw_upgrade_with_i_file();
  1950.         // calibrate();
  1951.  
  1952.         break;
  1953.  
  1954.         default:
  1955.         break;
  1956.  
  1957.     }
  1958.  
  1959.     return 0;
  1960. }
  1961.  
  1962. static int aw_release (struct inode *inode, struct file *file)
  1963. {
  1964.     struct i2c_client *client = file->private_data;
  1965.     #ifdef AW_DEBUG
  1966.         pr_info("enter aw_release function.\n");
  1967.     #endif
  1968.  
  1969.     i2c_put_adapter(client->adapter);
  1970.     kfree(client);
  1971.     file->private_data = NULL;
  1972.     return 0;
  1973. }
  1974.  
  1975. static const struct file_operations aw_i2c_ts_fops ={
  1976.     .owner = THIS_MODULE,
  1977.     //.read = aw_read,
  1978.     //.write = aw_write,
  1979.     .open = aw_open,
  1980.     .unlocked_ioctl = aw_ioctl,
  1981.     .release = aw_release,
  1982. };
  1983.  
  1984. static int __init ft5x_ts_init(void)
  1985. {
  1986.     int ret = -1;
  1987.     int err = -1;
  1988.  
  1989.     pr_info("===========================%s=====================\n", __func__);
  1990.  
  1991.     if (ctp_ops.fetch_sysconfig_para)
  1992.     {
  1993.         if(ctp_ops.fetch_sysconfig_para()){
  1994.             pr_info("%s: err.\n", __func__);
  1995.             return -1;
  1996.         }
  1997.     }
  1998.     pr_info("%s: after fetch_sysconfig_para:  normal_i2c: 0x%hx. normal_i2c[1]: 0x%hx \n", \
  1999.     __func__, u_i2c_addr.normal_i2c[0], u_i2c_addr.normal_i2c[1]);
  2000.  
  2001.     err = ctp_ops.init_platform_resource();
  2002.     if(0 != err){
  2003.         pr_info("%s:ctp_ops.init_platform_resource err. \n", __func__);
  2004.     }
  2005.  
  2006.     //reset
  2007.     ctp_ops.ts_reset();
  2008.     //wakeup
  2009.     ctp_ops.ts_wakeup();
  2010.  
  2011.     ft5x_ts_driver.detect = ctp_ops.ts_detect;
  2012.  
  2013.     ret= register_chrdev(I2C_MAJOR,"aw_i2c_ts",&aw_i2c_ts_fops );
  2014.     if(ret) {
  2015.         pr_info(KERN_ERR "%s:register chrdev failed\n",__FILE__);
  2016.         return ret;
  2017.     }
  2018.  
  2019.     i2c_dev_class = class_create(THIS_MODULE,"aw_i2c_dev");
  2020.     if (IS_ERR(i2c_dev_class)) {
  2021.         ret = PTR_ERR(i2c_dev_class);
  2022.         class_destroy(i2c_dev_class);
  2023.     }
  2024.  
  2025.     ret = i2c_add_driver(&ft5x_ts_driver);
  2026.  
  2027.     return ret;
  2028. }
  2029.  
  2030. static void __exit ft5x_ts_exit(void)
  2031. {
  2032.     pr_info("==ft5x_ts_exit==\n");
  2033.     i2c_del_driver(&ft5x_ts_driver);
  2034. }
  2035.  
  2036. late_initcall(ft5x_ts_init);
  2037. module_exit(ft5x_ts_exit);
  2038.  
  2039. MODULE_AUTHOR("<[email protected]>");
  2040. MODULE_DESCRIPTION("FocalTech ft5x TouchScreen driver");
  2041. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment