LCD触屏驱动

tiny4412多点触摸屏驱动程序(基于I2C协议):

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/irq.h>
#include <linux/input.h>
#include <linux/slab.h>

#include <mach/gpio.h>
#include <mach/irqs.h>
#include <plat/gpio-cfg.h>
#include <plat/ctouch.h>

#define MTP_MAX_ID  15     //由硬件决定
#define MTP_IRQ    gpio_to_irq(EXYNOS4_GPX1(6))
#define MTP_ADDR   (0x70 >> 1)
static struct input_dev *mtp_inputdev;
static struct work_struct mtp_work;
struct i2c_client *mtp_client;
static int mtp_points;
struct mtp_event {
      int x;
      int y;
      int id;
};
static struct mtp_event mtp_events[16];

static irqreturn_t mtp_handler(int irq, void *dev_id)
{
     /* 本该获取触点数据,并上报,但I2C是慢速设备,不该放在中断服务程序
      * 中断服务程序执行中断的上半部分,将下半部分放入工作队列,让内核线程
      * 来处理
      */
     schedule_work(&mtp_work);
     
     return IRQ_HANDLED;
}

static int mtp_ft5x0x_i2c_rxdata(struct i2c_client *client, char *rxdata, int length) {
    int ret;
    struct i2c_msg msgs[] = {
        {
            .addr    = client->addr,
            .flags    = 0,
            .len    = 1,
            .buf    = rxdata,
        },
        {
            .addr    = client->addr,
            .flags    = I2C_M_RD,
            .len    = length,
            .buf    = rxdata,
        },
    };

    ret = i2c_transfer(client->adapter, msgs, 2);
    if (ret < 0)
        pr_err("%s: i2c read error: %d\n", __func__, ret);

    return ret;
}

static int mtp_ft5x0x_read_data(void) 
{
    u8 buf[32] = { 0 };
    int ret;


    ret = mtp_ft5x0x_i2c_rxdata(mtp_client, buf, 31);

    if (ret < 0) {
        printk("%s: read touch data failed, %d\n", __func__, ret);
        return ret;
    }

    mtp_points = buf[2] & 0x0f;

    switch (mtp_points) {
        case 5:
            mtp_events[4].x = (s16)(buf[0x1b] & 0x0F)<<8 | (s16)buf[0x1c];
            mtp_events[4].y = (s16)(buf[0x1d] & 0x0F)<<8 | (s16)buf[0x1e];
            mtp_events[4].id   = buf[0x1d] >> 4;
        case 4:
            mtp_events[3].x = (s16)(buf[0x15] & 0x0F)<<8 | (s16)buf[0x16];
            mtp_events[3].y = (s16)(buf[0x17] & 0x0F)<<8 | (s16)buf[0x18];
            mtp_events[3].id   = buf[0x17] >> 4;
        case 3:
            mtp_events[2].x = (s16)(buf[0x0f] & 0x0F)<<8 | (s16)buf[0x10];
            mtp_events[2].y = (s16)(buf[0x11] & 0x0F)<<8 | (s16)buf[0x12];
            mtp_events[2].id   = buf[0x11] >> 4;
        case 2:
            mtp_events[1].x = (s16)(buf[0x09] & 0x0F)<<8 | (s16)buf[0x0a];
            mtp_events[1].y = (s16)(buf[0x0b] & 0x0F)<<8 | (s16)buf[0x0c];
            mtp_events[1].id   = buf[0x0b] >> 4;
        case 1:
            mtp_events[0].x = (s16)(buf[0x03] & 0x0F)<<8 | (s16)buf[0x04];
            mtp_events[0].y = (s16)(buf[0x05] & 0x0F)<<8 | (s16)buf[0x06];
            mtp_events[0].id   = buf[0x05] >> 4;
            break;
        default:
            //printk("%s: invalid touch data, %d\n", __func__, event->touch_point);
            return 0;
    }

    return 0;
}

