MT6573驅動開發日誌之touchpanel .

(1)添加一款TOUCHPANEL
在projectConfig.mk文件裏面修改如下:
CUSTOM_KERNEL_TOUCHPANEL = cy8ctma300
在mediatek\custom\common\kernel\touchpanel文件夾添加文件夾如下:
mediatek\custom\common\kernel\touchpanel\cy8ctma300.c
(2)源碼分析
1)我們知道linux內核,添加驅動的初始化函數,大都是通過宏module_init;添加退出函數大都通過宏module_exit,具體這兩個宏的作用,我這裏就不細說了。例如:
module_init(tpd_driver_init);
module_exit(tpd_driver_exit);
2)下面分析這個初始化函數和退出函數
/* called when loaded into kernel */
static int __init tpd_driver_init(void)
{
        if(tpd_driver_add(&tpd_device_driver) < 0)
            TPD_DMESG("add generic driver failed\n");
    return 0;
}

/* should never be called */
static void __exit tpd_driver_exit(void)
{
    tpd_driver_remove(&tpd_device_driver);
}
可以看出分別是添加和移除tpd 驅動
3)再接着分析 tpd_driver_init和 tpd_driver_exit
int tpd_driver_add(struct tpd_driver_t *tpd_drv)
{
    int i;
    
    if(g_tpd_drv != NULL)
    {
        TPD_DMESG("touch driver exist \n");
        return -1;
    }
    /* check parameter */
    if(tpd_drv == NULL)
    {
        return -1;
    }
    /* R-touch */
    if(strcmp(tpd_drv->tpd_device_name, "generic") == 0)
    {
            tpd_driver_list[0].tpd_device_name = tpd_drv->tpd_device_name;
            tpd_driver_list[0].tpd_local_init = tpd_drv->tpd_local_init;
            tpd_driver_list[0].suspend = tpd_drv->suspend;
            tpd_driver_list[0].resume = tpd_drv->resume;
            tpd_driver_list[0].tpd_have_button = tpd_drv->tpd_have_button;
            return 0;
    }
    for(i = 1; i < TP_DRV_MAX_COUNT; i++)
    {
        /* add tpd driver into list */
        if(tpd_driver_list[i].tpd_device_name == NULL)
        {
            tpd_driver_list[i].tpd_device_name = tpd_drv->tpd_device_name;
            tpd_driver_list[i].tpd_local_init = tpd_drv->tpd_local_init;
            tpd_driver_list[i].suspend = tpd_drv->suspend;
            tpd_driver_list[i].resume = tpd_drv->resume;
            tpd_driver_list[i].tpd_have_button = tpd_drv->tpd_have_button;
            #if 0
            if(tpd_drv->tpd_local_init()==0)
            {
                TPD_DMESG("load %s sucessfully\n", tpd_driver_list[i].tpd_device_name);
                g_tpd_drv = &tpd_driver_list[i];
            }
            #endif
            break;
        }
        if(strcmp(tpd_driver_list[i].tpd_device_name, tpd_drv->tpd_device_name) == 0)
        {
            return 1; // driver exist
        }
    }
    
    return 0;
}

