对于MTK TP驱动移植一般分为六部分:
1、硬件IO口配置;
2、TP驱动移植;
3、I2C通信;
4、中断触发;
5、数据上报;
6、虚拟按键;
硬件电路:
1、GPIO配置
打开 mediatek\dct\DrvGen.exe
选择 mediatek\custom\xiaoxi\kernel\dct\dct\codegen.dws 配置文件
配置EINT7_CTP引脚、CTP_RST复位引脚
2、TP驱动移植(以ft5x16为例)
在\mediatek\custom\common\kernel\touchpanel目录下创建ft5x16,将供应商提供的驱动驱动资料拷贝到该目录下;
修改配置文件:mediatek\config\prj\ProjectConfig.mk下的CUSTOM_KERNEL_TOUCHPANEL其值由改为ft5x16,表明对应ft5x16子目录;
打开ft5x16.c文件,修改一下:
static struct i2c_board_info __initdata ft5x16_i2c_tpd={ I2C_BOARD_INFO("ft5x16", (0x70>>1))}; //"ft5x16"为设备名 ,设备地址为高7位
static struct tpd_driver_t tpd_device_driver = {
.tpd_device_name = "FT5x16",
.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
};
/* called when loaded into kernel */
static int __init tpd_driver_init(void) {
printk("MediaTek FT5x16 touch panel driver init\n");
/* 注册板级设备信息 */
i2c_register_board_info(IIC_PORT, &ft5x16_i2c_tpd, 1); //IIC_PORT表示i2c控制器号,由电路原理图可知TP设备连接到i2c控制器0,ft5x16_i2c_tpd为i2c设备结构,1表示该i2c_board_info个数
if(tpd_driver_add(&tpd_device_driver) < 0)
printk("add FT5x16 driver failed\n");
return 0;
}
</span>
重新编译:./mk n k && ./mk bootimage
3、I2C通信
新驱动编译进内核,启动内核后,我们怎样验证i2c接口能够正常通信呢?
系统启动后通过串口或adb shell进入系统命令行窗口,查询/sys/bus/i2c/devices目录下是否有0-0038信息,查询/sys/bus/i2c/drivers目录下是否存在‘ft5x16’设备名;先保证i2c能够正常通信;
4、中断触发
中断注册函数:mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_TYPE, tpd_eint_interrupt_handler, 1);
//tpd_eint_interrupt_handler函数为中断回调函数
5、数据上报
当触摸屏产生中断的时候就会调用到该接口;然后在中断处理函数中唤醒运行在子线程中的等待队列,再通过子线程获取TP数据并上报到系统;
static DECLARE_WAIT_QUEUE_HEAD(waiter); //初始化等待队列
thread = kthread_run(touch_event_handler, 0, TPD_DEVICE); //新建线程
static int touch_event_handler(void *unused)
{
......
do
{
mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
set_current_state(TASK_INTERRUPTIBLE);
wait_event_interruptible(waiter,tpd_flag!=0); //等待队列进入休眠,等待唤醒
tpd_flag = 0;
set_current_state(TASK_RUNNING);
......
if (tpd_touchinfo(&cinfo, &pinfo)) //获取TP数据
{
//TPD_DEBUG("point_num = %d\n",point_num);
TPD_DEBUG_SET_TIME;
if(point_num >0)
{
for(i =0; i<point_num; i++)//only support 3 point
{
cinfo.x[i] = cinfo.x[i];
cinfo.y[i] = cinfo.y[i];
tpd_down(cinfo.x[i], cinfo.y[i], cinfo.id[i]); //上报按下数据
printk(KERN_DEBUG"----calibration----- X:%4d, Y:%4d, P:%4d \n", cinfo.x[i], cinfo.y[i], cinfo.id[i]);
}
input_sync(tpd->dev);
}
else
{
tpd_up(cinfo.x[0], cinfo.y[0]); //上报弹起数据
//TPD_DEBUG("release --->\n");
//input_mt_sync(tpd->dev);
input_sync(tpd->dev);
}
}
......
}while(!kthread_should_stop());
return 0;
}
TP数据可以通过打印的方式进行查看,也可以激活‘系统设置’中‘开发者选项’的‘指针位置’,现在触摸操作在LCD的轨迹,也可以在packages\apps\Launcher2\src\com\android\launcher2\Launcher.java的onCreate方法最后添加Settings.System.putInt(this.getContentResolver(),Settings.System.POINTER_LOCATION, 1); 在Launcher中开启‘指针位置’功能(需要mm Launcher模块并重新打包和烧录system.img文件);
注:如果TP获取到的数据比较乱的时候建议通过打开‘指针位置’功能进行查看,排除TP固件分辨与LCD没对应等问题;
6、虚拟按键
static struct tpd_driver_t tpd_device_driver = {
.tpd_device_name = "FT5x16",
.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
};
从tpd_driver_t结构可知tpd_have_button成员为虚拟按键标志位;由宏TPD_HAVA_BUTTON开关决定的,宏定义在tpd_custom_fts.h中;
在tpd_custom_fts.h中定义了一系列关于虚拟按键的宏:
#define TPD_HAVE_BUTTON //虚拟按键开关
#define TPD_BUTTON_WIDTH (200) //按键宽度
#define TPD_BUTTON_HEIGH (100) //按键高度
#define TPD_KEY_COUNT 3 //按键个数
#define TPD_KEYS {KEY_MENU, KEY_HOMEPAGE, KEY_BACK} //按键对应的功能
#define TPD_KEYS_DIM {{80,900,TPD_BUTTON_WIDTH,TPD_BUTTON_HEIGH}, {240,900,TPD_BUTTON_WIDTH,TPD_BUTTON_HEIGH}, {400,900,TPD_BUTTON_WIDTH,TPD_BUTTON_HEIGH}} //按键对应位置
TPD_KEYS_DIM中的坐标是该按键区域的中心点:
TP驱动简要分析
static struct tpd_driver_t tpd_device_driver = {
.tpd_device_name = FT5x16,
.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
};
/* called when loaded into kernel */
static int __init tpd_driver_init(void) {
printk("MediaTek FT5x16 touch panel driver init\n");
i2c_register_board_info(0, &ft5x16_i2c_tpd, 1); //注册板级设备信息
if(tpd_driver_add(&tpd_device_driver) < 0) //添加驱动
printk("add FT5x16 driver failed\n");
return 0;
}
MTK自己编写了一套TP框架,通过该框架管理TP设备,tpd_driver_add为框架的接口之一;系统通过tpd_driver_add添加驱动后会回调tpd_local_init函数;
#ifdef TPD_HAVE_BUTTON
static int tpd_keys_local[TPD_KEY_COUNT] = TPD_KEYS; //存放按键功能信息
static int tpd_keys_dim_local[TPD_KEY_COUNT][4] = TPD_KEYS_DIM; //存放虚拟按键信息
#endif
static int tpd_local_init(void)
{
TPD_DMESG("FTS I2C Touchscreen Driver (Built %s @ %s)\n", __DATE__, __TIME__);
if(i2c_add_driver(&tpd_i2c_driver)!=0) //注册i2c驱动
{
TPD_DMESG("FTS unable to add i2c driver.\n");
return -1;
}
if(tpd_load_status == 0)
{
TPD_DMESG("FTS 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_def_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驱动后,如果找到对应的设备就会调用tpd_probe函数;
static const struct i2c_device_id ft5x16_tpd_id[] = {{TPD_NAME,0},{}};
static struct i2c_driver tpd_i2c_driver = {
.driver = {
.name = TPD_NAME,
},
.probe = tpd_prob,
.remove = __devexit_p(tpd_remove),
.id_table = ft5x16_tpd_id,
.detect = tpd_detect,
};
static int __devinit tpd_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int retval = TPD_OK;
char data;
u8 report_rate=0;
int err=0;
int reset_count = 0;
u8 chip_id,i;
reset_proc:
i2c_client = client;
#ifdef MAIERXUN_TP_COM
if(touchpanel_flag){
return 0;
}
#endif
//复位
//power on, need confirm with SA
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(5);
TPD_DMESG(" fts ic reset\n");
//打开TP电源
#ifdef TPD_POWER_SOURCE_CUSTOM
hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_3300, "TP");
#else
hwPowerOn(MT65XX_POWER_LDO_VGP2, VOL_3300, "TP");
#endif
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);
#ifdef TPD_CLOSE_POWER_IN_SLEEP
hwPowerDown(TPD_POWER_SOURCE,"TP");
hwPowerOn(TPD_POWER_SOURCE,VOL_3300,"TP");
msleep(100);
#else /* 结束复位 */
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(5);
TPD_DMESG(" fts ic reset\n");
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);
#endif
/* 初始化中断引脚 */
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);
/* 中断配置和注册 */
mt_eint_set_hw_debounce(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_CN);
mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_TYPE, tpd_eint_interrupt_handler, 1); //注册中断处理函数,TP产生中断时就会回调tpd_eint_interrupt函数
mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
msleep(400);
err=i2c_smbus_read_i2c_block_data(i2c_client, 0x00, 1, &data);
TPD_DMESG("gao_i2c:err %d,data:%d\n", err,data);
if(err< 0 || data!=0)// reg0 data running state is 0; other state is not 0
{
TPD_DMESG("I2C transfer error, line: %d\n", __LINE__);
#ifdef TPD_RESET_ISSUE_WORKAROUND
if ( reset_count < TPD_MAX_RESET_COUNT )
{
reset_count++;
goto reset_proc;
}
#endif
//add at 20150330 by zhu
#ifdef MAIERXUN_TP_COM
touchpanel_flag=false;
#endif
return -1;
}
......
#ifdef VELOCITY_CUSTOM_FT5206
if((err = misc_register(&tpd_misc_device))) //注册混杂设备驱动
{
printk("mtk_tpd: tpd_misc_device register failed\n");
}
#endif
#ifdef TPD_AUTO_UPGRADE
printk("********************Enter CTP Auto Upgrade********************\n");
fts_ctpm_auto_upgrade(i2c_client);
#endif
thread = kthread_run(touch_event_handler, 0, TPD_DEVICE); //创建子线程,通过该子线程获取和上报数据
if (IS_ERR(thread))
{
retval = PTR_ERR(thread);
TPD_DMESG(TPD_DEVICE " failed to create kernel thread: %d\n", retval);
}
TPD_DMESG("FTS Touch Panel Device Probe %s\n", (retval < TPD_OK) ? "FAIL" : "PASS");
/* 初始化TP的P-sensor功能,暂不分析 */
#ifdef TPD_PROXIMITY
struct hwmsen_object obj_ps;
obj_ps.polling = 0;//interrupt mode
obj_ps.sensor_operate = tpd_ps_operate;
if((err = hwmsen_attach(ID_PROXIMITY, &obj_ps)))
{
APS_ERR("proxi_fts attach fail = %d\n", err);
}
else
{
APS_ERR("proxi_fts attach ok = %d\n", err);
}
#endif
#ifdef MAIERXUN_TP_COM
touchpanel_flag=true;
#endif
return 0;
}
/* 中断处理函数 */
static void tpd_eint_interrupt_handler(void)
{
//TPD_DEBUG("TPD interrupt has been triggered\n");
TPD_DEBUG_PRINT_INT;
tpd_flag = 1;
wake_up_interruptible(&waiter); //唤醒等待队列
}
中断处理遵循中断上下文的设计原则,使得中断子程序只是简单唤醒等待队列就可以了,没有多余的操作;
/* 子线程处理函数 */
static int touch_event_handler(void *unused)
{
struct touch_info cinfo, pinfo;
int i=0;
struct sched_param param = { .sched_priority = RTPM_PRIO_TPD };
sched_setscheduler(current, SCHED_RR, ¶m);
#ifdef TPD_PROXIMITY
int err;
hwm_sensor_data sensor_data;
u8 proximity_status;
#endif
u8 state;
do //进入while循环进行睡眠-等待唤醒的操作
{
mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM); //中断使能(解除屏蔽)
set_current_state(TASK_INTERRUPTIBLE);
wait_event_interruptible(waiter,tpd_flag!=0); //进入睡眠等待唤醒
tpd_flag = 0;
set_current_state(TASK_RUNNING);
......
#ifdef TPD_PROXIMITY //TP的P-sensor功能,暂不分析
if (tpd_proximity_flag == 1)
{
i2c_smbus_read_i2c_block_data(i2c_client, 0xB0, 1, &state);
TPD_PROXIMITY_DEBUG("proxi_5206 0xB0 state value is 1131 0x%02X\n", state);
if(!(state&0x01))
{
tpd_enable_ps(1);
}
i2c_smbus_read_i2c_block_data(i2c_client, 0x01, 1, &proximity_status);
TPD_PROXIMITY_DEBUG("proxi_5206 0x01 value is 1139 0x%02X\n", proximity_status);
if (proximity_status == 0xC0)
{
tpd_proximity_detect = 0;
}
else if(proximity_status == 0xE0)
{
tpd_proximity_detect = 1;
}
TPD_PROXIMITY_DEBUG("tpd_proximity_detect 1149 = %d\n", tpd_proximity_detect);
if ((err = tpd_read_ps()))
{
TPD_PROXIMITY_DMESG("proxi_5206 read ps data 1156: %d\n", err);
}
sensor_data.values[0] = tpd_get_ps_value();
sensor_data.value_divide = 1;
sensor_data.status = SENSOR_STATUS_ACCURACY_MEDIUM;
if ((err = hwmsen_get_interrupt_data(ID_PROXIMITY, &sensor_data)))
{
TPD_PROXIMITY_DMESG(" proxi_5206 call hwmsen_get_interrupt_data failed= %d\n", err);
}
}
#endif
if (tpd_touchinfo(&cinfo, &pinfo)) //获取TP设备数据,并把数据保存在cinfob buf中
{
//TPD_DEBUG("point_num = %d\n",point_num);
TPD_DEBUG_SET_TIME;
if(point_num >0)
{
for(i =0; i<point_num; i++)//only support 3 point
{
printk(KERN_DEBUG"X:%4d, Y:%4d, P:%4d \n", cinfo.x[i], cinfo.y[i], cinfo.id[i]);
cinfo.x[i] = cinfo.x[i];
cinfo.y[i] = cinfo.y[i];
tpd_down(cinfo.x[i], cinfo.y[i], cinfo.id[i]); //按下数据处理
printk(KERN_DEBUG"----calibration----- X:%4d, Y:%4d, P:%4d \n", cinfo.x[i], cinfo.y[i], cinfo.id[i]);
}
input_sync(tpd->dev);
}
else
{
tpd_up(cinfo.x[0], cinfo.y[0]); //弹起数据处理
//TPD_DEBUG("release --->\n");
//input_mt_sync(tpd->dev);
input_sync(tpd->dev);
}
}
......
}while(!kthread_should_stop());
return 0;
}
static int tpd_touchinfo(struct touch_info *cinfo, struct touch_info *pinfo)
{
int i = 0;
char data[128] = {0};
u16 high_byte,low_byte,reg;
u8 report_rate =0;
p_point_num = point_num;
if (tpd_halt)
{
TPD_DMESG( "tpd_touchinfo return ..\n");
return false;
}
mutex_lock(&i2c_access);
reg = 0x00;
fts_i2c_Read(i2c_client, ®, 1, data, 64); //获取TP数据,一些TP是支持多点触控的,所以有可能就产生多个触点的数据
mutex_unlock(&i2c_access);
/*get the number of the touch points*/
point_num= data[2] & 0x0f;
TPD_DEBUG("point_num =%d\n",point_num);
/* 根据芯片协议解析数据并存放在cinfo buf中 */
for(i = 0; i < point_num; i++)
{
cinfo->p[i] = data[3+6*i] >> 6; //event flag
cinfo->id[i] = data[3+6*i+2]>>4; //touch id
/*get the X coordinate, 2 bytes*/
high_byte = data[3+6*i];
high_byte <<= 8;
high_byte &= 0x0f00;
low_byte = data[3+6*i + 1];
cinfo->x[i] = high_byte |low_byte;
/*get the Y coordinate, 2 bytes*/
high_byte = data[3+6*i+2];
high_byte <<= 8;
high_byte &= 0x0f00;
low_byte = data[3+6*i+3];
cinfo->y[i] = high_byte |low_byte;
}
}
TPD_DEBUG(" cinfo->x[0] = %d, cinfo->y[0] = %d, cinfo->p[0] = %d\n", cinfo->x[0], cinfo->y[0], cinfo->p[0]);
return true;
}
static void tpd_down(int x, int y, int p) {
static int tpd_x = 0;
static int tpd_y = 0;
tpd_x = x;
tpd_y = y;
/* 通过输入子系统上报数据 */
input_report_key(tpd->dev, BTN_TOUCH, 1);
input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, 20);
input_report_abs(tpd->dev, ABS_MT_POSITION_X, x);
input_report_abs(tpd->dev, ABS_MT_POSITION_Y, y);
printk(KERN_ERR, "D[%4d %4d %4d] ", x, y, p);
/* track id Start 0 */
input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, p);
input_mt_sync(tpd->dev);
#ifndef MT6572
if (FACTORY_BOOT == get_boot_mode()|| RECOVERY_BOOT == get_boot_mode())
#endif
{
tpd_button(x, y, 1); //虚拟按键的处理
}
TPD_EM_PRINT(x, y, x, y, p-1, 1);
}
static void tpd_up(int x, int y) {
input_report_key(tpd->dev, BTN_TOUCH, 0);
input_mt_sync(tpd->dev);
TPD_EM_PRINT(x, y, x, y, 0, 0);
#ifndef MT6572
if (FACTORY_BOOT == get_boot_mode()|| RECOVERY_BOOT == get_boot_mode())
#endif
{
tpd_button(x, y, 0);
}
}
void tpd_button(unsigned int x, unsigned int y, unsigned int down) {
int i;
if(down) {
for(i=0;i<tpd_keycnt;i++)
{
/* 判断数据是否落在虚拟按键的范围内,数据处理算法实现了以坐标点为中心的虚拟按键 */
if(x>=tpd_keys_dim[i][0]-(tpd_keys_dim[i][2]/2) &&
x<=tpd_keys_dim[i][0]+(tpd_keys_dim[i][2]/2) &&
y>=tpd_keys_dim[i][1]-(tpd_keys_dim[i][3]/2) &&
y<=tpd_keys_dim[i][1]+(tpd_keys_dim[i][3]/2) &&
!(tpd->btn_state&(1<<i)))
{
input_report_key(tpd->kpd, tpd_keys[i], 1); //上报按键
input_sync(tpd->kpd);
tpd->btn_state|=(1<<i);
TPD_DEBUG("[mtk-tpd] press key %d (%d)\n",i, tpd_keys[i]);
printk("[mtk-tpd] press key %d (%d)\n",i, tpd_keys[i]);
}
}
} else {
for(i=0;i<tpd_keycnt;i++) {
if(tpd->btn_state&(1<<i)) {
input_report_key(tpd->kpd, tpd_keys[i], 0);
input_sync(tpd->kpd);
TPD_DEBUG("[mtk-tpd] release key %d (%d)\n",i, tpd_keys[i]);
printk("[mtk-tpd] release key %d (%d)\n",i, tpd_keys[i]);
}
}
tpd->btn_state=0;
}
}
tpd_keys_dim和tpd_keys的数据是通过tpd_button_setting初始化的,可以去看tpd_button_setting()的实现;
通过简单分析,由此可知MTK的TP驱动的整体框架跟普通TP驱动框架也是大致差不多;