日期:2014-05-16 浏览次数:20634 次
static int s3c2410_ts_open(struct inode *inode, struct file *filp) { TS_DEV *devp; devp = container_of(inode->i_cdev, TS_DEV, cdev); filp->private_data = devp; /* 创建等待队列和环形缓冲区 */ devp->head = devp->tail = 0; devp->penStatus = PEN_UP; init_waitqueue_head(&(devp->wq)); /* 注册中断 */ if (request_irq(IRQ_ADC, stylus_action, IRQF_SHARED, /* 共享中断 */ "s3c2410_action", &(devp->cdev))) { printk(KERN_ERR "Could not allocate ts IRQ_ADC\n"); return -EIO; } if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM, "s3c241_updown", &(devp->cdev))) { printk(KERN_ERR "Could not allocate ts IRQ_TC\n"); return -EIO; } DPRINTK("ts opened\n"); return 0; } static int s3c2410_ts_release(struct inode *inode, struct file *filp) { TS_DEV *devp = filp->private_data; disable_irq(IRQ_ADC); disable_irq(IRQ_TC); free_irq(IRQ_TC,&(devp->cdev)); free_irq(IRQ_ADC,&(devp->cdev)); DPRINTK("ts released\n"); return 0; } static int __init s3c2410_ts_init(void) { int ret; adc_clock = clk_get(NULL, "adc"); if (!adc_clock) { printk(KERN_ERR "Faild to get adc clock source\n"); return -ENOENT; } clk_enable(adc_clock); base_addr = ioremap(S3C2410_PA_ADC, 0x20); if (base_addr == NULL) { printk(KERN_ERR "Fiald to remap register block\n"); return -ENOMEM; } /* Config GPIOs */ s3c2410_ts_connect(); iowrite32(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(0xFF),\ base_addr+S3C2410_ADCCON); iowrite32(0xffff, base_addr+S3C2410_ADCDLY); iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC); /* Register char device */ if (alloc_chrdev_region(&tsdev_number, 0, 1, DEVICE_NAME) < 0) { printk(KERN_DEBUG "Can not register device\n"); return -1; } tsdev_class = class_create(THIS_MODULE, DEVICE_NAME); tsdev = kmalloc(sizeof(TS_DEV), GFP_KERNEL); if (!tsdev) { printk(KERN_ERR "Bad kmalloc\n"); return -ENOMEM; } cdev_init(&tsdev->cdev, &tsdev_fops); tsdev->cdev.owner = THIS_MODULE; ret = cdev_add(&tsdev->cdev, tsdev_number, 1); if (ret) { printk(KERN_ERR "Bad cdev\n"); return ret; } device_create(tsdev_class, NULL, tsdev_number, 0, "my-ts"); DPRINTK("Loading Module my-ts Success..\n"); return 0; } static void __exit s3c2410_ts_exit(void) { if (adc_clock) { clk_disable(adc_clock); clk_put(adc_clock); adc_clock = 0; } /* Unregister devce */ unregister_chrdev_region(tsdev_number, 1); device_destroy(tsdev_class, tsdev_number); cdev_del(&tsdev->cdev); kfree(tsdev); class_destroy(tsdev_class); iounmap(base_addr); DPRINTK("Unloading Module my-ts Success..\n"); }