int tpd_driver_remove(struct tpd_driver_t *tpd_drv)
{
    int i = 0;
    /* check parameter */
    if(tpd_drv == NULL)
    {
        return -1;
    }
    for(i = 0; i < TP_DRV_MAX_COUNT; i++)
    {
        /* find it */
        if (strcmp(tpd_driver_list[i].tpd_device_name, tpd_drv->tpd_device_name) == 0)
        {
            memset(&tpd_driver_list[i], 0, sizeof(struct tpd_driver_t));
            break;
        }
    }
    return 0;
}
可以看出主要是把當前的驅動從數組tpd_driver_list 加進或者移出,這裏我們發現一個重點的結構體
struct tpd_driver_t
{
        char *tpd_device_name;
        int (*tpd_local_init)(void);
         void (*suspend)(struct early_suspend *h);
         void (*resume)(struct early_suspend *h);
         int tpd_have_button;
};
下面我們接着分析如何初始化這個結構體
static struct tpd_driver_t tpd_device_driver = {
        .tpd_device_name = "cy8ctma300",
        .tpd_local_init = tpd_local_init,
        .suspend = tpd_suspend,
        .resume = tpd_resume,
#ifdef TPD_HAVE_BUTTON
        .tpd_have_button = 1,
#else
        .tpd_have_button = 0,
#endif
4)接下來就看上面三個函數是如何實現的
int tpd_local_init(void)
{
    if(tpd_em_debuglog==1) {
        TPD_DMESG("[mtk-tpd] %s\n", __FUNCTION__);
    }
     if(i2c_add_driver(&tpd_i2c_driver)!=0) {
      TPD_DMESG("unable to add i2c driver.\n");
      return -1;
    }
    if(tpd_load_status == 0)
    {
        TPD_DMESG("add error touch panel driver.\n");
        i2c_del_driver(&tpd_i2c_driver);
        return -1;
    }
#ifdef TPD_HAVE_BUTTON     
    tpd_button_setting(TPD_KEY_COUNT, tpd_keys_local, tpd_keys_dim_local);// initialize tpd button data
#endif   
 
#if (defined(TPD_WARP_START) && defined(TPD_WARP_END))    
    TPD_DO_WARP = 1;
    memcpy(tpd_wb_start, tpd_wb_start_local, TPD_WARP_CNT*4);
    memcpy(tpd_wb_end, tpd_wb_start_local, TPD_WARP_CNT*4);
#endif

#if (defined(TPD_HAVE_CALIBRATION) && !defined(TPD_CUSTOM_CALIBRATION))
    memcpy(tpd_calmat, tpd_calmat_local, 8*4);
    memcpy(tpd_def_calmat, tpd_def_calmat_local, 8*4);    
#endif  
        TPD_DMESG("end %s, %d\n", __FUNCTION__, __LINE__);  
        tpd_type_cap = 1;
    return 0;
}
這裏面核心的就是 i2c_add_driver,添加一個I2C設備
/* Function to manage low power suspend */
void tpd_suspend(struct early_suspend *h)
{
    if(tpd_em_debuglog==1) {
        TPD_DMESG("[mtk-tpd] %s\n", __FUNCTION__);
    }
      tpd_halt = 1;    
    while(1){
        if(tpd_flag == 1) msleep(1000);
        else break;    
    }
    mt65xx_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);

    #ifdef TPD_HAVE_POWER_ON_OFF
    mt_set_gpio_mode(GPIO_CTP_EN_PIN, GPIO_CTP_EN_PIN_M_GPIO);
    mt_set_gpio_dir(GPIO_CTP_EN_PIN, GPIO_DIR_OUT);
    mt_set_gpio_out(GPIO_CTP_EN_PIN, GPIO_OUT_ZERO);
    //msleep(1);
    mdelay(1);
    mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
    mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
    mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);    
    #endif
}
通過GPIO,掛起,使TPD不起作用
/* Function to manage power-on resume */
void tpd_resume(struct early_suspend *h)
{
    if(tpd_em_debuglog==1) {
        TPD_DMESG("[mtk-tpd] %s\n", __FUNCTION__);
    }
    #ifdef TPD_HAVE_POWER_ON_OFF
   // msleep(100);
    mt_set_gpio_mode(GPIO_CTP_EN_PIN, GPIO_CTP_EN_PIN_M_GPIO);
    mt_set_gpio_dir(GPIO_CTP_EN_PIN, GPIO_DIR_OUT);
    mt_set_gpio_out(GPIO_CTP_EN_PIN, GPIO_OUT_ONE);
    msleep(1);
    mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
    msleep(1);
    mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);    
    msleep(100);
    #endif

    mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
    tpd_halt = 0;
}
通過GPIO,恢復,使TPD起作用
5)下面看看I2C設備是如何被初始化的
struct i2c_driver tpd_i2c_driver = {                       
    .probe = tpd_i2c_probe,                                   
    .remove = tpd_i2c_remove,                           
    .detect = tpd_i2c_detect,                           
    .driver.name = "mtk-tpd",
    .id_table = tpd_i2c_id,                             
    .address_data = &addr_data,                        
};
6)下面下面接着分析三個I2C函數
static int tpd_i2c_detect(struct i2c_client *client, int kind, struct i2c_board_info *info) {
    strcpy(info->type, "mtk-tpd");
    return 0;
}
static int tpd_i2c_remove(struct i2c_client *client) {return 0;}

