您尚未登录。

楼主 #1 2020-05-09 21:08:36

缥缈九哥
会员
注册时间: 2020-05-09
已发帖子: 93
积分: 96

添加touchscreen驱动到基于linux-2.6.26的GEC2440开发板 --缥缈九哥

添加touchscreen驱动到基于linux-2.6.26的GEC2440开发板

1,修改drivers/input/touchscreen/Makefile文件
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# vi drivers/input/touchscreen/Makefile
在第24行添加:

obj-$(CONFIG_TOUCHSCREEN_S3C2410) += s3c2410_ts.o

2,修改drivers/input/touchscreen/Kconfig文件
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# 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,编译配置内核
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# 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文件
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# 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文件
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# vi include/asm-arm/plat-s3c24xx/devs.h
在第18行添加:
// add by yuanxihua@21cn.com
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;
// add by yuanxihua@21cn.com

6,修改arch/arm/plat-s3c24xx/devs.c文件
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# vi  arch/arm/plat-s3c24xx/devs.c
在第598行添加:
// add by yuanxihua@21cn.com

//  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);
// add by yuanxihua@21cn.com

7,添加drivers/input/touchscreen/s3c2410_ts.c文件
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# vi drivers/input/touchscreen/s3c2410_ts.c
添加的源码见附件。

8,编译内核生成驱动
root@yuanxh-desktop:/home/yuanxh/s3c2410/linux-2.6.26-9g24x0# 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

附件:

/* 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 <linux/delay.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/plat-s3c/regs-adc.h>
#include <asm/arch/regs-gpio.h>
#include <asm/arch/regs-clock.h>
#include <asm/plat-s3c24xx/devs.h>
#include <asm/plat-s3c24xx/clock.h>


#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<<s3c2410_ts->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("9g2440 <yuanxihua@21cn.com>");
MODULE_DESCRIPTION("s3c2440 TouchScreen Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:s3c2410-ts");

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn