- //*********************************************************************************************/
- //* Purpose:this is a s3c2410 touch screen drive
- //*
- //* Author: alvin.zhang
- //*
- //* Date: 2008/04/06
- //*
- //* Revision: 2009/01/12
- //*
- //* Description : the code about s3c2410 touch screen , you can reference it in your code
- //**********************************************************************************************/
- #include <linux/platform_device.h>
- //#include <linux/config.h>
- #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 <asm/io.h>
- #include <asm/irq.h>
- #include <asm/arch/regs-adc.h>
- #include <asm/arch/regs-gpio.h>
- #include <asm/arch/s3c2410_ts.h>
- #include <asm/arch/regs-clock.h>
- #include <linux/clk.h>
- /* 定义版本*/
- #define S3C2410TSVERSION 0x0101
- /*IT4INT(1) 为UP_INT WAIT4INT(0)为 DOWN_INT*/
- #define WAIT4INT(x) (((x)<<8) | S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | S3C2410_ADCTSC_XY_PST(3))
- /* 设置触摸屏的模式为AUTOPST */
- #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 DEBUG 1
- MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
- MODULE_DESCRIPTION("s3c2410 touchscreen driver");
- MODULE_LICENSE("GPL");
- /*
- * Definitions & global arrays.
- */
- static char *s3c2410ts_name = "s3c2410 TouchScreen";
- /*
- * Per-touchscreen data.
- */
- struct s3c2410ts {
- /*修改前
- struct input_dev dev;
- */
- /*add by alvin*/
- struct input_dev *dev;
- /*add by alvin*/
- long xp;
- long yp;
- int count;
- /*驱动内取样的频率定义在oversampling_shift 作法是取樣
- (1<<oversampling_shift) 次,然後取平均值當作最後抓到的座標。 */
- int shift;
- char phys[32];
- };
- static struct s3c2410ts ts;
- static void __iomem *base_addr;
- /* 配置GPIO PIN*/
- 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 down的时候,在中断函数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_ADCDAT1_UPDOWN));
- if (updown) {//注意updown变量只在stylus_updown中更新
- if (ts.count != 0) {/*<功能2>*/
- 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);
- printk(KERN_INFO "T: %06d, X: %03ld, Y: %03ld/n", (int)tv.tv_usec, ts.xp, ts.yp);
- }
- #endif
- /* 报告x、y的绝对坐标值 */
- input_report_abs(ts.dev/*&ts.dev*/, ABS_X, ts.xp);
- input_report_abs(ts.dev/*&ts.dev*/, ABS_Y, ts.yp);
- /* 报告按键事件,键值为1(代表触摸屏对应的按键被按下) */
- input_report_key(ts.dev/*&ts.dev*/, BTN_TOUCH, 1);
- /* 报告触摸屏的状态,1表明触摸屏被按下 */
- input_report_abs(ts.dev/*&ts.dev*/, ABS_PRESSURE, 1);
- /* 等待接收方受到数据后回复确认,用于同步 */
- input_sync(ts.dev/*&ts.dev*/);
- }
- /*<功能1>*/
- ts.xp = 0;
- ts.yp = 0;
- ts.count = 0;
- /* 设置触摸屏的模式为AUTOPST */
- writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
- /* 启动ADC转换 */
- writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
- } else {/*<功能3>*/
- ts.count = 0;
- input_report_key(ts.dev/*&ts.dev*/, BTN_TOUCH, 0);
- input_report_abs(ts.dev/*&ts.dev*/, ABS_PRESSURE, 0);
- input_sync(ts.dev/*&ts.dev*/);
- writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- }
- }
- /*这里初始化定时器,并把中断处理函数指向touch_timer_fire*/
- static struct timer_list touch_timer = TIMER_INITIALIZER(touch_timer_fire, 0, 0);
- static irqreturn_t stylus_updown(int irq, void *dev_id, struct pt_regs *regs)
- {
- unsigned long data0;
- unsigned long data1;
- int updown;
- /********************************debug************************************/
- printk(KERN_INFO "You touch the screen/n");
- /*************************************************************************/
- data0 = readl(base_addr+S3C2410_ADCDAT0);
- data1 = readl(base_addr+S3C2410_ADCDAT1);
- /*更新stylus状态寄变量updown:
- 1 = down
- 0 = up */
- updown = (!(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 (updown)
- touch_timer_fire(0);
- return IRQ_HANDLED;
- }
- /*ADC中断服务程序*/
- static irqreturn_t stylus_action(int irq, void *dev_id, struct pt_regs *regs)
- {
- 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<<ts.shift)) {/* 缓冲区未满,再次激活ADC转换 */
- 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);//修改定时器的expire
- 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 device *dev)
- {
- struct s3c2410_ts_mach_info *info;
- /*s3c2410_ts_mach_info这个结构需要我们去填充,里面存放的是触摸屏需要的一些配置参数,见下面的附录部分*/
- info = ( struct s3c2410_ts_mach_info *)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");//可以获得adc时钟
- if (!adc_clock) {
- printk(KERN_ERR "failed to get adc clock source/n");
- return -ENOENT;
- }
- //clk_use(adc_clock);//这个在高版本下已经不需要了
- clk_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();
- /* Set the prscvl*/
- 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 the adcdly registers */
- if ((info->delay&0xffff) > 0)
- writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
- writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- /* Initialise input stuff */
- memset(&ts, 0, sizeof(struct s3c2410ts));
- /* 以下配置2.6内核划分出来的输入设备 */
- /* 修改前
- init_input_dev(&ts.dev);//这是低版本linux函数,新版本中已经被取替
- ts.dev.evbit[0] = 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);
- sprintf(ts.phys, "ts0");
- ts.dev.private = &ts;
- ts.dev.name = s3c2410ts_name;
- ts.dev.phys = ts.phys;
- ts.dev.id.bustype = BUS_RS232;
- ts.dev.id.vendor = 0xDEAD;
- ts.dev.id.product = 0xBEEF;
- ts.dev.id.version = S3C2410TSVERSION;
- */
- /*add by alvin*/
- // int err;
- if (ts.dev) {
- return -ENODEV; /* already initialized */
- }
- ts.dev = input_allocate_device();
- if (!ts.dev)
- return -ENOMEM;
- /* evbit字段用来定义该输入设备可以支持的(产生和响应)的事件的类型,
- 在此触摸屏设置为支持同步(EN_SYN)、按键(EN_KEY)、绝对坐标(EV_ABS)事件*/
- ts.dev->evbit[0] = ts.dev->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
- /* 设置所支持的按键(键值),触摸屏可以看成只有一个按键的设备 */
- ts.dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
- /* 设置绝对坐标x和y的最小最大值,在这是0-0x3FF */
- input_set_abs_params(ts.dev, ABS_X, 0, 320/*0x3FF*/, 0, 0);
- input_set_abs_params(ts.dev, ABS_Y, 0, 240/*0x3FF*/, 0, 0);
- input_set_abs_params(ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
- sprintf(ts.phys, "ts0");
- ts.dev->private = &ts;
- ts.dev->name = s3c2410ts_name;
- ts.dev->phys = ts.phys;
- ts.dev->id.bustype = BUS_RS232;
- ts.dev->id.vendor = 0xDEAD;
- ts.dev->id.product = 0xBEEF;
- ts.dev->id.version = S3C2410TSVERSION;
- /*add by alvin*/
- ts.shift = info->oversampling_shift;
- /* 这个比较重要,配置输入数据的缓存区大小,
- 在这里oversampling_shift设为2,也就是缓存区的大小为4(1<<2) */
- /* Get irqs */
- /* ADC转换中断,转换结束后触发 */
- if (request_irq(IRQ_ADC, stylus_action, SA_SAMPLE_RANDOM,
- "s3c2410_action", ts.dev/*&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, SA_SAMPLE_RANDOM,
- "s3c2410_action", ts.dev/*&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 */
- input_register_device(ts.dev/*&ts.dev*/);
- return 0;
- }
- static int s3c2410ts_remove(struct device *dev)
- {
- disable_irq(IRQ_ADC);
- disable_irq(IRQ_TC);
- free_irq(IRQ_TC,ts.dev/*&ts.dev*/);
- free_irq(IRQ_ADC,ts.dev/*&ts.dev*/);
- if (adc_clock) {
- clk_disable(adc_clock);
- // clk_unuse(adc_clock);
- clk_put(adc_clock);
- adc_clock = NULL;
- }
- input_unregister_device(ts.dev/*&ts.dev*/);
- iounmap(base_addr);
- return 0;
- }
- static struct device_driver s3c2410ts_driver = {
- .name = "s3c2410-ts",
- .bus = &platform_bus_type,
- .probe = s3c2410ts_probe,
- .remove = s3c2410ts_remove,
- };
- int __init s3c2410ts_init(void)
- {
- return driver_register(&s3c2410ts_driver);
- }
- void __exit s3c2410ts_exit(void)
- {
- driver_unregister(&s3c2410ts_driver);
- }
- module_init(s3c2410ts_init);
- module_exit(s3c2410ts_exit);
- /*
- mach-smdk2410.c中
- static struct s3c2410_ts_mach_info s3c2410_ts_cfg __initdata = {
- .delay = 10000,
- .presc = 49,
- .oversampling_shift = 2,
- };
- 在devs.c中定义
- struct platform_device s3c_device_ts = {
- .name = "s3c2410-ts",
- .id = -1,
- .dev = {
- .platform_data = &s3c2410ts_info,
- }
- };
- */