Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318049
  • 博文数量: 68
  • 博客积分: 1501
  • 博客等级: 上尉
  • 技术积分: 1010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-30 09:52
文章分类

全部博文(68)

文章存档

2010年(1)

2009年(67)

我的朋友

分类:

2009-01-07 20:05:09

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include
/* For ts.dev.id.version */
#define S3C2410TSVERSION    0x0101
#define TSC_SLEEP  (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
#define WAIT4INT(x)  (((x)<<8) | S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | S3C2410_ADCTSC_XY_PST(3))
#define AUTOPST         (S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | S3C2410_ADCTSC_AUTO_PST | S3C2410_ADCTSC_XY_PST(0))
#define DEBUG_LVL    KERN_DEBUG
MODULE_AUTHOR("Arnaud Patard <[email=arnaud.patard@rtp-net.org]arnaud.patard@rtp-net.org[/email]>");
MODULE_DESCRIPTION("s3c2410 touchscreen driver");
MODULE_LICENSE("GPL");
/*
* Definitions & global arrays.
*/
/*===================================================================================================
添加进 devs.c ,用于设置触摸屏信息
struct s3c2410_ts_mach_info mini2440_ts_cfg __initdata = {
        .delay = 20000,          //ADCDLY
        .presc = 55,                                         // 分频
        .oversampling_shift = 2,       // 采样的数据为 1<<2 =4
};

struct platform_device s3c_device_ts={
        .name             = "s3c2410-ts",
        .id               = -1,
        .dev              ={
                           .platform_data = &mini2440_ts_cfg,
                           }
};
static struct platform_device *smdk2440_devices[] __initdata = {
        &s3c_device_ts,
};

// updown=0 状态表示,设置等待 DOWN 中断
// updown=1 状态表示,设置等待UP中断
=======================================================================================================*/
 
static char *s3c2410ts_name = "s3c2410 TouchScreen";
/*
* Per-touchscreen data.
*/
struct s3c2410ts {
    struct input_dev *dev;
    long xp;
    long yp;
    int count;
    int shift;
    char phys[32];
};
static struct s3c2410ts ts;
static void __iomem *base_addr;
static inline void s3c2410_ts_connect(void)
{
    s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
    s3c2410_gpio_cfgpin(S3C2410_GPG13, S3C2410_GPG13_nXPON);
    s3c2410_gpio_cfgpin(S3C2410_GPG14, S3C2410_GPG14_YMON);
    s3c2410_gpio_cfgpin(S3C2410_GPG15, S3C2410_GPG15_nYPON);
}
 
 
/*===========================================================================================
    touch_timer_fire这个函数主要实现以下功能:
   
    1、  触摸笔开始点击的时候, 在中断函数stylus_updown里面被调用,
         此时缓存区没有数据,ts.count为0,   并且开启AD转换,而后进入 AD 中断
        
    2、  ADC中断函数stylus_action把缓冲区填满的时候,作为中断后半段函数稍后被调用,
         此时ts.count为4,算出其平均值后,交给事件处理层(Event Handler)处理,
         主要是填写缓冲,然后唤醒等待输入数据的进程。
        
    3、  stylus抬起,等到缓冲区填满后(可能会包含一些无用的数据)被调用,
         这时候判断出stylus up,报告stylus up事件,重新等待stylus down。
============================================================================================*/
static void touch_timer_fire(unsigned long data)
{
      unsigned long data0;
      unsigned long data1;
    int updown;
      data0 = readl(base_addr+S3C2410_ADCDAT0);
      data1 = readl(base_addr+S3C2410_ADCDAT1);
     updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
      // 注意此处 updown 取反了,所以 updown=0 表示设置抬起状态, updown=1 表示设置触摸笔点击状态
     if (updown) {
         if (ts.count != 0) {
                long tmp;
                    tmp = ts.xp;
                 ts.xp = ts.yp;                // 交换 X,Y 的值
              ts.yp = tmp;
             // 计算出平均值
              ts.xp >>= ts.shift;                
              ts.yp >>= ts.shift;
#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
             {
                 struct timeval tv;
                 do_gettimeofday(&tv);
                 printk(DEBUG_LVL "T: %06d, X: %03ld, Y: %03ld\n", (int)tv.tv_usec, ts.xp, ts.yp);
             }
#endif
             input_report_abs(ts.dev, ABS_X, ts.xp);
             input_report_abs(ts.dev, ABS_Y, ts.yp);
             input_report_key(ts.dev, BTN_TOUCH, 1);
             input_report_abs(ts.dev, ABS_PRESSURE, 1);
             input_sync(ts.dev);
         }
         ts.xp = 0;
         ts.yp = 0;
         ts.count = 0;
         writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
         writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
     }
            // 当触摸笔为抬起状态时
  else     {                                    
         ts.count = 0;
         input_report_key(ts.dev, BTN_TOUCH, 0);
         input_report_abs(ts.dev, ABS_PRESSURE, 0);
         input_sync(ts.dev);
                // 设置INT 位,等待 DOWN 中断
         writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);               
     }
}
static struct timer_list touch_timer =
        TIMER_INITIALIZER(touch_timer_fire, 0, 0);
