Chinaunix首页 | 论坛 | 博客
  • 博客访问: 348036
  • 博文数量: 69
  • 博客积分: 3077
  • 博客等级: 中校
  • 技术积分: 602
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-11 09:40
个人简介

或以为孤权重,妄相忖度

文章分类

全部博文(69)

文章存档

2012年(1)

2011年(10)

2010年(39)

2009年(19)

我的朋友

分类: LINUX

2009-06-12 09:29:16

Linux2.6.26.8 移植之六:Touchscreen驱动移植

1,修改drivers/input/touchscreen/Makefile文件
# vi drivers/input/touchscreen/Makefile
 
在第24行添加:
  obj-$(CONFIG_TOUCHSCREEN_S3C2410) += s3c2410_ts.o

2,修改drivers/input/touchscreen/Kconfig文件
# vi drivers/input/touchscreen/Kconfig
 
在第188行添加:
  config TOUCHSCREEN_S3C2410
  tristate "Samsung S3C2410 touchscreen input driver"
  depends on ARCH_SMDK2410 && INPUT && INPUT_TOUCHSCREEN
  select SERIO
  help
    Say Y here if you have the s3c2410 touchscreen.
    If unsure, say N.
    To compile this driver as a module, choose M here: the
    module will be called s3c2410_ts.
  config TOUCHSCREEN_S3C2410_DEBUG
  boolean "Samsung S3C2410 touchscreen debug messages"
  depends on TOUCHSCREEN_S3C2410
  help
    Select this if you want debug messages
   3
,编译配置内核
# make menuconfig
 
进入配置菜单添加驱动到内核:
 Device Drivers  --->
 Input device support  --->
 [*]   Touchscreens  --->
 <*>   Samsung S3C2410 touchscreen input driver
 [*]     Samsung S3C2410 touchscreen debug messages

4,修改arch/arm/mach-s3c2440/mach-smdk2440.c文件
# vi arch/arm/mach-s3c2440/mach-smdk2440.c
 
166行添加:
  static struct s3c2410_ts_mach_info gec2440_ts_cfg __initdata = {
  .delay = 10000,
  .presc = 49,
  .oversampling_shift = 2,
  };

 static struct platform_device *smdk2440_devices[] __initdata = {结构中添加:
  &s3c_device_ts,
  
 
static void __init smdk2440_map_io(void)函数中添加:
  s3c24xx_init_touchscreen(&gec2440_ts_cfg);

5,修改include/asm-arm/plat-s3c24xx/devs.h文件
# vi include/asm-arm/plat-s3c24xx/devs.h
 
在第18行添加:
 struct s3c2410_ts_mach_info {
        int             delay;
        int             presc;
        int             oversampling_shift;
 };

 void __init s3c24xx_init_touchscreen(struct s3c2410_ts_mach_info *hard_s3c2410_ts_info);
 extern struct platform_device s3c_device_ts;


6
,修改arch/arm/plat-s3c24xx/devs.c文件
# vi  arch/arm/plat-s3c24xx/devs.c
 
在第598行添加:
 //  Touchscreen 
 
 static struct s3c2410_ts_mach_info s3c2410_ts_info;
 
 void __init s3c24xx_init_touchscreen(struct s3c2410_ts_mach_info *hard_s3c2410_ts_info)
 {
  memcpy(&s3c2410_ts_info,hard_s3c2410_ts_info,sizeof(struct s3c2410_ts_mach_info));
 }
 
 EXPORT_SYMBOL(s3c24xx_init_touchscreen);
 
 struct platform_device s3c_device_ts = {
  .name = "s3c2410-ts",
  .id = -1,
  .num_resources   = ARRAY_SIZE(s3c_adc_resource),
  .resource   = s3c_adc_resource,
  .dev = {
   .platform_data = &s3c2410_ts_info,
  }
 };
 EXPORT_SYMBOL(s3c_device_ts);


7,添加drivers/input/touchscreen/s3c2410_ts.c文件
# vi drivers/input/touchscreen/s3c2410_ts.c
 
添加的源码见附件。
 
8
,编译内核生成驱动
# make
 
9
,在内核启动可以看到input: s3c2410 Touchscreen as /class/input/input0
 
注册信息为:

 # cat /proc/bus/input/devices
 I: Bus=0019 Vendor=0001 Product=0002 Version=0100
 N: Name="s3c2410 Touchscreen"
 P: Phys=s3c2410ts/input0
 S: Sysfs=/class/input/input0
 U: Uniq=
 H: Handlers=mouse0 event0
 B: EV=b
 B: KEY=400 0 0 0 0 0 0 0 0 0 0
 B: ABS=1000003

 设备结点为:
 # ll  /dev/input/
 crw-rw----    1 root     root      13,  64 Dec 10  2008 event0
 crw-rw----    1 root     root      13,  32 Dec  7  2008 mouse0

10.如果发现你的LCD上下左右刚好相反,请修改代码中标红色的两句,很简单的这里就不一一列出来了。

附件:
/* drivers/input/touchscreen/s3c2410_ts.c
 *
 *  Touchscreen driver for GEC2440 with Samsung S3C2440A
 *
 *  modify from corgi_ts.c
 *
 *  Copyright (c) 2004-2005 Richard Purdie
 *
 *  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.
 *
 */


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#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

#define X_AXIS_MAX  0x3ff
#define X_AXIS_MIN  0
#define Y_AXIS_MAX  0x3ff
#define Y_AXIS_MIN  0
#define PRESSURE_MIN 0
#define PRESSURE_MAX 1

/*
 * Definitions & global arrays.
 */

/*
 * Per-touchscreen data.
 */
struct ts_event {
 short pressure;
 short xp;
 short yp;
};
struct s3c2410_ts {
 struct input_dev *input;
 struct resource *res;
 struct timer_list timer;
 struct ts_event tc;
 int pendown;
 int count;
 int shift;
 struct resource *irq_gpio[2];
 struct s3c2410_ts_mach_info *machinfo;
};

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);
}

