歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux中觸摸屏驅動的實現:基於S3C6410處理器

這幾篇文章主要是關於linux中觸摸屏驅動的,基於s3c6410處理器進行分析。這一篇主要是關於觸摸屏設備作為平台設備的實現,還有對應的probe函數和remove函數的源碼分析。

1、觸摸屏模塊的加載和卸載函數

static char banner[] __initdata = KERN_INFO "S3C Touchscreen driver, (c) 2008 Samsung Electronics\n";

static int __init s3c_ts_init(void)
{
printk(banner);
return platform_driver_register(&s3c_ts_driver);
}
static void __exit s3c_ts_exit(void)
{
platform_driver_unregister(&s3c_ts_driver);
}
module_init(s3c_ts_init);
module_exit(s3c_ts_exit);

萬變不離其宗,還是熟悉的那個他,只不過每一次都是一番新的歷程。

對應的平台設備資源:在Dev-ts.c (linux2.6.28\arch\arm\plat-s3c)文件中

/* Touch srcreen */
static struct resource s3c_ts_resource[] = {
[0] = {
.start = S3C_PA_ADC, I/O端口
.end   = S3C_PA_ADC + SZ_4K - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_PENDN, 中斷
.end   = IRQ_PENDN,
.flags = IORESOURCE_IRQ,
},
[2] = {
.start = IRQ_ADC,  中斷
.end   = IRQ_ADC,
.flags = IORESOURCE_IRQ,
}

};
struct platform_device s3c_device_ts = {
.name  = "s3c-ts",
.id  = -1,
.num_resources = ARRAY_SIZE(s3c_ts_resource),
.resource  = s3c_ts_resource,
};

對應的platform_driver結構體的定義如下:

static struct platform_driver s3c_ts_driver = {
       .probe          = s3c_ts_probe,
       .remove         = s3c_ts_remove,
       .suspend        = s3c_ts_suspend,
       .resume         = s3c_ts_resume,
       .driver = {
.owner = THIS_MODULE,
.name = "s3c-ts",
},
};

2、我想應該知道要做什麼了,接著來看probe函數,源碼如下:

/*
 * The functions for inserting/removing us as a module.
 */
static int __init s3c_ts_probe(struct platform_device *pdev)
{
struct resource *res;
struct device *dev;
struct input_dev *input_dev;
struct s3c_ts_mach_info * s3c_ts_cfg;
int ret, size;


dev = &pdev->dev;


res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(dev,"no memory resource specified\n");
return -ENOENT;
}   得到寄存器操作地址

size = (res->end - res->start) + 1;
ts_mem = request_mem_region(res->start, size, pdev->name);  申請這片內存區域

注:static struct resource*ts_mem;
if (ts_mem == NULL) {
dev_err(dev, "failed to get memory region\n");
ret = -ENOENT;
goto err_req;
}


ts_base = ioremap(res->start, size);  進行映射
if (ts_base == NULL) {
dev_err(dev, "failed to ioremap() region\n");
ret = -EINVAL;
goto err_map;
}

ts_clock = clk_get(&pdev->dev, "adc");  得到時鐘
if (IS_ERR(ts_clock)) {
dev_err(dev, "failed to find watchdog clock source\n");
ret = PTR_ERR(ts_clock);
goto err_clk;
}


clk_enable(ts_clock); 使能時鐘


s3c_ts_cfg = s3c_ts_get_platdata(&pdev->dev);

static struct s3c_ts_mach_info *s3c_ts_get_platdata (struct device *dev)
{
if (dev->platform_data != NULL)
return (struct s3c_ts_mach_info *)dev->platform_data;


return &s3c_ts_default_cfg;
}

默認值:

/* Touchscreen default configuration */
struct s3c_ts_mach_info s3c_ts_default_cfg __initdata = {
                .delay = 5000,//10000,
                .presc = 49,
                .oversampling_shift = 4,//2,
.resol_bit = 10
};

這裡涉及到一個結構體s3c_ts_mach_info

struct s3c_ts_mach_info {
int             delay;  延時時間
int             presc;  預分頻值
int             oversampling_shift; 轉化次數
int resol_bit; 分頻率
enum s3c_adc_types3c_adc_con;看下面:
};

其中有

enum s3c_adc_type {
ADC_TYPE_0,
ADC_TYPE_1, /* S3C2416, S3C2450 */
ADC_TYPE_2,/* S3C64XX, S5PC1XX */
};

if ((s3c_ts_cfg->presc&0xff) > 0)    設置預分頻值
writel(S3C_ADCCON_PRSCEN | S3C_ADCCON_PRSCVL(s3c_ts_cfg->presc&0xFF),\
ts_base+S3C_ADCCON);
else
writel(0, ts_base+S3C_ADCCON);沒有定義的話,寫0,其實也就是禁止預分頻

這裡主要和ADCCON寄存器的設置有關,而且有如下定義:

#define S3C_ADCCON_PRSCEN(1<<14)

#define S3C_ADCCON_PRSCVL(x)(((x)&0xFF)<<6)

看下圖:






/* Initialise registers */
if ((s3c_ts_cfg->delay&0xffff) > 0)
writel(s3c_ts_cfg->delay & 0xffff, ts_base+S3C_ADCDLY);
和上面差不多,主要和ADCDLY寄存器有關。直接看圖:注:在兩種模式下有不同的含義