static irqreturn_t stylus_updown(int irq, void *dev_id)
{
    unsigned long data0;
    unsigned long data1;
    int updown;
    data0 = readl(base_addr+S3C2410_ADCDAT0);
    data1 = readl(base_addr+S3C2410_ADCDAT1);
    updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
     /* 判断出stylus down,调用touch_timer_fire函数 */
    if (updown)
        touch_timer_fire(0);
    return IRQ_HANDLED;
}

static irqreturn_t stylus_action(int irq, void *dev_id)
{
    unsigned long data0;
    unsigned long data1;
    data0 = readl(base_addr+S3C2410_ADCDAT0);
    data1 = readl(base_addr+S3C2410_ADCDAT1);
    ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;                     // 计算坐标和
    ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
    ts.count++;
     if (ts.count < (1<   {
    // 采样未完成,继续下一次采样 ,通过 ENABLE_START 启动 AD 转换,一次一个数据
           writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
           writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
     } else {
        /* 缓冲区满, 采样完毕,激活下半部处理程序touch_timer_fire,处理接收数据 */
           mod_timer(&touch_timer, jiffies+1);
           writel(WAIT4INT(1), base_addr+S3C2410_ADCTSC);            // 采样完毕,抬起触摸笔,
       }
    return IRQ_HANDLED;
}
static struct clk    *adc_clock;
/*
* The functions for inserting/removing us as a module.
*/
static int __init s3c2410ts_probe(struct platform_device *pdev)
{
    int rc;
    struct s3c2410_ts_mach_info *info;
    struct input_dev *input_dev;
    info = ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
    if (!info)
    {
        printk(KERN_ERR "Hm... too bad : no platform data for ts\n");
        return -EINVAL;
    }
#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
    printk(DEBUG_LVL "Entering s3c2410ts_init\n");
#endif
    adc_clock = clk_get(NULL, "adc");                            // get adc clock
    if (!adc_clock) {
        printk(KERN_ERR "failed to get adc clock source\n");
        return -ENOENT;
    }
    clk_enable(adc_clock);                                         // enable adc clock
#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
    printk(DEBUG_LVL "got and enabled clock\n");
#endif
    base_addr=ioremap(S3C2410_PA_ADC,0x20);
    if (base_addr == NULL) {
        printk(KERN_ERR "Failed to remap register block\n");
        return -ENOMEM;
    }

    /* Configure GPIOs */
    s3c2410_ts_connect();                     
    if ((info->presc&0xff) > 0)
        writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF),\
                 base_addr+S3C2410_ADCCON);
    else
        writel(0,base_addr+S3C2410_ADCCON);                        // configure prescale of div

    /* Initialise registers */
    if ((info->delay&0xffff) > 0)
        writel(info->delay & 0xffff,  base_addr+S3C2410_ADCDLY);        // configure ADCDLY
    writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);   // 设置updown=0,等待触摸笔点击状态
    /* Initialise input stuff */
    memset(&ts, 0, sizeof(struct s3c2410ts));
    input_dev = input_allocate_device();
    if (!input_dev) {
        printk(KERN_ERR "Unable to allocate the input device !!\n");
        return -ENOMEM;
    }
    sprintf(ts.phys, "ts0");
    ts.dev=input_dev;
    ts.dev->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
    ts.dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
    input_set_abs_params(ts.dev, ABS_X, 0, 0x3FF, 0, 0);
    input_set_abs_params(ts.dev, ABS_Y, 0, 0x3FF, 0, 0);
    input_set_abs_params(ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
    ts.dev->name = s3c2410ts_name;
    ts.dev->id.bustype = BUS_RS232;
    ts.dev->id.vendor = 0xDEAD;
    ts.dev->id.product = 0xBEEF;
    ts.dev->id.version = S3C2410TSVERSION;
    ts.shift = info->oversampling_shift;
    /* Get irqs */
    if (request_irq(IRQ_ADC, stylus_action,IRQF_SAMPLE_RANDOM, "s3c2410_action", ts.dev))
      {
        printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
        iounmap(base_addr);
        return -EIO;
      }
    if (request_irq(IRQ_TC, stylus_updown,IRQF_SAMPLE_RANDOM, "s3c2410_action", ts.dev))
      {
        printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
        iounmap(base_addr);
        return -EIO;
      }
    printk(KERN_INFO "%s successfully loaded\n", s3c2410ts_name);
    /* All went ok, so register to the input system */
    rc=input_register_device(ts.dev);
    if (rc)
    {
        free_irq(IRQ_TC, ts.dev);
        free_irq(IRQ_ADC, ts.dev);
        clk_disable(adc_clock);
        iounmap(base_addr);
        return -EIO;
    }
    return 0;
}
static int s3c2410ts_remove(struct platform_device *pdev)
{
    disable_irq(IRQ_ADC);
    disable_irq(IRQ_TC);
    free_irq(IRQ_TC,ts.dev);
    free_irq(IRQ_ADC,ts.dev);
    if (adc_clock) {
        clk_disable(adc_clock);
        clk_put(adc_clock);
        adc_clock = NULL;
    }
    input_unregister_device(ts.dev);
    iounmap(base_addr);
    return 0;
}
#ifdef CONFIG_PM
static int s3c2410ts_suspend(struct platform_device *pdev, pm_message_t state)
{
    writel(TSC_SLEEP, base_addr+S3C2410_ADCTSC);
    writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_STDBM,base_addr+S3C2410_ADCCON);
    disable_irq(IRQ_ADC);
    disable_irq(IRQ_TC);
    clk_disable(adc_clock);
    return 0;
}
static int s3c2410ts_resume(struct platform_device *pdev)
{
    struct s3c2410_ts_mach_info *info =
        ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
        clk_enable(adc_clock);
    msleep(1);
        enable_irq(IRQ_ADC);
    enable_irq(IRQ_TC);
        if ((info->presc&0xff) > 0)
            writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF),base_addr+S3C2410_ADCCON);
    else
        writel(0,base_addr+S3C2410_ADCCON);
        /* Initialise registers */
        if ((info->delay&0xffff) > 0)
            writel(info->delay & 0xffff,  base_addr+S3C2410_ADCDLY);
        writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
    return 0;
}
#else
#define s3c2410ts_suspend NULL
#define s3c2410ts_resume  NULL
#endif