static void touch_timer_fire(struct s3c2410_ts *s3c2410_ts,unsigned long data)
{
   unsigned long data0;
   unsigned long data1;
 struct input_dev *dev = s3c2410_ts->input;

 /* Disable Interrupt */
 set_irq_type(s3c2410_ts->irq_gpio[0]->start, IRQT_NOEDGE);
 set_irq_type(s3c2410_ts->irq_gpio[1]->start, IRQT_NOEDGE);
   
   data0 = readl(base_addr+S3C2410_ADCDAT0);
   data1 = readl(base_addr+S3C2410_ADCDAT1);

  s3c2410_ts->pendown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));

  if (s3c2410_ts->pendown) {
   if (s3c2410_ts->count != 0) {
    s3c2410_ts->tc.xp >>= s3c2410_ts->shift;
    s3c2410_ts->tc.yp >>= s3c2410_ts->shift;

#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG

    {
     struct timeval tv;
     do_gettimeofday(&tv);
     printk(KERN_INFO "T: %06d, X: %03x, Y: %03x\n", (int)tv.tv_usec, s3c2410_ts->tc.xp, s3c2410_ts->tc.yp);
    }
#endif
    input_report_abs(dev, ABS_X, s3c2410_ts->tc.xp);
    input_report_abs(dev, ABS_Y, s3c2410_ts->tc.yp);
   input_report_abs(dev, ABS_PRESSURE, s3c2410_ts->tc.pressure);
   input_report_key(dev, BTN_TOUCH, s3c2410_ts->pendown);
   input_sync(dev);
   }

   s3c2410_ts->tc.xp = 0;
   s3c2410_ts->tc.yp = 0;
   s3c2410_ts->count = 0;
  s3c2410_ts->tc.pressure = 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 {
   s3c2410_ts->count = 0;

   input_report_key(dev, BTN_TOUCH, 0);
   input_report_abs(dev, ABS_PRESSURE, 0);
   input_sync(dev);

   writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
  }
  
 /* Enable Falling Edge */
 set_irq_type(s3c2410_ts->irq_gpio[0]->start, IRQT_BOTHEDGE);
 set_irq_type(s3c2410_ts->irq_gpio[1]->start, IRQT_BOTHEDGE);
}
static void s3c2410_ts_timer(unsigned long data)
{
 struct s3c2410_ts *s3c2410ts_data = (struct s3c2410_ts *) data;

 touch_timer_fire(s3c2410ts_data, 1);
}