static int tpd_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) {             
    int err = 0;
    char buffer[2];
    int status=0;
    i2c_client = client;    
    
    #ifdef TPD_NO_GPIO
    u16 temp;
    temp = *(volatile u16 *) TPD_RESET_PIN_ADDR;
    temp = temp | 0x40;
    *(volatile u16 *) TPD_RESET_PIN_ADDR = temp;
    #endif
    
    #ifndef TPD_NO_GPIO
    mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
    mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
    mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);  
    msleep(10);  
    mt_set_gpio_mode(GPIO_CTP_EN_PIN, GPIO_CTP_EN_PIN_M_GPIO);
    mt_set_gpio_dir(GPIO_CTP_EN_PIN, GPIO_DIR_OUT);
    mt_set_gpio_out(GPIO_CTP_EN_PIN, GPIO_OUT_ONE);
    
 //   mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
 //   mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
 //   mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
 //   msleep(10);
    mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
    msleep(1);
    mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
    
    mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_EINT);
    mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
    mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_ENABLE);
    mt_set_gpio_pull_select(GPIO_CTP_EINT_PIN, GPIO_PULL_UP);
    #endif

    msleep(50);
    status = i2c_smbus_read_i2c_block_data(i2c_client, 0x00, 1, &(buffer[0]));
    if(status<0)
    {
            TPD_DMESG("[mtk-tpd], cy8ctma300 tpd_i2c_probe failed!!\n");
            status = i2c_smbus_read_i2c_block_data(i2c_client, 0x00, 1, &(buffer[0]));
            if(status<0) {
                TPD_DMESG("[mtk-tpd], cy8ctma300 tpd_i2c_probe retry failed!!\n");
                return status;
            }
    }

        TPD_DMESG("[mtk-tpd], cy8ctma300 tpd_i2c_probe success!!\n");        
        tpd_load_status = 1;
        
    if ((buffer[0] & 0x70) != 0x00) {
        buffer[0] = 0x00; // switch to operation mode
        i2c_smbus_write_i2c_block_data(i2c_client, 0x00, 1, &(buffer[0]));
        msleep(50);
    }

    thread = kthread_run(touch_event_handler, 0, TPD_DEVICE);
    if (IS_ERR(thread)) {
        err = PTR_ERR(thread);
        TPD_DMESG(TPD_DEVICE " failed to create kernel thread: %d\n", err);
    }
    
    mt65xx_eint_set_sens(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_SENSITIVE);
    mt65xx_eint_set_hw_debounce(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_CN);
    mt65xx_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_EN, CUST_EINT_TOUCH_PANEL_POLARITY, tpd_eint_interrupt_handler, 1);
    mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
    
    return 0;
}
7)我們發現這個函數是主要函數
主要實現了配置TPD中斷的GPIO口,I2C讀寫數據,設置中斷相關函數,如下
    mt65xx_eint_set_sens(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_SENSITIVE);
    mt65xx_eint_set_hw_debounce(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_CN);
    mt65xx_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_EN, CUST_EINT_TOUCH_PANEL_POLARITY, tpd_eint_interrupt_handler, 1);
    mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