static struct platform_driver s3c2410ts_driver = {
    .driver        ={
                   .name           = "s3c2410-ts",
            .owner          = THIS_MODULE,
    },
       .probe          = s3c2410ts_probe,
       .remove         = s3c2410ts_remove,
       .suspend        = s3c2410ts_suspend,
       .resume         = s3c2410ts_resume,
};
 
int __init s3c2410ts_init(void)
{
    return platform_driver_register(&s3c2410ts_driver);
}
void __exit s3c2410ts_exit(void)
{
    platform_driver_unregister(&s3c2410ts_driver);
}
module_init(s3c2410ts_init);
module_exit(s3c2410ts_exit);
 
 
 
 
/* linux/include/asm-arm/arch-s3c2410/s3c2410_ts.h
 *
 * Copyright (c) 2005 Arnaud Patard
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *
 *  Changelog:
 *     24-Mar-2005     RTP     Created file
 *     03-Aug-2005     RTP     Renamed to ts.h
 */
#ifndef __ASM_ARM_TS_H
#define __ASM_ARM_TS_H
struct s3c2410_ts_mach_info {
       int             delay;
       int             presc;
       int             oversampling_shift;
};
void __init set_s3c2410ts_info(struct s3c2410_ts_mach_info *hard_s3c2410ts_info);
#endif /* __ASM_ARM_TS_H */
阅读(1125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~