static irqreturn_t stylus_updown(int irq, void *dev_id, struct pt_regs *regs)
{
 unsigned long data0;
 unsigned long data1;
 struct s3c2410_ts *s3c2410_ts = dev_id;

#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG  
 printk(KERN_ERR "%s() No.%d line:\n\r",__FUNCTION__,__LINE__);
#endif

 data0 = readl(base_addr+S3C2410_ADCDAT0);
 data1 = readl(base_addr+S3C2410_ADCDAT1);

 s3c2410_ts->pendown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT1_UPDOWN));

 /* TODO we should never get an interrupt with updown set while
  * the timer is running, but maybe we ought to verify that the
  * timer isn't running anyways. */

 if (s3c2410_ts->pendown)
  touch_timer_fire(s3c2410_ts, 0);

 return IRQ_HANDLED;
}


static irqreturn_t stylus_action(int irq, void *dev_id, struct pt_regs *regs)
{
 unsigned long data0;
 unsigned long data1;
 struct s3c2410_ts *s3c2410_ts = dev_id;

#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG 
 printk(KERN_ERR "%s() No.%d line:\n\r",__FUNCTION__,__LINE__);
#endif

 data0 = readl(base_addr+S3C2410_ADCDAT0);
 data1 = readl(base_addr+S3C2410_ADCDAT1);

 s3c2410_ts->tc.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
 s3c2410_ts->tc.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;

 s3c2410_ts->count++;
 s3c2410_ts->tc.pressure = 1;

 if (s3c2410_ts->count < (1<shift)) {
  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 {
  mod_timer(&s3c2410_ts->timer, jiffies + HZ / 100);
  writel(WAIT4INT(1), base_addr+S3C2410_ADCTSC);
 }

 return IRQ_HANDLED;
}

static struct clk *adc_clock;
/*
 * The functions for inserting/removing us as a module.
 */

#define s3c2410ts_suspend  NULL
#define s3c2410ts_resume  NULL
static int __init s3c2410ts_probe(struct platform_device *pdev)
{
 struct s3c2410_ts *s3c2410_ts;
 struct input_dev *input_dev;
 int size;
 int err = -ENOMEM;

 s3c2410_ts = kzalloc(sizeof(struct s3c2410_ts), GFP_KERNEL);
 input_dev = input_allocate_device();
 if (!s3c2410_ts || !input_dev)
  goto fail1;

 platform_set_drvdata(pdev, s3c2410_ts);

 s3c2410_ts->machinfo = pdev->dev.platform_data;
 s3c2410_ts->irq_gpio[0] = platform_get_resource(pdev, IORESOURCE_IRQ,0);
 s3c2410_ts->irq_gpio[1] = platform_get_resource(pdev, IORESOURCE_IRQ,1);
 
#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG 
 printk(KERN_ERR "s3c2410_ts->irq_gpio[0] = %d,IRQ_TC  = %d\n",s3c2410_ts->irq_gpio[0]->start,IRQ_TC);
 printk(KERN_ERR "s3c2410_ts->irq_gpio[1] = %d,IRQ_ADC = %d\n",s3c2410_ts->irq_gpio[1]->start,IRQ_ADC);
#endif
 
 if ((s3c2410_ts->irq_gpio[0]->start < 0)|(s3c2410_ts->irq_gpio[1]->start < 0)) {
  err = -ENODEV;
  printk(KERN_ERR "Hm... too bad : no irq for ts\n");
  goto fail1;
 }
  
    if (!s3c2410_ts->machinfo)
    {
        printk(KERN_ERR "Hm... too bad : no platform data for ts\n");
  return -EINVAL;
    }
 s3c2410_ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 if (s3c2410_ts->res == NULL) {
  printk(KERN_ERR "no memory resource specified\n");
  return -ENOENT;
 }

 adc_clock = clk_get(NULL, "adc");
 if (!adc_clock) {
  printk(KERN_ERR "failed to get adc clock source\n");
  return -ENOENT;
 }
 clk_enable(adc_clock);
  
 size = (s3c2410_ts->res->end-s3c2410_ts->res->start)+1;
 base_addr=ioremap(s3c2410_ts->res->start, size);

#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG 
 printk(KERN_ERR "ioremap %08x to base_addr=%08x , size=%08x\n",s3c2410_ts->res->start,(unsigned int)base_addr,size);
#endif 
 
 if (base_addr == NULL) {
        printk(KERN_ERR "Failed to remap register block\n");
        return -ENOMEM;
    }

 /* Configure GPIOs */
    s3c2410_ts_connect();
   
#ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
    printk(KERN_ERR "s3c2410_ts->machinfo delay = %d,presc = %d,oversampling_shift = %d\n",
    s3c2410_ts->machinfo->delay,
    s3c2410_ts->machinfo->presc,
    s3c2410_ts->machinfo->oversampling_shift);
#endif

 if ((s3c2410_ts->machinfo->presc&0xff) > 0)
        writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(s3c2410_ts->machinfo->presc&0xFF),\
        base_addr+S3C2410_ADCCON);
 else
        writel(0,base_addr+S3C2410_ADCCON);


    /* Initialise the adcdly registers */
    if ((s3c2410_ts->machinfo->delay&0xffff) > 0)
        writel(s3c2410_ts->machinfo->delay & 0xffff,  base_addr+S3C2410_ADCDLY);

 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
 __raw_writel(__raw_readl(S3C2410_GPGCON)|(0xff<<24), S3C2410_GPGCON);
 
 s3c2410_ts->input = input_dev;

 init_timer(&s3c2410_ts->timer);
 s3c2410_ts->timer.data = (unsigned long) s3c2410_ts;
 s3c2410_ts->timer.function = s3c2410_ts_timer;
 
// input_dev->private = s3c2410_ts;
 input_dev->name = "s3c2410 Touchscreen";
 input_dev->phys = "s3c2410ts/input0";
 input_dev->id.bustype = BUS_HOST;
 input_dev->id.vendor = 0x0001;
 input_dev->id.product = 0x0002;
 input_dev->id.version = 0x0100;
// input_dev->dev.parent = &pdev->dev;
 
 s3c2410_ts->shift = s3c2410_ts->machinfo->oversampling_shift;
 
 input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
 input_set_abs_params(input_dev, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
 input_set_abs_params(input_dev, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
 input_set_abs_params(input_dev, ABS_PRESSURE, PRESSURE_MIN, PRESSURE_MAX, 0, 0);

 /* Get irqs */
    if (request_irq(s3c2410_ts->irq_gpio[1]->start, stylus_action,  IRQF_DISABLED, pdev->name, s3c2410_ts)) {
        printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
  iounmap(base_addr);
  err = -EBUSY;
  goto fail1;
 }
    if (request_irq(s3c2410_ts->irq_gpio[0]->start, stylus_updown,  IRQF_DISABLED, pdev->name, s3c2410_ts)) {
        printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
  iounmap(base_addr);
  err = -EBUSY;
  goto fail1;
 }
    /* All went ok, so register to the input system */
 err = input_register_device(s3c2410_ts->input);
 if (err)
  goto fail2;
 /* Enable Falling Edge */
 set_irq_type(s3c2410_ts->irq_gpio[0]->start, IRQT_BOTHEDGE);
 set_irq_type(s3c2410_ts->irq_gpio[1]->start, IRQT_BOTHEDGE);
 return 0;

 fail2: free_irq(s3c2410_ts->irq_gpio, s3c2410_ts);
 fail1: input_free_device(input_dev);
 kfree(s3c2410_ts);
 return err;
}

static int s3c2410ts_remove(struct platform_device *pdev)
{
 struct s3c2410_ts *s3c2410_ts = platform_get_drvdata(pdev);
 
 disable_irq(s3c2410_ts->irq_gpio[0]->start);
 disable_irq(s3c2410_ts->irq_gpio[1]->start);
 free_irq(s3c2410_ts->irq_gpio[0]->start,s3c2410_ts);
 free_irq(s3c2410_ts->irq_gpio[1]->start,s3c2410_ts);
 del_timer_sync(&s3c2410_ts->timer);

 if (adc_clock) {
  clk_disable(adc_clock);
  clk_put(adc_clock);
  adc_clock = NULL;
 }
 input_unregister_device(s3c2410_ts->input);
 iounmap(base_addr);
 kfree(s3c2410_ts);
 return 0;
}

static struct platform_driver s3c2410ts_driver = {
 .probe  = s3c2410ts_probe,
 .remove  = s3c2410ts_remove,
 .suspend = s3c2410ts_suspend,
 .resume  = s3c2410ts_resume,
 .driver  = {
  .name = "s3c2410-ts",
  .owner = THIS_MODULE,
 },
};


static int __devinit s3c2410ts_init(void)
{
 return platform_driver_register(&s3c2410ts_driver);
}

static void __exit s3c2410ts_exit(void)
{
 platform_driver_unregister(&s3c2410ts_driver);
}

module_init(s3c2410ts_init);
module_exit(s3c2410ts_exit);

MODULE_AUTHOR("<>");
MODULE_DESCRIPTION("s3c2440 TouchScreen Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:s3c2410-ts");

 

 

 

参考:

阅读(2162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~