if (s3c_ts_cfg->resol_bit==12) {  分頻率
switch(s3c_ts_cfg->s3c_adc_con) {
case ADC_TYPE_2:
writel(readl(ts_base+S3C_ADCCON)|S3C_ADCCON_RESSEL_12BIT, ts_base+S3C_ADCCON);
break;

#define S3C_ADCCON_RESSEL_12BIT(0x1<<16)




case ADC_TYPE_1:
writel(readl(ts_base+S3C_ADCCON)|S3C_ADCCON_RESSEL_12BIT_1, ts_base+S3C_ADCCON);
break;

default:
dev_err(dev, "Touchscreen over this type of AP isn't supported !\n");
break;
}
}

writel(WAIT4INT(0), ts_base+S3C_ADCTSC);主要是對ADCTSC寄存器進行操作,使觸摸屏處於等待中斷模式




ts = kzalloc(sizeof(struct s3c_ts_info), GFP_KERNEL);

注:static struct s3c_ts_info*ts;

input_dev = input_allocate_device();申請並初始化一個輸入設備。通過輸入設備,驅動程序才能和用戶交互。
注:struct input_dev *input_dev;
if (!input_dev) {
ret = -ENOMEM;
goto err_alloc;
}

ts->dev = input_dev;


ts->dev->evbit[0] = ts->dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
ts->dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);


if (s3c_ts_cfg->resol_bit==12) {
input_set_abs_params(ts->dev, ABS_X, 0, 0xFFF, 0, 0);
input_set_abs_params(ts->dev, ABS_Y, 0, 0xFFF, 0, 0);
}
else {
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, "input(ts)");


ts->dev->name = s3c_ts_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 = S3C_TSVERSION;


ts->shift = s3c_ts_cfg->oversampling_shift;
ts->resol_bit = s3c_ts_cfg->resol_bit;
ts->s3c_adc_con = s3c_ts_cfg->s3c_adc_con;

上面這一段代碼都是初始化觸摸屏設備的全局量ts,對應的結構體原型是:

struct s3c_ts_info {
struct input_dev *dev;
long xp;
long yp;
int count;
int shift;
char phys[32];
int resol_bit;
enum s3c_adc_types3c_adc_con;
};

/* For IRQ_PENDUP */
ts_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);  得到觸摸屏中斷IRQ_PENDUP
if (ts_irq == NULL) {
dev_err(dev, "no irq resource specified\n");
ret = -ENOENT;
goto err_irq;
}


ret = request_irq(ts_irq->start, stylus_updown, IRQF_SAMPLE_RANDOM, "s3c_updown", ts);申請觸摸屏中斷,對應的中斷處理函數是stylus_updown
if (ret != 0) {
dev_err(dev,"s3c_ts.c: Could not allocate ts IRQ_PENDN !\n");
ret = -EIO;
goto err_irq;
}


/* For IRQ_ADC */
ts_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);  得到ADC中斷
if (ts_irq == NULL) {
dev_err(dev, "no irq resource specified\n");
ret = -ENOENT;
goto err_irq;
}


ret = request_irq(ts_irq->start, stylus_action, IRQF_SAMPLE_RANDOM, "s3c_action", ts);申請ADC中斷,對應的中斷函數是stylus_action
if (ret != 0) {
dev_err(dev, "s3c_ts.c: Could not allocate ts IRQ_ADC !\n");
ret =  -EIO;
goto err_irq;
}


printk(KERN_INFO "%s got loaded successfully : %d bits\n", s3c_ts_name, s3c_ts_cfg->resol_bit);


/* All went ok, so register to the input system */  將觸摸屏設備注冊到輸入子系統中
ret = input_register_device(ts->dev);

if(ret) {
dev_err(dev, "s3c_ts.c: Could not register input device(touchscreen)!\n");
ret = -EIO;
goto fail;
}


return 0;

下面這些是錯誤處理代碼
fail:
free_irq(ts_irq->start, ts->dev);
free_irq(ts_irq->end, ts->dev);

err_irq:
input_free_device(input_dev);
kfree(ts);


err_alloc:
clk_disable(ts_clock);
clk_put(ts_clock);

err_clk:
iounmap(ts_base);


err_map:
release_resource(ts_mem);
kfree(ts_mem);


err_req:
return ret;
}

到這裡,觸摸屏設備驅動的probe函數就講述完了。

3、當然,probe函數中幾個重要的函數都沒講,就是關於輸入子系統的,那不是我們現在關注的重點。接著看對應的remove函數,源碼如下:

static int s3c_ts_remove(struct platform_device *dev)
{
printk(KERN_INFO "s3c_ts_remove() of TS called !\n");


disable_irq(IRQ_ADC);
disable_irq(IRQ_PENDN);

free_irq(IRQ_PENDN, ts->dev);
free_irq(IRQ_ADC, ts->dev);


if (ts_clock) {
clk_disable(ts_clock);
clk_put(ts_clock);
ts_clock = NULL;
}


input_unregister_device(ts->dev);
iounmap(ts_base);


return 0;
}

其實看懂了probe函數,remove函數就完全不用看了。

Copyright © Linux教程網 All Rights Reserved