static void mtp_work_func(struct work_struct *work)
{
    int i;
    int ret = mtp_ft5x0x_read_data();
    if (ret< 0)
        return ;
    if (!mtp_points) {
        input_mt_sync(mtp_inputdev);
        input_sync(mtp_inputdev);
    }
    for (i = 0; i < mtp_points; i++) {
        input_event(mtp_inputdev, EV_ABS, ABS_MT_POSITION_X, mtp_events[i].x);
        input_event(mtp_inputdev, EV_ABS, ABS_MT_POSITION_Y, mtp_events[i].y);
        input_event(mtp_inputdev, EV_ABS, ABS_MT_TRACKING_ID, mtp_events[i].id);
        input_mt_sync(mtp_inputdev);
    }
    
    input_sync(mtp_inputdev);
}

static int __devinit mtp_input_probe(struct i2c_client *client,
                  const struct i2c_device_id *id)
{
    printk("mtp_input_probe \n");

    mtp_inputdev = input_allocate_device();
    if (!mtp_inputdev ) {
         printk("input_allocate_device error\n");
         return -1;
    }
    mtp_client = client;
    set_bit(EV_ABS, mtp_inputdev->evbit);
    set_bit(EV_SYN, mtp_inputdev->evbit);    
    set_bit(INPUT_PROP_DIRECT, mtp_inputdev->propbit);    //加上这句就不需要配置文件了
    
    set_bit(ABS_MT_TRACKING_ID, mtp_inputdev->absbit);
    set_bit(ABS_MT_POSITION_X, mtp_inputdev->absbit);
    set_bit(ABS_MT_POSITION_Y, mtp_inputdev->absbit);

    input_set_abs_params(mtp_inputdev, ABS_MT_TRACKING_ID, 0, MTP_MAX_ID, 0, 0);
    input_set_abs_params(mtp_inputdev, ABS_MT_POSITION_X, 0, 800, 0, 0);
    input_set_abs_params(mtp_inputdev, ABS_MT_POSITION_Y, 0, 480, 0, 0);

    mtp_inputdev->name = "mtp_input_ts";  //名字不能忽略,安卓系统要根据它找到配置文件
    
    input_register_device(mtp_inputdev);

    INIT_WORK(&mtp_work, mtp_work_func);
    request_irq(MTP_IRQ, mtp_handler, IRQ_TYPE_EDGE_BOTH, "MTP_input", mtp_inputdev);
    
    return 0;
}

static int __devexit mtp_input_remove(struct i2c_client *client)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    free_irq(MTP_IRQ, mtp_inputdev);
    cancel_work_sync(&mtp_work);
    input_unregister_device(mtp_inputdev);
    input_free_device(mtp_inputdev);
    return 0;
}

static const struct i2c_device_id mtp_input_id_table[] = {
    { "mtp_input", 0 },
    {}
};

static int mtp_input_detect(struct i2c_client *client,
               struct i2c_board_info *info)
{
    /* 能运行到这里, 表示该addr的设备是存在的
     * 但是有些设备单凭地址无法分辨(A芯片的地址是0x50, B芯片的地址也是0x50)
     * 还需要进一步读写I2C设备来分辨是哪款芯片
     * detect就是用来进一步分辨这个芯片是哪一款,并且设置info->type
     */
    u8 buf[32] = { 0 };
    int ret;

    printk("mtp_input_detect : addr = 0x%x\n", client->addr);

    /* 进一步判断是哪一款 */
    buf[0] = 0xa3;
    ret = mtp_ft5x0x_i2c_rxdata(client, buf, 1);
    if (ret < 0) {
        printk("there is no real device, read err\n");
        return ret;
    }
    if (buf[0] != 0x55) {
        printk("there is no real device, val err\n");
        return ret;
    }
    //printk("chip vendor id = 0x%x \n", buf[0]);
    strlcpy(info->type, "mtp_input", I2C_NAME_SIZE);   //在id_table中要有这个名字才能匹配,来调用probe函数
    return 0;
}

static const unsigned short addr_list[] = { MTP_ADDR, I2C_CLIENT_END };