還有個關鍵函數
touch_event_handler,在這個線程函數裏,實現了對touch事件的分析判斷和處理。
8)下面看看函數touch_event_handler
static int touch_event_handler(void *unused) {
    struct sched_param param = { .sched_priority = RTPM_PRIO_TPD };
    int x1=0, y1=0, x2=0, y2=0, x3=0, y3=0, x4=0, y4=0, p1=0, p2=0, p3=0, p4=0, id1=0xf, id2=0xf, id3=0xf, id4 = 0xf, pre_id1 = 0xf, pre_id2 = 0xf, pre_id3 = 0xf, pre_id4 = 0xf, pre_tt_mode = 0, finger_num = 0, pre_num = 0;
    int raw_x1=0, raw_y1=0, raw_x2=0, raw_y2=0, raw_x3=0, raw_y3=0, raw_x4=0, raw_y4=0;
    static char toggle;
    static char buffer[32];//[16];
//    int pending = 0;
    u32 temp;
    
    sched_setscheduler(current, SCHED_RR, &param);
    g_temptimerdiff=get_jiffies_64();//jiffies;
    do {
        if(tpd_em_debuglog==1) {
            TPD_DMESG("[mtk-tpd] %s\n", __FUNCTION__);
        }        
        set_current_state(TASK_INTERRUPTIBLE);
    if(tpd_em_debuglog==1)
        TPD_DMESG("[mtk-tpd], %s, tpd_halt=%d\n", __FUNCTION__, tpd_halt);
        while (tpd_halt) {tpd_flag = 0; msleep(20);}
        wait_event_interruptible(waiter, tpd_flag != 0);
        //tpd_flag = 0;
        TPD_DEBUG_SET_TIME;
        set_current_state(TASK_RUNNING);
        temp =  *(volatile u32 *)CHR_CON0;    
        temp &= (1<<13);
        if(temp!=0)
        {
            if(tpd_em_debuglog==1)    
                TPD_DMESG("[mtk-tpd], write 0x01 to 0x1D register!!\n");
            buffer[0] = 0x01;
            i2c_smbus_write_i2c_block_data(i2c_client, 0x1D, 1, &(buffer[0]));            
        }
        else
        {
            if(tpd_em_debuglog==1)            
                TPD_DMESG("[mtk-tpd], write 0x00 to 0x1D register!!\n");
            buffer[0] = 0x00;
            i2c_smbus_write_i2c_block_data(i2c_client, 0x1D, 1, &(buffer[0]));             
        }                 
        if (tpd_show_version) {
            tpd_show_version = 0;
                        
            mt_set_gpio_mode(GPIO1, 0x00);
            mt_set_gpio_dir(GPIO1, GPIO_DIR_OUT);
            mt_set_gpio_pull_enable(GPIO1, GPIO_PULL_ENABLE);
            mt_set_gpio_pull_select(GPIO1, GPIO_PULL_UP);
            
            mt_set_gpio_out(GPIO1, GPIO_OUT_ZERO);
            msleep(100);            
                        
            buffer[0] = 0x01; // reset touch panel mode
            i2c_smbus_write_i2c_block_data(i2c_client, 0x00, 1, &(buffer[0]));
            msleep(200);
            
            buffer[0] = 0x10; // swith to system information mode
            i2c_smbus_write_i2c_block_data(i2c_client, 0x00, 1, &(buffer[0]));
            msleep(200);
            
            i2c_smbus_read_i2c_block_data(i2c_client, 0x00, 8, &(buffer[0x0]));
            i2c_smbus_read_i2c_block_data(i2c_client, 0x08, 8, &(buffer[0x8]));
            i2c_smbus_read_i2c_block_data(i2c_client, 0x10, 8, &(buffer[0x10]));
            i2c_smbus_read_i2c_block_data(i2c_client, 0x18, 8, &(buffer[0x18]));
            printk("[mtk-tpd] Cypress Touch Panel ID %x.%x\n", buffer[0x07], buffer[0x08]);            
            printk("[mtk-tpd] Cypress Touch Panel Firmware Version %x.%x\n", buffer[0x15], buffer[0x16]);
            buffer[0] = 0x04; // switch to operation mode
            i2c_smbus_write_i2c_block_data(i2c_client, 0x00, 1, &(buffer[0]));
            msleep(200);
                      
            mt_set_gpio_out(GPIO1, GPIO_OUT_ONE);            
            mt_set_gpio_mode(GPIO1, 0x01);
            mt_set_gpio_pull_enable(GPIO1, GPIO_PULL_ENABLE);
            mt_set_gpio_pull_select(GPIO1, GPIO_PULL_UP);           
            continue;
        }        
        
        i2c_smbus_read_i2c_block_data(i2c_client, 0x00, 8, &(buffer[0]));
        i2c_smbus_read_i2c_block_data(i2c_client, 0x08, 8, &(buffer[8]));
        i2c_smbus_read_i2c_block_data(i2c_client, 0x10, 8, &(buffer[16]));
        i2c_smbus_read_i2c_block_data(i2c_client, 0x18, 8, &(buffer[24]));        
    if(tpd_em_debuglog==1)
    {
        TPD_DMESG("[mtk-tpd]HST_MODE  : %x\n", buffer[0]);
        TPD_DMESG("[mtk-tpd]TT_MODE   : %x\n", buffer[1]);
        TPD_DMESG("[mtk-tpd]TT_STAT   : %x\n", buffer[2]);
       // TPD_DEBUG("[mtk-tpd]TOUCH_ID  : %x\n", buffer[8]);
        TPD_DMESG("[mtk-tpd]TOUCH_12ID  : %x\n", buffer[8]);
        TPD_DMESG("[mtk-tpd]TOUCH_34ID  : %x\n", buffer[21]);
    }    
        
        finger_num = buffer[2] & 0x0f;
        
        if (finger_num == 0 && pre_num ==0) {
            msleep(10);
            tpd_flag = 0;
            pre_tt_mode = buffer[1];
            continue;   
        }
        
        if (pre_tt_mode == buffer[1]) {
            msleep(5);
            tpd_flag = 0;
            pre_tt_mode = buffer[1];
            continue;  
        }
        
        if (buffer[1] & 0x20) {
            TPD_DEBUG("buffer not ready\n");
            tpd_flag = 0;
            pre_tt_mode = buffer[1];
            continue; // buffer is not ready for use
        }
        
        id1 = (buffer[8] & 0xf0) >> 4;
        id2 = (buffer[8] & 0x0f);
        id3 = (buffer[21] & 0xf0) >> 4;
        id4 = (buffer[21] & 0x0f);
                
//        if (id1 != 0xf) {
       if(finger_num>=1) {
            x1 = (((int)buffer[3]) << 8) + buffer[4];
            y1 = (((int)buffer[5]) << 8) + buffer[6];
            if(x1>2048 || y1>2048) {
                tpd_flag = 0;
                pre_tt_mode = buffer[1];
                continue;                  
                }              
            p1 = buffer[7];
            raw_x1 = x1; raw_y1 = y1;
            tpd_calibrate(&x1, &y1);
            tpd_down(raw_x1, raw_y1, x1, y1, p1);
            if(counter_pointer==0)
                g_temptimerdiff=get_jiffies_64();//jiffies;
            if(x_min==0&&y_min==0&&x_max==0&&y_max==0) {
                    x_min = x1;
                    y_min = y1;
                    x_max = x1;
                    y_max = y1;
                }
            if(x1<x_min)
                x_min = x1;
            if(x1>x_max)
                x_max = x1;
            if(y1<y_min)
                y_min = y1;
            if(y1>y_max)
                y_max = y1;
            counter_pointer++;
            if (time_after(jiffies, g_temptimerdiff + 100)){    //1s
                TPD_DMESG("[mtk-tpd], x_min=%d, y_min=%d, x_max=%d, y_max=%d, counter_pointer=%d!!\n", x_min, y_min, x_max, y_max, counter_pointer);
                x_min=0;
                y_min=0;
                x_max=0;
                y_max=0;
                counter_pointer=0;
            }
        }
        
//        if (id2 != 0xf || finger_num==2) {
        if(finger_num>=2) {
            x2 = (((int)buffer[9]) << 8) + buffer[10];
            y2 = (((int)buffer[11]) << 8) + buffer[12];
            p2 = buffer[13];
            raw_x2 = x2; raw_y2 = y2;
            tpd_calibrate(&x2, &y2);
            tpd_down(raw_x2, raw_y2, x2, y2, p2);
        }
//    if(id3 != 0xf || finger_num==3) {
        if(finger_num>=3) {
            x3 = (((int)buffer[16]) << 8) + buffer[17];
            y3= (((int)buffer[18]) << 8) + buffer[19];
            p3= buffer[20];
            raw_x3 = x3; raw_y3 = y3;
            tpd_calibrate(&x3, &y3);
            tpd_down(raw_x3, raw_y3, x3, y3, p3);
        }

//    if(id4 != 0xf || finger_num==4) {
        if(finger_num>=4) {
            x4 = (((int)buffer[22]) << 8) + buffer[23];
            y4= (((int)buffer[24]) << 8) + buffer[25];
            p4= buffer[26];
            raw_x4 = x4; raw_y4 = y4;
            tpd_calibrate(&x4, &y4);
            tpd_down(raw_x4, raw_y4, x4, y4, p4);
        }    
        
    if(pre_num>=1 && pre_id1==0xf) {
        if(finger_num>=1 && id1==0xf) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger1 is still down!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger1 is up!!\n");
            tpd_up(raw_x1, raw_y1, x1, y1, p1);
        }        
        
    } else if(pre_num>=1 && pre_id1 !=0xf) {
        if(id1==pre_id1) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger1 is still down!!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger1 is up!\n");
            tpd_up(raw_x1, raw_y1, x1, y1, p1);
        }
    }
    
    if(pre_num>=2 && pre_id2==0xf) {
        if((finger_num>=2 && id2==0xf) || (finger_num==1 && id1==0xf)) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger2 is still down!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger2 is up!!\n");
            tpd_up(raw_x2, raw_y2, x2, y2, p2);
        }        
        
    } else if(pre_num>=2 && pre_id2 !=0xf) {
        if(id2==pre_id2 || id1==pre_id2) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger2 is still down!!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger2 is up!\n");
            tpd_up(raw_x2, raw_y2, x2, y2, p2);
        }
    }

    
    if(pre_num>=3 && pre_id3==0xf) {
        if((finger_num>=3 && id3==0xf) || (finger_num==2 && id2==0xf) || (finger_num==1 && id1==0xf)) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger3 is still down!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger3 is up!!\n");
            tpd_up(raw_x3, raw_y3, x3, y3, p3);
        }        
        
    } else if(pre_num>=3 && pre_id3 !=0xf) {
        if(id3==pre_id3 || id2==pre_id3 || id1==pre_id3) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger3 is still down!!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger3 is up!\n");
            tpd_up(raw_x3, raw_y3, x3, y3, p3);
        }
    }

    if(pre_num==4 && pre_id4==0xf) {
        if((finger_num>=4 && id4==0xf) || (finger_num==3 && id3==0xf) || (finger_num==2 && id2==0xf) || (finger_num==1 && id1==0xf)) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger4 is still down!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger4 is up!!\n");
            tpd_up(raw_x4, raw_y4, x4, y4, p4);
        }        
        
    } else if(pre_num==4 && pre_id4 !=0xf) {
        if(id4==pre_id4 || id3==pre_id4 || id2==pre_id4 || id1==pre_id4) {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger4 is still down!!\n");
        } else {
            if(tpd_em_debuglog==1)
                TPD_DMESG("finger4 is up!\n");
            tpd_up(raw_x4, raw_y4, x4, y4, p4);
        }
    }
        if(tpd_em_debuglog==1)    
            TPD_DMESG("pre_id1=%d, pre_id2=%d, pre_id3=%d, pre_id4=%d, id1=%d, id2=%d, id3=%d, id4=%d\n", pre_id1, pre_id2, pre_id3, pre_id4, id1, id2, id3, id4);                
        pre_id1 = id1; pre_id2 = id2; pre_id3 = id3; pre_id4 = id4; pre_tt_mode = buffer[1];
    pre_num = finger_num;
       
    if(tpd && tpd->dev && tpd_register_flag==1) {      
            input_sync(tpd->dev);
    }
        
        i2c_smbus_read_i2c_block_data(i2c_client, 0x00, 1, &toggle);
        if((toggle & 0x80) == 0)
            toggle = toggle | 0x80;
        else
            toggle = toggle & (~0x80);
        i2c_smbus_write_i2c_block_data(i2c_client, 0x00, 1, &toggle); // switch the read toggle bit to do next sampling
        tpd_flag = 0;
   
    } while (!kthread_should_stop());
    return 0;
}
9)這裏面,我們看到幾個新函數
tpd_calibrate,tpd_down,tpd_up
主要是通過傳入座標值,對TOUCH的down和up值做處理。
void tpd_down(int raw_x, int raw_y, int x, int y, int p) {
    if(tpd && tpd->dev && tpd_register_flag==1) {
    input_report_abs(tpd->dev, ABS_PRESSURE, p/PRESSURE_FACTOR);
    input_report_key(tpd->dev, BTN_TOUCH, 1);
    input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, p/PRESSURE_FACTOR);
    input_report_abs(tpd->dev, ABS_MT_WIDTH_MAJOR, p/PRESSURE_FACTOR);
    input_report_abs(tpd->dev, ABS_MT_POSITION_X, x);
    input_report_abs(tpd->dev, ABS_MT_POSITION_Y, y);
    input_mt_sync(tpd->dev);
    TPD_DEBUG("D[%4d %4d %4d]\n", x, y, p);
    TPD_EM_PRINT(raw_x, raw_y, x, y, p, 1);
  }  
}
void tpd_up(int raw_x, int raw_y, int x, int y, int p) {
    if(tpd && tpd->dev && tpd_register_flag==1) {
    input_report_abs(tpd->dev, ABS_PRESSURE, 0);
    input_report_key(tpd->dev, BTN_TOUCH, 0);
    input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, 0);
    input_report_abs(tpd->dev, ABS_MT_WIDTH_MAJOR, 0);
    input_report_abs(tpd->dev, ABS_MT_POSITION_X, x);
    input_report_abs(tpd->dev, ABS_MT_POSITION_Y, y);
    input_mt_sync(tpd->dev);
    TPD_DEBUG("U[%4d %4d %4d]\n", x, y, 0);
    TPD_EM_PRINT(raw_x, raw_y, x, y, p, 0);
  }  
}
這裏面主要通過驅動linux的輸入設備驅動函數input_report_abs, input_report_key把linux輸入設備的座標和輸入設備事件提交到linux內核裏面去。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章