Advertisement
Guest User

ft5x0x_ts.c

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