/* 1. 分配/设置i2c_driver */
static struct i2c_driver mtp_input_driver = {
    .class  = I2C_CLASS_HWMON, /* 表示去哪些适配器上找设备 */
    .driver    = {
        .name    = "mtp_input",
        .owner    = THIS_MODULE,
    },
    .probe        = mtp_input_probe,
    .remove        = __devexit_p(mtp_input_remove),
    .id_table    = mtp_input_id_table,
    .detect     = mtp_input_detect,  /* 用这个函数来检测设备确实存在 */
    .address_list    = addr_list,   /* 这些设备的地址 */
};

static int mtp_input_drv_init(void)
{
    /* 2. 注册i2c_driver */
    i2c_add_driver(&mtp_input_driver);
    
    return 0;
}

static void mtp_input_drv_exit(void)
{
    i2c_del_driver(&mtp_input_driver);
}


module_init(mtp_input_drv_init);
module_exit(mtp_input_drv_exit);
MODULE_LICENSE("GPL");

单点触摸屏和多点触摸屏对比:

单点触摸屏驱动程序(基于ADC获得输入):

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <asm/io.h>
#include <asm/irq.h>
//#include <asm/plat-s3c24xx/ts.h>
//#include <asm/arch/regs-adc.h>
//#include <asm/arch/regs-gpio.h>
struct s3c_ts_regs {
 unsigned long adccon;
 unsigned long adctsc;
 unsigned long adcdly;
 unsigned long adcdat0;
 unsigned long adcdat1;
 unsigned long adcupdn;
};
static struct input_dev *s3c_ts_dev;
static volatile struct s3c_ts_regs *s3c_ts_regs;
static struct timer_list ts_timer;
static void enter_wait_pen_down_mode(void)
{
 s3c_ts_regs->adctsc = 0xd3;
}
static void enter_wait_pen_up_mode(void)
{
 s3c_ts_regs->adctsc = 0x1d3;
}
static void enter_measure_xy_mode(void)
{
 s3c_ts_regs->adctsc = (1<<3)|(1<<2);
}
static void start_adc(void)
{
 s3c_ts_regs->adccon |= (1<<0);
}
static int s3c_filter_ts(int x[], int y[])
{
#define ERR_LIMIT 10
 int avr_x, avr_y;
 int det_x, det_y;
 avr_x = (x[0] + x[1])/2;
 avr_y = (y[0] + y[1])/2;
 det_x = (x[2] > avr_x) ? (x[2] - avr_x) : (avr_x - x[2]);
 det_y = (y[2] > avr_y) ? (y[2] - avr_y) : (avr_y - y[2]);
 if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
  return 0;
 avr_x = (x[1] + x[2])/2;
 avr_y = (y[1] + y[2])/2;
 det_x = (x[3] > avr_x) ? (x[3] - avr_x) : (avr_x - x[3]);
 det_y = (y[3] > avr_y) ? (y[3] - avr_y) : (avr_y - y[3]);
 if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
  return 0;
 
 return 1;
}
static void s3c_ts_timer_function(unsigned long data)
{
 if (s3c_ts_regs->adcdat0 & (1<<15))
 {
  /* 已经松开 */
  input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0);
  input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
  input_sync(s3c_ts_dev);
  enter_wait_pen_down_mode();
 }
 else
 {
  /* 测量X/Y坐标 */
  enter_measure_xy_mode();
  start_adc();
 }
}

static irqreturn_t pen_down_up_irq(int irq, void *dev_id)
{
 if (s3c_ts_regs->adcdat0 & (1<<15))
 {
  //printk("pen up\n");
  input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0);
  input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
  input_sync(s3c_ts_dev);
  enter_wait_pen_down_mode();
 }
 else
 {
  //printk("pen down\n");
  //enter_wait_pen_up_mode();
  enter_measure_xy_mode();
  start_adc();
 }
 return IRQ_HANDLED;
}
static irqreturn_t adc_irq(int irq, void *dev_id)
{
 static int cnt = 0;
 static int x[4], y[4];
 int adcdat0, adcdat1;
 
 
 /* 优化措施2: 如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 */
 adcdat0 = s3c_ts_regs->adcdat0;
 adcdat1 = s3c_ts_regs->adcdat1;
 if (s3c_ts_regs->adcdat0 & (1<<15))
 {
  /* 已经松开 */
  cnt = 0;
  input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0);
  input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
  input_sync(s3c_ts_dev);
  enter_wait_pen_down_mode();
 }
 else
 {
  // printk("adc_irq cnt = %d, x = %d, y = %d\n", ++cnt, adcdat0 & 0x3ff, adcdat1 & 0x3ff);
  /* 优化措施3: 多次测量求平均值 */
  x[cnt] = adcdat0 & 0x3ff;
  y[cnt] = adcdat1 & 0x3ff;
  ++cnt;
  if (cnt == 4)
  {
   /* 优化措施4: 软件过滤 */
   if (s3c_filter_ts(x, y))
   {   
    //printk("x = %d, y = %d\n", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
    input_report_abs(s3c_ts_dev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
    input_report_abs(s3c_ts_dev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
    input_report_abs(s3c_ts_dev, ABS_PRESSURE, 1);
    input_report_key(s3c_ts_dev, BTN_TOUCH, 1);
    input_sync(s3c_ts_dev);
   }
   cnt = 0;
   enter_wait_pen_up_mode();
   /* 启动定时器处理长按/滑动的情况 */
   mod_timer(&ts_timer, jiffies + HZ/100);
  }
  else
  {
   enter_measure_xy_mode();
   start_adc();
  }  
 }
 
 return IRQ_HANDLED;
}
static int s3c_ts_init(void)
{
 struct clk* clk;
 
 /* 1. 分配一个input_dev结构体 */
 s3c_ts_dev = input_allocate_device();
 /* 2. 设置 */
 /* 2.1 能产生哪类事件 */
 set_bit(EV_KEY, s3c_ts_dev->evbit);
 set_bit(EV_ABS, s3c_ts_dev->evbit);
 /* 2.2 能产生这类事件里的哪些事件 */
 set_bit(BTN_TOUCH, s3c_ts_dev->keybit);
 input_set_abs_params(s3c_ts_dev, ABS_X, 0, 0x3FF, 0, 0);
 input_set_abs_params(s3c_ts_dev, ABS_Y, 0, 0x3FF, 0, 0);
 input_set_abs_params(s3c_ts_dev, ABS_PRESSURE, 0, 1, 0, 0);

 /* 3. 注册 */
 input_register_device(s3c_ts_dev);
 /* 4. 硬件相关的操作 */
 /* 4.1 使能时钟(CLKCON[15]) */
 clk = clk_get(NULL, "adc");
 clk_enable(clk);
 
 /* 4.2 设置S3C2440的ADC/TS寄存器 */
 s3c_ts_regs = ioremap(0x58000000, sizeof(struct s3c_ts_regs));
 /* bit[14]  : 1-A/D converter prescaler enable
  * bit[13:6]: A/D converter prescaler value,
  *            49, ADCCLK=PCLK/(49+1)=50MHz/(49+1)=1MHz
  * bit[0]: A/D conversion starts by enable. 先设为0
  */
 s3c_ts_regs->adccon = (1<<14)|(49<<6);
 request_irq(IRQ_TC, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL);
 request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL);
 /* 优化措施1:
  * 设置ADCDLY为最大值, 这使得电压稳定后再发出IRQ_TC中断
  */
 s3c_ts_regs->adcdly = 0xffff;
 /* 优化措施5: 使用定时器处理长按,滑动的情况
  *
  */
 init_timer(&ts_timer);
 ts_timer.function = s3c_ts_timer_function;
 add_timer(&ts_timer);
 enter_wait_pen_down_mode();
 
 return 0;
}
static void s3c_ts_exit(void)
{
 free_irq(IRQ_TC, NULL);
 free_irq(IRQ_ADC, NULL);
 iounmap(s3c_ts_regs);
 input_unregister_device(s3c_ts_dev);
 input_free_device(s3c_ts_dev);
 del_timer(&ts_timer);
}
module_init(s3c_ts_init);
module_exit(s3c_ts_exit);

MODULE_LICENSE("GPL");
 

 

posted @ 2019-03-29 18:09  一条水煮鱼  阅读(750)  评论(0编辑  收藏  举报